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