convert-from-bsd-to-posix-string-and-memory-functions-20010807
[openafs.git] / src / ntp / test.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <sys/param.h>
11 #include <afsconfig.h>
12
13 RCSID("$Header$");
14
15 #include <afs/stds.h>
16 #include <stdio.h>
17 #include <sys/types.h>
18 #include <sys/time.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <netinet/in_systm.h>
22 #include <netinet/ip.h>
23 #include <netinet/udp.h>
24 #include <netdb.h>
25 #include <sys/ioctl.h>
26 #include <signal.h>
27 #include <errno.h>
28 #include <syslog.h>
29
30 #include "ntp.h"
31
32 #define TRUE    1
33 #define FALSE   0
34
35 int test1(), test2(), test3(), test4(), test5(), test6();
36
37 double value[8] = {5.1, -5.1, 1.5, -1.5, 0.5, -0.5, -0.05, 0.0};
38
39 #include "AFS_component_version_number.c"
40
41 main(argc, argv)
42         int argc;
43         char **argv;
44 {
45         if (argc > 1 && strcmp(argv[1], "-v") == 0) {
46                 int error;
47                 error = test1(1);
48                 error += test2(1);
49                 error += test3(1);
50                 error += test4(1);
51                 error += test5(1);
52                 error += test6(1);
53                 exit (error);
54         } else {
55                 if (test2(0))
56                         exit(2);
57                 if (test3(0))
58                         exit(3);
59                 if (test4(0))
60                         exit(4);
61                 if (test5(0))
62                         exit(5);
63                 if (test6(0))
64                         exit(6);
65         }
66         exit(0);
67 }
68
69
70 test1()
71 {
72         int i;
73         double l_fixed_to_double();
74         struct l_fixedpt sample;
75         double s_fixed_to_double();
76         struct s_fixedpt s_sample;
77
78         for (i = 0; i < 8; i++) {
79                 printf(" %4.2f ", value[i]);
80                 double_to_l_fixed(&sample, value[i]);
81                 printf(" x%#8X.%#8X ", sample.int_part, sample.fraction);
82 #if 0
83                 printf(" %4.2f", l_fixed_to_double(&sample));
84 #endif
85                 printf("\t");
86                 double_to_s_fixed(&s_sample, value[i]);
87                 printf(" x%#4X.%#4X ", s_sample.int_part, s_sample.fraction);
88                 printf(" %4.2f\n", s_fixed_to_double(&s_sample));
89         }
90         return 0;
91 }
92
93 test2(v)
94   int v;                                /* verbose */
95 {
96         struct timeval tp;
97         struct l_fixedpt time_lm;
98
99         (void)gettimeofday(&tp, (struct timezone *) 0);
100         tstamp(&time_lm, &tp);
101
102         if (v) {
103             printf("tv_sec:  %d tv_usec:  %d \n", tp.tv_sec, tp.tv_usec);
104             printf("intpart: %lu fraction: %lu \n",
105                    ntohl(time_lm.int_part), ntohl(time_lm.fraction));
106             printf("intpart: %lX fraction: %lX \n",
107                    ntohl(time_lm.int_part), ntohl(time_lm.fraction));
108         }
109         {   extern double ul_fixed_to_double();
110             double d;
111             d = ul_fixed_to_double (&time_lm);
112             if (v) printf ("ul_fixed_to_double returns %f\n", d);
113             if (d < 0) {
114                 printf ("ul_ftd returned negative number\n");
115                 return 1;
116             }
117             d = -d;
118             double_to_l_fixed (&time_lm, d);
119             if (v) printf("intpart: %lX fraction: %lX \n",
120                           ntohl(time_lm.int_part), ntohl(time_lm.fraction));
121             d = ul_fixed_to_double (&time_lm);
122             if (d < 0) {
123                 printf ("second ul_ftd returned negative number\n");
124                 return 1;
125             }
126             if (v) printf ("ul_fixed_to_double(double_to_l_fixed(-d)) returns %f\n", d);
127         }
128         if (v) printf("test2 passes\n");
129         return 0;
130 }
131
132 test3(v)
133         int v;
134 {
135         afs_uint32 ul = 0x80000001;
136         double dbl;
137
138 #ifdef  GENERIC_UNS_BUG
139         /*
140          *  Hopefully, we can avoid the unsigned issue altogether.  Make sure
141          *  that the high-order (sign) bit is zero, and fiddle from there 
142          */
143         dbl = (afs_int32)((ul >> 1) & 0x7fffffff);
144         dbl *= 2.0;
145         if (ul & 1)
146                 dbl += 1.0;
147 #else
148         dbl = ul;
149 #ifdef  VAX_COMPILER_FLT_BUG
150         if (dbl < 0.0) dbl += 4.294967296e9;
151 #endif
152 #endif
153         if (dbl != 2147483649.0) {
154                 printf("test3 fails: can't convert from unsigned long to float\n");
155                 printf("             (%lu != %15g)\n", ul, dbl);
156                 printf(
157   "Try defining VAX_COMPILER_FLT_BUG or GENERIC_UNS_BUG in the Makefile.\n");
158                 return 1;
159         } else {
160                 if (v)
161                         printf("test3 passes\n");
162                 return 0;
163         }
164 }
165
166 test4(v)
167         int v;
168 {
169         double dbl = 1024.0 * 1024.0 * 1024.0;  /* 2^30 */
170 #ifdef SUN_FLT_BUG
171         int l = 1.5 * dbl;
172         afs_uint32 ul = (l<<1);
173 #else
174         afs_uint32 ul = 3.0 * dbl;                      /* between 2^31 and 2^32 */
175 #endif
176         if (v)
177                 printf("test4: 3.0*1024.0*1024.0*1024.0 = 0x%08x\n", ul);
178
179         if (ul != 0xc0000000) {
180                 printf("test4 fails:\n");
181                 printf("Can't convert unsigned long to double.\n");
182                 printf("Try defining SUN_FLT_BUG in the Makefile\n");
183                 return 1;
184         } else {
185             if (v)
186                 printf("test4 passes\n");
187             return 0;
188         }
189 }
190
191 /* test5 - check for sign extension of int:8 in pkt precision variable. */
192
193 test5(v)
194         int v;
195 {
196     struct ntpdata pkt;
197     struct ntp_peer peer;
198     struct sysdata sys;
199     double delay;
200
201     memset(&peer, 0, sizeof(peer));
202     memset(&sys, 0, sizeof(sys));
203     pkt.precision = -6;
204     peer.precision = pkt.precision;
205     sys.precision = pkt.precision;
206     if ((pkt.precision != -6) ||
207         (peer.precision != -6) ||
208         (sys.precision != -6)) {
209         printf ("pkt %d, peer %d, sys %d, all should be %d\n",
210                 pkt.precision, peer.precision, sys.precision, -6);
211         return 1;
212     }
213     delay = 0;
214     delay += 1.0/(afs_uint32)(1L << -peer.precision)
215         + ((peer.flags&PEER_FL_REFCLOCK) ? NTP_REFMAXSKW : NTP_MAXSKW);
216     if (peer.precision < 0 && -peer.precision < sizeof(afs_int32)*NBBY)
217         delay += 1.0/(afs_uint32)(1L << -peer.precision);
218     if (v) printf ("delay is %f\n", delay);
219
220     if ((delay - 0.041249) > .000002) {
221         printf ("delay calculation in error: delay was %d, should be %d\n",
222                 delay, 0.04125);
223         return -1;
224     }
225     if (v) printf("test5 passes\n");
226     return 0;
227 }
228
229 /* test6 - calculates the machine clock's apparent precision. */
230
231 int test6(v)
232   int v;
233 {
234     int interval = 0;
235     int precision;
236     int code;
237 #if     defined(AFS_SUN_ENV) || defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV)
238 #define PRECISION       -20
239 #else
240 #define PRECISION       -18
241 #endif
242
243
244     code = 0;
245     precision = MeasurePrecision(&interval);
246     if ((precision >= 0) || (-precision >= sizeof(afs_int32)*NBBY)) {
247         printf ("Couldn't measure precision\n");
248         code = 1;
249     }
250     if (precision < PRECISION) {
251         printf ("Precision (%d) seems too small (best interval measured to date: 13 usec, 10/10/94). -JPM\n", precision);
252         printf ("Make certain that the reported interval is accurate (possible), and then relax\n");
253         printf ("this check only if appropriate.\n");
254         code = 2;
255     }
256     if (v || code) {
257         printf
258             ("precision is %d (%f) from measured interval of %d usec (%f)\n",
259              precision, 1.0 / (afs_uint32)(1L << -precision),
260              interval, interval / 1000000.0);
261         if (!code) printf ("test6 passes\n");
262     }
263     return code;
264 }