1 #include "config.h"
2
3 #include "lfptest.h"
4 #include "timevalops.h"
5
6 #include "unity.h"
7
8 #include <math.h> /* Required on Solaris for ldexp. */
9
10 void test_Seconds(void);
11 void test_MicrosecondsRounded(void);
12 void test_MicrosecondsExact(void);
13
14 void
test_Seconds(void)15 test_Seconds(void)
16 {
17 struct timeval input = {500, 0}; /* 500.0 s */
18 l_fp expected = {{500}, 0};
19 l_fp actual;
20
21 TVTOTS(&input, &actual);
22
23 TEST_ASSERT_TRUE(IsEqual(expected, actual));
24 }
25
26
27 void
test_MicrosecondsRounded(void)28 test_MicrosecondsRounded(void)
29 {
30 /* 0.0005 can not be represented exact in a l_fp structure.
31 * It would equal to 2147483,648. This means that
32 * HALF_PROMILLE_UP (which is 2147484) should be
33 * the correct rounding. */
34
35 struct timeval input = {0, 500}; /* 0.0005 exact */
36 l_fp expected = {{0}, HALF_PROMILLE_UP};
37 l_fp actual;
38
39 TVTOTS(&input, &actual);
40
41 TEST_ASSERT_TRUE(IsEqual(expected, actual));
42 }
43
44
45 void
test_MicrosecondsExact(void)46 test_MicrosecondsExact(void)
47 {
48 /* 0.5 can be represented exact in both l_fp and timeval. */
49 const struct timeval input = {10, 500000}; /* 0.5 exact */
50 const l_fp expected = {{10}, HALF}; /* 0.5 exact */
51 l_fp actual;
52
53 TVTOTS(&input, &actual);
54
55 /* Compare the fractional part with an absolute error given. */
56 TEST_ASSERT_EQUAL_UINT(expected.l_ui, actual.l_ui);
57
58 double expectedDouble, actualDouble;
59 M_LFPTOD(0, expected.l_uf, expectedDouble);
60 M_LFPTOD(0, actual.l_uf, actualDouble);
61
62 /* The error should be less than 0.5 us */
63 TEST_ASSERT_DOUBLE_WITHIN(0.0000005, expectedDouble, actualDouble);
64 }
65