7 #include "timespecops.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"); \
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"); \
26 //timespec has time_t, long, and time_t is basically long uint, 4 or 8 bytes size, depending on 32/64bit
28 static u_int32 my_tick_to_tsf(u_int32 ticks);
29 static u_int32 my_tsf_to_tick(u_int32 tsf);
37 //******************************************MY CUSTOM FUNCTIONS*******************************
39 typedef int bool; //TRUE and FALSE are already defined somewhere, so I can't do typedef enum { FALSE, TRUE } boolean;
41 const bool timespec_isValid(struct timespec V)
42 { return V.tv_nsec >= 0 && V.tv_nsec < 1000000000; }
44 struct timespec timespec_init(time_t hi, long lo){
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)
61 //also in timelalops.c!!!!!!
62 bool AssertFpClose(const l_fp m,const l_fp n, const l_fp limit)
66 if (L_ISGEQ(&m, &n)) {
73 if (L_ISGEQ(&limit, &diff)){
77 //<< m_expr << " which is " << l_fp_wrap(m)
79 //<< n_expr << " which is " << l_fp_wrap(n)
80 //<< "\nare not close; diff=" << l_fp_wrap(diff);
86 bool AssertTimespecClose(const struct timespec m,const struct timespec n, const struct timespec limit)
90 diff = abs_tspec(sub_tspec(m, n));
91 if (cmp_tspec(limit, diff) >= 0)
95 //<< m_expr << " which is " << timespec_wrap(m)
97 //<< n_expr << " which is " << timespec_wrap(n)
98 //<< "\nare not close; diff=" << timespec_wrap(diff);
103 //-----------------------------------------------
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 }
123 u_int32 my_tick_to_tsf(u_int32 ticks)
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.
130 return (u_int32)((( ((u_int64)(ticks)) << 32) + 500000000) / 1000000000);
132 return (u_int32)((double(ticks)) * 4.294967296 + 0.5);
134 // And before you ask: if ticks >= 1000000000, the result is
135 // truncated nonsense, so don't use it out-of-bounds.
138 u_int32 my_tsf_to_tick(u_int32 tsf)
140 // Inverse operation: converts fraction to microseconds.
142 return (u_int32)(( ((u_int64)(tsf)) * 1000000000 + 0x80000000) >> 32);
144 return (u_int32)(double(tsf) / 4.294967296 + 0.5);
146 // Beware: The result might be 10^9 due to rounding!
151 // ---------------------------------------------------------------------
152 // test support stuff -- part 1
153 // ---------------------------------------------------------------------
155 void test_Helpers1() {
158 for (x.tv_sec = -2; x.tv_sec < 3; x.tv_sec++) {
160 TEST_ASSERT_FALSE(timespec_isValid(x));
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));
171 //----------------------------------------------------------------------
172 // test normalisation
173 //----------------------------------------------------------------------
175 void test_Normalise() {
177 for ( ns = -2000000000; ns <= 2000000000; ns += 10000000) {
178 struct timespec x = timespec_init(0, ns);
180 x = normalize_tspec(x);
181 TEST_ASSERT_TRUE(timespec_isValid(x));
185 //----------------------------------------------------------------------
186 // test classification
187 //----------------------------------------------------------------------
189 void test_SignNoFrac() {
190 // sign test, no fraction
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);
197 TEST_ASSERT_EQUAL(E, r);
201 void test_SignWithFrac() {
202 // sign test, with fraction
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);
212 //----------------------------------------------------------------------
214 //----------------------------------------------------------------------
215 void test_CmpFracEQ() {
216 // fractions are equal
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);
228 void test_CmpFracGT() {
229 // fraction a bigger fraction b
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);
241 void test_CmpFracLT() {
242 // fraction a less fraction b
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);
254 //----------------------------------------------------------------------
255 // Test addition (sum)
256 //----------------------------------------------------------------------
258 void test_AddFullNorm() {
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);
268 TEST_ASSERT_EQUAL_timespec(E, c);
272 void test_AddFullOflow1() {
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);
282 TEST_ASSERT_EQUAL_timespec(E, c);
286 void test_AddNsecNorm() {
288 for (i = -4; i <= 4; ++i) {
289 struct timespec a = timespec_init(i, 200);
290 struct timespec E = timespec_init(i, 600);
293 c = add_tspec_ns(a, 600 - 200);
294 TEST_ASSERT_EQUAL_timespec(E, c);
298 void test_AddNsecOflow1() {
300 for (i = -4; i <= 4; ++i) {
301 struct timespec a = timespec_init(i, 200);
302 struct timespec E = timespec_init(i + 1, 100);
305 c = add_tspec_ns(a, NANOSECONDS - 100);
306 TEST_ASSERT_EQUAL_timespec(E, c);
310 //----------------------------------------------------------------------
311 // test subtraction (difference)
312 //----------------------------------------------------------------------
314 void test_SubFullNorm() {
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);
324 TEST_ASSERT_EQUAL_timespec(E, c);
328 void test_SubFullOflow() {
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);
338 TEST_ASSERT_EQUAL_timespec(E, c);
342 void test_SubNsecNorm() {
344 for (i = -4; i <= 4; ++i) {
345 struct timespec a = timespec_init(i, 600);
346 struct timespec E = timespec_init(i, 200);
349 c = sub_tspec_ns(a, 600 - 200);
350 TEST_ASSERT_EQUAL_timespec(E, c);
354 void test_SubNsecOflow() {
356 for (i = -4; i <= 4; ++i) {
357 struct timespec a = timespec_init( i , 100);
358 struct timespec E = timespec_init(i-1, 200);
361 c = sub_tspec_ns(a, NANOSECONDS - 100);
362 TEST_ASSERT_EQUAL_timespec(E, c);
366 //----------------------------------------------------------------------
368 //----------------------------------------------------------------------
372 for (i = -4; i <= 4; ++i) {
373 struct timespec a = timespec_init(i, 100);
379 TEST_ASSERT_EQUAL(0, test_tspec(c));
383 //----------------------------------------------------------------------
385 //----------------------------------------------------------------------
387 void test_AbsNoFrac() {
389 for (i = -4; i <= 4; ++i) {
390 struct timespec a = timespec_init(i , 0);
394 TEST_ASSERT_EQUAL((i != 0), test_tspec(b));
398 void test_AbsWithFrac() {
400 for (i = -4; i <= 4; ++i) {
401 struct timespec a = timespec_init(i, 100);
405 TEST_ASSERT_EQUAL(1, test_tspec(b));
409 // ---------------------------------------------------------------------
410 // test support stuff -- part 2
411 // ---------------------------------------------------------------------
413 void test_Helpers2() {
414 struct timespec limit = timespec_init(0,2);
416 struct timespec x, y;
419 for (x.tv_sec = -2; x.tv_sec < 3; x.tv_sec++)
421 x.tv_nsec < 1000000000;
422 x.tv_nsec += 499999999) {
423 for (i = -4; i < 5; i++) {
426 if (i >= -2 && i <= 2){
427 TEST_ASSERT_TRUE(AssertTimespecClose(x,y,limit));//ASSERT_PRED_FORMAT2(isClose, x, y);
431 TEST_ASSERT_FALSE(AssertTimespecClose(x,y,limit));//ASSERT_PRED_FORMAT2(!isClose, x, y);
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);
441 //----------------------------------------------------------------------
442 // conversion to l_fp
443 //----------------------------------------------------------------------
445 void test_ToLFPbittest() {
446 l_fp lfpClose = l_fp_init(0,1);
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));
453 r = tspec_intv_to_lfp(a);
454 TEST_ASSERT_TRUE(AssertFpClose(E,r,lfpClose)); //ASSERT_PRED_FORMAT2(FpClose, E, r);
458 void test_ToLFPrelPos() {
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);
465 r = tspec_intv_to_lfp(a);
466 TEST_ASSERT_EQUAL_l_fp(E, r);
470 void test_ToLFPrelNeg() {
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);
477 r = tspec_intv_to_lfp(a);
478 TEST_ASSERT_EQUAL_l_fp(E, r);
482 void test_ToLFPabs() {
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);
489 r = tspec_stamp_to_lfp(a);
490 TEST_ASSERT_EQUAL_l_fp(E, r);
494 //----------------------------------------------------------------------
495 // conversion from l_fp
496 //----------------------------------------------------------------------
497 void test_FromLFPbittest() {
498 struct timespec limit = timespec_init(0,2);
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.
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);
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);
516 void test_FromLFPrelPos() {
517 struct timespec limit = timespec_init(0,2);
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);
524 r = lfp_intv_to_tspec(a);
525 TEST_ASSERT_TRUE(AssertTimespecClose(E,r,limit)); //ASSERT_PRED_FORMAT2(TimespecClose, E, r);
529 void test_FromLFPrelNeg() {
530 struct timespec limit = timespec_init(0,2);
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);
537 r = lfp_intv_to_tspec(a);
538 TEST_ASSERT_TRUE(AssertTimespecClose(E,r,limit)); //ASSERT_PRED_FORMAT2(TimespecClose, E, r);
543 // nsec -> frac -> nsec roundtrip, using a prime start and increment
544 void test_LFProundtrip() {
547 for (t = -1; t < 2; ++t)
548 for (i = 4999; i < 1000000000; i+=10007) {
549 struct timespec E = timespec_init(t, i);
553 a = tspec_intv_to_lfp(E);
554 r = lfp_intv_to_tspec(a);
555 TEST_ASSERT_EQUAL_timespec(E, r);
559 //----------------------------------------------------------------------
561 //----------------------------------------------------------------------
563 void test_ToString() {
564 static const struct {
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" },
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);