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