]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/timevalops.c
Upgrade NTP to 4.2.8p4.
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / timevalops.c
1 #include "config.h"
2
3 //some unused features are still in the wrapper, unconverted
4
5 #include "ntp_types.h"
6 #include "ntp_fp.h"
7
8 #include "timevalops.h"
9
10 #include <math.h>
11 #include "unity.h"
12
13
14 #define TEST_ASSERT_EQUAL_timeval(a, b) { \
15     TEST_ASSERT_EQUAL_MESSAGE(a.tv_sec, b.tv_sec, "Field tv_sec"); \
16     TEST_ASSERT_EQUAL_MESSAGE(a.tv_usec, b.tv_usec, "Field tv_usec");   \
17 }
18
19
20 static u_int32 my_tick_to_tsf(u_int32 ticks);
21 static u_int32 my_tsf_to_tick(u_int32 tsf);
22
23
24 // that's it...
25 typedef struct {
26         long    usec;
27         u_int32 frac;
28 } lfpfracdata ;
29
30 typedef int bool;
31
32 struct timeval timeval_init( time_t hi, long lo);
33 const bool timeval_isValid(struct timeval V);
34 l_fp l_fp_init(int32 i, u_int32 f);
35 bool AssertTimevalClose(const struct timeval m, const struct timeval n, const struct timeval limit);
36 bool AssertFpClose(const l_fp m, const l_fp n, const l_fp limit);
37
38 void test_Helpers1(void);
39 void test_Normalise(void);
40 void test_SignNoFrac(void);
41 void test_SignWithFrac(void);
42 void test_CmpFracEQ(void);
43 void test_CmpFracGT(void);
44 void test_CmpFracLT(void);
45 void test_AddFullNorm(void);
46 void test_AddFullOflow1(void);
47 void test_AddUsecNorm(void);
48 void test_AddUsecOflow1(void);
49 void test_SubFullNorm(void);
50 void test_SubFullOflow(void);
51 void test_SubUsecNorm(void);
52 void test_SubUsecOflow(void);
53 void test_Neg(void);
54 void test_AbsNoFrac(void);
55 void test_AbsWithFrac(void);
56 void test_Helpers2(void);
57 void test_ToLFPbittest(void);
58 void test_ToLFPrelPos(void);
59 void test_ToLFPrelNeg(void);
60 void test_ToLFPabs(void);
61 void test_FromLFPbittest(void);
62 void test_FromLFPrelPos(void);
63 void test_FromLFPrelNeg(void);
64 void test_LFProundtrip(void);
65 void test_ToString(void);
66
67
68 //******************************************MY CUSTOM FUNCTIONS*******************************
69
70
71
72 struct timeval
73 timeval_init( time_t hi, long lo){
74         struct timeval V; 
75         V.tv_sec = hi; 
76         V.tv_usec = lo;
77         return V;
78 }
79
80
81 const bool
82 timeval_isValid(struct timeval V) {
83         return V.tv_usec >= 0 && V.tv_usec < 1000000;
84 }
85
86
87 l_fp
88 l_fp_init(int32 i, u_int32 f) {
89         l_fp temp;
90         temp.l_i  = i;
91         temp.l_uf = f;
92
93         return temp;
94 }
95
96
97 bool
98 AssertTimevalClose(const struct timeval m, const struct timeval n, const struct timeval limit) {
99         struct timeval diff;
100
101         diff = abs_tval(sub_tval(m, n));
102         if (cmp_tval(limit, diff) >= 0)
103                 return TRUE;
104         
105         else 
106         {
107                 printf("m_expr which is %ld.%lu \nand\nn_expr which is %ld.%lu\nare not close; diff=%ld.%luusec\n", m.tv_sec, m.tv_usec, n.tv_sec, n.tv_usec, diff.tv_sec, diff.tv_usec); 
108                 //I don't have variables m_expr and n_expr in unity, those are command line arguments which only getst has!!!
109                 
110                 return FALSE;
111         }
112 }
113
114
115 bool
116 AssertFpClose(const l_fp m, const l_fp n, const l_fp limit) {
117         l_fp diff;
118
119         if (L_ISGEQ(&m, &n)) {
120                 diff = m;
121                 L_SUB(&diff, &n);
122         } else {
123                 diff = n;
124                 L_SUB(&diff, &m);
125         }
126         if (L_ISGEQ(&limit, &diff)){
127                 return TRUE;
128         }
129         else {
130                 printf("m_expr which is %s \nand\nn_expr which is %s\nare not close; diff=%susec\n", lfptoa(&m, 10), lfptoa(&n, 10), lfptoa(&diff, 10)); 
131                 //printf("m_expr which is %d.%d \nand\nn_expr which is %d.%d\nare not close; diff=%d.%dusec\n", m.l_uf, m.Ul_i, n.l_uf, n.Ul_i, diff.l_uf, diff.Ul_i); 
132                 return FALSE;
133         }
134 }
135
136
137 //---------------------------------------------------
138
139 static const lfpfracdata fdata[] = {
140         {      0, 0x00000000 }, {   7478, 0x01ea1405 },
141         {  22077, 0x05a6d699 }, { 125000, 0x20000000 },
142         { 180326, 0x2e29d841 }, { 207979, 0x353e1c9b },
143         { 250000, 0x40000000 }, { 269509, 0x44fe8ab5 },
144         { 330441, 0x5497c808 }, { 333038, 0x5541fa76 },
145         { 375000, 0x60000000 }, { 394734, 0x650d4995 },
146         { 446327, 0x72427c7c }, { 500000, 0x80000000 },
147         { 517139, 0x846338b4 }, { 571953, 0x926b8306 },
148         { 587353, 0x965cc426 }, { 625000, 0xa0000000 },
149         { 692136, 0xb12fd32c }, { 750000, 0xc0000000 },
150         { 834068, 0xd5857aff }, { 848454, 0xd9344806 },
151         { 854222, 0xdaae4b02 }, { 861465, 0xdc88f862 },
152         { 875000, 0xe0000000 }, { 910661, 0xe921144d },
153         { 922162, 0xec12cf10 }, { 942190, 0xf1335d25 }
154 };
155
156
157 u_int32
158 my_tick_to_tsf(u_int32 ticks) {
159         // convert microseconds to l_fp fractional units, using double
160         // precision float calculations or, if available, 64bit integer
161         // arithmetic. This should give the precise fraction, rounded to
162         // the nearest representation.
163 #ifdef HAVE_U_INT64
164         return (u_int32)((( ((u_int64)(ticks)) << 32) + 500000) / 1000000); //I put too much () when casting just to be safe
165 #else
166         return (u_int32)( ((double)(ticks)) * 4294.967296 + 0.5);
167 #endif
168         // And before you ask: if ticks >= 1000000, the result is
169         // truncated nonsense, so don't use it out-of-bounds.
170 }
171
172
173 u_int32
174 my_tsf_to_tick(u_int32 tsf) {
175         // Inverse operation: converts fraction to microseconds.
176 #ifdef HAVE_U_INT64
177         return (u_int32)( ((u_int64)(tsf) * 1000000 + 0x80000000) >> 32); //CHECK ME!!!
178 #else
179         return (u_int32)(double(tsf) / 4294.967296 + 0.5);
180 #endif
181         // Beware: The result might be 10^6 due to rounding!
182 }
183
184
185 //***************************************END OF CUSTOM FUNCTIONS*****************************
186
187
188 // ---------------------------------------------------------------------
189 // test support stuff - part1
190 // ---------------------------------------------------------------------
191
192 void
193 test_Helpers1(void) {
194         struct timeval x;
195
196         for (x.tv_sec = -2; x.tv_sec < 3; x.tv_sec++) {
197                 x.tv_usec = -1;
198                 TEST_ASSERT_FALSE(timeval_isValid(x));
199                 x.tv_usec = 0;
200                 TEST_ASSERT_TRUE(timeval_isValid(x));
201                 x.tv_usec = 999999;
202                 TEST_ASSERT_TRUE(timeval_isValid(x));
203                 x.tv_usec = 1000000;
204                 TEST_ASSERT_FALSE(timeval_isValid(x));
205         }
206 }
207
208
209 //----------------------------------------------------------------------
210 // test normalisation
211 //----------------------------------------------------------------------
212
213 void
214 test_Normalise(void) {
215         long ns;
216         for (ns = -2000000000; ns <= 2000000000; ns += 10000000) {
217                 struct timeval x = timeval_init(0, ns);
218                 
219                 x = normalize_tval(x);
220                 TEST_ASSERT_TRUE(timeval_isValid(x));
221         }
222 }
223
224 //----------------------------------------------------------------------
225 // test classification
226 //----------------------------------------------------------------------
227
228 void
229 test_SignNoFrac(void) {
230         int i;
231         // sign test, no fraction
232         for (i = -4; i <= 4; ++i) {
233                 struct timeval a = timeval_init(i, 0);
234                 int          E = (i > 0) - (i < 0);
235                 int          r = test_tval(a);
236
237                 TEST_ASSERT_EQUAL(E, r);
238         }
239 }
240
241
242 void
243 test_SignWithFrac(void) {
244         // sign test, with fraction
245         int i;
246         for (i = -4; i <= 4; ++i) {
247                 struct timeval a = timeval_init(i, 10);
248                 int          E = (i >= 0) - (i < 0);
249                 int          r = test_tval(a);
250
251                 TEST_ASSERT_EQUAL(E, r);
252         }
253 }
254
255 //----------------------------------------------------------------------
256 // test compare
257 //----------------------------------------------------------------------
258 void
259 test_CmpFracEQ(void) {
260         int i, j;
261         // fractions are equal
262         for (i = -4; i <= 4; ++i)
263                 for (j = -4; j <= 4; ++j) {
264                         struct timeval a = timeval_init(i, 200);
265                         struct timeval b = timeval_init(j, 200);
266                         int          E = (i > j) - (i < j);
267                         int          r = cmp_tval_denorm(a, b);
268
269                         TEST_ASSERT_EQUAL(E, r);
270                 }
271 }
272
273
274 void
275 test_CmpFracGT(void) {
276         // fraction a bigger fraction b
277         int i, j;
278         for (i = -4; i <= 4; ++i)
279                 for (j = -4; j <= 4; ++j) {
280                         struct timeval a = timeval_init( i , 999800);
281                         struct timeval b = timeval_init( j , 200);
282                         int          E = (i >= j) - (i < j);
283                         int          r = cmp_tval_denorm(a, b);
284
285                         TEST_ASSERT_EQUAL(E, r);
286                 }
287 }
288
289
290 void
291 test_CmpFracLT(void) {
292         // fraction a less fraction b
293         int i, j;
294         for (i = -4; i <= 4; ++i)
295                 for (j = -4; j <= 4; ++j) {
296                         struct timeval a = timeval_init(i, 200);
297                         struct timeval b = timeval_init(j, 999800);
298                         int          E = (i > j) - (i <= j);
299                         int          r = cmp_tval_denorm(a, b);
300
301                         TEST_ASSERT_EQUAL(E, r);
302                 }
303 }
304
305 //----------------------------------------------------------------------
306 // Test addition (sum)
307 //----------------------------------------------------------------------
308
309 void
310 test_AddFullNorm(void) {
311         int i, j;
312         for (i = -4; i <= 4; ++i)
313                 for (j = -4; j <= 4; ++j) {
314                         struct timeval a = timeval_init(i, 200);
315                         struct timeval b = timeval_init(j, 400);
316                         struct timeval E = timeval_init(i + j, 200 + 400);
317                         struct timeval c;
318
319                         c = add_tval(a, b);
320                         TEST_ASSERT_EQUAL_timeval(E, c);
321                 }
322 }
323
324
325 void
326 test_AddFullOflow1(void) {
327         int i, j;
328         for (i = -4; i <= 4; ++i)
329                 for (j = -4; j <= 4; ++j) {
330                         struct timeval a = timeval_init(i, 200);
331                         struct timeval b = timeval_init(j, 999900);
332                         struct timeval E = timeval_init(i + j + 1, 100);
333                         struct timeval c;
334
335                         c = add_tval(a, b);
336                         TEST_ASSERT_EQUAL_timeval(E, c);
337                 }
338 }
339
340
341 void
342 test_AddUsecNorm(void) {
343         int i;
344         for (i = -4; i <= 4; ++i) {
345                 struct timeval a = timeval_init(i, 200);
346                 struct timeval E = timeval_init(i, 600);
347                 struct timeval c;
348
349                 c = add_tval_us(a, 600 - 200);
350                 TEST_ASSERT_EQUAL_timeval(E, c);
351         }
352 }
353
354
355 void
356 test_AddUsecOflow1(void) {
357         int i;
358         for (i = -4; i <= 4; ++i) {
359                 struct timeval a = timeval_init(i, 200);
360                 struct timeval E = timeval_init(i + 1, 100);
361                 struct timeval c;
362
363                 c = add_tval_us(a, MICROSECONDS - 100);
364                 TEST_ASSERT_EQUAL_timeval(E, c);
365         }
366 }
367
368 //----------------------------------------------------------------------
369 // test subtraction (difference)
370 //----------------------------------------------------------------------
371
372 void
373 test_SubFullNorm(void) {
374         int i, j;
375         for (i = -4; i <= 4; ++i)
376                 for (j = -4; j <= 4; ++j) {
377                         struct timeval a = timeval_init(i, 600);
378                         struct timeval b = timeval_init(j, 400);
379                         struct timeval E = timeval_init(i - j, 600 - 400);
380                         struct timeval c;
381
382                         c = sub_tval(a, b);
383                         TEST_ASSERT_EQUAL_timeval(E, c);
384                 }
385 }
386
387
388 void
389 test_SubFullOflow(void) {
390         int i, j;
391         for (i = -4; i <= 4; ++i)
392                 for (j = -4; j <= 4; ++j) {
393                         struct timeval a = timeval_init(i, 100);
394                         struct timeval b = timeval_init(j, 999900);
395                         struct timeval E = timeval_init(i - j - 1, 200);
396                         struct timeval c;
397
398                         c = sub_tval(a, b);
399                         TEST_ASSERT_EQUAL_timeval(E, c);
400                 }
401 }
402
403
404 void
405 test_SubUsecNorm(void) {
406         int i = -4;
407         for (i = -4; i <= 4; ++i) {
408                 struct timeval a = timeval_init(i, 600);
409                 struct timeval E = timeval_init(i, 200);
410                 struct timeval c;
411
412                 c = sub_tval_us(a, 600 - 200);
413                 TEST_ASSERT_EQUAL_timeval(E, c);
414         }
415 }
416
417
418 void
419 test_SubUsecOflow(void) {
420         int i = -4;
421         for (i = -4; i <= 4; ++i) {
422                 struct timeval a = timeval_init(i, 100);
423                 struct timeval E = timeval_init(i - 1, 200);
424                 struct timeval c;
425
426                 c = sub_tval_us(a, MICROSECONDS - 100);
427                 TEST_ASSERT_EQUAL_timeval(E, c);
428         }
429 }
430
431 //----------------------------------------------------------------------
432 // test negation
433 //----------------------------------------------------------------------
434
435 void
436 test_Neg(void) {
437         int i = -4;
438         for (i = -4; i <= 4; ++i) {
439                 struct timeval a = timeval_init(i, 100);
440                 struct timeval b;
441                 struct timeval c;
442
443                 b = neg_tval(a);
444                 c = add_tval(a, b);
445                 TEST_ASSERT_EQUAL(0, test_tval(c));
446         }
447 }
448
449 //----------------------------------------------------------------------
450 // test abs value
451 //----------------------------------------------------------------------
452
453 void
454 test_AbsNoFrac(void) {
455         int i = -4;
456         for (i = -4; i <= 4; ++i) {
457                 struct timeval a = timeval_init(i, 0);
458                 struct timeval b;
459
460                 b = abs_tval(a);
461                 TEST_ASSERT_EQUAL((i != 0), test_tval(b));
462         }
463 }
464
465
466 void
467 test_AbsWithFrac(void) {
468         int i = -4;
469         for (i = -4; i <= 4; ++i) {
470                 struct timeval a = timeval_init(i, 100);
471                 struct timeval b;
472
473                 b = abs_tval(a);
474                 TEST_ASSERT_EQUAL(1, test_tval(b));
475         }
476 }
477
478 // ---------------------------------------------------------------------
479 // test support stuff -- part 2
480 // ---------------------------------------------------------------------
481
482
483 void
484 test_Helpers2(void) {
485
486         struct timeval limit = timeval_init(0, 2);
487         struct timeval x, y;
488         long i; 
489
490         for (x.tv_sec = -2; x.tv_sec < 3; x.tv_sec++){
491                 for (x.tv_usec = 1;
492                      x.tv_usec < 1000000;
493                      x.tv_usec += 499999) {
494                         for (i = -4; i < 5; ++i) {
495                                 y = x;
496                                 y.tv_usec += i;
497                                 if (i >= -2 && i <= 2){
498                                         TEST_ASSERT_TRUE(AssertTimevalClose(x, y, limit));//ASSERT_PRED_FORMAT2(isClose, x, y);
499                                 }
500                                 else {
501                                         TEST_ASSERT_FALSE(AssertTimevalClose(x, y, limit));
502                                 }
503                         }
504                 }
505         }
506 }
507
508 // and the global predicate instances we're using here
509
510 //static l_fp lfpClose =  l_fp_init(0, 1); //static AssertFpClose FpClose(0, 1);
511 //static struct timeval timevalClose = timeval_init(0, 1); //static AssertTimevalClose TimevalClose(0, 1);
512
513 //----------------------------------------------------------------------
514 // conversion to l_fp
515 //----------------------------------------------------------------------
516
517 void
518 test_ToLFPbittest(void) {
519         l_fp lfpClose =  l_fp_init(0, 1);       
520
521         u_int32 i = 0;
522         for (i = 0; i < 1000000; ++i) {
523                 struct timeval a = timeval_init(1, i);
524                 l_fp E = l_fp_init(1, my_tick_to_tsf(i));
525                 l_fp r;
526
527                 r = tval_intv_to_lfp(a);
528                 TEST_ASSERT_TRUE(AssertFpClose(E, r, lfpClose));        //ASSERT_PRED_FORMAT2(FpClose, E, r);
529         }
530 }
531
532
533 void
534 test_ToLFPrelPos(void) {
535         l_fp lfpClose =  l_fp_init(0, 1);
536
537         int i = 0;
538         for (i = 0; i < COUNTOF(fdata); ++i) {
539                 struct timeval a = timeval_init(1, fdata[i].usec);
540                 l_fp E = l_fp_init(1, fdata[i].frac);
541                 l_fp r;
542
543                 r = tval_intv_to_lfp(a);
544                 TEST_ASSERT_TRUE(AssertFpClose(E, r, lfpClose));
545         }
546 }
547
548
549 void
550 test_ToLFPrelNeg(void) {
551         l_fp lfpClose =  l_fp_init(0, 1);
552         int i = 0;
553         for (i = 0; i < COUNTOF(fdata); ++i) {
554                 struct timeval a = timeval_init(-1, fdata[i].usec);
555                 l_fp E = l_fp_init(~0, fdata[i].frac);
556                 l_fp    r;
557
558                 r = tval_intv_to_lfp(a);
559                 TEST_ASSERT_TRUE(AssertFpClose(E, r, lfpClose));
560         }
561 }
562
563
564 void
565 test_ToLFPabs(void) {
566         l_fp lfpClose =  l_fp_init(0, 1);
567
568         int i = 0;
569         for (i = 0; i < COUNTOF(fdata); ++i) {
570                 struct timeval a = timeval_init(1, fdata[i].usec);
571                 l_fp E = l_fp_init(1 + JAN_1970, fdata[i].frac);
572                 l_fp    r;
573
574                 r = tval_stamp_to_lfp(a);
575                 TEST_ASSERT_TRUE(AssertFpClose(E, r, lfpClose));
576         }
577 }
578
579 //----------------------------------------------------------------------
580 // conversion from l_fp
581 //----------------------------------------------------------------------
582
583 void
584 test_FromLFPbittest(void) {
585         struct timeval timevalClose = timeval_init(0, 1);
586         // Not *exactly* a bittest, because 2**32 tests would take a
587         // really long time even on very fast machines! So we do test
588         // every 1000 fractional units.
589         u_int32 tsf = 0;
590         for (tsf = 0; tsf < ~((u_int32)(1000)); tsf += 1000) {
591                 struct timeval E = timeval_init(1, my_tsf_to_tick(tsf));
592                 l_fp a = l_fp_init(1, tsf);
593                 struct timeval r;
594
595                 r = lfp_intv_to_tval(a);
596                 // The conversion might be off by one microsecond when
597                 // comparing to calculated value.
598                 TEST_ASSERT_TRUE(AssertTimevalClose(E, r, timevalClose));
599         }
600 }
601
602
603 void
604 test_FromLFPrelPos(void) {
605         struct timeval timevalClose = timeval_init(0, 1);
606         int i = 0;      
607         for (i = 0; i < COUNTOF(fdata); ++i) {
608                 l_fp a = l_fp_init(1, fdata[i].frac);
609                 struct timeval E = timeval_init(1, fdata[i].usec);
610                 struct timeval r;
611
612                 r = lfp_intv_to_tval(a);
613                 TEST_ASSERT_TRUE(AssertTimevalClose(E, r, timevalClose));
614         }
615 }
616
617
618 void
619 test_FromLFPrelNeg(void) {
620         struct timeval timevalClose = timeval_init(0, 1);
621         int i = 0;
622         for (i = 0; i < COUNTOF(fdata); ++i) {
623                 l_fp a = l_fp_init(~0, fdata[i].frac);
624                 struct timeval E = timeval_init(-1, fdata[i].usec);
625                 struct timeval r;
626
627                 r = lfp_intv_to_tval(a);
628                 TEST_ASSERT_TRUE(AssertTimevalClose(E, r, timevalClose));
629         }
630 }
631
632
633 // usec -> frac -> usec roundtrip, using a prime start and increment
634 void
635 test_LFProundtrip(void) {
636         int32_t t = -1;
637         u_int32 i = 5;
638         for (t = -1; t < 2; ++t)
639                 for (i = 5; i < 1000000; i += 11) {
640                         struct timeval E = timeval_init(t, i);
641                         l_fp a;
642                         struct timeval r;
643
644                         a = tval_intv_to_lfp(E);
645                         r = lfp_intv_to_tval(a);
646                         TEST_ASSERT_EQUAL_timeval(E, r);
647                 }
648 }
649
650 //----------------------------------------------------------------------
651 // string formatting
652 //----------------------------------------------------------------------
653
654 void
655 test_ToString(void) {
656         static const struct {
657                 time_t       sec;
658                 long         usec;
659                 const char * repr;
660         } data [] = {
661                 { 0, 0,  "0.000000" },
662                 { 2, 0,  "2.000000" },
663                 {-2, 0, "-2.000000" },
664                 { 0, 1,  "0.000001" },
665                 { 0,-1, "-0.000001" },
666                 { 1,-1,  "0.999999" },
667                 {-1, 1, "-0.999999" },
668                 {-1,-1, "-1.000001" },
669         };
670         int i;
671         for (i = 0; i < COUNTOF(data); ++i) {
672                 struct timeval a = timeval_init(data[i].sec, data[i].usec);
673                 const char *  E = data[i].repr;
674                 const char *  r = tvaltoa(a);
675
676                 TEST_ASSERT_EQUAL_STRING(E, r);
677         }
678 }
679
680 // -*- EOF -*-