1212 t_error("%s failed (errno = %d)\n", #c, errno); \
1313 } while (0)
1414
15- static void test_elapsed_not_too_long (struct timespec * start_time ,
16- struct timespec * end_time ,
17- long ns_to_sleep ) {
15+ static void test_elapsed (struct timespec * start_time , struct timespec * end_time ,
16+ long ns_to_sleep ) {
1817 TEST (end_time -> tv_sec - start_time -> tv_sec <= 1 );
1918
2019 long nanoseconds_elapsed = (end_time -> tv_sec - start_time -> tv_sec ) * 1E9 -
2120 start_time -> tv_nsec + end_time -> tv_nsec ;
22-
23- // Test that the difference between the requested amount of sleep
24- // and the actual elapsed time is within an acceptable margin
25- double difference = abs (nanoseconds_elapsed - ns_to_sleep ) / ns_to_sleep ;
26-
27- // Allow the actual sleep time to be twice as much as the requested time
28- TEST (difference <= 1 );
21+ TEST (nanoseconds_elapsed >= ns_to_sleep );
2922}
3023
3124int main (void ) {
@@ -45,7 +38,7 @@ int main(void) {
4538 TEST (clock_nanosleep (CLOCK_MONOTONIC , 0 , & time_to_sleep , NULL ) == 0 );
4639 clock_gettime (CLOCK_MONOTONIC , & end_time );
4740
48- test_elapsed_not_too_long (& start_time , & end_time , ns_to_sleep );
41+ test_elapsed (& start_time , & end_time , ns_to_sleep );
4942
5043 // Sleep for another 50ms to get us pretty far from 5ms from the start of
5144 // this process to increase the chance the next test fails if it's not
@@ -64,7 +57,7 @@ int main(void) {
6457 0 );
6558 clock_gettime (CLOCK_MONOTONIC , & end_time );
6659
67- test_elapsed_not_too_long (& start_time , & end_time , ns_to_sleep );
60+ test_elapsed (& start_time , & end_time , ns_to_sleep );
6861
6962 return t_status ;
7063}
0 commit comments