3 #include "ntp_stdlib.h" /* test fail without this include, for some reason */
4 #include "ntp_calendar.h"
9 static int leapdays(int year);
12 int isGT(int first, int second);
13 int leapdays(int year);
14 char * CalendarFromCalToString(const struct calendar *cal);
15 char * CalendarFromIsoToString(const struct isodate *iso);
16 int IsEqualCal(const struct calendar *expected, const struct calendar *actual);
17 int IsEqualIso(const struct isodate *expected, const struct isodate *actual);
18 char * DateFromCalToString(const struct calendar *cal);
19 char * DateFromIsoToString(const struct isodate *iso);
20 int IsEqualDateCal(const struct calendar *expected, const struct calendar *actual);
21 int IsEqualDateIso(const struct isodate *expected, const struct isodate *actual);
22 void test_DaySplitMerge(void);
23 void test_SplitYearDays1(void);
24 void test_SplitYearDays2(void);
25 void test_RataDie1(void);
26 void test_LeapYears1(void);
27 void test_LeapYears2(void);
28 void test_RoundTripDate(void);
29 void test_RoundTripYearStart(void);
30 void test_RoundTripMonthStart(void);
31 void test_RoundTripWeekStart(void);
32 void test_RoundTripDayStart(void);
33 void test_IsoCalYearsToWeeks(void);
34 void test_IsoCalWeeksToYearStart(void);
35 void test_IsoCalWeeksToYearEnd(void);
36 void test_DaySecToDate(void);
49 * ---------------------------------------------------------------------
51 * ---------------------------------------------------------------------
54 isGT(int first, int second)
76 CalendarFromCalToString(
77 const struct calendar *cal)
79 char * str = malloc(sizeof (char) * 100);
80 snprintf(str, 100, "%u-%02u-%02u (%u) %02u:%02u:%02u",
81 cal->year, (u_int)cal->month, (u_int)cal->monthday,
83 (u_int)cal->hour, (u_int)cal->minute, (u_int)cal->second);
84 str[99] = '\0'; /* paranoia rulez! */
89 CalendarFromIsoToString(
90 const struct isodate *iso)
92 char * str = emalloc (sizeof (char) * 100);
93 snprintf(str, 100, "%u-W%02u-%02u %02u:%02u:%02u",
94 iso->year, (u_int)iso->week, (u_int)iso->weekday,
95 (u_int)iso->hour, (u_int)iso->minute, (u_int)iso->second);
96 str[99] = '\0'; /* paranoia rulez! */
102 const struct calendar *expected,
103 const struct calendar *actual)
105 if (expected->year == actual->year &&
106 (!expected->yearday || expected->yearday == actual->yearday) &&
107 expected->month == actual->month &&
108 expected->monthday == actual->monthday &&
109 expected->hour == actual->hour &&
110 expected->minute == actual->minute &&
111 expected->second == actual->second) {
114 char *p_exp = CalendarFromCalToString(expected);
115 char *p_act = CalendarFromCalToString(actual);
117 printf("expected: %s but was %s", p_exp, p_act);
128 const struct isodate *expected,
129 const struct isodate *actual)
131 if (expected->year == actual->year &&
132 expected->week == actual->week &&
133 expected->weekday == actual->weekday &&
134 expected->hour == actual->hour &&
135 expected->minute == actual->minute &&
136 expected->second == actual->second) {
139 printf("expected: %s but was %s",
140 CalendarFromIsoToString(expected),
141 CalendarFromIsoToString(actual));
148 const struct calendar *cal)
151 char * str = emalloc (sizeof (char) * 100);
152 snprintf(str, 100, "%u-%02u-%02u (%u)",
153 cal->year, (u_int)cal->month, (u_int)cal->monthday,
155 str[99] = '\0'; /* paranoia rulez! */
161 const struct isodate *iso)
164 char * str = emalloc (sizeof (char) * 100);
165 snprintf(str, 100, "%u-W%02u-%02u",
166 iso->year, (u_int)iso->week, (u_int)iso->weekday);
167 str[99] = '\0'; /* paranoia rulez! */
173 const struct calendar *expected,
174 const struct calendar *actual)
176 if (expected->year == actual->year &&
177 (!expected->yearday || expected->yearday == actual->yearday) &&
178 expected->month == actual->month &&
179 expected->monthday == actual->monthday) {
182 printf("expected: %s but was %s",
183 DateFromCalToString(expected),
184 DateFromCalToString(actual));
191 const struct isodate *expected,
192 const struct isodate *actual)
194 if (expected->year == actual->year &&
195 expected->week == actual->week &&
196 expected->weekday == actual->weekday) {
199 printf("expected: %s but was %s",
200 DateFromIsoToString(expected),
201 DateFromIsoToString(actual));
208 * ---------------------------------------------------------------------
210 * ---------------------------------------------------------------------
213 /* days before month, with a full-year pad at the upper end */
214 static const u_short real_month_table[2][13] = {
215 /* -*- table for regular years -*- */
216 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
217 /* -*- table for leap years -*- */
218 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
221 /* days in month, with one month wrap-around at both ends */
222 static const u_short real_month_days[2][14] = {
223 /* -*- table for regular years -*- */
224 { 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 },
225 /* -*- table for leap years -*- */
226 { 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 }
229 /* test the day/sec join & split ops, making sure that 32bit
230 * intermediate results would definitely overflow and the hi DWORD of
231 * the 'vint64' is definitely needed.
234 test_DaySplitMerge(void)
238 for (day = -1000000; day <= 1000000; day += 100) {
239 for (sec = -100000; sec <= 186400; sec += 10000) {
245 merge = ntpcal_dayjoin(day, sec);
246 split = ntpcal_daysplit(&merge);
250 while (esec >= 86400) {
259 TEST_ASSERT_EQUAL(eday, split.hi);
260 TEST_ASSERT_EQUAL(esec, split.lo);
268 test_SplitYearDays1(void)
272 for (eyd = -1; eyd <= 365; eyd++) {
273 ntpcal_split split = ntpcal_split_yeardays(eyd, 0);
274 if (split.lo >= 0 && split.hi >= 0) {
275 TEST_ASSERT_TRUE(isGT(12,split.hi));
276 TEST_ASSERT_TRUE(isGT(real_month_days[0][split.hi+1], split.lo));
277 int32 tyd = real_month_table[0][split.hi] + split.lo;
278 TEST_ASSERT_EQUAL(eyd, tyd);
280 TEST_ASSERT_TRUE(eyd < 0 || eyd > 364);
287 test_SplitYearDays2(void)
291 for (eyd = -1; eyd <= 366; eyd++) {
292 ntpcal_split split = ntpcal_split_yeardays(eyd, 1);
293 if (split.lo >= 0 && split.hi >= 0) {
294 /* basic checks do not work on compunds :( */
295 /* would like: TEST_ASSERT_TRUE(12 > split.hi); */
296 TEST_ASSERT_TRUE(isGT(12,split.hi));
297 TEST_ASSERT_TRUE(isGT(real_month_days[1][split.hi+1], split.lo));
298 int32 tyd = real_month_table[1][split.hi] + split.lo;
299 TEST_ASSERT_EQUAL(eyd, tyd);
301 TEST_ASSERT_TRUE(eyd < 0 || eyd > 365);
310 int32 testDate = 1; /* 0001-01-01 (proleptic date) */
311 struct calendar expected = { 1, 1, 1, 1 };
312 struct calendar actual;
314 ntpcal_rd_to_date(&actual, testDate);
315 TEST_ASSERT_TRUE(IsEqualDateCal(&expected, &actual));
320 /* check last day of february for first 10000 years */
322 test_LeapYears1(void)
324 struct calendar dateIn, dateOut;
326 for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) {
328 dateIn.monthday = 28 + leapdays(dateIn.year);
329 dateIn.yearday = 31 + dateIn.monthday;
331 ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn));
333 TEST_ASSERT_TRUE(IsEqualDateCal(&dateIn, &dateOut));
339 /* check first day of march for first 10000 years */
341 test_LeapYears2(void)
343 struct calendar dateIn, dateOut;
345 for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) {
348 dateIn.yearday = 60 + leapdays(dateIn.year);
350 ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn));
351 TEST_ASSERT_TRUE(IsEqualDateCal(&dateIn, &dateOut));
357 /* Full roundtrip from 1601-01-01 to 2400-12-31
358 * checks sequence of rata die numbers and validates date output
359 * (since the input is all nominal days of the calendar in that range
360 * and the result of the inverse calculation must match the input no
361 * invalid output can occur.)
364 test_RoundTripDate(void)
366 struct calendar truDate, expDate = { 1600, 0, 12, 31 };;
368 int32 truRdn, expRdn = ntpcal_date_to_rd(&expDate);
370 while (expDate.year < 2400) {
374 leaps = leapdays(expDate.year);
375 while (expDate.month < 12) {
377 expDate.monthday = 0;
378 while (expDate.monthday < real_month_days[leaps][expDate.month]) {
383 truRdn = ntpcal_date_to_rd(&expDate);
384 TEST_ASSERT_EQUAL(expRdn, truRdn);
386 ntpcal_rd_to_date(&truDate, truRdn);
387 TEST_ASSERT_TRUE(IsEqualDateCal(&expDate, &truDate));
395 /* Roundtrip testing on calyearstart */
397 test_RoundTripYearStart(void)
399 static const time_t pivot = 0;
400 u_int32 ntp, expys, truys;
401 struct calendar date;
403 for (ntp = 0; ntp < 0xFFFFFFFFu - 30000000u; ntp += 30000000u) {
404 truys = calyearstart(ntp, &pivot);
405 ntpcal_ntp_to_date(&date, ntp, &pivot);
406 date.month = date.monthday = 1;
407 date.hour = date.minute = date.second = 0;
408 expys = ntpcal_date_to_ntp(&date);
409 TEST_ASSERT_EQUAL(expys, truys);
415 /* Roundtrip testing on calmonthstart */
417 test_RoundTripMonthStart(void)
419 static const time_t pivot = 0;
420 u_int32 ntp, expms, trums;
421 struct calendar date;
423 for (ntp = 0; ntp < 0xFFFFFFFFu - 2000000u; ntp += 2000000u) {
424 trums = calmonthstart(ntp, &pivot);
425 ntpcal_ntp_to_date(&date, ntp, &pivot);
427 date.hour = date.minute = date.second = 0;
428 expms = ntpcal_date_to_ntp(&date);
429 TEST_ASSERT_EQUAL(expms, trums);
435 /* Roundtrip testing on calweekstart */
437 test_RoundTripWeekStart(void)
439 static const time_t pivot = 0;
440 u_int32 ntp, expws, truws;
443 for (ntp = 0; ntp < 0xFFFFFFFFu - 600000u; ntp += 600000u) {
444 truws = calweekstart(ntp, &pivot);
445 isocal_ntp_to_date(&date, ntp, &pivot);
446 date.hour = date.minute = date.second = 0;
448 expws = isocal_date_to_ntp(&date);
449 TEST_ASSERT_EQUAL(expws, truws);
455 /* Roundtrip testing on caldaystart */
457 test_RoundTripDayStart(void)
459 static const time_t pivot = 0;
460 u_int32 ntp, expds, truds;
461 struct calendar date;
463 for (ntp = 0; ntp < 0xFFFFFFFFu - 80000u; ntp += 80000u) {
464 truds = caldaystart(ntp, &pivot);
465 ntpcal_ntp_to_date(&date, ntp, &pivot);
466 date.hour = date.minute = date.second = 0;
467 expds = ntpcal_date_to_ntp(&date);
468 TEST_ASSERT_EQUAL(expds, truds);
474 /* ---------------------------------------------------------------------
475 * ISO8601 week calendar internals
477 * The ISO8601 week calendar implementation is simple in the terms of
478 * the math involved, but the implementation of the calculations must
479 * take care of a few things like overflow, floor division, and sign
482 * Most of the functions are straight forward, but converting from years
483 * to weeks and from weeks to years warrants some extra tests. These use
484 * an independent reference implementation of the conversion from years
486 * ---------------------------------------------------------------------
489 /* helper / reference implementation for the first week of year in the
490 * ISO8601 week calendar. This is based on the reference definition of
491 * the ISO week calendar start: The Monday closest to January,1st of the
492 * corresponding year in the Gregorian calendar.
495 refimpl_WeeksInIsoYears(
500 days = ntpcal_weekday_close(
501 ntpcal_days_in_years(years) + 1,
503 /* the weekday functions operate on RDN, while we want elapsed
504 * units here -- we have to add / sub 1 in the midlle / at the
505 * end of the operation that gets us the first day of the ISO
510 TEST_ASSERT_EQUAL(0, days); /* paranoia check... */
515 /* The next tests loop over 5000yrs, but should still be very fast. If
516 * they are not, the calendar needs a better implementation...
519 test_IsoCalYearsToWeeks(void)
524 for (years = -1000; years < 4000; ++years) {
525 /* get number of weeks before years (reference) */
526 wref = refimpl_WeeksInIsoYears(years);
527 /* get number of weeks before years (object-under-test) */
528 wcal = isocal_weeks_in_years(years);
529 TEST_ASSERT_EQUAL(wref, wcal);
536 test_IsoCalWeeksToYearStart(void)
542 for (years = -1000; years < 4000; ++years) {
543 /* get number of weeks before years (reference) */
544 wref = refimpl_WeeksInIsoYears(years);
546 ysplit = isocal_split_eraweeks(wref);
547 /* check invariants: same year, week 0 */
548 TEST_ASSERT_EQUAL(years, ysplit.hi);
549 TEST_ASSERT_EQUAL(0, ysplit.lo);
556 test_IsoCalWeeksToYearEnd(void)
562 for (years = -1000; years < 4000; ++years) {
563 /* get last week of previous year */
564 wref = refimpl_WeeksInIsoYears(years) - 1;
566 ysplit = isocal_split_eraweeks(wref);
567 /* check invariants: previous year, week 51 or 52 */
568 TEST_ASSERT_EQUAL(years-1, ysplit.hi);
569 TEST_ASSERT(ysplit.lo == 51 || ysplit.lo == 52);
576 test_DaySecToDate(void)
581 days = ntpcal_daysec_to_date(&cal, -86400);
582 TEST_ASSERT_MESSAGE((days==-1 && cal.hour==0 && cal.minute==0 && cal.second==0),
583 "failed for -86400");
585 days = ntpcal_daysec_to_date(&cal, -86399);
586 TEST_ASSERT_MESSAGE((days==-1 && cal.hour==0 && cal.minute==0 && cal.second==1),
587 "failed for -86399");
589 days = ntpcal_daysec_to_date(&cal, -1);
590 TEST_ASSERT_MESSAGE((days==-1 && cal.hour==23 && cal.minute==59 && cal.second==59),
593 days = ntpcal_daysec_to_date(&cal, 0);
594 TEST_ASSERT_MESSAGE((days==0 && cal.hour==0 && cal.minute==0 && cal.second==0),
597 days = ntpcal_daysec_to_date(&cal, 1);
598 TEST_ASSERT_MESSAGE((days==0 && cal.hour==0 && cal.minute==0 && cal.second==1),
601 days = ntpcal_daysec_to_date(&cal, 86399);
602 TEST_ASSERT_MESSAGE((days==0 && cal.hour==23 && cal.minute==59 && cal.second==59),
605 days = ntpcal_daysec_to_date(&cal, 86400);
606 TEST_ASSERT_MESSAGE((days==1 && cal.hour==0 && cal.minute==0 && cal.second==0),