3 #include "ntp_stdlib.h" /* test fail without this include, for some reason */
4 #include "ntp_calendar.h"
9 static int leapdays(int year);
11 int isGT(int first, int second);
12 int leapdays(int year);
13 char * CalendarFromCalToString(const struct calendar *cal);
14 char * CalendarFromIsoToString(const struct isodate *iso);
15 int IsEqualCal(const struct calendar *expected, const struct calendar *actual);
16 int IsEqualIso(const struct isodate *expected, const struct isodate *actual);
17 char * DateFromCalToString(const struct calendar *cal);
18 char * DateFromIsoToString(const struct isodate *iso);
19 int IsEqualDateCal(const struct calendar *expected, const struct calendar *actual);
20 int IsEqualDateIso(const struct isodate *expected, const struct isodate *actual);
21 void test_DaySplitMerge(void);
22 void test_SplitYearDays1(void);
23 void test_SplitYearDays2(void);
24 void test_RataDie1(void);
25 void test_LeapYears1(void);
26 void test_LeapYears2(void);
27 void test_RoundTripDate(void);
28 void test_RoundTripYearStart(void);
29 void test_RoundTripMonthStart(void);
30 void test_RoundTripWeekStart(void);
31 void test_RoundTripDayStart(void);
32 void test_IsoCalYearsToWeeks(void);
33 void test_IsoCalWeeksToYearStart(void);
34 void test_IsoCalWeeksToYearEnd(void);
35 void test_DaySecToDate(void);
38 * ---------------------------------------------------------------------
40 * ---------------------------------------------------------------------
43 isGT(int first, int second)
65 CalendarFromCalToString(
66 const struct calendar *cal)
68 char * str = malloc(sizeof (char) * 100);
69 snprintf(str, 100, "%u-%02u-%02u (%u) %02u:%02u:%02u",
70 cal->year, (u_int)cal->month, (u_int)cal->monthday,
72 (u_int)cal->hour, (u_int)cal->minute, (u_int)cal->second);
73 str[99] = '\0'; /* paranoia rulez! */
78 CalendarFromIsoToString(
79 const struct isodate *iso)
81 char * str = emalloc (sizeof (char) * 100);
82 snprintf(str, 100, "%u-W%02u-%02u %02u:%02u:%02u",
83 iso->year, (u_int)iso->week, (u_int)iso->weekday,
84 (u_int)iso->hour, (u_int)iso->minute, (u_int)iso->second);
85 str[99] = '\0'; /* paranoia rulez! */
91 const struct calendar *expected,
92 const struct calendar *actual)
94 if (expected->year == actual->year &&
95 (!expected->yearday || expected->yearday == actual->yearday) &&
96 expected->month == actual->month &&
97 expected->monthday == actual->monthday &&
98 expected->hour == actual->hour &&
99 expected->minute == actual->minute &&
100 expected->second == actual->second) {
103 printf("expected: %s but was %s",
104 CalendarFromCalToString(expected),
105 CalendarFromCalToString(actual));
112 const struct isodate *expected,
113 const struct isodate *actual)
115 if (expected->year == actual->year &&
116 expected->week == actual->week &&
117 expected->weekday == actual->weekday &&
118 expected->hour == actual->hour &&
119 expected->minute == actual->minute &&
120 expected->second == actual->second) {
123 printf("expected: %s but was %s",
124 CalendarFromIsoToString(expected),
125 CalendarFromIsoToString(actual));
132 const struct calendar *cal)
135 char * str = emalloc (sizeof (char) * 100);
136 snprintf(str, 100, "%u-%02u-%02u (%u)",
137 cal->year, (u_int)cal->month, (u_int)cal->monthday,
139 str[99] = '\0'; /* paranoia rulez! */
145 const struct isodate *iso)
148 char * str = emalloc (sizeof (char) * 100);
149 snprintf(str, 100, "%u-W%02u-%02u",
150 iso->year, (u_int)iso->week, (u_int)iso->weekday);
151 str[99] = '\0'; /* paranoia rulez! */
157 const struct calendar *expected,
158 const struct calendar *actual)
160 if (expected->year == actual->year &&
161 (!expected->yearday || expected->yearday == actual->yearday) &&
162 expected->month == actual->month &&
163 expected->monthday == actual->monthday) {
166 printf("expected: %s but was %s",
167 DateFromCalToString(expected),
168 DateFromCalToString(actual));
175 const struct isodate *expected,
176 const struct isodate *actual)
178 if (expected->year == actual->year &&
179 expected->week == actual->week &&
180 expected->weekday == actual->weekday) {
183 printf("expected: %s but was %s",
184 DateFromIsoToString(expected),
185 DateFromIsoToString(actual));
192 * ---------------------------------------------------------------------
194 * ---------------------------------------------------------------------
197 /* days before month, with a full-year pad at the upper end */
198 static const u_short real_month_table[2][13] = {
199 /* -*- table for regular years -*- */
200 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
201 /* -*- table for leap years -*- */
202 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
205 /* days in month, with one month wrap-around at both ends */
206 static const u_short real_month_days[2][14] = {
207 /* -*- table for regular years -*- */
208 { 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 },
209 /* -*- table for leap years -*- */
210 { 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 }
213 /* test the day/sec join & split ops, making sure that 32bit
214 * intermediate results would definitely overflow and the hi DWORD of
215 * the 'vint64' is definitely needed.
218 test_DaySplitMerge(void) {
220 for (day = -1000000; day <= 1000000; day += 100) {
221 for (sec = -100000; sec <= 186400; sec += 10000) {
227 merge = ntpcal_dayjoin(day, sec);
228 split = ntpcal_daysplit(&merge);
232 while (esec >= 86400) {
241 TEST_ASSERT_EQUAL(eday, split.hi);
242 TEST_ASSERT_EQUAL(esec, split.lo);
248 test_SplitYearDays1(void) {
250 for (eyd = -1; eyd <= 365; eyd++) {
251 ntpcal_split split = ntpcal_split_yeardays(eyd, 0);
252 if (split.lo >= 0 && split.hi >= 0) {
253 TEST_ASSERT_TRUE(isGT(12,split.hi));
254 TEST_ASSERT_TRUE(isGT(real_month_days[0][split.hi+1], split.lo));
255 int32 tyd = real_month_table[0][split.hi] + split.lo;
256 TEST_ASSERT_EQUAL(eyd, tyd);
258 TEST_ASSERT_TRUE(eyd < 0 || eyd > 364);
263 test_SplitYearDays2(void) {
265 for (eyd = -1; eyd <= 366; eyd++) {
266 ntpcal_split split = ntpcal_split_yeardays(eyd, 1);
267 if (split.lo >= 0 && split.hi >= 0) {
268 /* basic checks do not work on compunds :( */
269 /* would like: TEST_ASSERT_TRUE(12 > split.hi); */
270 TEST_ASSERT_TRUE(isGT(12,split.hi));
271 TEST_ASSERT_TRUE(isGT(real_month_days[1][split.hi+1], split.lo));
272 int32 tyd = real_month_table[1][split.hi] + split.lo;
273 TEST_ASSERT_EQUAL(eyd, tyd);
275 TEST_ASSERT_TRUE(eyd < 0 || eyd > 365);
280 test_RataDie1(void) {
281 int32 testDate = 1; /* 0001-01-01 (proleptic date) */
282 struct calendar expected = { 1, 1, 1, 1 };
283 struct calendar actual;
285 ntpcal_rd_to_date(&actual, testDate);
286 TEST_ASSERT_TRUE(IsEqualDateCal(&expected, &actual));
289 /* check last day of february for first 10000 years */
291 test_LeapYears1(void) {
292 struct calendar dateIn, dateOut;
294 for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) {
296 dateIn.monthday = 28 + leapdays(dateIn.year);
297 dateIn.yearday = 31 + dateIn.monthday;
299 ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn));
301 TEST_ASSERT_TRUE(IsEqualDateCal(&dateIn, &dateOut));
305 /* check first day of march for first 10000 years */
307 test_LeapYears2(void) {
308 struct calendar dateIn, dateOut;
310 for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) {
313 dateIn.yearday = 60 + leapdays(dateIn.year);
315 ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn));
316 TEST_ASSERT_TRUE(IsEqualDateCal(&dateIn, &dateOut));
320 /* Full roundtrip from 1601-01-01 to 2400-12-31
321 * checks sequence of rata die numbers and validates date output
322 * (since the input is all nominal days of the calendar in that range
323 * and the result of the inverse calculation must match the input no
324 * invalid output can occur.)
327 test_RoundTripDate(void) {
328 struct calendar truDate, expDate = { 1600, 0, 12, 31 };;
330 int32 truRdn, expRdn = ntpcal_date_to_rd(&expDate);
332 while (expDate.year < 2400) {
336 leaps = leapdays(expDate.year);
337 while (expDate.month < 12) {
339 expDate.monthday = 0;
340 while (expDate.monthday < real_month_days[leaps][expDate.month]) {
345 truRdn = ntpcal_date_to_rd(&expDate);
346 TEST_ASSERT_EQUAL(expRdn, truRdn);
348 ntpcal_rd_to_date(&truDate, truRdn);
349 TEST_ASSERT_TRUE(IsEqualDateCal(&expDate, &truDate));
355 /* Roundtrip testing on calyearstart */
357 test_RoundTripYearStart(void) {
358 static const time_t pivot = 0;
359 u_int32 ntp, expys, truys;
360 struct calendar date;
362 for (ntp = 0; ntp < 0xFFFFFFFFu - 30000000u; ntp += 30000000u) {
363 truys = calyearstart(ntp, &pivot);
364 ntpcal_ntp_to_date(&date, ntp, &pivot);
365 date.month = date.monthday = 1;
366 date.hour = date.minute = date.second = 0;
367 expys = ntpcal_date_to_ntp(&date);
368 TEST_ASSERT_EQUAL(expys, truys);
372 /* Roundtrip testing on calmonthstart */
374 test_RoundTripMonthStart(void) {
375 static const time_t pivot = 0;
376 u_int32 ntp, expms, trums;
377 struct calendar date;
379 for (ntp = 0; ntp < 0xFFFFFFFFu - 2000000u; ntp += 2000000u) {
380 trums = calmonthstart(ntp, &pivot);
381 ntpcal_ntp_to_date(&date, ntp, &pivot);
383 date.hour = date.minute = date.second = 0;
384 expms = ntpcal_date_to_ntp(&date);
385 TEST_ASSERT_EQUAL(expms, trums);
389 /* Roundtrip testing on calweekstart */
391 test_RoundTripWeekStart(void) {
392 static const time_t pivot = 0;
393 u_int32 ntp, expws, truws;
396 for (ntp = 0; ntp < 0xFFFFFFFFu - 600000u; ntp += 600000u) {
397 truws = calweekstart(ntp, &pivot);
398 isocal_ntp_to_date(&date, ntp, &pivot);
399 date.hour = date.minute = date.second = 0;
401 expws = isocal_date_to_ntp(&date);
402 TEST_ASSERT_EQUAL(expws, truws);
406 /* Roundtrip testing on caldaystart */
408 test_RoundTripDayStart(void) {
409 static const time_t pivot = 0;
410 u_int32 ntp, expds, truds;
411 struct calendar date;
413 for (ntp = 0; ntp < 0xFFFFFFFFu - 80000u; ntp += 80000u) {
414 truds = caldaystart(ntp, &pivot);
415 ntpcal_ntp_to_date(&date, ntp, &pivot);
416 date.hour = date.minute = date.second = 0;
417 expds = ntpcal_date_to_ntp(&date);
418 TEST_ASSERT_EQUAL(expds, truds);
422 /* ---------------------------------------------------------------------
423 * ISO8601 week calendar internals
425 * The ISO8601 week calendar implementation is simple in the terms of
426 * the math involved, but the implementation of the calculations must
427 * take care of a few things like overflow, floor division, and sign
430 * Most of the functions are straight forward, but converting from years
431 * to weeks and from weeks to years warrants some extra tests. These use
432 * an independent reference implementation of the conversion from years
434 * ---------------------------------------------------------------------
437 /* helper / reference implementation for the first week of year in the
438 * ISO8601 week calendar. This is based on the reference definition of
439 * the ISO week calendar start: The Monday closest to January,1st of the
440 * corresponding year in the Gregorian calendar.
443 refimpl_WeeksInIsoYears(
447 days = ntpcal_weekday_close(
448 ntpcal_days_in_years(years) + 1,
450 /* the weekday functions operate on RDN, while we want elapsed
451 * units here -- we have to add / sub 1 in the midlle / at the
452 * end of the operation that gets us the first day of the ISO
457 TEST_ASSERT_EQUAL(0, days); /* paranoia check... */
461 /* The next tests loop over 5000yrs, but should still be very fast. If
462 * they are not, the calendar needs a better implementation...
465 test_IsoCalYearsToWeeks(void) {
468 for (years = -1000; years < 4000; ++years) {
469 /* get number of weeks before years (reference) */
470 wref = refimpl_WeeksInIsoYears(years);
471 /* get number of weeks before years (object-under-test) */
472 wcal = isocal_weeks_in_years(years);
473 TEST_ASSERT_EQUAL(wref, wcal);
478 test_IsoCalWeeksToYearStart(void) {
482 for (years = -1000; years < 4000; ++years) {
483 /* get number of weeks before years (reference) */
484 wref = refimpl_WeeksInIsoYears(years);
486 ysplit = isocal_split_eraweeks(wref);
487 /* check invariants: same year, week 0 */
488 TEST_ASSERT_EQUAL(years, ysplit.hi);
489 TEST_ASSERT_EQUAL(0, ysplit.lo);
494 test_IsoCalWeeksToYearEnd(void) {
498 for (years = -1000; years < 4000; ++years) {
499 /* get last week of previous year */
500 wref = refimpl_WeeksInIsoYears(years) - 1;
502 ysplit = isocal_split_eraweeks(wref);
503 /* check invariants: previous year, week 51 or 52 */
504 TEST_ASSERT_EQUAL(years-1, ysplit.hi);
505 TEST_ASSERT(ysplit.lo == 51 || ysplit.lo == 52);
510 test_DaySecToDate(void) {
514 days = ntpcal_daysec_to_date(&cal, -86400);
515 TEST_ASSERT_MESSAGE((days==-1 && cal.hour==0 && cal.minute==0 && cal.second==0),
516 "failed for -86400");
518 days = ntpcal_daysec_to_date(&cal, -86399);
519 TEST_ASSERT_MESSAGE((days==-1 && cal.hour==0 && cal.minute==0 && cal.second==1),
520 "failed for -86399");
522 days = ntpcal_daysec_to_date(&cal, -1);
523 TEST_ASSERT_MESSAGE((days==-1 && cal.hour==23 && cal.minute==59 && cal.second==59),
526 days = ntpcal_daysec_to_date(&cal, 0);
527 TEST_ASSERT_MESSAGE((days==0 && cal.hour==0 && cal.minute==0 && cal.second==0),
530 days = ntpcal_daysec_to_date(&cal, 1);
531 TEST_ASSERT_MESSAGE((days==0 && cal.hour==0 && cal.minute==0 && cal.second==1),
534 days = ntpcal_daysec_to_date(&cal, 86399);
535 TEST_ASSERT_MESSAGE((days==0 && cal.hour==23 && cal.minute==59 && cal.second==59),
538 days = ntpcal_daysec_to_date(&cal, 86400);
539 TEST_ASSERT_MESSAGE((days==1 && cal.hour==0 && cal.minute==0 && cal.second==0),