]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/g_calendar.cpp
Fix a regression with SA-15:24 patch that prevented NIS from
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / g_calendar.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 calendarTest : public libntptest {
11 protected:
12         static int leapdays(int year);
13
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);
18
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);
23 };
24
25
26 // ---------------------------------------------------------------------
27 // test support stuff
28 // ---------------------------------------------------------------------
29 int
30 calendarTest::leapdays(int year)
31 {
32         if (year % 400 == 0)
33                 return 1;
34         if (year % 100 == 0)
35                 return 0;
36         if (year % 4 == 0)
37                 return 1;
38         return 0;
39 }
40
41 std::string 
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;
47         return ss.str();
48 }
49
50 std::string
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;
55         return ss.str();
56 }
57
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();
68         } else {
69                 return ::testing::AssertionFailure()
70                     << "expected: " << CalendarToString(expected) << " but was "
71                     << CalendarToString(actual);
72         }
73 }
74
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();
84         } else {
85                 return ::testing::AssertionFailure()
86                     << "expected: " << CalendarToString(expected) << " but was "
87                     << CalendarToString(actual);
88         }
89 }
90
91 std::string
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 << ")";
96         return ss.str();
97 }
98
99 std::string
100 calendarTest:: DateToString(const isodate &iso) {
101         std::ostringstream ss;
102         ss << iso.year << "-" << (u_int)iso.week << "-" << (u_int)iso.weekday;
103         return ss.str();
104 }
105
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();
113         } else {
114                 return ::testing::AssertionFailure()
115                     << "expected: " << DateToString(expected) << " but was "
116                     << DateToString(actual);
117         }
118 }
119
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();
126         } else {
127                 return ::testing::AssertionFailure()
128                     << "expected: " << DateToString(expected) << " but was "
129                     << DateToString(actual);
130         }
131 }
132
133
134 // ---------------------------------------------------------------------
135 // test cases
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 }
142 };
143
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 }
150 };
151
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);
160                         int32        eday  = day;
161                         int32        esec  = sec;
162
163                         while (esec >= 86400) {
164                                 eday += 1;
165                                 esec -= 86400;
166                         }
167                         while (esec < 0) {
168                                 eday -= 1;
169                                 esec += 86400;
170                         }
171
172                         EXPECT_EQ(eday, split.hi);
173                         EXPECT_EQ(esec, split.lo);
174                 }
175         }
176 }
177
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;
185                         EXPECT_EQ(eyd, tyd);
186                 } else
187                         EXPECT_TRUE(eyd < 0 || eyd > 364);
188         }
189 }
190                 
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;
198                         EXPECT_EQ(eyd, tyd);
199                 } else
200                         EXPECT_TRUE(eyd < 0 || eyd > 365);
201                 }
202 }
203                 
204 TEST_F(calendarTest, RataDie1) {
205         int32    testDate = 1; // 0001-01-01 (proleptic date)
206         calendar expected = { 1, 1, 1, 1 };
207         calendar actual;
208
209         ntpcal_rd_to_date(&actual, testDate);
210         EXPECT_TRUE(IsEqualDate(expected, actual));
211 }
212
213 // check last day of february for first 10000 years
214 TEST_F(calendarTest, LeapYears1) {
215         calendar dateIn, dateOut;
216
217         for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) {
218                 dateIn.month    = 2;
219                 dateIn.monthday = 28 + leapdays(dateIn.year);
220                 dateIn.yearday  = 31 + dateIn.monthday;
221
222                 ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn));
223
224                 EXPECT_TRUE(IsEqualDate(dateIn, dateOut));
225         }
226 }
227
228 // check first day of march for first 10000 years
229 TEST_F(calendarTest, LeapYears2) {
230         calendar dateIn, dateOut;
231
232         for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) {
233                 dateIn.month    = 3;
234                 dateIn.monthday = 1;
235                 dateIn.yearday  = 60 + leapdays(dateIn.year);
236
237                 ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn));
238                 EXPECT_TRUE(IsEqualDate(dateIn, dateOut));
239         }
240 }
241
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);
250         int      leaps;
251
252         while (expDate.year < 2400) {
253                 expDate.year++;
254                 expDate.month   = 0;
255                 expDate.yearday = 0;
256                 leaps = leapdays(expDate.year);
257                 while (expDate.month < 12) {
258                         expDate.month++;                        
259                         expDate.monthday = 0;
260                         while (expDate.monthday < real_month_days[leaps][expDate.month]) {
261                                 expDate.monthday++;
262                                 expDate.yearday++;
263                                 expRdn++;
264
265                                 truRdn = ntpcal_date_to_rd(&expDate);
266                                 EXPECT_EQ(expRdn, truRdn);
267
268                                 ntpcal_rd_to_date(&truDate, truRdn);
269                                 EXPECT_TRUE(IsEqualDate(expDate, truDate));
270                         }
271                 }
272         }
273 }
274
275 // Roundtrip testing on calyearstart
276 TEST_F(calendarTest, RoundTripYearStart) {
277         static const time_t pivot = 0;
278         u_int32 ntp, expys, truys;
279         calendar date;
280
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);
288         }
289 }       
290
291 // Roundtrip testing on calymonthstart
292 TEST_F(calendarTest, RoundTripMonthStart) {
293         static const time_t pivot = 0;
294         u_int32 ntp, expms, trums;
295         calendar date;
296
297         for (ntp = 0; ntp < 0xFFFFFFFFu - 2000000u; ntp += 2000000u) {
298                 trums = calmonthstart(ntp, &pivot);
299                 ntpcal_ntp_to_date(&date, ntp, &pivot);
300                 date.monthday = 1;
301                 date.hour = date.minute = date.second = 0;
302                 expms = ntpcal_date_to_ntp(&date);
303                 EXPECT_EQ(expms, trums);
304         }
305 }       
306
307 // Roundtrip testing on calweekstart
308 TEST_F(calendarTest, RoundTripWeekStart) {
309         static const time_t pivot = 0;
310         u_int32 ntp, expws, truws;
311         isodate date;
312
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;
317                 date.weekday = 1;
318                 expws = isocal_date_to_ntp(&date);
319                 EXPECT_EQ(expws, truws);
320         }
321 }       
322
323 // Roundtrip testing on caldaystart
324 TEST_F(calendarTest, RoundTripDayStart) {
325         static const time_t pivot = 0;
326         u_int32 ntp, expds, truds;
327         calendar date;
328
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);
335         }
336 }       
337