]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/g_tstotv.cpp
Fix a regression with SA-15:24 patch that prevented NIS from
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / g_tstotv.cpp
1 #include "g_libntptest.h"
2
3 extern "C" {
4 #include "ntp_fp.h"
5 #include "timevalops.h"
6 };
7
8 class tstotvTest : public libntptest {
9 protected:
10         ::testing::AssertionResult IsEqual(const timeval& expected,
11                                                                            const timeval& actual) {
12                 if (expected.tv_sec == actual.tv_sec &&
13                         expected.tv_usec == actual.tv_usec) {
14                         // Success
15                         return ::testing::AssertionSuccess();
16                 } else {
17                         return ::testing::AssertionFailure()
18                                 << "expected: " << expected.tv_sec << "."
19                                 << expected.tv_usec
20                                 << " but was: " << actual.tv_sec << "."
21                                 << actual.tv_usec;
22                 }
23         }
24
25         static const u_long HALF = 2147483648UL;
26 };
27
28 TEST_F(tstotvTest, Seconds) {
29         const l_fp input = {50, 0}; // 50.0 s
30         const timeval expected = {50, 0};
31         timeval actual;
32
33         TSTOTV(&input, &actual);
34
35         EXPECT_TRUE(IsEqual(expected, actual));
36 }
37
38 TEST_F(tstotvTest, MicrosecondsExact) {
39         const l_fp input = {50, HALF}; // 10.5 s
40         const timeval expected = {50, 500000};
41         timeval actual;
42
43         TSTOTV(&input, &actual);
44
45         EXPECT_TRUE(IsEqual(expected, actual));
46
47 }
48
49 TEST_F(tstotvTest, MicrosecondsRounding) {
50         const l_fp input = {50, 3865471UL}; // Should round to 50.0009
51         const timeval expected = {50, 900};
52         timeval actual;
53
54         TSTOTV(&input, &actual);
55
56         EXPECT_TRUE(IsEqual(expected, actual));
57 }