summaryrefslogtreecommitdiff
path: root/tests/libntp/caljulian.cpp
blob: d9cc952933e7e60b23dfe7bf00c9f68b1d05c9ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "libntptest.h"

extern "C" {
#include "ntp_calendar.h"
}

#include <string>
#include <sstream>

class caljulianTest : public libntptest {
protected:
	virtual void SetUp();
	virtual void TearDown();

	std::string CalendarToString(const calendar &cal) {
		std::ostringstream ss;
		ss << cal.year << "-" << (u_int)cal.month << "-" << (u_int)cal.monthday
		   << " (" << cal.yearday << ") " << (u_int)cal.hour << ":"
		   << (u_int)cal.minute << ":" << (u_int)cal.second;
		return ss.str();
	}

	::testing::AssertionResult IsEqual(const calendar &expected, const calendar &actual) {
		if (expected.year == actual.year &&
			(expected.yearday == actual.yearday ||
			 (expected.month == actual.month &&
			  expected.monthday == actual.monthday)) &&
			expected.hour == actual.hour &&
			expected.minute == actual.minute &&
			expected.second == actual.second) {
			return ::testing::AssertionSuccess();
		} else {
			return ::testing::AssertionFailure()
				<< "expected: " << CalendarToString(expected) << " but was "
				<< CalendarToString(actual);
		}
	}
};

void caljulianTest::SetUp()
{
    ntpcal_set_timefunc(timefunc);
    settime(1970, 1, 1, 0, 0, 0);
}

void caljulianTest::TearDown()
{
    ntpcal_set_timefunc(NULL);
}


TEST_F(caljulianTest, RegularTime) {
	u_long testDate = 3485080800UL; // 2010-06-09 14:00:00
	calendar expected = {2010,160,6,9,14,0,0};

	calendar actual;

	caljulian(testDate, &actual);

	EXPECT_TRUE(IsEqual(expected, actual));
}

TEST_F(caljulianTest, LeapYear) {
	u_long input = 3549902400UL; // 2012-06-28 20:00:00Z
	calendar expected = {2012, 179, 6, 28, 20, 0, 0};

	calendar actual;

	caljulian(input, &actual);

	EXPECT_TRUE(IsEqual(expected, actual));
}

TEST_F(caljulianTest, uLongBoundary) {
	u_long time = 4294967295UL; // 2036-02-07 6:28:15
	calendar expected = {2036,0,2,7,6,28,15};

	calendar actual;

	caljulian(time, &actual);

	EXPECT_TRUE(IsEqual(expected, actual));
}

TEST_F(caljulianTest, uLongWrapped) {
	u_long time = 0;
	calendar expected = {2036,0,2,7,6,28,16};

	calendar actual;

	caljulian(time, &actual);

	EXPECT_TRUE(IsEqual(expected, actual));
}