]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/tvtots.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / tvtots.c
1 #include "config.h"
2
3 #include "lfptest.h"
4 #include "timevalops.h"
5
6 #include "unity.h"
7 #include <math.h>// Required on Solaris for ldexp.
8
9
10 void test_Seconds(void)
11 {
12         struct timeval input = {500, 0}; // 500.0 s
13         l_fp expected = {500, 0};
14         l_fp actual;
15
16         TVTOTS(&input, &actual);
17
18         TEST_ASSERT_TRUE(IsEqual(expected, actual));
19 }
20
21 void test_MicrosecondsRounded(void)
22 {
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. */
27
28         struct timeval input = {0, 500}; // 0.0005 exact
29         l_fp expected = {0, HALF_PROMILLE_UP};
30         l_fp actual;
31
32         TVTOTS(&input, &actual);
33         TEST_ASSERT_TRUE(IsEqual(expected, actual));
34 }
35
36 void test_MicrosecondsExact(void)
37 {
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
41         l_fp actual;
42
43         TVTOTS(&input, &actual);
44
45         // Compare the fractional part with an absolute error given.
46         TEST_ASSERT_EQUAL_UINT(expected.l_ui, actual.l_ui);
47
48         double expectedDouble, actualDouble;
49         M_LFPTOD(0, expected.l_uf, expectedDouble);
50         M_LFPTOD(0, actual.l_uf, actualDouble);
51
52         // The error should be less than 0.5 us
53         TEST_ASSERT_DOUBLE_WITHIN(0000005, expectedDouble, actualDouble);
54 }