test_ns_node_remapping.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009, 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 the 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 #include <ros/ros.h>
31 #include <gtest/gtest.h>
32 
33 static int argc_;
34 static char** argv_;
35 
36 #define PRINT(cmd) printf(#cmd"\n"); cmd; printf("\n");
37 
38 TEST(NamespaceRemappingTest, unqualified_remaps)
39 {
40  ros::M_string local_remappings;
41  local_remappings.insert(std::make_pair("a", "Ra"));
42  local_remappings.insert(std::make_pair("b", "Rb"));
43  local_remappings.insert(std::make_pair("c", "Rc"));
44 
45  PRINT(ros::NodeHandle base("a", local_remappings));
46  PRINT(ros::NodeHandle a1(base, "a"));
47  PRINT(ros::NodeHandle a2(base, "a", ros::M_string()));
48  PRINT(ros::NodeHandle b(base, "b"));
49  PRINT(ros::NodeHandle c(base, "c", ros::M_string())); // Same as b, but different constructor
50 
51  EXPECT_STREQ(base.getNamespace().c_str(), "/a");
52  EXPECT_STREQ(a1.getNamespace().c_str(), "/a/Ra");
53  EXPECT_STREQ(a2.getNamespace().c_str(), "/a/Ra");
54  EXPECT_STREQ( b.getNamespace().c_str(), "/a/Rb");
55  EXPECT_STREQ( c.getNamespace().c_str(), "/a/Rc");
56 }
57 
58 TEST(NamespaceRemappingTest, qualified_remaps)
59 {
60  ros::M_string local_remappings;
61  local_remappings.insert(std::make_pair("/a", "/Ra"));
62 
63  PRINT(ros::NodeHandle a("a", local_remappings)); // local_remappings don't apply to this nodehandle's name
64  PRINT(ros::NodeHandle sub_a(a, "a")); // remapping were fully qualified, so don't apply to /a/a
65 
66  EXPECT_STREQ( a.getNamespace().c_str(), "/a");
67  EXPECT_STREQ(sub_a.getNamespace().c_str(), "/a/a");
68 }
69 
70 TEST(NamespaceRemappingTest, unqualified_root_remaps)
71 {
72  ros::M_string local_remappings;
73  local_remappings.insert(std::make_pair("a", "Ra"));
74  local_remappings.insert(std::make_pair("b", "Rb"));
75 
76  ros::NodeHandle base("", local_remappings);
77  ros::NodeHandle a(base, "a");
78  ros::NodeHandle b(base, "b", ros::M_string());
79 
80  EXPECT_STREQ(a.getNamespace().c_str(), "/Ra");
81  EXPECT_STREQ(b.getNamespace().c_str(), "/Rb");
82 }
83 
84 TEST(NamespaceRemappingTest, tilde_namespaces)
85 {
86  ros::M_string local_remappings;
87  local_remappings.insert(std::make_pair("a", "Ra"));
88  local_remappings.insert(std::make_pair("b", "Rb"));
89 
90  ros::NodeHandle base("~", local_remappings);
91  ros::NodeHandle a(base, "a");
92  ros::NodeHandle b(base, "b", ros::M_string());
93 
94  EXPECT_STREQ(base.getNamespace().c_str(), ros::this_node::getName().c_str());
95  EXPECT_STREQ(a.getNamespace().c_str(), (ros::this_node::getName() + "/Ra").c_str());
96  EXPECT_STREQ(b.getNamespace().c_str(), (ros::this_node::getName() + "/Rb").c_str());
97 }
98 
99 int main(int argc, char** argv)
100 {
101  testing::InitGoogleTest(&argc, argv);
102  ros::init(argc, argv, "remapping_tester");
103  argc_ = argc;
104  argv_ = argv;
105  return RUN_ALL_TESTS();
106 }
int main(int argc, char **argv)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROSCPP_DECL const std::string & getName()
static char ** argv_
std::map< std::string, std::string > M_string
static int argc_
TEST(NamespaceRemappingTest, unqualified_remaps)
const std::string & getNamespace() const
#define PRINT(cmd)


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