]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/timespecops.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / timespecops.c
1 #include "config.h"
2
3 #include "ntp_types.h"
4 #include "ntp_fp.h"
5
6 #include <math.h>
7 #include "timespecops.h"
8
9 #include "unity.h"
10
11
12 #include <string.h>
13
14 //in unity_helper.h :
15 #define TEST_ASSERT_EQUAL_timespec(a, b) { \
16     TEST_ASSERT_EQUAL_MESSAGE(a.tv_sec, b.tv_sec, "Field tv_sec"); \
17     TEST_ASSERT_EQUAL_MESSAGE(a.tv_nsec, b.tv_nsec, "Field tv_nsec");   \
18 }
19
20 //what about l_fp.l_ui  ??? it's a union so it's either l_fp.l_ui or l_fp.l_i?
21 #define TEST_ASSERT_EQUAL_l_fp(a, b) { \
22     TEST_ASSERT_EQUAL_MESSAGE(a.l_i, b.l_i, "Field l_i"); \
23     TEST_ASSERT_EQUAL_UINT_MESSAGE(a.l_uf, b.l_uf, "Field l_uf");       \
24 }
25
26 //timespec has time_t, long, and time_t is basically long uint, 4 or 8 bytes size, depending on 32/64bit
27
28 static u_int32 my_tick_to_tsf(u_int32 ticks);
29 static u_int32 my_tsf_to_tick(u_int32 tsf);
30
31 // that's it...
32 struct lfpfracdata {
33         long    nsec;
34         u_int32 frac;
35 };
36
37 //******************************************MY CUSTOM FUNCTIONS*******************************
38
39 typedef int bool; //TRUE and FALSE are already defined somewhere, so I can't do typedef enum { FALSE, TRUE } boolean;
40
41 const bool timespec_isValid(struct timespec V)
42                 { return V.tv_nsec >= 0 && V.tv_nsec < 1000000000; }
43
44 struct timespec timespec_init(time_t hi, long lo){
45         struct timespec V;      
46         V.tv_sec = hi;
47         V.tv_nsec = lo;
48         return V;
49 }
50
51 //taken from lfpfunc.c -> maybe remove this from timevalops.c and lfpfunc. and put in c_timstructs.h ????!!!!!
52 l_fp l_fp_init(int32 i, u_int32 f)
53 {
54         l_fp temp;
55         temp.l_i  = i;
56         temp.l_uf = f;
57
58         return temp;
59 }
60
61 //also in timelalops.c!!!!!!
62 bool AssertFpClose(const l_fp m,const l_fp n, const l_fp limit)
63 {
64         l_fp diff;
65
66         if (L_ISGEQ(&m, &n)) {
67                 diff = m;
68                 L_SUB(&diff, &n);
69         } else {
70                 diff = n;
71                 L_SUB(&diff, &m);
72         }
73         if (L_ISGEQ(&limit, &diff)){
74                 return TRUE;
75         }
76         else {
77                 //<< m_expr << " which is " << l_fp_wrap(m)
78                 //<< "\nand\n"
79                 //<< n_expr << " which is " << l_fp_wrap(n)
80                 //<< "\nare not close; diff=" << l_fp_wrap(diff);
81                 return FALSE;
82         }
83 }
84
85
86 bool AssertTimespecClose(const struct timespec m,const struct timespec n, const struct timespec limit)
87 {
88         struct timespec diff;
89
90         diff = abs_tspec(sub_tspec(m, n));
91         if (cmp_tspec(limit, diff) >= 0)
92                 return TRUE;
93         else
94         {
95                 //<< m_expr << " which is " << timespec_wrap(m)
96                 //<< "\nand\n"
97                 //<< n_expr << " which is " << timespec_wrap(n)
98                 //<< "\nare not close; diff=" << timespec_wrap(diff);
99                 return FALSE;
100         }
101 }
102
103 //-----------------------------------------------
104
105 static const struct lfpfracdata fdata[] = {
106         {         0, 0x00000000 }, {   2218896, 0x00916ae6 },
107         {  16408100, 0x0433523d }, { 125000000, 0x20000000 },
108         { 250000000, 0x40000000 }, { 287455871, 0x4996b53d },
109         { 375000000, 0x60000000 }, { 500000000, 0x80000000 },
110         { 518978897, 0x84dbcd0e }, { 563730222, 0x90509fb3 },
111         { 563788007, 0x9054692c }, { 583289882, 0x95527c57 },
112         { 607074509, 0x9b693c2a }, { 625000000, 0xa0000000 },
113         { 645184059, 0xa52ac851 }, { 676497788, 0xad2ef583 },
114         { 678910895, 0xadcd1abb }, { 679569625, 0xadf84663 },
115         { 690926741, 0xb0e0932d }, { 705656483, 0xb4a5e73d },
116         { 723553854, 0xb93ad34c }, { 750000000, 0xc0000000 },
117         { 763550253, 0xc3780785 }, { 775284917, 0xc6791284 },
118         { 826190764, 0xd3813ce8 }, { 875000000, 0xe0000000 },
119         { 956805507, 0xf4f134a9 }, { 982570733, 0xfb89c16c }
120         };
121
122
123 u_int32 my_tick_to_tsf(u_int32 ticks)
124 {
125         // convert nanoseconds to l_fp fractional units, using double
126         // precision float calculations or, if available, 64bit integer
127         // arithmetic. This should give the precise fraction, rounded to
128         // the nearest representation.
129 #ifdef HAVE_U_INT64
130         return (u_int32)((( ((u_int64)(ticks)) << 32) + 500000000) / 1000000000);
131 #else
132         return (u_int32)((double(ticks)) * 4.294967296 + 0.5);
133 #endif
134         // And before you ask: if ticks >= 1000000000, the result is
135         // truncated nonsense, so don't use it out-of-bounds.
136 }
137
138 u_int32 my_tsf_to_tick(u_int32 tsf)
139 {
140         // Inverse operation: converts fraction to microseconds.
141 #ifdef HAVE_U_INT64
142         return (u_int32)(( ((u_int64)(tsf)) * 1000000000 + 0x80000000) >> 32);
143 #else
144         return (u_int32)(double(tsf) / 4.294967296 + 0.5);
145 #endif
146         // Beware: The result might be 10^9 due to rounding!
147 }
148
149
150
151 // ---------------------------------------------------------------------
152 // test support stuff -- part 1
153 // ---------------------------------------------------------------------
154
155 void test_Helpers1() {
156         struct timespec x;
157
158         for (x.tv_sec = -2; x.tv_sec < 3; x.tv_sec++) {
159                 x.tv_nsec = -1;
160                 TEST_ASSERT_FALSE(timespec_isValid(x));
161                 x.tv_nsec = 0;
162                 TEST_ASSERT_TRUE(timespec_isValid(x));
163                 x.tv_nsec = 999999999;
164                 TEST_ASSERT_TRUE(timespec_isValid(x));
165                 x.tv_nsec = 1000000000;
166                 TEST_ASSERT_FALSE(timespec_isValid(x));
167         }
168 }
169
170
171 //----------------------------------------------------------------------
172 // test normalisation
173 //----------------------------------------------------------------------
174
175 void test_Normalise() {
176         long ns;
177         for ( ns = -2000000000; ns <= 2000000000; ns += 10000000) {
178                 struct timespec x = timespec_init(0, ns);
179
180                 x = normalize_tspec(x);
181                 TEST_ASSERT_TRUE(timespec_isValid(x));
182         }
183 }
184
185 //----------------------------------------------------------------------
186 // test classification
187 //----------------------------------------------------------------------
188
189 void test_SignNoFrac() {
190         // sign test, no fraction
191         int i;
192         for (i = -4; i <= 4; ++i) {
193                 struct timespec a = timespec_init(i, 0);
194                 int E = (i > 0) - (i < 0);
195                 int r = test_tspec(a);
196
197                 TEST_ASSERT_EQUAL(E, r);
198         }
199 }
200
201 void test_SignWithFrac() {
202         // sign test, with fraction
203         int i;
204         for (i = -4; i <= 4; ++i) {
205                 struct timespec a = timespec_init(i, 10);
206                 int E = (i >= 0) - (i < 0);
207                 int r = test_tspec(a);
208                 TEST_ASSERT_EQUAL(E, r);
209         }
210 }
211
212 //----------------------------------------------------------------------
213 // test compare
214 //----------------------------------------------------------------------
215 void test_CmpFracEQ() {
216         // fractions are equal
217         int i,j;
218         for (i = -4; i <= 4; ++i)
219                 for (j = -4; j <= 4; ++j) {
220                         struct timespec a = timespec_init( i , 200);
221                         struct timespec b = timespec_init( j , 200);
222                         int   E = (i > j) - (i < j);
223                         int   r = cmp_tspec_denorm(a, b);
224                         TEST_ASSERT_EQUAL(E, r);
225                 }
226 }
227
228 void test_CmpFracGT() {
229         // fraction a bigger fraction b
230         int i,j;
231         for (i = -4; i <= 4; ++i)
232                 for (j = -4; j <= 4; ++j) {
233                         struct timespec a = timespec_init(i, 999999800);
234                         struct timespec b = timespec_init(j, 200);
235                         int   E = (i >= j) - (i < j);
236                         int   r = cmp_tspec_denorm(a, b);
237                         TEST_ASSERT_EQUAL(E, r);
238                 }
239 }
240
241 void test_CmpFracLT() {
242         // fraction a less fraction b
243         int i,j;
244         for (i = -4; i <= 4; ++i)
245                 for (j = -4; j <= 4; ++j) {
246                         struct timespec a = timespec_init(i, 200);
247                         struct timespec b = timespec_init(j, 999999800);
248                         int   E = (i > j) - (i <= j);
249                         int   r = cmp_tspec_denorm(a, b);
250                         TEST_ASSERT_EQUAL(E, r);
251                 }
252 }
253
254 //----------------------------------------------------------------------
255 // Test addition (sum)
256 //----------------------------------------------------------------------
257
258 void test_AddFullNorm() {
259         int i,j;
260         for (i = -4; i <= 4; ++i)
261                 for (j = -4; j <= 4; ++j) {
262                         struct timespec a = timespec_init(i, 200);
263                         struct timespec b = timespec_init(j, 400);
264                         struct timespec E = timespec_init(i + j, 200 + 400);
265                         struct timespec c;
266
267                         c = add_tspec(a, b);
268                         TEST_ASSERT_EQUAL_timespec(E, c);
269                 }
270 }
271
272 void test_AddFullOflow1() {
273         int i,j;
274         for (i = -4; i <= 4; ++i)
275                 for (j = -4; j <= 4; ++j) {
276                         struct timespec a = timespec_init(i, 200);
277                         struct timespec b = timespec_init(j, 999999900);
278                         struct timespec E = timespec_init(i + j + 1, 100);
279                         struct timespec c;
280
281                         c = add_tspec(a, b);
282                         TEST_ASSERT_EQUAL_timespec(E, c);
283                 }
284 }
285
286 void test_AddNsecNorm() {
287         int i;
288         for (i = -4; i <= 4; ++i) {
289                 struct timespec a = timespec_init(i, 200);
290                 struct timespec E = timespec_init(i, 600);
291                 struct timespec c;
292
293                 c = add_tspec_ns(a, 600 - 200);
294                 TEST_ASSERT_EQUAL_timespec(E, c);
295         }
296 }
297
298 void test_AddNsecOflow1() {
299         int i;
300         for (i = -4; i <= 4; ++i) {
301                 struct timespec a = timespec_init(i, 200);
302                 struct timespec E = timespec_init(i + 1, 100);
303                 struct timespec c;
304
305                 c = add_tspec_ns(a, NANOSECONDS - 100);
306                 TEST_ASSERT_EQUAL_timespec(E, c);
307         }
308 }
309
310 //----------------------------------------------------------------------
311 // test subtraction (difference)
312 //----------------------------------------------------------------------
313
314 void test_SubFullNorm() {
315         int i,j;
316         for (i = -4; i <= 4; ++i)
317                 for (j = -4; j <= 4; ++j) {
318                         struct timespec a = timespec_init( i , 600);
319                         struct timespec b = timespec_init( j , 400);
320                         struct timespec E = timespec_init(i-j, 200);
321                         struct timespec c;
322
323                         c = sub_tspec(a, b);
324                         TEST_ASSERT_EQUAL_timespec(E, c);
325                 }
326 }
327
328 void test_SubFullOflow() {
329         int i,j;
330         for (i = -4; i <= 4; ++i)
331                 for (j = -4; j <= 4; ++j) {
332                         struct timespec a = timespec_init(  i  , 100);
333                         struct timespec b = timespec_init(  j  , 999999900);
334                         struct timespec E = timespec_init(i-j-1, 200);
335                         struct timespec c;
336
337                         c = sub_tspec(a, b);
338                         TEST_ASSERT_EQUAL_timespec(E, c);
339                 }
340 }
341
342 void test_SubNsecNorm() {
343         int i;
344         for (i = -4; i <= 4; ++i) {
345                 struct timespec a = timespec_init(i, 600);
346                 struct timespec E = timespec_init(i, 200);
347                 struct timespec c;
348
349                 c = sub_tspec_ns(a, 600 - 200);
350                 TEST_ASSERT_EQUAL_timespec(E, c);
351         }
352 }
353
354 void test_SubNsecOflow() {
355         int i;
356         for (i = -4; i <= 4; ++i) {
357                 struct timespec a = timespec_init( i , 100);
358                 struct timespec E = timespec_init(i-1, 200);
359                 struct timespec c;
360
361                 c = sub_tspec_ns(a, NANOSECONDS - 100);
362                 TEST_ASSERT_EQUAL_timespec(E, c);
363         }
364 }
365
366 //----------------------------------------------------------------------
367 // test negation
368 //----------------------------------------------------------------------
369
370 void test_Neg() {
371         int i;
372         for (i = -4; i <= 4; ++i) {
373                 struct timespec a = timespec_init(i, 100);
374                 struct timespec b;
375                 struct timespec c;
376
377                 b = neg_tspec(a);
378                 c = add_tspec(a, b);
379                 TEST_ASSERT_EQUAL(0, test_tspec(c));
380         }
381 }
382
383 //----------------------------------------------------------------------
384 // test abs value
385 //----------------------------------------------------------------------
386
387 void test_AbsNoFrac() {
388         int i;
389         for (i = -4; i <= 4; ++i) {
390                 struct timespec a = timespec_init(i , 0);
391                 struct timespec b;
392
393                 b = abs_tspec(a);
394                 TEST_ASSERT_EQUAL((i != 0), test_tspec(b));
395         }
396 }
397
398 void test_AbsWithFrac() {
399         int i;
400         for (i = -4; i <= 4; ++i) {
401                 struct timespec a = timespec_init(i, 100);
402                 struct timespec b;
403
404                 b = abs_tspec(a);
405                 TEST_ASSERT_EQUAL(1, test_tspec(b));
406         }
407 }
408
409 // ---------------------------------------------------------------------
410 // test support stuff -- part 2
411 // ---------------------------------------------------------------------
412
413 void test_Helpers2() {
414         struct timespec limit = timespec_init(0,2);
415         
416         struct timespec x, y;
417         long i;
418
419         for (x.tv_sec = -2; x.tv_sec < 3; x.tv_sec++)
420                 for (x.tv_nsec = 1;
421                      x.tv_nsec < 1000000000;
422                      x.tv_nsec += 499999999) {
423                         for (i = -4; i < 5; i++) {
424                                 y = x;
425                                 y.tv_nsec += i;
426                                 if (i >= -2 && i <= 2){
427                                         TEST_ASSERT_TRUE(AssertTimespecClose(x,y,limit));//ASSERT_PRED_FORMAT2(isClose, x, y);
428                                 }
429                                 else
430                                 {
431                                         TEST_ASSERT_FALSE(AssertTimespecClose(x,y,limit));//ASSERT_PRED_FORMAT2(!isClose, x, y);
432                                 }
433                         }
434                 }
435 }
436
437 // global predicate instances we're using here
438 //static l_fp lfpClose =  l_fp_init(0,1); //static AssertFpClose FpClose(0, 1);
439 //static struct timespec limit = timespec_init(0,2); //static AssertTimespecClose TimespecClose(0, 2);
440
441 //----------------------------------------------------------------------
442 // conversion to l_fp
443 //----------------------------------------------------------------------
444
445 void test_ToLFPbittest() {
446         l_fp lfpClose =  l_fp_init(0,1);
447         u_int32 i;
448         for (i = 0; i < 1000000000; i+=1000) {
449                 struct timespec a = timespec_init(1, i);
450                 l_fp E= l_fp_init(1, my_tick_to_tsf(i));
451                 l_fp r;
452
453                 r = tspec_intv_to_lfp(a);
454                 TEST_ASSERT_TRUE(AssertFpClose(E,r,lfpClose)); //ASSERT_PRED_FORMAT2(FpClose, E, r);
455         }
456 }
457
458 void test_ToLFPrelPos() {
459         int i;
460         for (i = 0; i < COUNTOF(fdata); i++) {
461                 struct timespec a = timespec_init(1, fdata[i].nsec);
462                 l_fp E = l_fp_init(1, fdata[i].frac);
463                 l_fp r;
464
465                 r = tspec_intv_to_lfp(a);
466                 TEST_ASSERT_EQUAL_l_fp(E, r);
467         }
468 }
469
470 void test_ToLFPrelNeg() {
471         int i;
472         for (i = 0; i < COUNTOF(fdata); i++) {
473                 struct timespec a = timespec_init(-1, fdata[i].nsec);
474                 l_fp E = l_fp_init(~0, fdata[i].frac);
475                 l_fp r;
476
477                 r = tspec_intv_to_lfp(a);
478                 TEST_ASSERT_EQUAL_l_fp(E, r);
479         }
480 }
481
482 void test_ToLFPabs() {
483         int i;
484         for (i = 0; i < COUNTOF(fdata); i++) {
485                 struct timespec a = timespec_init(1, fdata[i].nsec);
486                 l_fp E = l_fp_init(1 + JAN_1970, fdata[i].frac);
487                 l_fp r;
488
489                 r = tspec_stamp_to_lfp(a);
490                 TEST_ASSERT_EQUAL_l_fp(E, r);
491         }
492 }
493
494 //----------------------------------------------------------------------
495 // conversion from l_fp
496 //----------------------------------------------------------------------
497 void test_FromLFPbittest() {
498         struct timespec limit = timespec_init(0,2);
499
500         // Not *exactly* a bittest, because 2**32 tests would take a
501         // really long time even on very fast machines! So we do test
502         // every 1000 fractional units.
503         u_int32 tsf;
504         for (tsf = 0; tsf < ~((u_int32)(1000)); tsf += 1000) {
505                 struct timespec E = timespec_init(1, my_tsf_to_tick(tsf));
506                 l_fp a = l_fp_init(1, tsf);
507                 struct timespec r;
508
509                 r = lfp_intv_to_tspec(a);
510                 // The conversion might be off by one nanosecond when
511                 // comparing to calculated value.
512                 TEST_ASSERT_TRUE(AssertTimespecClose(E,r,limit)); //ASSERT_PRED_FORMAT2(TimespecClose, E, r);
513         }
514 }
515
516 void test_FromLFPrelPos() {
517         struct timespec limit = timespec_init(0,2);
518         int i;
519         for (i = 0; i < COUNTOF(fdata); i++) {
520                 l_fp a = l_fp_init(1, fdata[i].frac);
521                 struct timespec E = timespec_init(1, fdata[i].nsec);
522                 struct timespec r;
523
524                 r = lfp_intv_to_tspec(a);
525                 TEST_ASSERT_TRUE(AssertTimespecClose(E,r,limit)); //ASSERT_PRED_FORMAT2(TimespecClose, E, r);
526         }
527 }
528
529 void test_FromLFPrelNeg() {
530         struct timespec limit = timespec_init(0,2);
531         int i;
532         for (i = 0; i < COUNTOF(fdata); i++) {
533                 l_fp a = l_fp_init(~0, fdata[i].frac);
534                 struct timespec E = timespec_init(-1, fdata[i].nsec);
535                 struct timespec r;
536
537                 r = lfp_intv_to_tspec(a);
538                 TEST_ASSERT_TRUE(AssertTimespecClose(E,r,limit)); //ASSERT_PRED_FORMAT2(TimespecClose, E, r);
539         }
540 }
541
542
543 // nsec -> frac -> nsec roundtrip, using a prime start and increment
544 void test_LFProundtrip() {
545         int32_t t;
546         u_int32 i;
547         for (t = -1; t < 2; ++t)
548                 for (i = 4999; i < 1000000000; i+=10007) {
549                         struct timespec E = timespec_init(t, i);
550                         l_fp a;
551                         struct timespec r;
552
553                         a = tspec_intv_to_lfp(E);
554                         r = lfp_intv_to_tspec(a);
555                         TEST_ASSERT_EQUAL_timespec(E, r);
556                 }
557 }
558
559 //----------------------------------------------------------------------
560 // string formatting
561 //----------------------------------------------------------------------
562
563 void test_ToString() {
564         static const struct {
565                 time_t          sec;
566                 long            nsec;
567                 const char *    repr;
568         } data [] = {
569                 { 0, 0,  "0.000000000" },
570                 { 2, 0,  "2.000000000" },
571                 {-2, 0, "-2.000000000" },
572                 { 0, 1,  "0.000000001" },
573                 { 0,-1, "-0.000000001" },
574                 { 1,-1,  "0.999999999" },
575                 {-1, 1, "-0.999999999" },
576                 {-1,-1, "-1.000000001" },
577         };
578         int i;
579         for (i = 0; i < COUNTOF(data); i++) {
580                 struct timespec a = timespec_init(data[i].sec, data[i].nsec);
581                 const char * E = data[i].repr;
582                 const char * r = tspectoa(a);
583                 TEST_ASSERT_EQUAL_STRING(E, r);
584         }
585 }
586
587 // -*- EOF -*-