7 #include <timevalops.h>
10 //#include "bug-2803.h"
12 /* microseconds per second */
13 #define MICROSECONDS 1000000
18 //void test_main(void);
20 static int verbose = 1; // if not 0, also print results if test passed
21 static int exit_on_err = 0; // if not 0, exit if test failed
25 * Test function calling the old and new code mentioned in
26 * http://bugs.ntp.org/show_bug.cgi?id=2803#c22
28 static int do_test( struct timeval timetv, struct timeval tvlast )
30 struct timeval tvdiff_old;
31 struct timeval tvdiff_new;
40 // Here is the old code:
41 tvdiff_old = abs_tval(sub_tval(timetv, tvlast));
42 if (tvdiff_old.tv_sec > 0) {
46 // Here is the new code:
47 tvdiff_new = sub_tval(timetv, tvlast);
48 if (tvdiff_new.tv_sec != 0) {
52 failed = cond_new != cond_old;
54 if ( failed || verbose )
55 printf( "timetv %lli|%07li, tvlast %lli|%07li: tvdiff_old: %lli|%07li -> %i, tvdiff_new: %lli|%07li -> %i, same cond: %s\n",
56 (long long) timetv.tv_sec, timetv.tv_usec,
57 (long long) tvlast.tv_sec, tvlast.tv_usec,
58 (long long) tvdiff_old.tv_sec, tvdiff_old.tv_usec, cond_old,
59 (long long) tvdiff_new.tv_sec, tvdiff_new.tv_usec, cond_new,
60 failed ? "NO <<" : "yes" );
62 return failed ? -1 : 0;
68 * Call the test function in a loop for a given set of parameters.
69 * Both timetv and tvlast iterate over the given range, in all combinations.
72 int test_loop( long long start_sec, long start_usec,
73 long long stop_sec, long stop_usec,
74 long long step_sec, long step_usec )
76 struct timeval timetv;
77 struct timeval tvlast;
79 for ( timetv.tv_sec = start_sec; timetv.tv_sec <= stop_sec; timetv.tv_sec += step_sec )
80 for ( timetv.tv_usec = start_usec; timetv.tv_usec <= stop_usec; timetv.tv_usec += step_usec )
81 for ( tvlast.tv_sec = start_sec; tvlast.tv_sec <= stop_sec; tvlast.tv_sec += step_sec )
82 for ( tvlast.tv_usec = start_usec; tvlast.tv_usec <= stop_usec; tvlast.tv_usec += step_usec )
84 int rc = do_test( timetv, tvlast );
85 if (rc < 0 && exit_on_err )
94 int simpleTest( void )
97 // loop from {0.0} to {1.1000000} stepping by tv_sec by 1 and tv_usec by 100000
98 x = test_loop( 0, 0, 1, MICROSECONDS, 1, MICROSECONDS / 10 );
100 // x = test_loop( 0, 0, 5, MICROSECONDS, 1, MICROSECONDS / 1000 );
101 // x = test_loop( 0, 0, -5, -MICROSECONDS, -1, -MICROSECONDS / 1000 );
120 void test_main( void )
122 TEST_ASSERT_EQUAL(0, simpleTest());