40 #include <gtest/gtest.h>
47 #include <test_roscpp/TestArray.h>
48 #include <test_roscpp/TestStringString.h>
50 #include <boost/scoped_ptr.hpp>
64 : expected_period_(period)
67 , testing_period_(false)
68 , calls_before_testing_period_(0)
76 bool first = last_call_.isZero();
84 if (time_error > 5.0 || time_error < -0.01)
86 ROS_ERROR(
"Call came at wrong time (expected: %f, expired: %f, callback: %f)",
96 if(total_calls_ == calls_before_testing_period_)
104 else if(total_calls_ == (calls_before_testing_period_+1))
112 else if(total_calls_ == (calls_before_testing_period_+2))
120 else if(total_calls_ == (calls_before_testing_period_+3))
133 timer_.setPeriod(p, reset);
134 expected_period_ = p;
156 TEST(RoscppTimerCallbacks, singleSteadyTimeCallback)
162 for (int32_t i = 0; i < 1000 && n.
ok(); ++i)
180 TEST(RoscppTimerCallbacks, multipleSteadyTimeCallbacks)
183 const int count = 100;
184 typedef boost::scoped_ptr<SteadyTimerHelper> HelperPtr;
185 HelperPtr helpers[count];
186 for (
int i = 0; i < count; ++i)
192 const int spin_count = 1000;
193 for (int32_t i = 0; i < spin_count && n.
ok(); ++i)
199 for (
int i = 0; i < count; ++i)
201 if (helpers[i]->failed_)
207 int32_t expected_count = (spin_count *
d.toSec()) / helpers[i]->expected_period_.toSec();
208 if (helpers[i]->total_calls_ < (expected_count - 1))
210 ROS_ERROR(
"Helper %d total calls: %d (at least %d expected)", i, helpers[i]->total_calls_, expected_count);
216 TEST(RoscppTimerCallbacks, steadySetPeriod)
294 TEST(RoscppTimerCallbacks, stopSteadyTimer)
299 for (int32_t i = 0; i < 1000 && n.
ok(); ++i)
309 for (int32_t i = 0; i < 1000 && n.
ok(); ++i)
324 TEST(RoscppTimerCallbacks, steadyStopThenSpin)
338 TEST(RoscppTimerCallbacks, oneShotSteadyTimer)
343 for (int32_t i = 0; i < 1000 && n.
ok(); ++i)
358 : expected_period_(period)
361 , testing_period_(false)
362 , calls_before_testing_period_(0)
370 bool first = last_call_.isZero();
378 if (time_error > 5.0 || time_error < -0.01)
380 ROS_ERROR(
"Call came at wrong time (expected: %f, expired: %f, callback: %f)",
390 if(total_calls_ == calls_before_testing_period_)
398 else if(total_calls_ == (calls_before_testing_period_+1))
406 else if(total_calls_ == (calls_before_testing_period_+2))
414 else if(total_calls_ == (calls_before_testing_period_+3))
427 timer_.setPeriod(p, reset);
428 expected_period_ = p;
450 TEST(RoscppTimerCallbacks, singleWallTimeCallback)
456 for (int32_t i = 0; i < 1000 && n.
ok(); ++i)
474 TEST(RoscppTimerCallbacks, multipleWallTimeCallbacks)
477 const int count = 100;
478 typedef boost::scoped_ptr<WallTimerHelper> HelperPtr;
479 HelperPtr helpers[count];
480 for (
int i = 0; i < count; ++i)
486 const int spin_count = 1000;
487 for (int32_t i = 0; i < spin_count && n.
ok(); ++i)
493 for (
int i = 0; i < count; ++i)
495 if (helpers[i]->failed_)
501 int32_t expected_count = (spin_count *
d.toSec()) / helpers[i]->expected_period_.toSec();
502 if (helpers[i]->total_calls_ < (expected_count - 1))
504 ROS_ERROR(
"Helper %d total calls: %d (at least %d expected)", i, helpers[i]->total_calls_, expected_count);
510 TEST(RoscppTimerCallbacks, setPeriod)
592 TEST(RoscppTimerCallbacks, stopWallTimer)
597 for (int32_t i = 0; i < 1000 && n.
ok(); ++i)
607 for (int32_t i = 0; i < 1000 && n.
ok(); ++i)
622 TEST(RoscppTimerCallbacks, stopThenSpin)
636 TEST(RoscppTimerCallbacks, oneShotWallTimer)
641 for (int32_t i = 0; i < 1000 && n.
ok(); ++i)
655 , expected_period_(period)
664 , expected_period_(r.expectedCycleTime())
684 TEST(RoscppTimerCallbacks, singleROSTimeCallback)
694 for (int32_t i = 0; i < 1000 && n.
ok(); ++i)
718 TEST(RoscppTimerCallbacks, singleROSTimeCallbackFromRate)
728 for (int32_t i = 0; i < 1000 && n.
ok(); ++i)
752 TEST(RoscppTimerCallbacks, oneshotROSTimer)
762 for (int32_t i = 0; i < 1000 && n.
ok(); ++i)
782 TEST(RoscppTimerCallbacks, singleROSTimeCallbackLargeTimestep)
792 for (int32_t i = 0; i < 100 && n.
ok(); ++i)
816 TEST(RoscppTimerCallbacks, multipleROSTimeCallbacks)
823 const int count = 100;
824 typedef boost::scoped_ptr<TimerHelper> HelperPtr;
825 HelperPtr helpers[count];
826 for (
int i = 0; i < count; ++i)
832 const int spin_count = 1000;
833 for (int32_t i = 0; i < spin_count && n.
ok(); ++i)
838 bool pending =
false;
843 for (
int i = 0; i < count; ++i)
845 pending |= helpers[i]->timer_.hasPending();
853 for (
int i = 0; i < count; ++i)
855 if (helpers[i]->failed_)
861 int32_t expected_count = (spin_count *
d.toSec()) / helpers[i]->expected_period_.toSec();
862 if (helpers[i]->total_calls_ < (expected_count - 1) || helpers[i]->total_calls_ > (expected_count + 1))
864 ROS_ERROR(
"Helper %d total calls: %d (%d expected)", i, helpers[i]->total_calls_, expected_count);
884 TEST(RoscppTimerCallbacks, trackedObject)
915 ASSERT_EQ(last_count,
g_count);
918 TEST(RoscppTimerCallbacks, stopROSTimer)
951 int main(
int argc,
char** argv)
953 testing::InitGoogleTest(&argc, argv);
956 return RUN_ALL_TESTS();