1 /* $NetBSD: bug-2803.c,v 1.2 2020/05/25 20:47:36 christos Exp $ */ 2 3 #include <config.h> 4 5 #include <stdio.h> 6 #include <sys/time.h> 7 8 #include <ntp_fp.h> 9 #include <timevalops.h> 10 11 #include "unity.h" 12 //#include "bug-2803.h" 13 14 /* microseconds per second */ 15 #define MICROSECONDS 1000000 16 17 int simpleTest(void); 18 void setUp(void); 19 void tearDown(void); 20 void test_main(void); 21 22 static int verbose = 1; // if not 0, also print results if test passed 23 static int exit_on_err = 0; // if not 0, exit if test failed 24 25 26 /* 27 * Test function calling the old and new code mentioned in 28 * http://bugs.ntp.org/show_bug.cgi?id=2803#c22 29 */ 30 static int do_test( struct timeval timetv, struct timeval tvlast ) 31 { 32 struct timeval tvdiff_old; 33 struct timeval tvdiff_new; 34 35 int cond_old; 36 int cond_new; 37 int failed; 38 39 cond_old = 0; 40 cond_new = 0; 41 42 // Here is the old code: 43 tvdiff_old = abs_tval(sub_tval(timetv, tvlast)); 44 if (tvdiff_old.tv_sec > 0) { 45 cond_old = 1; 46 } 47 48 // Here is the new code: 49 tvdiff_new = sub_tval(timetv, tvlast); 50 if (tvdiff_new.tv_sec != 0) { 51 cond_new = 1; 52 } 53 54 failed = cond_new != cond_old; 55 56 if ( failed || verbose ) 57 printf( "timetv %lli|%07li, tvlast %lli|%07li: tvdiff_old: %lli|%07li -> %i, tvdiff_new: %lli|%07li -> %i, same cond: %s\n", 58 (long long) timetv.tv_sec, timetv.tv_usec, 59 (long long) tvlast.tv_sec, tvlast.tv_usec, 60 (long long) tvdiff_old.tv_sec, tvdiff_old.tv_usec, cond_old, 61 (long long) tvdiff_new.tv_sec, tvdiff_new.tv_usec, cond_new, 62 failed ? "NO <<" : "yes" ); 63 64 return failed ? -1 : 0; 65 } 66 67 68 69 /* 70 * Call the test function in a loop for a given set of parameters. 71 * Both timetv and tvlast iterate over the given range, in all combinations. 72 */ 73 static 74 int test_loop( long long start_sec, long start_usec, 75 long long stop_sec, long stop_usec, 76 long long step_sec, long step_usec ) 77 { 78 struct timeval timetv; 79 struct timeval tvlast; 80 81 for ( timetv.tv_sec = start_sec; timetv.tv_sec <= stop_sec; timetv.tv_sec += step_sec ) 82 for ( timetv.tv_usec = start_usec; timetv.tv_usec <= stop_usec; timetv.tv_usec += step_usec ) 83 for ( tvlast.tv_sec = start_sec; tvlast.tv_sec <= stop_sec; tvlast.tv_sec += step_sec ) 84 for ( tvlast.tv_usec = start_usec; tvlast.tv_usec <= stop_usec; tvlast.tv_usec += step_usec ) 85 { 86 int rc = do_test( timetv, tvlast ); 87 if (rc < 0 && exit_on_err ) 88 return rc; 89 } 90 91 return 0; 92 } 93 94 95 96 int simpleTest( void ) 97 { 98 int x; 99 // loop from {0.0} to {1.1000000} stepping by tv_sec by 1 and tv_usec by 100000 100 x = test_loop( 0, 0, 1, MICROSECONDS, 1, MICROSECONDS / 10 ); 101 102 // x = test_loop( 0, 0, 5, MICROSECONDS, 1, MICROSECONDS / 1000 ); 103 // x = test_loop( 0, 0, -5, -MICROSECONDS, -1, -MICROSECONDS / 1000 ); 104 105 return x; 106 } 107 108 109 110 111 112 void setUp(void) 113 { 114 115 } 116 117 void tearDown(void) 118 { 119 } 120 121 122 void test_main( void ) 123 { 124 TEST_ASSERT_EQUAL(0, simpleTest()); 125 } 126