7 #include <timevalops.h>
9 /* microseconds per second */
10 #define MICROSECONDS 1000000
13 static int verbose = 1; // if not 0, also print results if test passed
14 static int exit_on_err = 0; // if not 0, exit if test failed
18 * Test function calling the old and new code mentioned in
19 * http://bugs.ntp.org/show_bug.cgi?id=2803#c22
22 int do_test( struct timeval timetv, struct timeval tvlast )
24 struct timeval tvdiff_old;
25 struct timeval tvdiff_new;
34 // Here is the old code:
35 tvdiff_old = abs_tval(sub_tval(timetv, tvlast));
36 if (tvdiff_old.tv_sec > 0) {
40 // Here is the new code:
41 tvdiff_new = sub_tval(timetv, tvlast);
42 if (tvdiff_new.tv_sec != 0) {
46 failed = cond_new != cond_old;
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" );
56 return failed ? -1 : 0;
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.
66 int 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 )
70 struct timeval timetv;
71 struct timeval tvlast;
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 )
78 int rc = do_test( timetv, tvlast );
79 if (rc < 0 && exit_on_err )
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 );
94 // test_loop( 0, 0, 5, MICROSECONDS, 1, MICROSECONDS / 1000 );
95 // test_loop( 0, 0, -5, -MICROSECONDS, -1, -MICROSECONDS / 1000 );