Go to the documentation of this file.00001 #include <cmath>
00002 #include <stdexcept>
00003
00004 #include <gtest/gtest.h>
00005
00006 #include <ros/ros.h>
00007 #include <ros/message.h>
00008 #include <ros/serialization.h>
00009 #include <sensor_msgs/LaserScan.h>
00010
00011 #include <lama_interfaces/AddInterface.h>
00012 #include <lama_interfaces/GetVectorDouble.h>
00013 #include <lama_interfaces/SetVectorDouble.h>
00014 #include <lama_interfaces/GetVectorLaserScan.h>
00015 #include <lama_interfaces/SetVectorLaserScan.h>
00016
00017 #define PREPARE_GETTER_SETTER(iface_name, msg, iface_type) \
00018 std::string get_service_name; \
00019 std::string set_service_name; \
00020 if (!initMapInterface(#iface_name, "lama_interfaces/Get" #msg, "lama_interfaces/Set" #msg, get_service_name, set_service_name, iface_type)) \
00021 { \
00022 throw std::runtime_error("Could not create map interface iface_name"); \
00023 } \
00024 ros::NodeHandle nh; \
00025 ros::ServiceClient getter = nh.serviceClient<lama_interfaces::Get##msg>(get_service_name); \
00026 getter.waitForExistence(); \
00027 ros::ServiceClient setter = nh.serviceClient<lama_interfaces::Set##msg>(set_service_name); \
00028 setter.waitForExistence();
00029
00030 #define PREPARE_GETTER_SETTER_SERIALIZED(iface_name, msg) PREPARE_GETTER_SETTER(iface_name, msg, lama_interfaces::AddInterfaceRequest::SERIALIZED)
00031 #define PREPARE_GETTER_SETTER_CLEARTEXT(iface_name, msg) PREPARE_GETTER_SETTER(iface_name, msg, lama_interfaces::AddInterfaceRequest::CLEARTEXT)
00032
00033 bool initMapInterface(const std::string& interface_name,
00034 const std::string& get_service_message,
00035 const std::string& set_service_message,
00036 std::string& get_service_name,
00037 std::string& set_service_name,
00038 uint32_t interface_type)
00039 {
00040 ros::NodeHandle nh;
00041 ros::ServiceClient client = nh.serviceClient<lama_interfaces::AddInterface>("interface_factory");
00042 client.waitForExistence();
00043 lama_interfaces::AddInterface srv;
00044 srv.request.interface_name = interface_name;
00045 srv.request.interface_type = interface_type;
00046 srv.request.get_service_message = get_service_message;
00047 srv.request.set_service_message = set_service_message;
00048 if (!client.call(srv))
00049 {
00050 ROS_ERROR_STREAM("Failed to create the Lama interface " << interface_name);
00051 return false;
00052 }
00053 get_service_name = srv.response.get_service_name;
00054 set_service_name = srv.response.set_service_name;
00055 return true;
00056 }
00057
00058 testing::AssertionResult headerEqual(const std_msgs::Header& a, const std_msgs::Header& b)
00059 {
00060 if (a.seq != b.seq)
00061 {
00062 return testing::AssertionFailure() << "seq differ: " << a.seq << " is not " << b.seq;
00063 }
00064 if (a.frame_id != b.frame_id)
00065 {
00066 return testing::AssertionFailure() << "frame_id differ: " << a.frame_id << " is not " << b.frame_id;
00067 }
00068
00069
00070
00071 return testing::AssertionSuccess();
00072 }
00073
00074 testing::AssertionResult laserScanEqual(sensor_msgs::LaserScan& a, sensor_msgs::LaserScan& b)
00075 {
00076 if (!headerEqual(a.header, b.header))
00077 {
00078 return testing::AssertionFailure() << "header differ";
00079 }
00080
00081 if (a.angle_min != b.angle_min)
00082 {
00083 return testing::AssertionFailure() << "angle_min differ: " << a.angle_min << " is not " << b.angle_min;
00084 }
00085
00086 if (a.angle_increment != b.angle_increment)
00087 {
00088 return testing::AssertionFailure() << "angle_increment differ: " << a.angle_increment << " is not " << b.angle_increment;
00089 }
00090
00091
00092
00093 if (a.angle_max != b.angle_max)
00094 {
00095 return testing::AssertionFailure() << "angle_max differ: " << a.angle_max << " is not " << b.angle_max;
00096 }
00097
00098 if (a.ranges != b.ranges)
00099 {
00100 return testing::AssertionFailure() << "ranges differ";
00101 }
00102
00103 return testing::AssertionSuccess();
00104 }
00105
00106 TEST(TestSuite, testVectorDoubleSerialized)
00107 {
00108 PREPARE_GETTER_SETTER_SERIALIZED(test_roscpp_double_serialized, VectorDouble);
00109
00110 lama_interfaces::SetVectorDouble set_srv;
00111 set_srv.request.descriptor.push_back(45.69);
00112 set_srv.request.descriptor.push_back(-46.3);
00113
00114 setter.call(set_srv);
00115
00116 lama_interfaces::GetVectorDouble get_srv;
00117 get_srv.request.id = set_srv.response.id;
00118 getter.call(get_srv);
00119
00120 ASSERT_EQ(set_srv.request.descriptor.size(), get_srv.response.descriptor.size());
00121 for (size_t i = 0; i < set_srv.request.descriptor.size(); ++i)
00122 {
00123 EXPECT_EQ(set_srv.request.descriptor[i], get_srv.response.descriptor[i]);
00124 }
00125 }
00126
00127 TEST(TestSuite, testVectorLaserScanSerialized)
00128 {
00129 PREPARE_GETTER_SETTER_SERIALIZED(test_roscpp_laserscan_serialized, VectorLaserScan);
00130
00131 sensor_msgs::LaserScan scan0;
00132 scan0.header.seq = 1;
00133 scan0.header.stamp = ros::Time::now();
00134 scan0.header.frame_id = "frame0";
00135 scan0.angle_min = -M_PI;
00136 scan0.angle_max = M_PI;
00137 scan0.range_max = 10.;
00138 scan0.ranges.push_back(0);
00139 scan0.ranges.push_back(1);
00140 sensor_msgs::LaserScan scan1;
00141 scan1.header.seq = 2;
00142 scan1.header.stamp = ros::Time::now();
00143 scan1.header.frame_id = "frame1";
00144 scan1.angle_min = -M_PI / 2;
00145 scan1.angle_max = M_PI / 2;
00146 scan1.range_max = 9.;
00147 scan1.ranges.push_back(2);
00148 scan1.ranges.push_back(3);
00149
00150 lama_interfaces::SetVectorLaserScan set_srv;
00151 set_srv.request.descriptor.push_back(scan0);
00152 set_srv.request.descriptor.push_back(scan1);
00153 setter.call(set_srv);
00154
00155 lama_interfaces::GetVectorLaserScan get_srv;
00156 get_srv.request.id = set_srv.response.id;
00157 getter.call(get_srv);
00158
00159 ASSERT_EQ(set_srv.request.descriptor.size(), get_srv.response.descriptor.size());
00160 for (size_t i = 0; i < set_srv.request.descriptor.size(); ++i)
00161 {
00162 EXPECT_TRUE(laserScanEqual(set_srv.request.descriptor[i], get_srv.response.descriptor[i]));
00163 }
00164 }
00165
00166 TEST(TestSuite, testVectorDoubleCleartext)
00167 {
00168 PREPARE_GETTER_SETTER_CLEARTEXT(test_roscpp_double_cleartext, VectorDouble);
00169
00170 lama_interfaces::SetVectorDouble set_srv;
00171 set_srv.request.descriptor.push_back(45.69);
00172 set_srv.request.descriptor.push_back(-46.3);
00173
00174 setter.call(set_srv);
00175
00176 lama_interfaces::GetVectorDouble get_srv;
00177 get_srv.request.id = set_srv.response.id;
00178 getter.call(get_srv);
00179
00180 ASSERT_EQ(set_srv.request.descriptor.size(), get_srv.response.descriptor.size());
00181 for (size_t i = 0; i < set_srv.request.descriptor.size(); ++i)
00182 {
00183 EXPECT_EQ(set_srv.request.descriptor[i], get_srv.response.descriptor[i]);
00184 }
00185 }
00186
00187 TEST(TestSuite, testVectorLaserScanCleartext)
00188 {
00189 PREPARE_GETTER_SETTER_CLEARTEXT(test_roscpp_laserscan_cleartext, VectorLaserScan);
00190
00191 sensor_msgs::LaserScan scan0;
00192 scan0.header.seq = 1;
00193 scan0.header.stamp = ros::Time::now();
00194 scan0.header.frame_id = "frame0";
00195 scan0.angle_min = -M_PI;
00196 scan0.angle_max = M_PI;
00197 scan0.range_max = 10.;
00198 scan0.ranges.push_back(0);
00199 scan0.ranges.push_back(1);
00200 sensor_msgs::LaserScan scan1;
00201 scan1.header.seq = 2;
00202 scan1.header.stamp = ros::Time::now();
00203 scan1.header.frame_id = "frame1";
00204 scan1.angle_min = -M_PI / 2;
00205 scan1.angle_max = M_PI / 2;
00206 scan1.range_max = 9.;
00207 scan1.ranges.push_back(2);
00208 scan1.ranges.push_back(3);
00209
00210 lama_interfaces::SetVectorLaserScan set_srv;
00211 set_srv.request.descriptor.push_back(scan0);
00212 set_srv.request.descriptor.push_back(scan1);
00213 setter.call(set_srv);
00214
00215 lama_interfaces::GetVectorLaserScan get_srv;
00216 get_srv.request.id = set_srv.response.id;
00217 getter.call(get_srv);
00218
00219 ASSERT_EQ(set_srv.request.descriptor.size(), get_srv.response.descriptor.size());
00220 for (size_t i = 0; i < set_srv.request.descriptor.size(); ++i)
00221 {
00222 EXPECT_TRUE(laserScanEqual(set_srv.request.descriptor[i], get_srv.response.descriptor[i]));
00223 }
00224 }
00225
00226 int main(int argc, char** argv)
00227 {
00228 ros::init(argc, argv, "test_cpp_interface_factory");
00229 testing::InitGoogleTest(&argc, argv);
00230 return RUN_ALL_TESTS();
00231 }
00232