]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/g_timespecops.cpp
Fix a regression with SA-15:24 patch that prevented NIS from
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / g_timespecops.cpp
1 #include "g_libntptest.h"
2 #include "g_timestructs.h"
3
4 extern "C" {
5 #include <math.h>
6 #include "timespecops.h"
7 }
8
9 #include <string>
10 #include <sstream>
11
12 using namespace timeStruct;
13
14 class timespecTest : public libntptest {
15 protected:
16         static u_int32 my_tick_to_tsf(u_int32 ticks);
17         static u_int32 my_tsf_to_tick(u_int32 tsf);
18
19         // that's it...
20         struct lfpfracdata {
21                 long    nsec;
22                 u_int32 frac;
23         };
24         static const lfpfracdata fdata[];
25 };
26
27 u_int32
28 timespecTest::my_tick_to_tsf(
29         u_int32 ticks
30         )
31 {
32         // convert nanoseconds to l_fp fractional units, using double
33         // precision float calculations or, if available, 64bit integer
34         // arithmetic. This should give the precise fraction, rounded to
35         // the nearest representation.
36 #ifdef HAVE_U_INT64
37         return u_int32(((u_int64(ticks) << 32) + 500000000) / 1000000000);
38 #else
39         return u_int32(double(ticks) * 4.294967296 + 0.5);
40 #endif
41         // And before you ask: if ticks >= 1000000000, the result is
42         // truncated nonsense, so don't use it out-of-bounds.
43 }
44
45 u_int32
46 timespecTest::my_tsf_to_tick(
47         u_int32 tsf
48         )
49 {
50         // Inverse operation: converts fraction to microseconds.
51 #ifdef HAVE_U_INT64
52         return u_int32((u_int64(tsf) * 1000000000 + 0x80000000) >> 32);
53 #else
54         return u_int32(double(tsf) / 4.294967296 + 0.5);
55 #endif
56         // Beware: The result might be 10^9 due to rounding!
57 }
58
59 const timespecTest::lfpfracdata timespecTest::fdata [] = {
60                 {         0, 0x00000000 }, {   2218896, 0x00916ae6 },
61                 {  16408100, 0x0433523d }, { 125000000, 0x20000000 },
62                 { 250000000, 0x40000000 }, { 287455871, 0x4996b53d },
63                 { 375000000, 0x60000000 }, { 500000000, 0x80000000 },
64                 { 518978897, 0x84dbcd0e }, { 563730222, 0x90509fb3 },
65                 { 563788007, 0x9054692c }, { 583289882, 0x95527c57 },
66                 { 607074509, 0x9b693c2a }, { 625000000, 0xa0000000 },
67                 { 645184059, 0xa52ac851 }, { 676497788, 0xad2ef583 },
68                 { 678910895, 0xadcd1abb }, { 679569625, 0xadf84663 },
69                 { 690926741, 0xb0e0932d }, { 705656483, 0xb4a5e73d },
70                 { 723553854, 0xb93ad34c }, { 750000000, 0xc0000000 },
71                 { 763550253, 0xc3780785 }, { 775284917, 0xc6791284 },
72                 { 826190764, 0xd3813ce8 }, { 875000000, 0xe0000000 },
73                 { 956805507, 0xf4f134a9 }, { 982570733, 0xfb89c16c }
74 };
75
76
77 // ---------------------------------------------------------------------
78 // test support stuff -- part 1
79 // ---------------------------------------------------------------------
80
81 TEST_F(timespecTest, Helpers1) {
82         timespec_wrap x;
83
84         for (x.V.tv_sec = -2; x.V.tv_sec < 3; x.V.tv_sec++) {
85                 x.V.tv_nsec = -1;
86                 ASSERT_FALSE(x.valid());
87                 x.V.tv_nsec = 0;
88                 ASSERT_TRUE(x.valid());
89                 x.V.tv_nsec = 999999999;
90                 ASSERT_TRUE(x.valid());
91                 x.V.tv_nsec = 1000000000;
92                 ASSERT_FALSE(x.valid());
93         }
94 }
95
96 //----------------------------------------------------------------------
97 // test normalisation
98 //----------------------------------------------------------------------
99
100 TEST_F(timespecTest, Normalise) {
101         for (long ns = -2000000000; ns <= 2000000000; ns += 10000000) {
102                 timespec_wrap x(0, ns);
103
104                 x = normalize_tspec(x);
105                 ASSERT_TRUE(x.valid());
106         }
107 }
108
109 //----------------------------------------------------------------------
110 // test classification
111 //----------------------------------------------------------------------
112
113 TEST_F(timespecTest, SignNoFrac) {
114         // sign test, no fraction
115         for (int i = -4; i <= 4; ++i) {
116                 timespec_wrap a(i, 0);
117                 int E = (i > 0) - (i < 0);
118                 int r = test_tspec(a);
119
120                 ASSERT_EQ(E, r);
121         }
122 }
123
124 TEST_F(timespecTest, SignWithFrac) {
125         // sign test, with fraction
126         for (int i = -4; i <= 4; ++i) {
127                 timespec_wrap a(i, 10);
128                 int E = (i >= 0) - (i < 0);
129                 int r = test_tspec(a);
130                 ASSERT_EQ(E, r);
131         }
132 }
133
134 //----------------------------------------------------------------------
135 // test compare
136 //----------------------------------------------------------------------
137 TEST_F(timespecTest, CmpFracEQ) {
138         // fractions are equal
139         for (int i = -4; i <= 4; ++i)
140                 for (int j = -4; j <= 4; ++j) {
141                         timespec_wrap a( i , 200);
142                         timespec_wrap b( j , 200);
143                         int   E = (i > j) - (i < j);
144                         int   r = cmp_tspec_denorm(a, b);
145                         ASSERT_EQ(E, r);
146                 }
147 }
148
149 TEST_F(timespecTest, CmpFracGT) {
150         // fraction a bigger fraction b
151         for (int i = -4; i <= 4; ++i)
152                 for (int j = -4; j <= 4; ++j) {
153                         timespec_wrap a(i, 999999800);
154                         timespec_wrap b(j, 200);
155                         int   E = (i >= j) - (i < j);
156                         int   r = cmp_tspec_denorm(a, b);
157                         ASSERT_EQ(E, r);
158                 }
159 }
160
161 TEST_F(timespecTest, CmpFracLT) {
162         // fraction a less fraction b
163         for (int i = -4; i <= 4; ++i)
164                 for (int j = -4; j <= 4; ++j) {
165                         timespec_wrap a(i, 200);
166                         timespec_wrap b(j, 999999800);
167                         int   E = (i > j) - (i <= j);
168                         int   r = cmp_tspec_denorm(a, b);
169                         ASSERT_EQ(E, r);
170                 }
171 }
172
173 //----------------------------------------------------------------------
174 // Test addition (sum)
175 //----------------------------------------------------------------------
176
177 TEST_F(timespecTest, AddFullNorm) {
178         for (int i = -4; i <= 4; ++i)
179                 for (int j = -4; j <= 4; ++j) {
180                         timespec_wrap a(i, 200);
181                         timespec_wrap b(j, 400);
182                         timespec_wrap E(i + j, 200 + 400);
183                         timespec_wrap c;
184
185                         c = add_tspec(a, b);
186                         ASSERT_EQ(E, c);
187                 }
188 }
189
190 TEST_F(timespecTest, AddFullOflow1) {
191         for (int i = -4; i <= 4; ++i)
192                 for (int j = -4; j <= 4; ++j) {
193                         timespec_wrap a(i, 200);
194                         timespec_wrap b(j, 999999900);
195                         timespec_wrap E(i + j + 1, 100);
196                         timespec_wrap c;
197
198                         c = add_tspec(a, b);
199                         ASSERT_EQ(E, c);
200                 }
201 }
202
203 TEST_F(timespecTest, AddNsecNorm) {
204         for (int i = -4; i <= 4; ++i) {
205                 timespec_wrap a(i, 200);
206                 timespec_wrap E(i, 600);
207                 timespec_wrap c;
208
209                 c = add_tspec_ns(a, 600 - 200);
210                 ASSERT_EQ(E, c);
211         }
212 }
213
214 TEST_F(timespecTest, AddNsecOflow1) {
215         for (int i = -4; i <= 4; ++i) {
216                 timespec_wrap a(i, 200);
217                 timespec_wrap E(i + 1, 100);
218                 timespec_wrap c;
219
220                 c = add_tspec_ns(a, NANOSECONDS - 100);
221                 ASSERT_EQ(E, c);
222         }
223 }
224
225 //----------------------------------------------------------------------
226 // test subtraction (difference)
227 //----------------------------------------------------------------------
228
229 TEST_F(timespecTest, SubFullNorm) {
230         for (int i = -4; i <= 4; ++i)
231                 for (int j = -4; j <= 4; ++j) {
232                         timespec_wrap a( i , 600);
233                         timespec_wrap b( j , 400);
234                         timespec_wrap E(i-j, 200);
235                         timespec_wrap c;
236
237                         c = sub_tspec(a, b);
238                         ASSERT_EQ(E, c);
239                 }
240 }
241
242 TEST_F(timespecTest, SubFullOflow) {
243         for (int i = -4; i <= 4; ++i)
244                 for (int j = -4; j <= 4; ++j) {
245                         timespec_wrap a(  i  , 100);
246                         timespec_wrap b(  j  , 999999900);
247                         timespec_wrap E(i-j-1, 200);
248                         timespec_wrap c;
249
250                         c = sub_tspec(a, b);
251                         ASSERT_EQ(E, c);
252                 }
253 }
254
255 TEST_F(timespecTest, SubNsecNorm) {
256         for (int i = -4; i <= 4; ++i) {
257                 timespec_wrap a(i, 600);
258                 timespec_wrap E(i, 200);
259                 timespec_wrap c;
260
261                 c = sub_tspec_ns(a, 600 - 200);
262                 ASSERT_EQ(E, c);
263         }
264 }
265
266 TEST_F(timespecTest, SubNsecOflow) {
267         for (int i = -4; i <= 4; ++i) {
268                 timespec_wrap a( i , 100);
269                 timespec_wrap E(i-1, 200);
270                 timespec_wrap c;
271
272                 c = sub_tspec_ns(a, NANOSECONDS - 100);
273                 ASSERT_EQ(E, c);
274         }
275 }
276
277 //----------------------------------------------------------------------
278 // test negation
279 //----------------------------------------------------------------------
280
281 TEST_F(timespecTest, Neg) {
282         for (int i = -4; i <= 4; ++i) {
283                 timespec_wrap a(i, 100);
284                 timespec_wrap b;
285                 timespec_wrap c;
286
287                 b = neg_tspec(a);
288                 c = add_tspec(a, b);
289                 ASSERT_EQ(0, test_tspec(c));
290         }
291 }
292
293 //----------------------------------------------------------------------
294 // test abs value
295 //----------------------------------------------------------------------
296
297 TEST_F(timespecTest, AbsNoFrac) {
298         for (int i = -4; i <= 4; ++i) {
299                 timespec_wrap a(i , 0);
300                 timespec_wrap b;
301
302                 b = abs_tspec(a);
303                 ASSERT_EQ((i != 0), test_tspec(b));
304         }
305 }
306
307 TEST_F(timespecTest, AbsWithFrac) {
308         for (int i = -4; i <= 4; ++i) {
309                 timespec_wrap a(i, 100);
310                 timespec_wrap b;
311
312                 b = abs_tspec(a);
313                 ASSERT_EQ(1, test_tspec(b));
314         }
315 }
316
317 // ---------------------------------------------------------------------
318 // test support stuff -- part 2
319 // ---------------------------------------------------------------------
320
321 TEST_F(timespecTest, Helpers2) {
322         AssertTimespecClose isClose(0, 2);
323         timespec_wrap x, y;
324
325         for (x.V.tv_sec = -2; x.V.tv_sec < 3; x.V.tv_sec++)
326                 for (x.V.tv_nsec = 1;
327                      x.V.tv_nsec < 1000000000;
328                      x.V.tv_nsec += 499999999) {
329                         for (long i = -4; i < 5; i++) {
330                                 y = x;
331                                 y.V.tv_nsec += i;
332                                 if (i >= -2 && i <= 2)
333                                         ASSERT_PRED_FORMAT2(isClose, x, y);
334                                 else
335                                         ASSERT_PRED_FORMAT2(!isClose, x, y);
336                         }
337                 }
338 }
339
340 // global predicate instances we're using here
341 static AssertFpClose FpClose(0, 1);
342 static AssertTimespecClose TimespecClose(0, 2);
343
344 //----------------------------------------------------------------------
345 // conversion to l_fp
346 //----------------------------------------------------------------------
347
348 TEST_F(timespecTest, ToLFPbittest) {
349         for (u_int32 i = 0; i < 1000000000; i+=1000) {
350                 timespec_wrap a(1, i);
351                 l_fp_wrap     E(1, my_tick_to_tsf(i));
352                 l_fp_wrap     r;
353
354                 r = tspec_intv_to_lfp(a);
355                 ASSERT_PRED_FORMAT2(FpClose, E, r);
356         }
357 }
358
359 TEST_F(timespecTest, ToLFPrelPos) {
360         for (int i = 0; i < COUNTOF(fdata); i++) {
361                 timespec_wrap a(1, fdata[i].nsec);
362                 l_fp_wrap     E(1, fdata[i].frac);
363                 l_fp_wrap     r;
364
365                 r = tspec_intv_to_lfp(a);
366                 ASSERT_EQ(E, r);
367         }
368 }
369
370 TEST_F(timespecTest, ToLFPrelNeg) {
371         for (int i = 0; i < COUNTOF(fdata); i++) {
372                 timespec_wrap a(-1, fdata[i].nsec);
373                 l_fp_wrap     E(~0, fdata[i].frac);
374                 l_fp_wrap     r;
375
376                 r = tspec_intv_to_lfp(a);
377                 ASSERT_EQ(E, r);
378         }
379 }
380
381 TEST_F(timespecTest, ToLFPabs) {
382         for (int i = 0; i < COUNTOF(fdata); i++) {
383                 timespec_wrap a(1, fdata[i].nsec);
384                 l_fp_wrap     E(1 + JAN_1970, fdata[i].frac);
385                 l_fp_wrap     r;
386
387                 r = tspec_stamp_to_lfp(a);
388                 ASSERT_EQ(E, r);
389         }
390 }
391
392 //----------------------------------------------------------------------
393 // conversion from l_fp
394 //----------------------------------------------------------------------
395 TEST_F(timespecTest, FromLFPbittest) {
396         // Not *exactly* a bittest, because 2**32 tests would take a
397         // really long time even on very fast machines! So we do test
398         // every 1000 fractional units.
399         for (u_int32 tsf = 0; tsf < ~u_int32(1000); tsf += 1000) {
400                 timespec_wrap E(1, my_tsf_to_tick(tsf));
401                 l_fp_wrap     a(1, tsf);
402                 timespec_wrap r;
403
404                 r = lfp_intv_to_tspec(a);
405                 // The conversion might be off by one nanosecond when
406                 // comparing to calculated value.
407                 ASSERT_PRED_FORMAT2(TimespecClose, E, r);
408         }
409 }
410
411 TEST_F(timespecTest, FromLFPrelPos) {
412         for (int i = 0; i < COUNTOF(fdata); i++) {
413                 l_fp_wrap     a(1, fdata[i].frac);
414                 timespec_wrap E(1, fdata[i].nsec);
415                 timespec_wrap r;
416
417                 r = lfp_intv_to_tspec(a);
418                 ASSERT_PRED_FORMAT2(TimespecClose, E, r);
419         }
420 }
421
422 TEST_F(timespecTest, FromLFPrelNeg) {
423         for (int i = 0; i < COUNTOF(fdata); i++) {
424                 l_fp_wrap     a(~0, fdata[i].frac);
425                 timespec_wrap E(-1, fdata[i].nsec);
426                 timespec_wrap r;
427
428                 r = lfp_intv_to_tspec(a);
429                 ASSERT_PRED_FORMAT2(TimespecClose, E, r);
430         }
431 }
432
433
434 // nsec -> frac -> nsec roundtrip, using a prime start and increment
435 TEST_F(timespecTest, LFProundtrip) {
436         for (int32_t t = -1; t < 2; ++t)
437                 for (u_int32 i = 4999; i < 1000000000; i+=10007) {
438                         timespec_wrap E(t, i);
439                         l_fp_wrap     a;
440                         timespec_wrap r;
441
442                         a = tspec_intv_to_lfp(E);
443                         r = lfp_intv_to_tspec(a);
444                         ASSERT_EQ(E, r);
445                 }
446 }
447
448 //----------------------------------------------------------------------
449 // string formatting
450 //----------------------------------------------------------------------
451
452 TEST_F(timespecTest, ToString) {
453         static const struct {
454                 time_t          sec;
455                 long            nsec;
456                 const char *    repr;
457         } data [] = {
458                 { 0, 0,  "0.000000000" },
459                 { 2, 0,  "2.000000000" },
460                 {-2, 0, "-2.000000000" },
461                 { 0, 1,  "0.000000001" },
462                 { 0,-1, "-0.000000001" },
463                 { 1,-1,  "0.999999999" },
464                 {-1, 1, "-0.999999999" },
465                 {-1,-1, "-1.000000001" },
466         };
467         for (int i = 0; i < COUNTOF(data); i++) {
468                 timespec_wrap a(data[i].sec, data[i].nsec);
469                 std::string E(data[i].repr);
470                 std::string r(tspectoa(a));
471                 ASSERT_EQ(E, r);
472         }
473 }
474
475 // -*- EOF -*-