1284990Scy#include "config.h"
2284990Scy
3284990Scy//some unused features are still in the wrapper, unconverted
4284990Scy
5284990Scy#include "ntp_types.h"
6284990Scy#include "ntp_fp.h"
7284990Scy
8284990Scy#include "timevalops.h"
9284990Scy
10289997Sglebius#include <math.h>
11284990Scy#include "unity.h"
12284990Scy
13284990Scy
14293650Sglebius#define TEST_ASSERT_EQUAL_timeval(a, b) {				\
15293650Sglebius    TEST_ASSERT_EQUAL_MESSAGE(a.tv_sec, b.tv_sec, "Field tv_sec");	\
16284990Scy    TEST_ASSERT_EQUAL_MESSAGE(a.tv_usec, b.tv_usec, "Field tv_usec");	\
17284990Scy}
18284990Scy
19284990Scy
20284990Scystatic u_int32 my_tick_to_tsf(u_int32 ticks);
21284990Scystatic u_int32 my_tsf_to_tick(u_int32 tsf);
22284990Scy
23289997Sglebius
24284990Scy// that's it...
25284990Scytypedef struct {
26284990Scy	long	usec;
27284990Scy	u_int32	frac;
28284990Scy} lfpfracdata ;
29284990Scy
30289997Sglebiusstruct timeval timeval_init( time_t hi, long lo);
31289997Sglebiusconst bool timeval_isValid(struct timeval V);
32289997Sglebiusl_fp l_fp_init(int32 i, u_int32 f);
33289997Sglebiusbool AssertTimevalClose(const struct timeval m, const struct timeval n, const struct timeval limit);
34289997Sglebiusbool AssertFpClose(const l_fp m, const l_fp n, const l_fp limit);
35289997Sglebius
36293650Sglebiusvoid setUp(void);
37289997Sglebiusvoid test_Helpers1(void);
38289997Sglebiusvoid test_Normalise(void);
39289997Sglebiusvoid test_SignNoFrac(void);
40289997Sglebiusvoid test_SignWithFrac(void);
41289997Sglebiusvoid test_CmpFracEQ(void);
42289997Sglebiusvoid test_CmpFracGT(void);
43289997Sglebiusvoid test_CmpFracLT(void);
44289997Sglebiusvoid test_AddFullNorm(void);
45289997Sglebiusvoid test_AddFullOflow1(void);
46289997Sglebiusvoid test_AddUsecNorm(void);
47289997Sglebiusvoid test_AddUsecOflow1(void);
48289997Sglebiusvoid test_SubFullNorm(void);
49289997Sglebiusvoid test_SubFullOflow(void);
50289997Sglebiusvoid test_SubUsecNorm(void);
51289997Sglebiusvoid test_SubUsecOflow(void);
52289997Sglebiusvoid test_Neg(void);
53289997Sglebiusvoid test_AbsNoFrac(void);
54289997Sglebiusvoid test_AbsWithFrac(void);
55289997Sglebiusvoid test_Helpers2(void);
56289997Sglebiusvoid test_ToLFPbittest(void);
57289997Sglebiusvoid test_ToLFPrelPos(void);
58289997Sglebiusvoid test_ToLFPrelNeg(void);
59289997Sglebiusvoid test_ToLFPabs(void);
60289997Sglebiusvoid test_FromLFPbittest(void);
61289997Sglebiusvoid test_FromLFPrelPos(void);
62289997Sglebiusvoid test_FromLFPrelNeg(void);
63289997Sglebiusvoid test_LFProundtrip(void);
64289997Sglebiusvoid test_ToString(void);
65289997Sglebius
66289997Sglebius
67293650Sglebius//**********************************MY CUSTOM FUNCTIONS***********************
68284990Scy
69284990Scy
70293650Sglebiusvoid
71293650SglebiussetUp(void)
72293650Sglebius{
73293650Sglebius	init_lib();
74289997Sglebius
75293650Sglebius	return;
76293650Sglebius}
77293650Sglebius
78293650Sglebius
79289997Sglebiusstruct timeval
80293650Sglebiustimeval_init(time_t hi, long lo)
81293650Sglebius{
82284990Scy	struct timeval V;
83293650Sglebius
84284990Scy	V.tv_sec = hi;
85284990Scy	V.tv_usec = lo;
86293650Sglebius
87284990Scy	return V;
88284990Scy}
89284990Scy
90284990Scy
91289997Sglebiusconst bool
92293650Sglebiustimeval_isValid(struct timeval V)
93293650Sglebius{
94293650Sglebius
95289997Sglebius	return V.tv_usec >= 0 && V.tv_usec < 1000000;
96289997Sglebius}
97289997Sglebius
98289997Sglebius
99289997Sglebiusl_fp
100293650Sglebiusl_fp_init(int32 i, u_int32 f)
101293650Sglebius{
102284990Scy	l_fp temp;
103293650Sglebius
104284990Scy	temp.l_i  = i;
105284990Scy	temp.l_uf = f;
106284990Scy
107284990Scy	return temp;
108284990Scy}
109284990Scy
110289997Sglebius
111289997Sglebiusbool
112293650SglebiusAssertTimevalClose(const struct timeval m, const struct timeval n, const struct timeval limit)
113293650Sglebius{
114284990Scy	struct timeval diff;
115284990Scy
116284990Scy	diff = abs_tval(sub_tval(m, n));
117284990Scy	if (cmp_tval(limit, diff) >= 0)
118284990Scy		return TRUE;
119284990Scy	else
120284990Scy	{
121289997Sglebius		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);
122289997Sglebius		//I don't have variables m_expr and n_expr in unity, those are command line arguments which only getst has!!!
123293650Sglebius
124284990Scy		return FALSE;
125284990Scy	}
126284990Scy}
127284990Scy
128289997Sglebius
129289997Sglebiusbool
130293650SglebiusAssertFpClose(const l_fp m, const l_fp n, const l_fp limit)
131293650Sglebius{
132284990Scy	l_fp diff;
133284990Scy
134284990Scy	if (L_ISGEQ(&m, &n)) {
135284990Scy		diff = m;
136284990Scy		L_SUB(&diff, &n);
137284990Scy	} else {
138284990Scy		diff = n;
139284990Scy		L_SUB(&diff, &m);
140284990Scy	}
141293650Sglebius	if (L_ISGEQ(&limit, &diff)) {
142284990Scy		return TRUE;
143284990Scy	}
144284990Scy	else {
145289997Sglebius		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));
146289997Sglebius		//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);
147284990Scy		return FALSE;
148284990Scy	}
149284990Scy}
150284990Scy
151284990Scy
152284990Scy//---------------------------------------------------
153284990Scy
154284990Scystatic const lfpfracdata fdata[] = {
155284990Scy	{      0, 0x00000000 }, {   7478, 0x01ea1405 },
156284990Scy	{  22077, 0x05a6d699 }, { 125000, 0x20000000 },
157284990Scy	{ 180326, 0x2e29d841 }, { 207979, 0x353e1c9b },
158284990Scy	{ 250000, 0x40000000 }, { 269509, 0x44fe8ab5 },
159284990Scy	{ 330441, 0x5497c808 }, { 333038, 0x5541fa76 },
160284990Scy	{ 375000, 0x60000000 }, { 394734, 0x650d4995 },
161284990Scy	{ 446327, 0x72427c7c }, { 500000, 0x80000000 },
162284990Scy	{ 517139, 0x846338b4 }, { 571953, 0x926b8306 },
163284990Scy	{ 587353, 0x965cc426 }, { 625000, 0xa0000000 },
164284990Scy	{ 692136, 0xb12fd32c }, { 750000, 0xc0000000 },
165284990Scy	{ 834068, 0xd5857aff }, { 848454, 0xd9344806 },
166284990Scy	{ 854222, 0xdaae4b02 }, { 861465, 0xdc88f862 },
167284990Scy	{ 875000, 0xe0000000 }, { 910661, 0xe921144d },
168284990Scy	{ 922162, 0xec12cf10 }, { 942190, 0xf1335d25 }
169284990Scy};
170284990Scy
171284990Scy
172289997Sglebiusu_int32
173293650Sglebiusmy_tick_to_tsf(u_int32 ticks)
174293650Sglebius{
175284990Scy	// convert microseconds to l_fp fractional units, using double
176284990Scy	// precision float calculations or, if available, 64bit integer
177284990Scy	// arithmetic. This should give the precise fraction, rounded to
178284990Scy	// the nearest representation.
179293650Sglebius
180284990Scy#ifdef HAVE_U_INT64
181284990Scy	return (u_int32)((( ((u_int64)(ticks)) << 32) + 500000) / 1000000); //I put too much () when casting just to be safe
182284990Scy#else
183284990Scy	return (u_int32)( ((double)(ticks)) * 4294.967296 + 0.5);
184284990Scy#endif
185284990Scy	// And before you ask: if ticks >= 1000000, the result is
186284990Scy	// truncated nonsense, so don't use it out-of-bounds.
187284990Scy}
188284990Scy
189289997Sglebius
190289997Sglebiusu_int32
191293650Sglebiusmy_tsf_to_tick(u_int32 tsf)
192293650Sglebius{
193284990Scy	// Inverse operation: converts fraction to microseconds.
194284990Scy#ifdef HAVE_U_INT64
195284990Scy	return (u_int32)( ((u_int64)(tsf) * 1000000 + 0x80000000) >> 32); //CHECK ME!!!
196284990Scy#else
197284990Scy	return (u_int32)(double(tsf) / 4294.967296 + 0.5);
198284990Scy#endif
199284990Scy	// Beware: The result might be 10^6 due to rounding!
200284990Scy}
201284990Scy
202284990Scy
203293650Sglebius//*******************************END OF CUSTOM FUNCTIONS*********************
204284990Scy
205284990Scy
206284990Scy// ---------------------------------------------------------------------
207284990Scy// test support stuff - part1
208284990Scy// ---------------------------------------------------------------------
209284990Scy
210289997Sglebiusvoid
211293650Sglebiustest_Helpers1(void)
212293650Sglebius{
213284990Scy	struct timeval x;
214284990Scy
215284990Scy	for (x.tv_sec = -2; x.tv_sec < 3; x.tv_sec++) {
216284990Scy		x.tv_usec = -1;
217284990Scy		TEST_ASSERT_FALSE(timeval_isValid(x));
218284990Scy		x.tv_usec = 0;
219284990Scy		TEST_ASSERT_TRUE(timeval_isValid(x));
220284990Scy		x.tv_usec = 999999;
221284990Scy		TEST_ASSERT_TRUE(timeval_isValid(x));
222284990Scy		x.tv_usec = 1000000;
223284990Scy		TEST_ASSERT_FALSE(timeval_isValid(x));
224284990Scy	}
225293650Sglebius
226293650Sglebius	return;
227284990Scy}
228284990Scy
229284990Scy
230284990Scy//----------------------------------------------------------------------
231284990Scy// test normalisation
232284990Scy//----------------------------------------------------------------------
233284990Scy
234289997Sglebiusvoid
235293650Sglebiustest_Normalise(void)
236293650Sglebius{
237284990Scy	long ns;
238293650Sglebius
239284990Scy	for (ns = -2000000000; ns <= 2000000000; ns += 10000000) {
240284990Scy		struct timeval x = timeval_init(0, ns);
241293650Sglebius
242284990Scy		x = normalize_tval(x);
243284990Scy		TEST_ASSERT_TRUE(timeval_isValid(x));
244284990Scy	}
245293650Sglebius
246293650Sglebius	return;
247284990Scy}
248284990Scy
249284990Scy//----------------------------------------------------------------------
250284990Scy// test classification
251284990Scy//----------------------------------------------------------------------
252284990Scy
253289997Sglebiusvoid
254293650Sglebiustest_SignNoFrac(void)
255293650Sglebius{
256284990Scy	int i;
257293650Sglebius
258284990Scy	// sign test, no fraction
259284990Scy	for (i = -4; i <= 4; ++i) {
260284990Scy		struct timeval a = timeval_init(i, 0);
261284990Scy		int	     E = (i > 0) - (i < 0);
262284990Scy		int	     r = test_tval(a);
263284990Scy
264284990Scy		TEST_ASSERT_EQUAL(E, r);
265284990Scy	}
266293650Sglebius
267293650Sglebius	return;
268284990Scy}
269284990Scy
270289997Sglebius
271289997Sglebiusvoid
272293650Sglebiustest_SignWithFrac(void)
273293650Sglebius{
274284990Scy	// sign test, with fraction
275284990Scy	int i;
276293650Sglebius
277284990Scy	for (i = -4; i <= 4; ++i) {
278284990Scy		struct timeval a = timeval_init(i, 10);
279284990Scy		int	     E = (i >= 0) - (i < 0);
280284990Scy		int	     r = test_tval(a);
281284990Scy
282284990Scy		TEST_ASSERT_EQUAL(E, r);
283284990Scy	}
284293650Sglebius
285293650Sglebius	return;
286284990Scy}
287284990Scy
288284990Scy//----------------------------------------------------------------------
289284990Scy// test compare
290284990Scy//----------------------------------------------------------------------
291289997Sglebiusvoid
292293650Sglebiustest_CmpFracEQ(void)
293293650Sglebius{
294289997Sglebius	int i, j;
295293650Sglebius
296284990Scy	// fractions are equal
297284990Scy	for (i = -4; i <= 4; ++i)
298284990Scy		for (j = -4; j <= 4; ++j) {
299284990Scy			struct timeval a = timeval_init(i, 200);
300284990Scy			struct timeval b = timeval_init(j, 200);
301284990Scy			int	     E = (i > j) - (i < j);
302284990Scy			int	     r = cmp_tval_denorm(a, b);
303284990Scy
304284990Scy			TEST_ASSERT_EQUAL(E, r);
305284990Scy		}
306293650Sglebius
307293650Sglebius	return;
308284990Scy}
309284990Scy
310289997Sglebius
311289997Sglebiusvoid
312293650Sglebiustest_CmpFracGT(void)
313293650Sglebius{
314284990Scy	// fraction a bigger fraction b
315289997Sglebius	int i, j;
316293650Sglebius
317284990Scy	for (i = -4; i <= 4; ++i)
318284990Scy		for (j = -4; j <= 4; ++j) {
319284990Scy			struct timeval a = timeval_init( i , 999800);
320284990Scy			struct timeval b = timeval_init( j , 200);
321284990Scy			int	     E = (i >= j) - (i < j);
322284990Scy			int	     r = cmp_tval_denorm(a, b);
323284990Scy
324284990Scy			TEST_ASSERT_EQUAL(E, r);
325284990Scy		}
326293650Sglebius
327293650Sglebius	return;
328284990Scy}
329284990Scy
330289997Sglebius
331289997Sglebiusvoid
332293650Sglebiustest_CmpFracLT(void)
333293650Sglebius{
334284990Scy	// fraction a less fraction b
335289997Sglebius	int i, j;
336293650Sglebius
337284990Scy	for (i = -4; i <= 4; ++i)
338284990Scy		for (j = -4; j <= 4; ++j) {
339284990Scy			struct timeval a = timeval_init(i, 200);
340284990Scy			struct timeval b = timeval_init(j, 999800);
341284990Scy			int	     E = (i > j) - (i <= j);
342284990Scy			int	     r = cmp_tval_denorm(a, b);
343284990Scy
344284990Scy			TEST_ASSERT_EQUAL(E, r);
345284990Scy		}
346293650Sglebius
347293650Sglebius	return;
348284990Scy}
349284990Scy
350284990Scy//----------------------------------------------------------------------
351284990Scy// Test addition (sum)
352284990Scy//----------------------------------------------------------------------
353284990Scy
354289997Sglebiusvoid
355293650Sglebiustest_AddFullNorm(void)
356293650Sglebius{
357289997Sglebius	int i, j;
358293650Sglebius
359284990Scy	for (i = -4; i <= 4; ++i)
360284990Scy		for (j = -4; j <= 4; ++j) {
361284990Scy			struct timeval a = timeval_init(i, 200);
362284990Scy			struct timeval b = timeval_init(j, 400);
363284990Scy			struct timeval E = timeval_init(i + j, 200 + 400);
364284990Scy			struct timeval c;
365284990Scy
366284990Scy			c = add_tval(a, b);
367284990Scy			TEST_ASSERT_EQUAL_timeval(E, c);
368284990Scy		}
369293650Sglebius
370293650Sglebius	return;
371284990Scy}
372284990Scy
373289997Sglebius
374289997Sglebiusvoid
375293650Sglebiustest_AddFullOflow1(void)
376293650Sglebius{
377289997Sglebius	int i, j;
378293650Sglebius
379284990Scy	for (i = -4; i <= 4; ++i)
380284990Scy		for (j = -4; j <= 4; ++j) {
381284990Scy			struct timeval a = timeval_init(i, 200);
382284990Scy			struct timeval b = timeval_init(j, 999900);
383284990Scy			struct timeval E = timeval_init(i + j + 1, 100);
384284990Scy			struct timeval c;
385284990Scy
386284990Scy			c = add_tval(a, b);
387284990Scy			TEST_ASSERT_EQUAL_timeval(E, c);
388284990Scy		}
389293650Sglebius
390293650Sglebius	return;
391284990Scy}
392284990Scy
393289997Sglebius
394289997Sglebiusvoid
395293650Sglebiustest_AddUsecNorm(void)
396293650Sglebius{
397284990Scy	int i;
398293650Sglebius
399284990Scy	for (i = -4; i <= 4; ++i) {
400284990Scy		struct timeval a = timeval_init(i, 200);
401284990Scy		struct timeval E = timeval_init(i, 600);
402284990Scy		struct timeval c;
403284990Scy
404284990Scy		c = add_tval_us(a, 600 - 200);
405284990Scy		TEST_ASSERT_EQUAL_timeval(E, c);
406284990Scy	}
407293650Sglebius
408293650Sglebius	return;
409284990Scy}
410284990Scy
411289997Sglebius
412289997Sglebiusvoid
413293650Sglebiustest_AddUsecOflow1(void)
414293650Sglebius{
415284990Scy	int i;
416293650Sglebius
417284990Scy	for (i = -4; i <= 4; ++i) {
418284990Scy		struct timeval a = timeval_init(i, 200);
419284990Scy		struct timeval E = timeval_init(i + 1, 100);
420284990Scy		struct timeval c;
421284990Scy
422284990Scy		c = add_tval_us(a, MICROSECONDS - 100);
423284990Scy		TEST_ASSERT_EQUAL_timeval(E, c);
424284990Scy	}
425293650Sglebius
426293650Sglebius	return;
427284990Scy}
428284990Scy
429284990Scy//----------------------------------------------------------------------
430284990Scy// test subtraction (difference)
431284990Scy//----------------------------------------------------------------------
432284990Scy
433289997Sglebiusvoid
434293650Sglebiustest_SubFullNorm(void)
435293650Sglebius{
436289997Sglebius	int i, j;
437293650Sglebius
438284990Scy	for (i = -4; i <= 4; ++i)
439284990Scy		for (j = -4; j <= 4; ++j) {
440284990Scy			struct timeval a = timeval_init(i, 600);
441284990Scy			struct timeval b = timeval_init(j, 400);
442284990Scy			struct timeval E = timeval_init(i - j, 600 - 400);
443284990Scy			struct timeval c;
444284990Scy
445284990Scy			c = sub_tval(a, b);
446284990Scy			TEST_ASSERT_EQUAL_timeval(E, c);
447284990Scy		}
448293650Sglebius
449293650Sglebius	return;
450284990Scy}
451284990Scy
452289997Sglebius
453289997Sglebiusvoid
454293650Sglebiustest_SubFullOflow(void)
455293650Sglebius{
456289997Sglebius	int i, j;
457293650Sglebius
458284990Scy	for (i = -4; i <= 4; ++i)
459284990Scy		for (j = -4; j <= 4; ++j) {
460284990Scy			struct timeval a = timeval_init(i, 100);
461284990Scy			struct timeval b = timeval_init(j, 999900);
462284990Scy			struct timeval E = timeval_init(i - j - 1, 200);
463284990Scy			struct timeval c;
464284990Scy
465284990Scy			c = sub_tval(a, b);
466284990Scy			TEST_ASSERT_EQUAL_timeval(E, c);
467284990Scy		}
468293650Sglebius
469293650Sglebius	return;
470284990Scy}
471284990Scy
472289997Sglebius
473289997Sglebiusvoid
474293650Sglebiustest_SubUsecNorm(void)
475293650Sglebius{
476284990Scy	int i = -4;
477293650Sglebius
478284990Scy	for (i = -4; i <= 4; ++i) {
479284990Scy		struct timeval a = timeval_init(i, 600);
480284990Scy		struct timeval E = timeval_init(i, 200);
481284990Scy		struct timeval c;
482284990Scy
483284990Scy		c = sub_tval_us(a, 600 - 200);
484284990Scy		TEST_ASSERT_EQUAL_timeval(E, c);
485284990Scy	}
486293650Sglebius
487293650Sglebius	return;
488284990Scy}
489284990Scy
490289997Sglebius
491289997Sglebiusvoid
492293650Sglebiustest_SubUsecOflow(void)
493293650Sglebius{
494284990Scy	int i = -4;
495293650Sglebius
496284990Scy	for (i = -4; i <= 4; ++i) {
497284990Scy		struct timeval a = timeval_init(i, 100);
498284990Scy		struct timeval E = timeval_init(i - 1, 200);
499284990Scy		struct timeval c;
500284990Scy
501284990Scy		c = sub_tval_us(a, MICROSECONDS - 100);
502284990Scy		TEST_ASSERT_EQUAL_timeval(E, c);
503284990Scy	}
504293650Sglebius
505293650Sglebius	return;
506284990Scy}
507284990Scy
508284990Scy//----------------------------------------------------------------------
509284990Scy// test negation
510284990Scy//----------------------------------------------------------------------
511284990Scy
512289997Sglebiusvoid
513293650Sglebiustest_Neg(void)
514293650Sglebius{
515284990Scy	int i = -4;
516293650Sglebius
517284990Scy	for (i = -4; i <= 4; ++i) {
518284990Scy		struct timeval a = timeval_init(i, 100);
519284990Scy		struct timeval b;
520284990Scy		struct timeval c;
521284990Scy
522284990Scy		b = neg_tval(a);
523284990Scy		c = add_tval(a, b);
524284990Scy		TEST_ASSERT_EQUAL(0, test_tval(c));
525284990Scy	}
526293650Sglebius
527293650Sglebius	return;
528284990Scy}
529284990Scy
530284990Scy//----------------------------------------------------------------------
531284990Scy// test abs value
532284990Scy//----------------------------------------------------------------------
533284990Scy
534289997Sglebiusvoid
535293650Sglebiustest_AbsNoFrac(void)
536293650Sglebius{
537284990Scy	int i = -4;
538293650Sglebius
539284990Scy	for (i = -4; i <= 4; ++i) {
540284990Scy		struct timeval a = timeval_init(i, 0);
541284990Scy		struct timeval b;
542284990Scy
543284990Scy		b = abs_tval(a);
544284990Scy		TEST_ASSERT_EQUAL((i != 0), test_tval(b));
545284990Scy	}
546293650Sglebius
547293650Sglebius	return;
548284990Scy}
549284990Scy
550289997Sglebius
551289997Sglebiusvoid
552293650Sglebiustest_AbsWithFrac(void)
553293650Sglebius{
554284990Scy	int i = -4;
555293650Sglebius
556284990Scy	for (i = -4; i <= 4; ++i) {
557284990Scy		struct timeval a = timeval_init(i, 100);
558284990Scy		struct timeval b;
559284990Scy
560284990Scy		b = abs_tval(a);
561284990Scy		TEST_ASSERT_EQUAL(1, test_tval(b));
562284990Scy	}
563293650Sglebius
564293650Sglebius	return;
565284990Scy}
566284990Scy
567284990Scy// ---------------------------------------------------------------------
568284990Scy// test support stuff -- part 2
569284990Scy// ---------------------------------------------------------------------
570284990Scy
571284990Scy
572289997Sglebiusvoid
573293650Sglebiustest_Helpers2(void)
574293650Sglebius{
575284990Scy	struct timeval limit = timeval_init(0, 2);
576284990Scy	struct timeval x, y;
577293650Sglebius	long i;
578284990Scy
579293650Sglebius	for (x.tv_sec = -2; x.tv_sec < 3; x.tv_sec++) {
580284990Scy		for (x.tv_usec = 1;
581284990Scy		     x.tv_usec < 1000000;
582284990Scy		     x.tv_usec += 499999) {
583289997Sglebius			for (i = -4; i < 5; ++i) {
584284990Scy				y = x;
585284990Scy				y.tv_usec += i;
586293650Sglebius				if (i >= -2 && i <= 2) {
587289997Sglebius					TEST_ASSERT_TRUE(AssertTimevalClose(x, y, limit));//ASSERT_PRED_FORMAT2(isClose, x, y);
588284990Scy				}
589284990Scy				else {
590289997Sglebius					TEST_ASSERT_FALSE(AssertTimevalClose(x, y, limit));
591284990Scy				}
592284990Scy			}
593284990Scy		}
594284990Scy	}
595293650Sglebius
596293650Sglebius	return;
597284990Scy}
598284990Scy
599284990Scy// and the global predicate instances we're using here
600284990Scy
601289997Sglebius//static l_fp lfpClose =  l_fp_init(0, 1); //static AssertFpClose FpClose(0, 1);
602289997Sglebius//static struct timeval timevalClose = timeval_init(0, 1); //static AssertTimevalClose TimevalClose(0, 1);
603284990Scy
604284990Scy//----------------------------------------------------------------------
605284990Scy// conversion to l_fp
606284990Scy//----------------------------------------------------------------------
607284990Scy
608289997Sglebiusvoid
609293650Sglebiustest_ToLFPbittest(void)
610293650Sglebius{
611293650Sglebius	l_fp lfpClose =  l_fp_init(0, 1);
612284990Scy
613284990Scy	u_int32 i = 0;
614289997Sglebius	for (i = 0; i < 1000000; ++i) {
615284990Scy		struct timeval a = timeval_init(1, i);
616289997Sglebius		l_fp E = l_fp_init(1, my_tick_to_tsf(i));
617284990Scy		l_fp r;
618284990Scy
619284990Scy		r = tval_intv_to_lfp(a);
620289997Sglebius		TEST_ASSERT_TRUE(AssertFpClose(E, r, lfpClose));	//ASSERT_PRED_FORMAT2(FpClose, E, r);
621284990Scy	}
622293650Sglebius
623293650Sglebius	return;
624284990Scy}
625284990Scy
626284990Scy
627289997Sglebiusvoid
628293650Sglebiustest_ToLFPrelPos(void)
629293650Sglebius{
630289997Sglebius	l_fp lfpClose =  l_fp_init(0, 1);
631293650Sglebius	int i = 0;
632284990Scy
633289997Sglebius	for (i = 0; i < COUNTOF(fdata); ++i) {
634284990Scy		struct timeval a = timeval_init(1, fdata[i].usec);
635284990Scy		l_fp E = l_fp_init(1, fdata[i].frac);
636284990Scy		l_fp r;
637284990Scy
638284990Scy		r = tval_intv_to_lfp(a);
639289997Sglebius		TEST_ASSERT_TRUE(AssertFpClose(E, r, lfpClose));
640284990Scy	}
641293650Sglebius
642293650Sglebius	return;
643284990Scy}
644284990Scy
645289997Sglebius
646289997Sglebiusvoid
647293650Sglebiustest_ToLFPrelNeg(void)
648293650Sglebius{
649289997Sglebius	l_fp lfpClose =  l_fp_init(0, 1);
650284990Scy	int i = 0;
651293650Sglebius
652289997Sglebius	for (i = 0; i < COUNTOF(fdata); ++i) {
653284990Scy		struct timeval a = timeval_init(-1, fdata[i].usec);
654284990Scy		l_fp E = l_fp_init(~0, fdata[i].frac);
655284990Scy		l_fp    r;
656284990Scy
657284990Scy		r = tval_intv_to_lfp(a);
658289997Sglebius		TEST_ASSERT_TRUE(AssertFpClose(E, r, lfpClose));
659284990Scy	}
660293650Sglebius
661293650Sglebius	return;
662284990Scy}
663284990Scy
664284990Scy
665289997Sglebiusvoid
666293650Sglebiustest_ToLFPabs(void)
667293650Sglebius{
668289997Sglebius	l_fp lfpClose =  l_fp_init(0, 1);
669293650Sglebius	int i = 0;
670289997Sglebius
671289997Sglebius	for (i = 0; i < COUNTOF(fdata); ++i) {
672284990Scy		struct timeval a = timeval_init(1, fdata[i].usec);
673284990Scy		l_fp E = l_fp_init(1 + JAN_1970, fdata[i].frac);
674284990Scy		l_fp    r;
675284990Scy
676284990Scy		r = tval_stamp_to_lfp(a);
677289997Sglebius		TEST_ASSERT_TRUE(AssertFpClose(E, r, lfpClose));
678284990Scy	}
679293650Sglebius
680293650Sglebius	return;
681284990Scy}
682284990Scy
683284990Scy//----------------------------------------------------------------------
684284990Scy// conversion from l_fp
685284990Scy//----------------------------------------------------------------------
686284990Scy
687289997Sglebiusvoid
688293650Sglebiustest_FromLFPbittest(void)
689293650Sglebius{
690289997Sglebius	struct timeval timevalClose = timeval_init(0, 1);
691284990Scy	// Not *exactly* a bittest, because 2**32 tests would take a
692284990Scy	// really long time even on very fast machines! So we do test
693284990Scy	// every 1000 fractional units.
694284990Scy	u_int32 tsf = 0;
695293650Sglebius
696284990Scy	for (tsf = 0; tsf < ~((u_int32)(1000)); tsf += 1000) {
697284990Scy		struct timeval E = timeval_init(1, my_tsf_to_tick(tsf));
698284990Scy		l_fp a = l_fp_init(1, tsf);
699284990Scy		struct timeval r;
700284990Scy
701284990Scy		r = lfp_intv_to_tval(a);
702284990Scy		// The conversion might be off by one microsecond when
703284990Scy		// comparing to calculated value.
704289997Sglebius		TEST_ASSERT_TRUE(AssertTimevalClose(E, r, timevalClose));
705284990Scy	}
706293650Sglebius
707293650Sglebius	return;
708284990Scy}
709284990Scy
710289997Sglebius
711289997Sglebiusvoid
712293650Sglebiustest_FromLFPrelPos(void)
713293650Sglebius{
714289997Sglebius	struct timeval timevalClose = timeval_init(0, 1);
715293650Sglebius	int i = 0;
716293650Sglebius
717289997Sglebius	for (i = 0; i < COUNTOF(fdata); ++i) {
718284990Scy		l_fp a = l_fp_init(1, fdata[i].frac);
719284990Scy		struct timeval E = timeval_init(1, fdata[i].usec);
720284990Scy		struct timeval r;
721284990Scy
722284990Scy		r = lfp_intv_to_tval(a);
723289997Sglebius		TEST_ASSERT_TRUE(AssertTimevalClose(E, r, timevalClose));
724284990Scy	}
725293650Sglebius
726293650Sglebius	return;
727284990Scy}
728284990Scy
729289997Sglebius
730289997Sglebiusvoid
731293650Sglebiustest_FromLFPrelNeg(void)
732293650Sglebius{
733289997Sglebius	struct timeval timevalClose = timeval_init(0, 1);
734284990Scy	int i = 0;
735293650Sglebius
736289997Sglebius	for (i = 0; i < COUNTOF(fdata); ++i) {
737284990Scy		l_fp a = l_fp_init(~0, fdata[i].frac);
738284990Scy		struct timeval E = timeval_init(-1, fdata[i].usec);
739284990Scy		struct timeval r;
740284990Scy
741284990Scy		r = lfp_intv_to_tval(a);
742289997Sglebius		TEST_ASSERT_TRUE(AssertTimevalClose(E, r, timevalClose));
743284990Scy	}
744293650Sglebius
745293650Sglebius	return;
746284990Scy}
747284990Scy
748289997Sglebius
749284990Scy// usec -> frac -> usec roundtrip, using a prime start and increment
750289997Sglebiusvoid
751293650Sglebiustest_LFProundtrip(void)
752293650Sglebius{
753284990Scy	int32_t t = -1;
754284990Scy	u_int32 i = 5;
755293650Sglebius
756284990Scy	for (t = -1; t < 2; ++t)
757289997Sglebius		for (i = 5; i < 1000000; i += 11) {
758284990Scy			struct timeval E = timeval_init(t, i);
759284990Scy			l_fp a;
760284990Scy			struct timeval r;
761284990Scy
762284990Scy			a = tval_intv_to_lfp(E);
763284990Scy			r = lfp_intv_to_tval(a);
764284990Scy			TEST_ASSERT_EQUAL_timeval(E, r);
765284990Scy		}
766293650Sglebius
767293650Sglebius	return;
768284990Scy}
769284990Scy
770284990Scy//----------------------------------------------------------------------
771284990Scy// string formatting
772284990Scy//----------------------------------------------------------------------
773284990Scy
774289997Sglebiusvoid
775293650Sglebiustest_ToString(void)
776293650Sglebius{
777284990Scy	static const struct {
778284990Scy		time_t	     sec;
779284990Scy		long	     usec;
780284990Scy		const char * repr;
781284990Scy	} data [] = {
782284990Scy		{ 0, 0,	 "0.000000" },
783284990Scy		{ 2, 0,	 "2.000000" },
784284990Scy		{-2, 0, "-2.000000" },
785284990Scy		{ 0, 1,	 "0.000001" },
786284990Scy		{ 0,-1,	"-0.000001" },
787284990Scy		{ 1,-1,	 "0.999999" },
788284990Scy		{-1, 1, "-0.999999" },
789284990Scy		{-1,-1, "-1.000001" },
790284990Scy	};
791284990Scy	int i;
792293650Sglebius
793284990Scy	for (i = 0; i < COUNTOF(data); ++i) {
794284990Scy		struct timeval a = timeval_init(data[i].sec, data[i].usec);
795289997Sglebius		const char *  E = data[i].repr;
796284990Scy		const char *  r = tvaltoa(a);
797284990Scy
798284990Scy		TEST_ASSERT_EQUAL_STRING(E, r);
799284990Scy	}
800293650Sglebius
801293650Sglebius	return;
802284990Scy}
803284990Scy
804284990Scy// -*- EOF -*-
805