name_remapping.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /* Author: Josh Faust */
31 
32 /*
33  * Test name remapping. Assumes the parameter "test" is remapped to "test_remap", and the node name is remapped to "name_remapped"
34  */
35 
36 #include <string>
37 #include <sstream>
38 #include <fstream>
39 
40 #include <gtest/gtest.h>
41 
42 #include <time.h>
43 #include <stdlib.h>
44 
45 #include "ros/ros.h"
46 #include <ros/param.h>
47 #include <ros/names.h>
48 
49 TEST(roscpp, parameterRemapping)
50 {
51  std::string param;
52  ASSERT_TRUE(ros::param::get("mapfrom", param));
53  ASSERT_STREQ(ros::names::resolve("mapfrom").c_str(), "/mapto");
54 
55  ASSERT_TRUE(ros::param::get("/mapto", param));
56  ASSERT_STREQ(ros::names::resolve("/mapfrom").c_str(), "/mapto");
57 }
58 
59 TEST(roscpp, nodeNameRemapping)
60 {
61  std::string node_name = ros::this_node::getName();
62  ASSERT_STREQ(node_name.c_str(), "/name_remapped");
63 }
64 
65 TEST(roscpp, cleanName)
66 {
67  ASSERT_STREQ(ros::names::clean("////asdf///").c_str(), "/asdf");
68  ASSERT_STREQ(ros::names::clean("////asdf///jioweioj").c_str(), "/asdf/jioweioj");
69  ASSERT_STREQ(ros::names::clean("////asdf///jioweioj/").c_str(), "/asdf/jioweioj");
70 }
71 
72 TEST(RoscppHandles, nodeHandleNameRemapping)
73 {
75  remap["a"] = "b";
76  remap["/a/a"] = "/a/b";
77  remap["c"] = "/a/c";
78  remap["d/d"] = "/c/e";
79  remap["d/e"] = "c/f";
80  ros::NodeHandle n("", remap);
81 
82  EXPECT_STREQ(n.resolveName("a").c_str(), "/b");
83  EXPECT_STREQ(n.resolveName("/a/a").c_str(), "/a/b");
84  EXPECT_STREQ(n.resolveName("c").c_str(), "/a/c");
85  EXPECT_STREQ(n.resolveName("d/d").c_str(), "/c/e");
86 
87  ros::NodeHandle n2("z", remap);
88  EXPECT_STREQ(n2.resolveName("a").c_str(), "/z/b");
89  EXPECT_STREQ(n2.resolveName("/a/a").c_str(), "/a/b");
90  EXPECT_STREQ(n2.resolveName("c").c_str(), "/a/c");
91  EXPECT_STREQ(n2.resolveName("d/d").c_str(), "/c/e");
92  EXPECT_STREQ(n2.resolveName("d/e").c_str(), "/z/c/f");
93 }
94 
95 int
96 main(int argc, char** argv)
97 {
98  testing::InitGoogleTest(&argc, argv);
99  ros::init( argc, argv, "name_remapping" );
100  ros::NodeHandle nh;
101 
102  return RUN_ALL_TESTS();
103 }
bool param(const std::string &param_name, T &param_val, const T &default_val)
std::string resolveName(const std::string &name, bool remap=true) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROSCPP_DECL const std::string & getName()
int main(int argc, char **argv)
ROSCPP_DECL std::string resolve(const std::string &name, bool remap=true)
ROSCPP_DECL std::string clean(const std::string &name)
std::map< std::string, std::string > M_string
ROSCPP_DECL std::string remap(const std::string &name)
ROSCPP_DECL bool get(const std::string &key, std::string &s)
TEST(roscpp, parameterRemapping)


test_roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas
autogenerated on Mon Nov 2 2020 03:52:46