22 #include <boost/bind.hpp> 27 #define EPSILON 0.000000002 35 using namespace boost;
39 hbg = TimeService::Instance();
40 long_ns = 9007199254740992LL;
43 long_S = 9007199.254740992;
45 normal_ns = 1000000000;
50 normal_t = 1000000000;
56 hbg->enableSystemClock(
true );
62 std::vector< std::pair<Timer::TimerId, Seconds> >
occured;
69 mstart = TimeService::Instance()->secondsSince(0);
73 Seconds now = TimeService::Instance()->secondsSince( 0 );
74 occured.push_back( std::make_pair(
id, now) );
125 BOOST_REQUIRE_CLOSE( (
double)long_ns , (
double)TimeService::ticks2nsecs( TimeService::nsecs2ticks( long_ns )), margin );
126 BOOST_REQUIRE_CLOSE( (
double)normal_ns, (
double)TimeService::ticks2nsecs( TimeService::nsecs2ticks( normal_ns )), margin );
128 BOOST_REQUIRE_CLOSE( (
double)long_t , (
double)TimeService::nsecs2ticks( TimeService::ticks2nsecs( long_t )), margin );
129 BOOST_REQUIRE_CLOSE( (
double)normal_t, (
double)TimeService::nsecs2ticks( TimeService::ticks2nsecs( normal_t )), margin );
138 BOOST_CHECK( t != hbg->getTicks() );
139 BOOST_CHECK( 0 != hbg->ticksSince(t) );
140 BOOST_CHECK( 0 != hbg->secondsSince(t) );
149 hbg->enableSystemClock(
false );
151 BOOST_REQUIRE_EQUAL( t , hbg->getTicks() );
155 Seconds change_S = 0.123456789;
157 hbg->secondsChange( change_S );
158 BOOST_CHECK( t != hbg->getTicks() );
159 BOOST_CHECK( -
EPSILON < (change_S - hbg->secondsSince(t)) &&
160 EPSILON > (change_S - hbg->secondsSince(t)) );
163 hbg->enableSystemClock(
true );
164 BOOST_CHECK( t != hbg->getTicks() );
166 BOOST_CHECK(
Seconds(0.0) != hbg->secondsSince(t) );
173 Seconds now = hbg->secondsSince( 0 );
175 BOOST_CHECK( timer.arm(0, 0.5) );
176 BOOST_CHECK( timer.arm(1, 0.6) );
177 BOOST_CHECK( timer.arm(2, 0.5) );
179 BOOST_CHECK( timer.arm(3, 0.8) );
180 BOOST_CHECK( timer.arm(3, 0.9) );
182 BOOST_CHECK( timer.isArmed( 0 ) );
183 BOOST_CHECK( timer.isArmed( 1 ) );
184 BOOST_CHECK( timer.isArmed( 2 ) );
185 BOOST_CHECK( timer.isArmed( 3 ) );
190 BOOST_CHECK( !timer.isArmed( 0 ) );
191 BOOST_CHECK( !timer.isArmed( 1 ) );
192 BOOST_CHECK( !timer.isArmed( 2 ) );
193 BOOST_CHECK( !timer.isArmed( 3 ) );
196 BOOST_CHECK( timer.
occured.size() == 4 );
197 BOOST_CHECK( timer.
occured[0].first == 0 );
198 BOOST_CHECK( timer.
occured[1].first == 2 );
199 BOOST_CHECK( timer.
occured[2].first == 1 );
200 BOOST_CHECK( timer.
occured[3].first == 3 );
203 BOOST_REQUIRE_CLOSE( timer.
occured[0].second, now+0.5, 0.1 );
204 BOOST_REQUIRE_CLOSE( timer.
occured[1].second, now+0.5, 0.1 );
205 BOOST_REQUIRE_CLOSE( timer.
occured[2].second, now+0.6, 0.1 );
206 BOOST_REQUIRE_CLOSE( timer.
occured[3].second, now+0.9, 0.1 );
209 BOOST_CHECK( timer.arm(4, -0.1) ==
false);
210 BOOST_CHECK( timer.arm(32, 0.1) ==
false);
215 BOOST_CHECK( timer.arm(10, 0.5) );
216 timer.setMaxTimers( 5 );
218 BOOST_CHECK( timer.
occured.size() == 0 );
224 Seconds now = hbg->secondsSince( 0 );
226 BOOST_CHECK( timer.startTimer(0, 0.1) );
227 BOOST_CHECK( timer.startTimer(1, 0.6) );
228 BOOST_CHECK( timer.startTimer(2, 0.5) );
230 BOOST_CHECK( timer.startTimer(3, 0.5) );
231 BOOST_CHECK( timer.startTimer(3, 0.2) );
233 BOOST_CHECK( timer.isArmed( 0 ) );
234 BOOST_CHECK( timer.isArmed( 1 ) );
235 BOOST_CHECK( timer.isArmed( 2 ) );
236 BOOST_CHECK( timer.isArmed( 3 ) );
241 BOOST_CHECK( timer.killTimer( 0 ) );
242 BOOST_CHECK( timer.killTimer( 1 ) );
243 BOOST_CHECK( timer.killTimer( 2 ) );
244 BOOST_CHECK( timer.killTimer( 3 ) );
245 BOOST_CHECK( !timer.isArmed( 0 ) );
246 BOOST_CHECK( !timer.isArmed( 1 ) );
247 BOOST_CHECK( !timer.isArmed( 2 ) );
248 BOOST_CHECK( !timer.isArmed( 3 ) );
252 BOOST_CHECK( timer.
occured[0].first == 0 );
253 BOOST_CHECK( timer.
occured[1].first == 0 );
254 BOOST_CHECK( timer.
occured[2].first == 3 );
255 BOOST_CHECK( timer.
occured[3].first == 0 );
256 BOOST_CHECK( timer.
occured[4].first == 0 );
257 BOOST_CHECK( timer.
occured[5].first == 3 );
259 BOOST_CHECK( timer.
occured[6].first == 0 );
260 BOOST_CHECK( timer.
occured[7].first == 2 );
262 BOOST_CHECK( timer.
occured[8].first == 0 );
263 BOOST_CHECK( timer.
occured[9].first == 1 );
264 BOOST_CHECK( timer.
occured[10].first == 3 );
265 BOOST_CHECK( timer.
occured[11].first == 0 );
266 BOOST_CHECK( timer.
occured[12].first == 0 );
267 BOOST_CHECK( timer.
occured[13].first == 3 );
268 BOOST_CHECK( timer.
occured[14].first == 0 );
271 BOOST_REQUIRE_CLOSE( timer.
occured[0].second, now+0.1, 0.1 );
272 BOOST_REQUIRE_CLOSE( timer.
occured[1].second, now+0.2, 0.1 );
273 BOOST_REQUIRE_CLOSE( timer.
occured[2].second, now+0.2, 0.1 );
274 BOOST_REQUIRE_CLOSE( timer.
occured[3].second, now+0.3, 0.1 );
275 BOOST_REQUIRE_CLOSE( timer.
occured[4].second, now+0.4, 0.1 );
276 BOOST_REQUIRE_CLOSE( timer.
occured[5].second, now+0.4, 0.1 );
277 BOOST_REQUIRE_CLOSE( timer.
occured[6].second, now+0.5, 0.1 );
278 BOOST_REQUIRE_CLOSE( timer.
occured[7].second, now+0.5, 0.1 );
279 BOOST_REQUIRE_CLOSE( timer.
occured[8].second, now+0.6, 0.1 );
280 BOOST_REQUIRE_CLOSE( timer.
occured[9].second, now+0.6, 0.1 );
281 BOOST_REQUIRE_CLOSE( timer.
occured[10].second, now+0.6, 0.1 );
282 BOOST_REQUIRE_CLOSE( timer.
occured[11].second, now+0.7, 0.1 );
283 BOOST_REQUIRE_CLOSE( timer.
occured[12].second, now+0.8, 0.1 );
284 BOOST_REQUIRE_CLOSE( timer.
occured[13].second, now+0.8, 0.1 );
285 BOOST_REQUIRE_CLOSE( timer.
occured[14].second, now+0.9, 0.1 );
288 BOOST_CHECK( timer.startTimer(4, -0.1) ==
false);
289 BOOST_CHECK( timer.startTimer(500, 0.1) ==
false);
294 BOOST_CHECK( timer.startTimer(10, 0.5) );
295 timer.setMaxTimers( 5 );
297 BOOST_CHECK( timer.
occured.size() == 0 );
303 Seconds now = hbg->secondsSince( 0 );
306 BOOST_CHECK( timer.arm(0, 1.0) );
307 BOOST_CHECK( timer.isArmed( 0 ) == true );
308 BOOST_CHECK( timer.waitFor( 0 ) == true );
309 BOOST_CHECK( timer.isArmed( 0 ) == false );
310 BOOST_REQUIRE_CLOSE( hbg->secondsSince(0), now + 1.0, 0.1 );
313 now = hbg->secondsSince( 0 );
314 BOOST_CHECK( timer.arm(0, 1.0) );
315 BOOST_CHECK( timer.isArmed( 0 ) == true );
317 BOOST_CHECK( timer.isArmed( 0 ) == true );
318 BOOST_REQUIRE_CLOSE( hbg->secondsSince( 0 ), now + 0.5, 0.1 );
320 BOOST_CHECK( timer.isArmed( 0 ) == false );
323 now = hbg->secondsSince( 0 );
324 BOOST_CHECK( timer.arm(0, 1.0) );
325 BOOST_CHECK( timer.isArmed( 0 ) );
327 timer.
mcallback = boost::bind(&Timer::killTimer, &timer, 0);
328 BOOST_CHECK( timer.arm(1, 0.5) );
329 BOOST_CHECK( timer.isArmed( 1 ) );
331 BOOST_CHECK( timer.waitFor( 0 ) );
333 BOOST_CHECK( timer.isArmed( 0 ) == false );
334 BOOST_CHECK( timer.isArmed( 1 ) == false );
335 BOOST_REQUIRE_CLOSE( hbg->secondsSince(0), now + 0.5, 0.1 );
338 now = hbg->secondsSince( 0 );
339 BOOST_CHECK( timer.arm(0, 1.0) );
340 BOOST_CHECK( timer.isArmed( 0 ) );
342 timer.
mcallback = boost::bind(&ActivityInterface::stop, timer.getActivity());
343 BOOST_CHECK( timer.arm(1, 0.5) );
344 BOOST_CHECK( timer.isArmed( 1 ) );
346 BOOST_CHECK( timer.waitFor( 0 ) );
348 BOOST_CHECK( timer.isArmed( 0 ) == false );
349 BOOST_CHECK( timer.isArmed( 1 ) == false );
350 BOOST_REQUIRE_CLOSE( hbg->secondsSince(0), now + 0.5, 0.1 );
void timeout(Timer::TimerId id)
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
boost::function< void(Timer::TimerId)> mcallback
Seconds nsecs_to_Seconds(const nsecs ns)
#define BOOST_AUTO_TEST_SUITE_END()
int usleep(unsigned int us)
const int HighestPriority
unsigned int sleep(unsigned int s)
BOOST_AUTO_TEST_CASE(testSecondsConversion)
nsecs Seconds_to_nsecs(const Seconds s)
TimeService::Seconds mstart
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
std::vector< std::pair< Timer::TimerId, Seconds > > occured