-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from clearpathrobotics/timespec-refactor2
Timespec refactor (again)
- Loading branch information
Showing
7 changed files
with
177 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
if(UNIX) | ||
catkin_add_gtest(${PROJECT_NAME}-test unix_serial_tests.cc) | ||
target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME} ${Boost_LIBRARIES}) | ||
if(NOT APPLE) | ||
target_link_libraries(${PROJECT_NAME}-test util) | ||
endif() | ||
|
||
catkin_add_gtest(${PROJECT_NAME}-test-timer unit/unix_timer_tests.cc) | ||
target_link_libraries(${PROJECT_NAME}-test-timer ${PROJECT_NAME}) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include "gtest/gtest.h" | ||
#include "serial/impl/unix.h" | ||
|
||
#include <unistd.h> | ||
#include <stdlib.h> | ||
|
||
using serial::MillisecondTimer; | ||
|
||
namespace { | ||
|
||
/** | ||
* Do 100 trials of timing gaps between 0 and 19 milliseconds. | ||
* Expect accuracy within one millisecond. | ||
*/ | ||
TEST(timer_tests, short_intervals) { | ||
for (int trial = 0; trial < 100; trial++) | ||
{ | ||
uint32_t ms = rand() % 20; | ||
MillisecondTimer mt(ms); | ||
usleep(1000 * ms); | ||
int32_t r = mt.remaining(); | ||
|
||
// 1ms slush, for the cost of calling usleep. | ||
EXPECT_NEAR(r+1, 0, 1); | ||
} | ||
} | ||
|
||
TEST(timer_tests, overlapping_long_intervals) { | ||
MillisecondTimer* timers[10]; | ||
|
||
// Experimentally determined. Corresponds to the extra time taken by the loops, | ||
// the big usleep, and the test infrastructure itself. | ||
const int slush_factor = 14; | ||
|
||
// Set up the timers to each time one second, 1ms apart. | ||
for (int t = 0; t < 10; t++) | ||
{ | ||
timers[t] = new MillisecondTimer(1000); | ||
usleep(1000); | ||
} | ||
|
||
// Check in on them after 500ms. | ||
usleep(500000); | ||
for (int t = 0; t < 10; t++) | ||
{ | ||
EXPECT_NEAR(timers[t]->remaining(), 500 - slush_factor + t, 5); | ||
} | ||
|
||
// Check in on them again after another 500ms and free them. | ||
usleep(500000); | ||
for (int t = 0; t < 10; t++) | ||
{ | ||
EXPECT_NEAR(timers[t]->remaining(), -slush_factor + t, 5); | ||
delete timers[t]; | ||
} | ||
} | ||
|
||
} // namespace | ||
|
||
int main(int argc, char **argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
File renamed without changes.