4 #include "timevalops.h"
7 #include <math.h>// Required on Solaris for ldexp.
10 void test_Seconds(void)
12 struct timeval input = {500, 0}; // 500.0 s
13 l_fp expected = {500, 0};
16 TVTOTS(&input, &actual);
18 TEST_ASSERT_TRUE(IsEqual(expected, actual));
21 void test_MicrosecondsRounded(void)
23 /* 0.0005 can not be represented exact in a l_fp structure.
24 * It would equal to 2147483,648. This means that
25 * HALF_PROMILLE_UP (which is 2147484) should be
26 * the correct rounding. */
28 struct timeval input = {0, 500}; // 0.0005 exact
29 l_fp expected = {0, HALF_PROMILLE_UP};
32 TVTOTS(&input, &actual);
33 TEST_ASSERT_TRUE(IsEqual(expected, actual));
36 void test_MicrosecondsExact(void)
38 // 0.5 can be represented exact in both l_fp and timeval.
39 const struct timeval input = {10, 500000}; // 0.5 exact
40 const l_fp expected = {10, HALF}; // 0.5 exact
43 TVTOTS(&input, &actual);
45 // Compare the fractional part with an absolute error given.
46 TEST_ASSERT_EQUAL_UINT(expected.l_ui, actual.l_ui);
48 double expectedDouble, actualDouble;
49 M_LFPTOD(0, expected.l_uf, expectedDouble);
50 M_LFPTOD(0, actual.l_uf, actualDouble);
52 // The error should be less than 0.5 us
53 TEST_ASSERT_DOUBLE_WITHIN(0000005, expectedDouble, actualDouble);