unittest_brake_test_utils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Pilz GmbH & Co. KG
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <atomic>
19 #include <algorithm>
20 #include <mutex>
21 #include <thread>
22 #include <memory>
23 
24 #include <gtest/gtest.h>
25 
26 #include <ros/ros.h>
27 #include <sensor_msgs/JointState.h>
28 
32 
34 {
35 
36 using namespace prbt_hardware_support;
37 using sensor_msgs::JointState;
38 using sensor_msgs::JointStatePtr;
39 using sensor_msgs::JointStateConstPtr;
40 
44 static ::testing::AssertionResult compareJointStateMessages(const JointStateConstPtr &msg1, const JointStateConstPtr &msg2)
45 {
46  if (msg1->name.size() != msg2->name.size())
47  {
48  return ::testing::AssertionFailure() << "Joint numbers in joint state messages do not match.";
49  }
50  if (!std::equal(msg1->name.begin(), msg1->name.end(), msg2->name.begin(),
51  [](std::string name1, std::string name2) { return name1 == name2; }))
52  {
53  return ::testing::AssertionFailure() << "Joint names in joint state messages do not match.";
54  }
55  if (msg1->position.size() != msg2->position.size())
56  {
57  return ::testing::AssertionFailure() << "Joint numbers in joint state messages do not match.";
58  }
60  {
61  return ::testing::AssertionFailure() << "Joint positions in joint state messages do not match.";
62  }
63 
64  return ::testing::AssertionSuccess();
65 }
66 
71 TEST(BrakeTestUtilsTest, testExceptionDtor)
72 {
73  {
74  std::shared_ptr<BrakeTestUtilsException> ex {new BrakeTestUtilsException("Test msg")};
75  }
76 
77  {
78  std::shared_ptr<GetCurrentJointStatesException> ex {new GetCurrentJointStatesException("Test msg")};
79  }
80 }
81 
96 TEST(BrakeTestUtilsTest, testCompareJointStatePositions)
97 {
98  /**********
99  * Step 1 *
100  **********/
101  JointStatePtr msg1{boost::make_shared<JointState>()};
102  msg1->name = {"joint1", "joint2"};
103  msg1->position = {0.1, 0.11};
104 
105  JointStatePtr msg2{boost::make_shared<JointState>()};
106  msg2->name = {"joint1", "joint2"};
107  msg2->position = {0.1, 0.11};
108 
109  EXPECT_TRUE(BrakeTestUtils::compareJointStatePositions(msg1, msg2));
110 
111  /**********
112  * Step 2 *
113  **********/
114  double tolerance = 0.0001;
115  for (size_t i = 0; i < msg2->position.size(); ++i)
116  {
117  msg2->position[i] += 0.9 * tolerance;
118  }
119 
120  EXPECT_TRUE(BrakeTestUtils::compareJointStatePositions(msg1, msg2, tolerance));
121 
122  /**********
123  * Step 3 *
124  **********/
125  for (size_t i = 0; i < msg2->position.size(); ++i)
126  {
127  for (size_t j = 0; j < msg2->position.size(); ++j)
128  {
129  msg2->position[j] = msg1->position[j];
130  }
131  msg2->position[i] += 1.1 * tolerance;
132 
133  EXPECT_FALSE(BrakeTestUtils::compareJointStatePositions(msg1, msg2, tolerance));
134  }
135 }
136 
148 TEST(BrakeTestUtilsTest, testGetCurrentJointStates)
149 {
150  /**********
151  * Step 1 *
152  **********/
154 
155  /**********
156  * Step 2 *
157  **********/
158  JointStatesPublisherMock joint_states_pub;
159  auto expected_msg = joint_states_pub.getNextMessage();
160  joint_states_pub.startAsync();
161 
162  try
163  {
165  EXPECT_TRUE(compareJointStateMessages(msg, expected_msg));
166  }
167  catch (const std::exception &e)
168  {
169  ADD_FAILURE() << e.what();
170  }
171 
172  joint_states_pub.terminate();
173 }
174 
186 TEST(BrakeTestUtilsTest, testDetectRobotMotion)
187 {
188  /**********
189  * Step 1 *
190  **********/
191  JointStatesPublisherMock joint_states_pub;
192  joint_states_pub.startAsync();
193 
194  EXPECT_FALSE(BrakeTestUtils::detectRobotMotion());
195 
196  joint_states_pub.terminate();
197 
198  /**********
199  * Step 2 *
200  **********/
201  joint_states_pub.startAsync(true);
202 
203  EXPECT_TRUE(BrakeTestUtils::detectRobotMotion());
204 }
205 
206 } // namespace brake_test_utils_test
207 
208 int main(int argc, char *argv[])
209 {
210  ros::init(argc, argv, "unittest_brake_test_utils");
211  ros::NodeHandle nh;
212 
213  testing::InitGoogleTest(&argc, argv);
214  return RUN_ALL_TESTS();
215 }
msg
int main(int argc, char *argv[])
static bool detectRobotMotion(double timeout_s=DEFAULT_ROBOT_MOTION_TIMEOUT_S)
return true if a robot motion was detected, false otherwise.
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
::testing::AssertionResult compareJointStateMessages(const JointStateConstPtr &msg1, const JointStateConstPtr &msg2)
Checks for identical names and positions in joint state messages.
static sensor_msgs::JointStateConstPtr getCurrentJointStates()
wait for a single message on the joint_states topic and return it.
void startAsync(bool move=false)
Start a new thread publishing joint states.
Asynchronously publishes predefined messages on the /joint_states topic with rate ~100Hz...
Expection thrown by BrakeTestUtils::getCurrentJointStates().
TEST(BrakeTestUtilsTest, testExceptionDtor)
Test increases function coverage by ensuring that all Dtor variants are called.
Exception thrown by a BrakeTestUtils function.
sensor_msgs::JointStateConstPtr getNextMessage()
Obtain the message that is published next.
static bool compareJointStatePositions(const sensor_msgs::JointStateConstPtr &msg1, const sensor_msgs::JointStateConstPtr &msg2, const double tol=DEFAULT_JOINT_STATES_COMPARISON_TOLERANCE)
return true if the joint state positions are equal up to a given tolerance, false otherwise...


prbt_hardware_support
Author(s):
autogenerated on Tue Feb 2 2021 03:50:17