5 #include "timespecops.h"
13 #define TEST_ASSERT_EQUAL_timespec(a, b) { \
14 TEST_ASSERT_EQUAL_MESSAGE(a.tv_sec, b.tv_sec, "Field tv_sec"); \
15 TEST_ASSERT_EQUAL_MESSAGE(a.tv_nsec, b.tv_nsec, "Field tv_nsec"); \
19 #define TEST_ASSERT_EQUAL_l_fp(a, b) { \
20 TEST_ASSERT_EQUAL_MESSAGE(a.l_i, b.l_i, "Field l_i"); \
21 TEST_ASSERT_EQUAL_UINT_MESSAGE(a.l_uf, b.l_uf, "Field l_uf"); \
25 static u_int32 my_tick_to_tsf(u_int32 ticks);
26 static u_int32 my_tsf_to_tick(u_int32 tsf);
37 void test_Helpers1(void);
38 void test_Normalise(void);
39 void test_SignNoFrac(void);
40 void test_SignWithFrac(void);
41 void test_CmpFracEQ(void);
42 void test_CmpFracGT(void);
43 void test_CmpFracLT(void);
44 void test_AddFullNorm(void);
45 void test_AddFullOflow1(void);
46 void test_AddNsecNorm(void);
47 void test_AddNsecOflow1(void);
48 void test_SubFullNorm(void);
49 void test_SubFullOflow(void);
50 void test_SubNsecNorm(void);
51 void test_SubNsecOflow(void);
53 void test_AbsNoFrac(void);
54 void test_AbsWithFrac(void);
55 void test_Helpers2(void);
56 void test_ToLFPbittest(void);
57 void test_ToLFPrelPos(void);
58 void test_ToLFPrelNeg(void);
59 void test_ToLFPabs(void);
60 void test_FromLFPbittest(void);
61 void test_FromLFPrelPos(void);
62 void test_FromLFPrelNeg(void);
63 void test_LFProundtrip(void);
64 void test_ToString(void);
68 const bool timespec_isValid(struct timespec V);
69 struct timespec timespec_init(time_t hi, long lo);
70 l_fp l_fp_init(int32 i, u_int32 f);
71 bool AssertFpClose(const l_fp m, const l_fp n, const l_fp limit);
72 bool AssertTimespecClose(const struct timespec m,
73 const struct timespec n,
74 const struct timespec limit);
77 //***************************MY CUSTOM FUNCTIONS***************************
90 timespec_isValid(struct timespec V)
93 return V.tv_nsec >= 0 && V.tv_nsec < 1000000000;
98 timespec_init(time_t hi, long lo)
110 l_fp_init(int32 i, u_int32 f)
122 AssertFpClose(const l_fp m, const l_fp n, const l_fp limit)
126 if (L_ISGEQ(&m, &n)) {
133 if (L_ISGEQ(&limit, &diff)) {
137 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));
144 AssertTimespecClose(const struct timespec m, const struct timespec n,
145 const struct timespec limit)
147 struct timespec diff;
149 diff = abs_tspec(sub_tspec(m, n));
150 if (cmp_tspec(limit, diff) >= 0)
154 printf("m_expr which is %ld.%lu \nand\nn_expr which is %ld.%lu\nare not close; diff=%ld.%lunsec\n", m.tv_sec, m.tv_nsec, n.tv_sec, n.tv_nsec, diff.tv_sec, diff.tv_nsec);
159 //-----------------------------------------------
161 static const struct lfpfracdata fdata[] = {
162 { 0, 0x00000000 }, { 2218896, 0x00916ae6 },
163 { 16408100, 0x0433523d }, { 125000000, 0x20000000 },
164 { 250000000, 0x40000000 }, { 287455871, 0x4996b53d },
165 { 375000000, 0x60000000 }, { 500000000, 0x80000000 },
166 { 518978897, 0x84dbcd0e }, { 563730222, 0x90509fb3 },
167 { 563788007, 0x9054692c }, { 583289882, 0x95527c57 },
168 { 607074509, 0x9b693c2a }, { 625000000, 0xa0000000 },
169 { 645184059, 0xa52ac851 }, { 676497788, 0xad2ef583 },
170 { 678910895, 0xadcd1abb }, { 679569625, 0xadf84663 },
171 { 690926741, 0xb0e0932d }, { 705656483, 0xb4a5e73d },
172 { 723553854, 0xb93ad34c }, { 750000000, 0xc0000000 },
173 { 763550253, 0xc3780785 }, { 775284917, 0xc6791284 },
174 { 826190764, 0xd3813ce8 }, { 875000000, 0xe0000000 },
175 { 956805507, 0xf4f134a9 }, { 982570733, 0xfb89c16c }
180 my_tick_to_tsf(u_int32 ticks)
182 // convert nanoseconds to l_fp fractional units, using double
183 // precision float calculations or, if available, 64bit integer
184 // arithmetic. This should give the precise fraction, rounded to
185 // the nearest representation.
188 return (u_int32)((( ((u_int64)(ticks)) << 32) + 500000000) / 1000000000);
190 return (u_int32)((double(ticks)) * 4.294967296 + 0.5);
192 // And before you ask: if ticks >= 1000000000, the result is
193 // truncated nonsense, so don't use it out-of-bounds.
198 my_tsf_to_tick(u_int32 tsf)
201 // Inverse operation: converts fraction to microseconds.
203 return (u_int32)(( ((u_int64)(tsf)) * 1000000000 + 0x80000000) >> 32);
205 return (u_int32)(double(tsf) / 4.294967296 + 0.5);
207 // Beware: The result might be 10^9 due to rounding!
212 // ---------------------------------------------------------------------
213 // test support stuff -- part 1
214 // ---------------------------------------------------------------------
221 for (x.tv_sec = -2; x.tv_sec < 3; x.tv_sec++) {
223 TEST_ASSERT_FALSE(timespec_isValid(x));
225 TEST_ASSERT_TRUE(timespec_isValid(x));
226 x.tv_nsec = 999999999;
227 TEST_ASSERT_TRUE(timespec_isValid(x));
228 x.tv_nsec = 1000000000;
229 TEST_ASSERT_FALSE(timespec_isValid(x));
236 //----------------------------------------------------------------------
237 // test normalisation
238 //----------------------------------------------------------------------
245 for ( ns = -2000000000; ns <= 2000000000; ns += 10000000) {
246 struct timespec x = timespec_init(0, ns);
248 x = normalize_tspec(x);
249 TEST_ASSERT_TRUE(timespec_isValid(x));
255 //----------------------------------------------------------------------
256 // test classification
257 //----------------------------------------------------------------------
260 test_SignNoFrac(void)
262 // sign test, no fraction
265 for (i = -4; i <= 4; ++i) {
266 struct timespec a = timespec_init(i, 0);
267 int E = (i > 0) - (i < 0);
268 int r = test_tspec(a);
270 TEST_ASSERT_EQUAL(E, r);
278 test_SignWithFrac(void)
280 // sign test, with fraction
283 for (i = -4; i <= 4; ++i) {
284 struct timespec a = timespec_init(i, 10);
285 int E = (i >= 0) - (i < 0);
286 int r = test_tspec(a);
288 TEST_ASSERT_EQUAL(E, r);
294 //----------------------------------------------------------------------
296 //----------------------------------------------------------------------
300 // fractions are equal
302 for (i = -4; i <= 4; ++i)
303 for (j = -4; j <= 4; ++j) {
304 struct timespec a = timespec_init( i , 200);
305 struct timespec b = timespec_init( j , 200);
306 int E = (i > j) - (i < j);
307 int r = cmp_tspec_denorm(a, b);
309 TEST_ASSERT_EQUAL(E, r);
319 // fraction a bigger fraction b
322 for (i = -4; i <= 4; ++i)
323 for (j = -4; j <= 4; ++j) {
324 struct timespec a = timespec_init(i, 999999800);
325 struct timespec b = timespec_init(j, 200);
326 int E = (i >= j) - (i < j);
327 int r = cmp_tspec_denorm(a, b);
329 TEST_ASSERT_EQUAL(E, r);
339 // fraction a less fraction b
342 for (i = -4; i <= 4; ++i)
343 for (j = -4; j <= 4; ++j) {
344 struct timespec a = timespec_init(i, 200);
345 struct timespec b = timespec_init(j, 999999800);
346 int E = (i > j) - (i <= j);
347 int r = cmp_tspec_denorm(a, b);
349 TEST_ASSERT_EQUAL(E, r);
355 //----------------------------------------------------------------------
356 // Test addition (sum)
357 //----------------------------------------------------------------------
360 test_AddFullNorm(void)
364 for (i = -4; i <= 4; ++i)
365 for (j = -4; j <= 4; ++j) {
366 struct timespec a = timespec_init(i, 200);
367 struct timespec b = timespec_init(j, 400);
368 struct timespec E = timespec_init(i + j, 200 + 400);
372 TEST_ASSERT_EQUAL_timespec(E, c);
380 test_AddFullOflow1(void)
384 for (i = -4; i <= 4; ++i)
385 for (j = -4; j <= 4; ++j) {
386 struct timespec a = timespec_init(i, 200);
387 struct timespec b = timespec_init(j, 999999900);
388 struct timespec E = timespec_init(i + j + 1, 100);
392 TEST_ASSERT_EQUAL_timespec(E, c);
400 test_AddNsecNorm(void) {
403 for (i = -4; i <= 4; ++i) {
404 struct timespec a = timespec_init(i, 200);
405 struct timespec E = timespec_init(i, 600);
408 c = add_tspec_ns(a, 600 - 200);
409 TEST_ASSERT_EQUAL_timespec(E, c);
417 test_AddNsecOflow1(void)
421 for (i = -4; i <= 4; ++i) {
422 struct timespec a = timespec_init(i, 200);
423 struct timespec E = timespec_init(i + 1, 100);
426 c = add_tspec_ns(a, NANOSECONDS - 100);
427 TEST_ASSERT_EQUAL_timespec(E, c);
433 //----------------------------------------------------------------------
434 // test subtraction (difference)
435 //----------------------------------------------------------------------
438 test_SubFullNorm(void)
442 for (i = -4; i <= 4; ++i)
443 for (j = -4; j <= 4; ++j) {
444 struct timespec a = timespec_init( i , 600);
445 struct timespec b = timespec_init( j , 400);
446 struct timespec E = timespec_init(i-j, 200);
450 TEST_ASSERT_EQUAL_timespec(E, c);
458 test_SubFullOflow(void)
462 for (i = -4; i <= 4; ++i)
463 for (j = -4; j <= 4; ++j) {
464 struct timespec a = timespec_init(i, 100);
465 struct timespec b = timespec_init(j, 999999900);
466 struct timespec E = timespec_init(i - j - 1, 200);
470 TEST_ASSERT_EQUAL_timespec(E, c);
478 test_SubNsecNorm(void)
482 for (i = -4; i <= 4; ++i) {
483 struct timespec a = timespec_init(i, 600);
484 struct timespec E = timespec_init(i, 200);
487 c = sub_tspec_ns(a, 600 - 200);
488 TEST_ASSERT_EQUAL_timespec(E, c);
496 test_SubNsecOflow(void)
500 for (i = -4; i <= 4; ++i) {
501 struct timespec a = timespec_init( i , 100);
502 struct timespec E = timespec_init(i-1, 200);
505 c = sub_tspec_ns(a, NANOSECONDS - 100);
506 TEST_ASSERT_EQUAL_timespec(E, c);
512 //----------------------------------------------------------------------
514 //----------------------------------------------------------------------
522 for (i = -4; i <= 4; ++i) {
523 struct timespec a = timespec_init(i, 100);
529 TEST_ASSERT_EQUAL(0, test_tspec(c));
535 //----------------------------------------------------------------------
537 //----------------------------------------------------------------------
544 for (i = -4; i <= 4; ++i) {
545 struct timespec a = timespec_init(i , 0);
549 TEST_ASSERT_EQUAL((i != 0), test_tspec(b));
557 test_AbsWithFrac(void)
561 for (i = -4; i <= 4; ++i) {
562 struct timespec a = timespec_init(i, 100);
566 TEST_ASSERT_EQUAL(1, test_tspec(b));
572 // ---------------------------------------------------------------------
573 // test support stuff -- part 2
574 // ---------------------------------------------------------------------
579 struct timespec limit = timespec_init(0, 2);
580 struct timespec x, y;
583 for (x.tv_sec = -2; x.tv_sec < 3; x.tv_sec++)
585 x.tv_nsec < 1000000000;
586 x.tv_nsec += 499999999) {
587 for (i = -4; i < 5; ++i) {
590 if (i >= -2 && i <= 2) {
591 TEST_ASSERT_TRUE(AssertTimespecClose(x, y, limit));
595 TEST_ASSERT_FALSE(AssertTimespecClose(x, y, limit));
603 //----------------------------------------------------------------------
604 // conversion to l_fp
605 //----------------------------------------------------------------------
608 test_ToLFPbittest(void)
610 l_fp lfpClose = l_fp_init(0, 1);
613 for (i = 0; i < 1000000000; i+=1000) {
614 struct timespec a = timespec_init(1, i);
615 l_fp E= l_fp_init(1, my_tick_to_tsf(i));
618 r = tspec_intv_to_lfp(a);
619 TEST_ASSERT_TRUE(AssertFpClose(E, r, lfpClose));
627 test_ToLFPrelPos(void)
631 for (i = 0; i < COUNTOF(fdata); ++i) {
632 struct timespec a = timespec_init(1, fdata[i].nsec);
633 l_fp E = l_fp_init(1, fdata[i].frac);
636 r = tspec_intv_to_lfp(a);
637 TEST_ASSERT_EQUAL_l_fp(E, r);
645 test_ToLFPrelNeg(void)
649 for (i = 0; i < COUNTOF(fdata); ++i) {
650 struct timespec a = timespec_init(-1, fdata[i].nsec);
651 l_fp E = l_fp_init(~0, fdata[i].frac);
654 r = tspec_intv_to_lfp(a);
655 TEST_ASSERT_EQUAL_l_fp(E, r);
667 for (i = 0; i < COUNTOF(fdata); ++i) {
668 struct timespec a = timespec_init(1, fdata[i].nsec);
669 l_fp E = l_fp_init(1 + JAN_1970, fdata[i].frac);
672 r = tspec_stamp_to_lfp(a);
673 TEST_ASSERT_EQUAL_l_fp(E, r);
679 //----------------------------------------------------------------------
680 // conversion from l_fp
681 //----------------------------------------------------------------------
684 test_FromLFPbittest(void)
686 struct timespec limit = timespec_init(0, 2);
688 // Not *exactly* a bittest, because 2**32 tests would take a
689 // really long time even on very fast machines! So we do test
690 // every 1000 fractional units.
692 for (tsf = 0; tsf < ~((u_int32)(1000)); tsf += 1000) {
693 struct timespec E = timespec_init(1, my_tsf_to_tick(tsf));
694 l_fp a = l_fp_init(1, tsf);
697 r = lfp_intv_to_tspec(a);
698 // The conversion might be off by one nanosecond when
699 // comparing to calculated value.
700 TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit));
708 test_FromLFPrelPos(void)
710 struct timespec limit = timespec_init(0, 2);
713 for (i = 0; i < COUNTOF(fdata); ++i) {
714 l_fp a = l_fp_init(1, fdata[i].frac);
715 struct timespec E = timespec_init(1, fdata[i].nsec);
718 r = lfp_intv_to_tspec(a);
719 TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit));
727 test_FromLFPrelNeg(void)
729 struct timespec limit = timespec_init(0, 2);
732 for (i = 0; i < COUNTOF(fdata); ++i) {
733 l_fp a = l_fp_init(~0, fdata[i].frac);
734 struct timespec E = timespec_init(-1, fdata[i].nsec);
737 r = lfp_intv_to_tspec(a);
738 TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit));
745 // nsec -> frac -> nsec roundtrip, using a prime start and increment
747 test_LFProundtrip(void)
752 for (t = -1; t < 2; ++t)
753 for (i = 4999; i < 1000000000; i += 10007) {
754 struct timespec E = timespec_init(t, i);
758 a = tspec_intv_to_lfp(E);
759 r = lfp_intv_to_tspec(a);
760 TEST_ASSERT_EQUAL_timespec(E, r);
766 //----------------------------------------------------------------------
768 //----------------------------------------------------------------------
773 static const struct {
778 { 0, 0, "0.000000000" },
779 { 2, 0, "2.000000000" },
780 {-2, 0, "-2.000000000" },
781 { 0, 1, "0.000000001" },
782 { 0,-1, "-0.000000001" },
783 { 1,-1, "0.999999999" },
784 {-1, 1, "-0.999999999" },
785 {-1,-1, "-1.000000001" },
789 for (i = 0; i < COUNTOF(data); ++i) {
790 struct timespec a = timespec_init(data[i].sec, data[i].nsec);
791 const char * E = data[i].repr;
792 const char * r = tspectoa(a);
793 TEST_ASSERT_EQUAL_STRING(E, r);