1 #include "g_libntptest.h"
4 #include "ntp_calendar.h"
10 class calendarTest : public libntptest {
12 static int leapdays(int year);
14 std::string CalendarToString(const calendar &cal);
15 std::string CalendarToString(const isodate &iso);
16 ::testing::AssertionResult IsEqual(const calendar &expected, const calendar &actual);
17 ::testing::AssertionResult IsEqual(const isodate &expected, const isodate &actual);
19 std::string DateToString(const calendar &cal);
20 std::string DateToString(const isodate &iso);
21 ::testing::AssertionResult IsEqualDate(const calendar &expected, const calendar &actual);
22 ::testing::AssertionResult IsEqualDate(const isodate &expected, const isodate &actual);
26 // ---------------------------------------------------------------------
28 // ---------------------------------------------------------------------
30 calendarTest::leapdays(int year)
42 calendarTest::CalendarToString(const calendar &cal) {
43 std::ostringstream ss;
44 ss << cal.year << "-" << (u_int)cal.month << "-" << (u_int)cal.monthday
45 << " (" << cal.yearday << ") " << (u_int)cal.hour << ":"
46 << (u_int)cal.minute << ":" << (u_int)cal.second;
51 calendarTest:: CalendarToString(const isodate &iso) {
52 std::ostringstream ss;
53 ss << iso.year << "-" << (u_int)iso.week << "-" << (u_int)iso.weekday
54 << (u_int)iso.hour << ":" << (u_int)iso.minute << ":" << (u_int)iso.second;
58 ::testing::AssertionResult
59 calendarTest:: IsEqual(const calendar &expected, const calendar &actual) {
60 if (expected.year == actual.year &&
61 (!expected.yearday || expected.yearday == actual.yearday) &&
62 expected.month == actual.month &&
63 expected.monthday == actual.monthday &&
64 expected.hour == actual.hour &&
65 expected.minute == actual.minute &&
66 expected.second == actual.second) {
67 return ::testing::AssertionSuccess();
69 return ::testing::AssertionFailure()
70 << "expected: " << CalendarToString(expected) << " but was "
71 << CalendarToString(actual);
75 ::testing::AssertionResult
76 calendarTest:: IsEqual(const isodate &expected, const isodate &actual) {
77 if (expected.year == actual.year &&
78 expected.week == actual.week &&
79 expected.weekday == actual.weekday &&
80 expected.hour == actual.hour &&
81 expected.minute == actual.minute &&
82 expected.second == actual.second) {
83 return ::testing::AssertionSuccess();
85 return ::testing::AssertionFailure()
86 << "expected: " << CalendarToString(expected) << " but was "
87 << CalendarToString(actual);
92 calendarTest:: DateToString(const calendar &cal) {
93 std::ostringstream ss;
94 ss << cal.year << "-" << (u_int)cal.month << "-" << (u_int)cal.monthday
95 << " (" << cal.yearday << ")";
100 calendarTest:: DateToString(const isodate &iso) {
101 std::ostringstream ss;
102 ss << iso.year << "-" << (u_int)iso.week << "-" << (u_int)iso.weekday;
106 ::testing::AssertionResult
107 calendarTest:: IsEqualDate(const calendar &expected, const calendar &actual) {
108 if (expected.year == actual.year &&
109 (!expected.yearday || expected.yearday == actual.yearday) &&
110 expected.month == actual.month &&
111 expected.monthday == actual.monthday) {
112 return ::testing::AssertionSuccess();
114 return ::testing::AssertionFailure()
115 << "expected: " << DateToString(expected) << " but was "
116 << DateToString(actual);
120 ::testing::AssertionResult
121 calendarTest:: IsEqualDate(const isodate &expected, const isodate &actual) {
122 if (expected.year == actual.year &&
123 expected.week == actual.week &&
124 expected.weekday == actual.weekday) {
125 return ::testing::AssertionSuccess();
127 return ::testing::AssertionFailure()
128 << "expected: " << DateToString(expected) << " but was "
129 << DateToString(actual);
134 // ---------------------------------------------------------------------
136 // ---------------------------------------------------------------------
137 static const u_short real_month_table[2][13] = {
138 /* -*- table for regular years -*- */
139 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
140 /* -*- table for leap years -*- */
141 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
144 // days in month, with one month wrap-around at both ends
145 static const u_short real_month_days[2][14] = {
146 /* -*- table for regular years -*- */
147 { 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 },
148 /* -*- table for leap years -*- */
149 { 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 }
152 // test the day/sec join & split ops, making sure that 32bit
153 // intermediate results would definitely overflow and the hi DWORD of
154 // the 'vint64' is definitely needed.
155 TEST_F(calendarTest, DaySplitMerge) {
156 for (int32 day = -1000000; day <= 1000000; day += 100) {
157 for (int32 sec = -100000; sec <= 186400; sec += 10000) {
158 vint64 merge = ntpcal_dayjoin(day, sec);
159 ntpcal_split split = ntpcal_daysplit(&merge);
163 while (esec >= 86400) {
172 EXPECT_EQ(eday, split.hi);
173 EXPECT_EQ(esec, split.lo);
178 TEST_F(calendarTest, SplitYearDays1) {
179 for (int32 eyd = -1; eyd <= 365; eyd++) {
180 ntpcal_split split = ntpcal_split_yeardays(eyd, 0);
181 if (split.lo >= 0 && split.hi >= 0) {
182 EXPECT_GT(12, split.hi);
183 EXPECT_GT(real_month_days[0][split.hi+1], split.lo);
184 int32 tyd = real_month_table[0][split.hi] + split.lo;
187 EXPECT_TRUE(eyd < 0 || eyd > 364);
191 TEST_F(calendarTest, SplitYearDays2) {
192 for (int32 eyd = -1; eyd <= 366; eyd++) {
193 ntpcal_split split = ntpcal_split_yeardays(eyd, 1);
194 if (split.lo >= 0 && split.hi >= 0) {
195 EXPECT_GT(12, split.hi);
196 EXPECT_GT(real_month_days[1][split.hi+1], split.lo);
197 int32 tyd = real_month_table[1][split.hi] + split.lo;
200 EXPECT_TRUE(eyd < 0 || eyd > 365);
204 TEST_F(calendarTest, RataDie1) {
205 int32 testDate = 1; // 0001-01-01 (proleptic date)
206 calendar expected = { 1, 1, 1, 1 };
209 ntpcal_rd_to_date(&actual, testDate);
210 EXPECT_TRUE(IsEqualDate(expected, actual));
213 // check last day of february for first 10000 years
214 TEST_F(calendarTest, LeapYears1) {
215 calendar dateIn, dateOut;
217 for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) {
219 dateIn.monthday = 28 + leapdays(dateIn.year);
220 dateIn.yearday = 31 + dateIn.monthday;
222 ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn));
224 EXPECT_TRUE(IsEqualDate(dateIn, dateOut));
228 // check first day of march for first 10000 years
229 TEST_F(calendarTest, LeapYears2) {
230 calendar dateIn, dateOut;
232 for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) {
235 dateIn.yearday = 60 + leapdays(dateIn.year);
237 ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn));
238 EXPECT_TRUE(IsEqualDate(dateIn, dateOut));
242 // Full roundtrip for 1601-01-01 to 2400-12-31
243 // checks sequence of rata die numbers and validates date output
244 // (since the input is all nominal days of the calendar in that range
245 // and the result of the inverse calculation must match the input no
246 // invalid output can occur.)
247 TEST_F(calendarTest, RoundTripDate) {
248 calendar truDate, expDate = { 1600, 0, 12, 31 };;
249 int32 truRdn, expRdn = ntpcal_date_to_rd(&expDate);
252 while (expDate.year < 2400) {
256 leaps = leapdays(expDate.year);
257 while (expDate.month < 12) {
259 expDate.monthday = 0;
260 while (expDate.monthday < real_month_days[leaps][expDate.month]) {
265 truRdn = ntpcal_date_to_rd(&expDate);
266 EXPECT_EQ(expRdn, truRdn);
268 ntpcal_rd_to_date(&truDate, truRdn);
269 EXPECT_TRUE(IsEqualDate(expDate, truDate));
275 // Roundtrip testing on calyearstart
276 TEST_F(calendarTest, RoundTripYearStart) {
277 static const time_t pivot = 0;
278 u_int32 ntp, expys, truys;
281 for (ntp = 0; ntp < 0xFFFFFFFFu - 30000000u; ntp += 30000000u) {
282 truys = calyearstart(ntp, &pivot);
283 ntpcal_ntp_to_date(&date, ntp, &pivot);
284 date.month = date.monthday = 1;
285 date.hour = date.minute = date.second = 0;
286 expys = ntpcal_date_to_ntp(&date);
287 EXPECT_EQ(expys, truys);
291 // Roundtrip testing on calymonthstart
292 TEST_F(calendarTest, RoundTripMonthStart) {
293 static const time_t pivot = 0;
294 u_int32 ntp, expms, trums;
297 for (ntp = 0; ntp < 0xFFFFFFFFu - 2000000u; ntp += 2000000u) {
298 trums = calmonthstart(ntp, &pivot);
299 ntpcal_ntp_to_date(&date, ntp, &pivot);
301 date.hour = date.minute = date.second = 0;
302 expms = ntpcal_date_to_ntp(&date);
303 EXPECT_EQ(expms, trums);
307 // Roundtrip testing on calweekstart
308 TEST_F(calendarTest, RoundTripWeekStart) {
309 static const time_t pivot = 0;
310 u_int32 ntp, expws, truws;
313 for (ntp = 0; ntp < 0xFFFFFFFFu - 600000u; ntp += 600000u) {
314 truws = calweekstart(ntp, &pivot);
315 isocal_ntp_to_date(&date, ntp, &pivot);
316 date.hour = date.minute = date.second = 0;
318 expws = isocal_date_to_ntp(&date);
319 EXPECT_EQ(expws, truws);
323 // Roundtrip testing on caldaystart
324 TEST_F(calendarTest, RoundTripDayStart) {
325 static const time_t pivot = 0;
326 u_int32 ntp, expds, truds;
329 for (ntp = 0; ntp < 0xFFFFFFFFu - 80000u; ntp += 80000u) {
330 truds = caldaystart(ntp, &pivot);
331 ntpcal_ntp_to_date(&date, ntp, &pivot);
332 date.hour = date.minute = date.second = 0;
333 expds = ntpcal_date_to_ntp(&date);
334 EXPECT_EQ(expds, truds);