test_utils.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #ifndef ROS_IGN_IMAGE__TEST_UTILS_H_
19 #define ROS_IGN_IMAGE__TEST_UTILS_H_
20 
21 #include <gtest/gtest.h>
22 #include <ros/ros.h>
23 #include <std_msgs/Header.h>
24 #include <sensor_msgs/Image.h>
25 #include <chrono>
26 #include <string>
27 #include <thread>
28 #include <ignition/msgs.hh>
29 
30 namespace ros_ign_image
31 {
32 namespace testing
33 {
43  template <class Rep, class Period>
45  bool &_boolVar,
46  const std::chrono::duration<Rep, Period> &_sleepEach,
47  const int _retries)
48  {
49  int i = 0;
50  while (!_boolVar && i < _retries)
51  {
52  ++i;
53  std::this_thread::sleep_for(_sleepEach);
54  }
55  }
56 
66  template <class Rep, class Period>
68  bool &_boolVar,
69  const std::chrono::duration<Rep, Period> &_sleepEach,
70  const int _retries)
71  {
72  int i = 0;
73  while (!_boolVar && i < _retries)
74  {
75  ++i;
76  std::this_thread::sleep_for(_sleepEach);
77  ros::spinOnce();
78  }
79  }
80 
84 
87  void createTestMsg(std_msgs::Header &_msg)
88  {
89  _msg.seq = 1;
90  _msg.stamp.sec = 2;
91  _msg.stamp.nsec = 3;
92  _msg.frame_id = "frame_id_value";
93  }
94 
97  void compareTestMsg(const std_msgs::Header &_msg)
98  {
99  std_msgs::Header expected_msg;
100  createTestMsg(expected_msg);
101 
102  EXPECT_GE(expected_msg.seq, 0u);
103  EXPECT_EQ(expected_msg.stamp.sec, _msg.stamp.sec);
104  EXPECT_EQ(expected_msg.stamp.nsec, _msg.stamp.nsec);
105  EXPECT_EQ(expected_msg.frame_id, _msg.frame_id);
106  }
107 
110  void createTestMsg(sensor_msgs::Image &_msg)
111  {
112  std_msgs::Header header_msg;
113  createTestMsg(header_msg);
114 
115  _msg.header = header_msg;
116  _msg.width = 320;
117  _msg.height = 240;
118  _msg.encoding = "rgb8";
119  _msg.is_bigendian = false;
120  _msg.step = _msg.width * 3;
121  _msg.data.resize(_msg.height * _msg.step, '1');
122  }
123 
126  void compareTestMsg(const sensor_msgs::Image &_msg)
127  {
128  sensor_msgs::Image expected_msg;
129  createTestMsg(expected_msg);
130 
131  compareTestMsg(_msg.header);
132  EXPECT_EQ(expected_msg.width, _msg.width);
133  EXPECT_EQ(expected_msg.height, _msg.height);
134  EXPECT_EQ(expected_msg.encoding, _msg.encoding);
135  EXPECT_EQ(expected_msg.is_bigendian, _msg.is_bigendian);
136  EXPECT_EQ(expected_msg.step, _msg.step);
137  }
138 
142 
145  void createTestMsg(ignition::msgs::Header &_msg)
146  {
147  auto seq_entry = _msg.add_data();
148  seq_entry->set_key("seq");
149  seq_entry->add_value("1");
150  _msg.mutable_stamp()->set_sec(2);
151  _msg.mutable_stamp()->set_nsec(3);
152  auto frame_id_entry = _msg.add_data();
153  frame_id_entry->set_key("frame_id");
154  frame_id_entry->add_value("frame_id_value");
155  }
156 
159  void compareTestMsg(const ignition::msgs::Header &_msg)
160  {
161  ignition::msgs::Header expected_msg;
162  createTestMsg(expected_msg);
163 
164  EXPECT_EQ(expected_msg.stamp().sec(), _msg.stamp().sec());
165  EXPECT_EQ(expected_msg.stamp().nsec(), _msg.stamp().nsec());
166  EXPECT_GE(_msg.data_size(), 2);
167  EXPECT_EQ(expected_msg.data(0).key(), _msg.data(0).key());
168  EXPECT_EQ(1, _msg.data(0).value_size());
169  std::string value = _msg.data(0).value(0);
170  try
171  {
172  uint32_t ul = std::stoul(value, nullptr);
173  EXPECT_GE(ul, 0u);
174  }
175  catch (std::exception & e)
176  {
177  FAIL();
178  }
179  EXPECT_EQ(expected_msg.data(1).key(), _msg.data(1).key());
180  EXPECT_EQ(1, _msg.data(1).value_size());
181  EXPECT_EQ(expected_msg.data(1).value(0), _msg.data(1).value(0));
182  }
183 
186  void createTestMsg(ignition::msgs::Image &_msg)
187  {
188  ignition::msgs::Header header_msg;
189  createTestMsg(header_msg);
190 
191  _msg.mutable_header()->CopyFrom(header_msg);
192  _msg.set_width(320);
193  _msg.set_height(240);
194  _msg.set_pixel_format_type(ignition::msgs::PixelFormatType::RGB_INT8);
195  _msg.set_step(_msg.width() * 3);
196  _msg.set_data(std::string(_msg.height() * _msg.step(), '1'));
197  }
198 
201  void compareTestMsg(const ignition::msgs::Image &_msg)
202  {
203  ignition::msgs::Image expected_msg;
204  createTestMsg(expected_msg);
205 
206  compareTestMsg(_msg.header());
207  EXPECT_EQ(expected_msg.width(), _msg.width());
208  EXPECT_EQ(expected_msg.height(), _msg.height());
209  EXPECT_EQ(expected_msg.pixel_format_type(), _msg.pixel_format_type());
210  EXPECT_EQ(expected_msg.step(), _msg.step());
211  EXPECT_EQ(expected_msg.data(), _msg.data());
212  }
213 }
214 }
215 
216 #endif // ROS_IGN_BRIDGE__TEST_UTILS_H_
ros.h
ros::spinOnce
ROSCPP_DECL void spinOnce()
ros_ign_image::testing::createTestMsg
void createTestMsg(std_msgs::Header &_msg)
ROS test utils.
Definition: test_utils.h:87
ros_ign_image::testing::compareTestMsg
void compareTestMsg(const std_msgs::Header &_msg)
Compare a message with the populated for testing.
Definition: test_utils.h:97
ros_ign_image::testing::waitUntilBoolVarAndSpin
void waitUntilBoolVarAndSpin(bool &_boolVar, const std::chrono::duration< Rep, Period > &_sleepEach, const int _retries)
Wait until a boolean variable is set to true for a given number of times. This function calls ros::sp...
Definition: test_utils.h:67
ros_ign_image
Definition: test_utils.h:30
ros_ign_image::testing::waitUntilBoolVar
void waitUntilBoolVar(bool &_boolVar, const std::chrono::duration< Rep, Period > &_sleepEach, const int _retries)
Wait until a boolean variable is set to true for a given number of times.
Definition: test_utils.h:44


ros_ign_image
Author(s):
autogenerated on Sat Mar 11 2023 04:02:19