]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/g_caljulian.cpp
Fix a regression with SA-15:24 patch that prevented NIS from
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / g_caljulian.cpp
1 #include "g_libntptest.h"
2
3 extern "C" {
4 #include "ntp_calendar.h"
5 }
6
7 #include <string>
8 #include <sstream>
9
10 class caljulianTest : public libntptest {
11 protected:
12         virtual void SetUp();
13         virtual void TearDown();
14
15         std::string CalendarToString(const calendar &cal) {
16                 std::ostringstream ss;
17                 ss << cal.year << "-" << (u_int)cal.month << "-" << (u_int)cal.monthday
18                    << " (" << cal.yearday << ") " << (u_int)cal.hour << ":"
19                    << (u_int)cal.minute << ":" << (u_int)cal.second;
20                 return ss.str();
21         }
22
23         ::testing::AssertionResult IsEqual(const calendar &expected, const calendar &actual) {
24                 if (expected.year == actual.year &&
25                         (expected.yearday == actual.yearday ||
26                          (expected.month == actual.month &&
27                           expected.monthday == actual.monthday)) &&
28                         expected.hour == actual.hour &&
29                         expected.minute == actual.minute &&
30                         expected.second == actual.second) {
31                         return ::testing::AssertionSuccess();
32                 } else {
33                         return ::testing::AssertionFailure()
34                                 << "expected: " << CalendarToString(expected) << " but was "
35                                 << CalendarToString(actual);
36                 }
37         }
38 };
39
40 void caljulianTest::SetUp()
41 {
42     ntpcal_set_timefunc(timefunc);
43     settime(1970, 1, 1, 0, 0, 0);
44 }
45
46 void caljulianTest::TearDown()
47 {
48     ntpcal_set_timefunc(NULL);
49 }
50
51
52 TEST_F(caljulianTest, RegularTime) {
53         u_long testDate = 3485080800UL; // 2010-06-09 14:00:00
54         calendar expected = {2010,160,6,9,14,0,0};
55
56         calendar actual;
57
58         caljulian(testDate, &actual);
59
60         EXPECT_TRUE(IsEqual(expected, actual));
61 }
62
63 TEST_F(caljulianTest, LeapYear) {
64         u_long input = 3549902400UL; // 2012-06-28 20:00:00Z
65         calendar expected = {2012, 179, 6, 28, 20, 0, 0};
66
67         calendar actual;
68
69         caljulian(input, &actual);
70
71         EXPECT_TRUE(IsEqual(expected, actual));
72 }
73
74 TEST_F(caljulianTest, uLongBoundary) {
75         u_long time = 4294967295UL; // 2036-02-07 6:28:15
76         calendar expected = {2036,0,2,7,6,28,15};
77
78         calendar actual;
79
80         caljulian(time, &actual);
81
82         EXPECT_TRUE(IsEqual(expected, actual));
83 }
84
85 TEST_F(caljulianTest, uLongWrapped) {
86         u_long time = 0;
87         calendar expected = {2036,0,2,7,6,28,16};
88
89         calendar actual;
90
91         caljulian(time, &actual);
92
93         EXPECT_TRUE(IsEqual(expected, actual));
94 }