]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/g_buftvtots.cpp
Fix a regression with SA-15:24 patch that prevented NIS from
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / g_buftvtots.cpp
1 #include "g_lfptest.h"
2
3 extern "C" {
4 #include "ntp_unixtime.h"
5 };
6
7 // Required for Solaris.
8 #include <math.h>
9
10 class buftvtotsTest : public lfptest {
11 };
12
13 #ifndef SYS_WINNT
14 TEST_F(buftvtotsTest, ZeroBuffer) {
15         const timeval input = {0, 0};
16         const l_fp expected = {0 + JAN_1970, 0};
17
18         l_fp actual;
19
20         ASSERT_TRUE(buftvtots((const char*)(&input), &actual));
21         EXPECT_TRUE(IsEqual(expected, actual));
22 }
23
24 TEST_F(buftvtotsTest, IntegerAndFractionalBuffer) {
25         const timeval input = {5, 500000}; // 5.5
26         const l_fp expected = {5 + JAN_1970, HALF};
27
28         l_fp actual;
29
30         ASSERT_TRUE(buftvtots((const char*)(&input), &actual));
31
32         // Compare the fractional part with an absolute error given.
33         EXPECT_EQ(expected.l_ui, actual.l_ui);
34
35         double expectedDouble, actualDouble;
36         M_LFPTOD(0, expected.l_uf, expectedDouble);
37         M_LFPTOD(0, actual.l_uf, actualDouble);
38
39         // The error should be less than 0.5 us
40         EXPECT_NEAR(expectedDouble, actualDouble, 0.0000005);
41 }
42
43 TEST_F(buftvtotsTest, IllegalMicroseconds) {
44         const timeval input = {0, 1100000}; // > 999 999 microseconds.
45         
46         l_fp actual;
47
48         ASSERT_FALSE(buftvtots((const char*)(&input), &actual));
49 }
50
51 #else
52 TEST_F(buftvtotsTest, AlwaysFalseOnWindows) {
53         /*
54          * Under Windows, buftvtots will just return
55          * 0 (false).
56          */
57         l_fp actual;
58         ASSERT_FALSE(buftvtots("", &actual));
59 }
60
61 #endif