3 #include "ntp_stdlib.h" /* test fail without this include, for some reason */
4 #include "ntp_calendar.h"
5 #include "ntp_unixtime.h"
10 static int leapdays(int year);
13 int isGT(int first, int second);
14 int leapdays(int year);
15 char * CalendarFromCalToString(const struct calendar *cal);
16 char * CalendarFromIsoToString(const struct isodate *iso);
17 int IsEqualCal(const struct calendar *expected, const struct calendar *actual);
18 int IsEqualIso(const struct isodate *expected, const struct isodate *actual);
19 char * DateFromCalToString(const struct calendar *cal);
20 char * DateFromIsoToString(const struct isodate *iso);
21 int IsEqualDateCal(const struct calendar *expected, const struct calendar *actual);
22 int IsEqualDateIso(const struct isodate *expected, const struct isodate *actual);
24 void test_DaySplitMerge(void);
25 void test_SplitYearDays1(void);
26 void test_SplitYearDays2(void);
27 void test_RataDie1(void);
28 void test_LeapYears1(void);
29 void test_LeapYears2(void);
30 void test_RoundTripDate(void);
31 void test_RoundTripYearStart(void);
32 void test_RoundTripMonthStart(void);
33 void test_RoundTripWeekStart(void);
34 void test_RoundTripDayStart(void);
35 void test_IsoCalYearsToWeeks(void);
36 void test_IsoCalWeeksToYearStart(void);
37 void test_IsoCalWeeksToYearEnd(void);
38 void test_DaySecToDate(void);
40 void test_NtpToNtp(void);
41 void test_NtpToTime(void);
53 * ---------------------------------------------------------------------
55 * ---------------------------------------------------------------------
58 isGT(int first, int second)
80 CalendarFromCalToString(
81 const struct calendar *cal)
83 char * str = malloc(sizeof (char) * 100);
84 snprintf(str, 100, "%u-%02u-%02u (%u) %02u:%02u:%02u",
85 cal->year, (u_int)cal->month, (u_int)cal->monthday,
87 (u_int)cal->hour, (u_int)cal->minute, (u_int)cal->second);
88 str[99] = '\0'; /* paranoia rulez! */
93 CalendarFromIsoToString(
94 const struct isodate *iso)
96 char * str = emalloc (sizeof (char) * 100);
97 snprintf(str, 100, "%u-W%02u-%02u %02u:%02u:%02u",
98 iso->year, (u_int)iso->week, (u_int)iso->weekday,
99 (u_int)iso->hour, (u_int)iso->minute, (u_int)iso->second);
100 str[99] = '\0'; /* paranoia rulez! */
106 const struct calendar *expected,
107 const struct calendar *actual)
109 if (expected->year == actual->year &&
110 (!expected->yearday || expected->yearday == actual->yearday) &&
111 expected->month == actual->month &&
112 expected->monthday == actual->monthday &&
113 expected->hour == actual->hour &&
114 expected->minute == actual->minute &&
115 expected->second == actual->second) {
118 char *p_exp = CalendarFromCalToString(expected);
119 char *p_act = CalendarFromCalToString(actual);
121 printf("expected: %s but was %s", p_exp, p_act);
132 const struct isodate *expected,
133 const struct isodate *actual)
135 if (expected->year == actual->year &&
136 expected->week == actual->week &&
137 expected->weekday == actual->weekday &&
138 expected->hour == actual->hour &&
139 expected->minute == actual->minute &&
140 expected->second == actual->second) {
143 printf("expected: %s but was %s",
144 CalendarFromIsoToString(expected),
145 CalendarFromIsoToString(actual));
152 const struct calendar *cal)
155 char * str = emalloc (sizeof (char) * 100);
156 snprintf(str, 100, "%u-%02u-%02u (%u)",
157 cal->year, (u_int)cal->month, (u_int)cal->monthday,
159 str[99] = '\0'; /* paranoia rulez! */
165 const struct isodate *iso)
168 char * str = emalloc (sizeof (char) * 100);
169 snprintf(str, 100, "%u-W%02u-%02u",
170 iso->year, (u_int)iso->week, (u_int)iso->weekday);
171 str[99] = '\0'; /* paranoia rulez! */
177 const struct calendar *expected,
178 const struct calendar *actual)
180 if (expected->year == actual->year &&
181 (!expected->yearday || expected->yearday == actual->yearday) &&
182 expected->month == actual->month &&
183 expected->monthday == actual->monthday) {
186 printf("expected: %s but was %s",
187 DateFromCalToString(expected),
188 DateFromCalToString(actual));
195 const struct isodate *expected,
196 const struct isodate *actual)
198 if (expected->year == actual->year &&
199 expected->week == actual->week &&
200 expected->weekday == actual->weekday) {
203 printf("expected: %s but was %s",
204 DateFromIsoToString(expected),
205 DateFromIsoToString(actual));
212 * ---------------------------------------------------------------------
214 * ---------------------------------------------------------------------
217 /* days before month, with a full-year pad at the upper end */
218 static const u_short real_month_table[2][13] = {
219 /* -*- table for regular years -*- */
220 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
221 /* -*- table for leap years -*- */
222 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
225 /* days in month, with one month wrap-around at both ends */
226 static const u_short real_month_days[2][14] = {
227 /* -*- table for regular years -*- */
228 { 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 },
229 /* -*- table for leap years -*- */
230 { 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 }
233 /* test the day/sec join & split ops, making sure that 32bit
234 * intermediate results would definitely overflow and the hi DWORD of
235 * the 'vint64' is definitely needed.
238 test_DaySplitMerge(void)
242 for (day = -1000000; day <= 1000000; day += 100) {
243 for (sec = -100000; sec <= 186400; sec += 10000) {
249 merge = ntpcal_dayjoin(day, sec);
250 split = ntpcal_daysplit(&merge);
254 while (esec >= 86400) {
263 TEST_ASSERT_EQUAL(eday, split.hi);
264 TEST_ASSERT_EQUAL(esec, split.lo);
272 test_SplitYearDays1(void)
276 for (eyd = -1; eyd <= 365; eyd++) {
277 ntpcal_split split = ntpcal_split_yeardays(eyd, 0);
278 if (split.lo >= 0 && split.hi >= 0) {
279 TEST_ASSERT_TRUE(isGT(12,split.hi));
280 TEST_ASSERT_TRUE(isGT(real_month_days[0][split.hi+1], split.lo));
281 int32 tyd = real_month_table[0][split.hi] + split.lo;
282 TEST_ASSERT_EQUAL(eyd, tyd);
284 TEST_ASSERT_TRUE(eyd < 0 || eyd > 364);
291 test_SplitYearDays2(void)
295 for (eyd = -1; eyd <= 366; eyd++) {
296 ntpcal_split split = ntpcal_split_yeardays(eyd, 1);
297 if (split.lo >= 0 && split.hi >= 0) {
298 /* basic checks do not work on compunds :( */
299 /* would like: TEST_ASSERT_TRUE(12 > split.hi); */
300 TEST_ASSERT_TRUE(isGT(12,split.hi));
301 TEST_ASSERT_TRUE(isGT(real_month_days[1][split.hi+1], split.lo));
302 int32 tyd = real_month_table[1][split.hi] + split.lo;
303 TEST_ASSERT_EQUAL(eyd, tyd);
305 TEST_ASSERT_TRUE(eyd < 0 || eyd > 365);
314 int32 testDate = 1; /* 0001-01-01 (proleptic date) */
315 struct calendar expected = { 1, 1, 1, 1 };
316 struct calendar actual;
318 ntpcal_rd_to_date(&actual, testDate);
319 TEST_ASSERT_TRUE(IsEqualDateCal(&expected, &actual));
324 /* check last day of february for first 10000 years */
326 test_LeapYears1(void)
328 struct calendar dateIn, dateOut;
330 for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) {
332 dateIn.monthday = 28 + leapdays(dateIn.year);
333 dateIn.yearday = 31 + dateIn.monthday;
335 ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn));
337 TEST_ASSERT_TRUE(IsEqualDateCal(&dateIn, &dateOut));
343 /* check first day of march for first 10000 years */
345 test_LeapYears2(void)
347 struct calendar dateIn, dateOut;
349 for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) {
352 dateIn.yearday = 60 + leapdays(dateIn.year);
354 ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn));
355 TEST_ASSERT_TRUE(IsEqualDateCal(&dateIn, &dateOut));
361 /* Full roundtrip from 1601-01-01 to 2400-12-31
362 * checks sequence of rata die numbers and validates date output
363 * (since the input is all nominal days of the calendar in that range
364 * and the result of the inverse calculation must match the input no
365 * invalid output can occur.)
368 test_RoundTripDate(void)
370 struct calendar truDate, expDate = { 1600, 0, 12, 31 };;
372 int32 truRdn, expRdn = ntpcal_date_to_rd(&expDate);
374 while (expDate.year < 2400) {
378 leaps = leapdays(expDate.year);
379 while (expDate.month < 12) {
381 expDate.monthday = 0;
382 while (expDate.monthday < real_month_days[leaps][expDate.month]) {
387 truRdn = ntpcal_date_to_rd(&expDate);
388 TEST_ASSERT_EQUAL(expRdn, truRdn);
390 ntpcal_rd_to_date(&truDate, truRdn);
391 TEST_ASSERT_TRUE(IsEqualDateCal(&expDate, &truDate));
399 /* Roundtrip testing on calyearstart */
401 test_RoundTripYearStart(void)
403 static const time_t pivot = 0;
404 u_int32 ntp, expys, truys;
405 struct calendar date;
407 for (ntp = 0; ntp < 0xFFFFFFFFu - 30000000u; ntp += 30000000u) {
408 truys = calyearstart(ntp, &pivot);
409 ntpcal_ntp_to_date(&date, ntp, &pivot);
410 date.month = date.monthday = 1;
411 date.hour = date.minute = date.second = 0;
412 expys = ntpcal_date_to_ntp(&date);
413 TEST_ASSERT_EQUAL(expys, truys);
419 /* Roundtrip testing on calmonthstart */
421 test_RoundTripMonthStart(void)
423 static const time_t pivot = 0;
424 u_int32 ntp, expms, trums;
425 struct calendar date;
427 for (ntp = 0; ntp < 0xFFFFFFFFu - 2000000u; ntp += 2000000u) {
428 trums = calmonthstart(ntp, &pivot);
429 ntpcal_ntp_to_date(&date, ntp, &pivot);
431 date.hour = date.minute = date.second = 0;
432 expms = ntpcal_date_to_ntp(&date);
433 TEST_ASSERT_EQUAL(expms, trums);
439 /* Roundtrip testing on calweekstart */
441 test_RoundTripWeekStart(void)
443 static const time_t pivot = 0;
444 u_int32 ntp, expws, truws;
447 for (ntp = 0; ntp < 0xFFFFFFFFu - 600000u; ntp += 600000u) {
448 truws = calweekstart(ntp, &pivot);
449 isocal_ntp_to_date(&date, ntp, &pivot);
450 date.hour = date.minute = date.second = 0;
452 expws = isocal_date_to_ntp(&date);
453 TEST_ASSERT_EQUAL(expws, truws);
459 /* Roundtrip testing on caldaystart */
461 test_RoundTripDayStart(void)
463 static const time_t pivot = 0;
464 u_int32 ntp, expds, truds;
465 struct calendar date;
467 for (ntp = 0; ntp < 0xFFFFFFFFu - 80000u; ntp += 80000u) {
468 truds = caldaystart(ntp, &pivot);
469 ntpcal_ntp_to_date(&date, ntp, &pivot);
470 date.hour = date.minute = date.second = 0;
471 expds = ntpcal_date_to_ntp(&date);
472 TEST_ASSERT_EQUAL(expds, truds);
478 /* ---------------------------------------------------------------------
479 * ISO8601 week calendar internals
481 * The ISO8601 week calendar implementation is simple in the terms of
482 * the math involved, but the implementation of the calculations must
483 * take care of a few things like overflow, floor division, and sign
486 * Most of the functions are straight forward, but converting from years
487 * to weeks and from weeks to years warrants some extra tests. These use
488 * an independent reference implementation of the conversion from years
490 * ---------------------------------------------------------------------
493 /* helper / reference implementation for the first week of year in the
494 * ISO8601 week calendar. This is based on the reference definition of
495 * the ISO week calendar start: The Monday closest to January,1st of the
496 * corresponding year in the Gregorian calendar.
499 refimpl_WeeksInIsoYears(
504 days = ntpcal_weekday_close(
505 ntpcal_days_in_years(years) + 1,
507 /* the weekday functions operate on RDN, while we want elapsed
508 * units here -- we have to add / sub 1 in the midlle / at the
509 * end of the operation that gets us the first day of the ISO
514 TEST_ASSERT_EQUAL(0, days); /* paranoia check... */
519 /* The next tests loop over 5000yrs, but should still be very fast. If
520 * they are not, the calendar needs a better implementation...
523 test_IsoCalYearsToWeeks(void)
528 for (years = -1000; years < 4000; ++years) {
529 /* get number of weeks before years (reference) */
530 wref = refimpl_WeeksInIsoYears(years);
531 /* get number of weeks before years (object-under-test) */
532 wcal = isocal_weeks_in_years(years);
533 TEST_ASSERT_EQUAL(wref, wcal);
540 test_IsoCalWeeksToYearStart(void)
546 for (years = -1000; years < 4000; ++years) {
547 /* get number of weeks before years (reference) */
548 wref = refimpl_WeeksInIsoYears(years);
550 ysplit = isocal_split_eraweeks(wref);
551 /* check invariants: same year, week 0 */
552 TEST_ASSERT_EQUAL(years, ysplit.hi);
553 TEST_ASSERT_EQUAL(0, ysplit.lo);
560 test_IsoCalWeeksToYearEnd(void)
566 for (years = -1000; years < 4000; ++years) {
567 /* get last week of previous year */
568 wref = refimpl_WeeksInIsoYears(years) - 1;
570 ysplit = isocal_split_eraweeks(wref);
571 /* check invariants: previous year, week 51 or 52 */
572 TEST_ASSERT_EQUAL(years-1, ysplit.hi);
573 TEST_ASSERT(ysplit.lo == 51 || ysplit.lo == 52);
580 test_DaySecToDate(void)
585 days = ntpcal_daysec_to_date(&cal, -86400);
586 TEST_ASSERT_MESSAGE((days==-1 && cal.hour==0 && cal.minute==0 && cal.second==0),
587 "failed for -86400");
589 days = ntpcal_daysec_to_date(&cal, -86399);
590 TEST_ASSERT_MESSAGE((days==-1 && cal.hour==0 && cal.minute==0 && cal.second==1),
591 "failed for -86399");
593 days = ntpcal_daysec_to_date(&cal, -1);
594 TEST_ASSERT_MESSAGE((days==-1 && cal.hour==23 && cal.minute==59 && cal.second==59),
597 days = ntpcal_daysec_to_date(&cal, 0);
598 TEST_ASSERT_MESSAGE((days==0 && cal.hour==0 && cal.minute==0 && cal.second==0),
601 days = ntpcal_daysec_to_date(&cal, 1);
602 TEST_ASSERT_MESSAGE((days==0 && cal.hour==0 && cal.minute==0 && cal.second==1),
605 days = ntpcal_daysec_to_date(&cal, 86399);
606 TEST_ASSERT_MESSAGE((days==0 && cal.hour==23 && cal.minute==59 && cal.second==59),
609 days = ntpcal_daysec_to_date(&cal, 86400);
610 TEST_ASSERT_MESSAGE((days==1 && cal.hour==0 && cal.minute==0 && cal.second==0),
616 /* --------------------------------------------------------------------
617 * unfolding of (truncated) NTP time stamps to full 64bit values.
619 * Note: These tests need a 64bit time_t to be useful.
625 # if SIZEOF_TIME_T <= 4
627 TEST_IGNORE_MESSAGE("test only useful for sizeof(time_t) > 4, skipped");
631 static const uint32_t ntp_vals[6] = {
632 UINT32_C(0x00000000),
633 UINT32_C(0x00000001),
634 UINT32_C(0x7FFFFFFF),
635 UINT32_C(0x80000000),
636 UINT32_C(0x80000001),
640 static char lbuf[128];
642 time_t pivot, texp, diff;
646 for (loops = 0; loops < 16; ++loops) {
647 for (iloop = 0; iloop < 6; ++iloop) {
648 hold = ntpcal_ntp_to_ntp(
649 ntp_vals[iloop], &pivot);
650 texp = vint64_to_time(&hold);
652 /* constraint 1: texp must be in the
653 * (right-open) intervall [p-(2^31), p+(2^31)[,
654 * but the pivot 'p' must be taken in full NTP
657 diff = texp - (pivot + JAN_1970);
658 snprintf(lbuf, sizeof(lbuf),
659 "bounds check: piv=%lld exp=%lld dif=%lld",
663 TEST_ASSERT_MESSAGE((diff >= INT32_MIN) && (diff <= INT32_MAX),
666 /* constraint 2: low word must be equal to
669 snprintf(lbuf, sizeof(lbuf),
670 "low check: ntp(in)=$%08lu ntp(out[0:31])=$%08lu",
671 (unsigned long)ntp_vals[iloop],
672 (unsigned long)hold.D_s.lo);
673 TEST_ASSERT_EQUAL_MESSAGE(ntp_vals[iloop], hold.D_s.lo, lbuf);
683 # if SIZEOF_TIME_T <= 4
685 TEST_IGNORE_MESSAGE("test only useful for sizeof(time_t) > 4, skipped");
689 static const uint32_t ntp_vals[6] = {
690 UINT32_C(0x00000000),
691 UINT32_C(0x00000001),
692 UINT32_C(0x7FFFFFFF),
693 UINT32_C(0x80000000),
694 UINT32_C(0x80000001),
698 static char lbuf[128];
700 time_t pivot, texp, diff;
705 for (loops = 0; loops < 16; ++loops) {
706 for (iloop = 0; iloop < 6; ++iloop) {
707 hold = ntpcal_ntp_to_time(
708 ntp_vals[iloop], &pivot);
709 texp = vint64_to_time(&hold);
711 /* constraint 1: texp must be in the
712 * (right-open) intervall [p-(2^31), p+(2^31)[
715 snprintf(lbuf, sizeof(lbuf),
716 "bounds check: piv=%lld exp=%lld dif=%lld",
720 TEST_ASSERT_MESSAGE((diff >= INT32_MIN) && (diff <= INT32_MAX),
723 /* constraint 2: conversion from full time back
724 * to truncated NTP time must yield same result
727 back = (uint32_t)texp + JAN_1970;
728 snprintf(lbuf, sizeof(lbuf),
729 "modulo check: ntp(in)=$%08lu ntp(out)=$%08lu",
730 (unsigned long)ntp_vals[iloop],
731 (unsigned long)back);
732 TEST_ASSERT_EQUAL_MESSAGE(ntp_vals[iloop], back, lbuf);