unix_timer_tests.cc
Go to the documentation of this file.
00001 #include "gtest/gtest.h"
00002 #include "serial/impl/unix.h"
00003 
00004 #include <unistd.h>
00005 #include <stdlib.h>
00006 
00007 using serial::MillisecondTimer;
00008 
00009 namespace {
00010 
00015 TEST(timer_tests, short_intervals) {
00016   for (int trial = 0; trial < 100; trial++)
00017   {
00018     uint32_t ms = rand() % 20;
00019     MillisecondTimer mt(ms);
00020     usleep(1000 * ms);
00021     int32_t r = mt.remaining(); 
00022 
00023     // 1ms slush, for the cost of calling usleep.
00024     EXPECT_NEAR(r+1, 0, 1);
00025   }
00026 }
00027 
00028 TEST(timer_tests, overlapping_long_intervals) {
00029   MillisecondTimer* timers[10];
00030 
00031   // Experimentally determined. Corresponds to the extra time taken by the loops,
00032   // the big usleep, and the test infrastructure itself.
00033   const int slush_factor = 14;
00034 
00035   // Set up the timers to each time one second, 1ms apart.
00036   for (int t = 0; t < 10; t++)
00037   {
00038     timers[t] = new MillisecondTimer(1000);
00039     usleep(1000);
00040   }
00041 
00042   // Check in on them after 500ms.
00043   usleep(500000);
00044   for (int t = 0; t < 10; t++)
00045   {
00046     EXPECT_NEAR(timers[t]->remaining(), 500 - slush_factor + t, 5);
00047   }
00048 
00049   // Check in on them again after another 500ms and free them.
00050   usleep(500000);
00051   for (int t = 0; t < 10; t++)
00052   {
00053     EXPECT_NEAR(timers[t]->remaining(), -slush_factor + t, 5);
00054     delete timers[t];
00055   }
00056 }
00057 
00058 }  // namespace
00059 
00060 int main(int argc, char **argv) {
00061   ::testing::InitGoogleTest(&argc, argv);
00062   return RUN_ALL_TESTS();
00063 }


serial
Author(s): William Woodall , John Harrison
autogenerated on Mon Oct 6 2014 07:34:37