bug-2803.c revision 290001
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#include "unity.h" 10//#include "bug-2803.h" 11 12/* microseconds per second */ 13#define MICROSECONDS 1000000 14 15int simpleTest(void); 16void setUp(void); 17void tearDown(void); 18//void test_main(void); 19 20static int verbose = 1; // if not 0, also print results if test passed 21static int exit_on_err = 0; // if not 0, exit if test failed 22 23 24/* 25 * Test function calling the old and new code mentioned in 26 * http://bugs.ntp.org/show_bug.cgi?id=2803#c22 27 */ 28static int do_test( struct timeval timetv, struct timeval tvlast ) 29{ 30 struct timeval tvdiff_old; 31 struct timeval tvdiff_new; 32 33 int cond_old; 34 int cond_new; 35 int failed; 36 37 cond_old = 0; 38 cond_new = 0; 39 40 // Here is the old code: 41 tvdiff_old = abs_tval(sub_tval(timetv, tvlast)); 42 if (tvdiff_old.tv_sec > 0) { 43 cond_old = 1; 44 } 45 46 // Here is the new code: 47 tvdiff_new = sub_tval(timetv, tvlast); 48 if (tvdiff_new.tv_sec != 0) { 49 cond_new = 1; 50 } 51 52 failed = cond_new != cond_old; 53 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" ); 61 62 return failed ? -1 : 0; 63} 64 65 66 67/* 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. 70 */ 71static 72int 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 ) 75{ 76 struct timeval timetv; 77 struct timeval tvlast; 78 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 ) 83 { 84 int rc = do_test( timetv, tvlast ); 85 if (rc < 0 && exit_on_err ) 86 return rc; 87 } 88 89 return 0; 90} 91 92 93 94int simpleTest( void ) 95{ 96 int x; 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 ); 99 100 // x = test_loop( 0, 0, 5, MICROSECONDS, 1, MICROSECONDS / 1000 ); 101 // x = test_loop( 0, 0, -5, -MICROSECONDS, -1, -MICROSECONDS / 1000 ); 102 103 return x; 104} 105 106 107 108 109 110void setUp(void) 111{ 112 113} 114 115void tearDown(void) 116{ 117} 118 119 120void test_main( void ) 121{ 122 TEST_ASSERT_EQUAL(0, simpleTest()); 123} 124