]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/g_tvtots.cpp
Fix a regression with SA-15:24 patch that prevented NIS from
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / g_tvtots.cpp
1 #include "g_lfptest.h"
2
3 extern "C" {
4 #include "timevalops.h"
5 };
6
7 // Required on Solaris for ldexp.
8 #include <math.h>
9
10 class tvtotsTest : public lfptest {
11 };
12
13 TEST_F(tvtotsTest, Seconds) {
14         timeval input = {500, 0}; // 500.0 s
15         l_fp expected = {500, 0};
16         l_fp actual;
17
18         TVTOTS(&input, &actual);
19
20         EXPECT_TRUE(IsEqual(expected, actual));
21 }
22
23 TEST_F(tvtotsTest, MicrosecondsRounded) {
24         /* 0.0005 can not be represented exact in a l_fp structure.
25          * It would equal to 2147483,648. This means that
26          * HALF_PROMILLE_UP (which is 2147484) should be
27          * the correct rounding. */
28
29         timeval input = {0, 500}; // 0.0005 exact
30         l_fp expected = {0, HALF_PROMILLE_UP};
31         l_fp actual;
32
33         TVTOTS(&input, &actual);
34         EXPECT_TRUE(IsEqual(expected, actual));
35 }
36
37 TEST_F(tvtotsTest, MicrosecondsExact) {
38         // 0.5 can be represented exact in both l_fp and timeval.
39         const 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         EXPECT_EQ(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         EXPECT_NEAR(expectedDouble, actualDouble, 0.0000005);
54 }