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
00024 EXPECT_NEAR(r+1, 0, 1);
00025 }
00026 }
00027
00028 TEST(timer_tests, overlapping_long_intervals) {
00029 MillisecondTimer* timers[10];
00030
00031
00032
00033 const int slush_factor = 14;
00034
00035
00036 for (int t = 0; t < 10; t++)
00037 {
00038 timers[t] = new MillisecondTimer(1000);
00039 usleep(1000);
00040 }
00041
00042
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
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 }
00059
00060 int main(int argc, char **argv) {
00061 ::testing::InitGoogleTest(&argc, argv);
00062 return RUN_ALL_TESTS();
00063 }