diff options
Diffstat (limited to 'contrib/ntp/tests/libntp/calendar.c')
-rw-r--r-- | contrib/ntp/tests/libntp/calendar.c | 521 |
1 files changed, 323 insertions, 198 deletions
diff --git a/contrib/ntp/tests/libntp/calendar.c b/contrib/ntp/tests/libntp/calendar.c index 4ac1df4..2c8a2d5 100644 --- a/contrib/ntp/tests/libntp/calendar.c +++ b/contrib/ntp/tests/libntp/calendar.c @@ -1,50 +1,56 @@ #include "config.h" -#include "ntp_stdlib.h" //test fail without this include, for some reason +#include "ntp_stdlib.h" /* test fail without this include, for some reason */ #include "ntp_calendar.h" #include "unity.h" -//#include "test-libntp.h" - - #include <string.h> -//#include <sstream> static int leapdays(int year); -char * CalendarFromCalToString(const struct calendar cal); //& -char * CalendarFromIsoToString(const struct isodate iso); //& - -//tehnically, booleans -int IsEqualCal(const struct calendar expected, const struct calendar actual); //&& -int IsEqualIso(const struct isodate expected, const struct isodate actual); //&& - -char * DateFromCalToStringCal(const struct calendar cal); //& -char * DateFromIsoToStringIso(const struct isodate iso); //& - -//tehnically, booleans -int sEqualDateCal(const struct calendar expected, const struct calendar actual); //&& -int IsEqualDateIso(const struct isodate expected, const struct isodate actual); //&& - - - -// --------------------------------------------------------------------- -// test support stuff -// --------------------------------------------------------------------- - -//function which, in combination with TEST_ASSERT_TRUE replaces google test framework's EXPECT_GT(a,b); -> GT means Greather Than -//boolean -int isGT(int first,int second){ - if(first > second){ - - return TRUE; +int isGT(int first, int second); +int leapdays(int year); +char * CalendarFromCalToString(const struct calendar *cal); +char * CalendarFromIsoToString(const struct isodate *iso); +int IsEqualCal(const struct calendar *expected, const struct calendar *actual); +int IsEqualIso(const struct isodate *expected, const struct isodate *actual); +char * DateFromCalToString(const struct calendar *cal); +char * DateFromIsoToString(const struct isodate *iso); +int IsEqualDateCal(const struct calendar *expected, const struct calendar *actual); +int IsEqualDateIso(const struct isodate *expected, const struct isodate *actual); +void test_DaySplitMerge(void); +void test_SplitYearDays1(void); +void test_SplitYearDays2(void); +void test_RataDie1(void); +void test_LeapYears1(void); +void test_LeapYears2(void); +void test_RoundTripDate(void); +void test_RoundTripYearStart(void); +void test_RoundTripMonthStart(void); +void test_RoundTripWeekStart(void); +void test_RoundTripDayStart(void); +void test_IsoCalYearsToWeeks(void); +void test_IsoCalWeeksToYearStart(void); +void test_IsoCalWeeksToYearEnd(void); +void test_DaySecToDate(void); + +/* + * --------------------------------------------------------------------- + * test support stuff + * --------------------------------------------------------------------- + */ +int +isGT(int first, int second) +{ + if(first > second) { + return TRUE; + } else { + return FALSE; } - - else return FALSE; } - -int leapdays(int year) +int +leapdays(int year) { if (year % 400 == 0) return 1; @@ -55,160 +61,140 @@ int leapdays(int year) return 0; } -char * CalendarFromCalToString(const struct calendar cal) { //& - char * ss = malloc (sizeof (char) * 100); - - char buffer[100] =""; - sprintf(buffer, "%u", cal.year); - strcat(ss,buffer); - strcat(ss,"-"); - sprintf(buffer, "%u", (u_int)cal.month); - strcat(ss,buffer); - strcat(ss,"-"); - sprintf(buffer, "%u", (u_int)cal.monthday); - strcat(ss,buffer); - strcat(ss," ("); - sprintf(buffer, "%u", cal.yearday); - strcat(ss,buffer); - strcat(ss,") "); - sprintf(buffer, "%u", (u_int)cal.hour); - strcat(ss,buffer); - strcat(ss,":"); - sprintf(buffer, "%u", (u_int)cal.minute); - strcat(ss,buffer); - strcat(ss,":"); - sprintf(buffer, "%u", (u_int)cal.second); - strcat(ss,buffer); - //ss << cal.year << "-" << (u_int)cal.month << "-" << (u_int)cal.monthday << " (" << cal.yearday << ") " << (u_int)cal.hour << ":" << (u_int)cal.minute << ":" << (u_int)cal.second; - return ss; - +char * +CalendarFromCalToString( + const struct calendar *cal) +{ + char * str = malloc(sizeof (char) * 100); + snprintf(str, 100, "%u-%02u-%02u (%u) %02u:%02u:%02u", + cal->year, (u_int)cal->month, (u_int)cal->monthday, + cal->yearday, + (u_int)cal->hour, (u_int)cal->minute, (u_int)cal->second); + str[99] = '\0'; /* paranoia rulez! */ + return str; } -char * CalendarFromIsoToString(const struct isodate iso) { //& - - char * ss = malloc (sizeof (char) * 100); - - char buffer[100] =""; - sprintf(buffer, "%u", iso.year); - strcat(ss,buffer); - strcat(ss,"-"); - sprintf(buffer, "%u", (u_int)iso.week); - strcat(ss,buffer); - strcat(ss,"-"); - sprintf(buffer, "%u", (u_int)iso.weekday); - strcat(ss,buffer); - sprintf(buffer, "%u", (u_int)iso.hour); - strcat(ss,buffer); - strcat(ss,":"); - sprintf(buffer, "%u", (u_int)iso.minute); - strcat(ss,buffer); - strcat(ss,":"); - sprintf(buffer, "%u", (u_int)iso.second); - strcat(ss,buffer); - //ss << iso.year << "-" << (u_int)iso.week << "-" << (u_int)iso.weekday << (u_int)iso.hour << ":" << (u_int)iso.minute << ":" << (u_int)iso.second; - return ss; - +char * +CalendarFromIsoToString( + const struct isodate *iso) +{ + char * str = emalloc (sizeof (char) * 100); + snprintf(str, 100, "%u-W%02u-%02u %02u:%02u:%02u", + iso->year, (u_int)iso->week, (u_int)iso->weekday, + (u_int)iso->hour, (u_int)iso->minute, (u_int)iso->second); + str[99] = '\0'; /* paranoia rulez! */ + return str; } -int IsEqualCal(const struct calendar expected, const struct calendar actual) { //&& - if (expected.year == actual.year && - (!expected.yearday || expected.yearday == actual.yearday) && - expected.month == actual.month && - expected.monthday == actual.monthday && - expected.hour == actual.hour && - expected.minute == actual.minute && - expected.second == actual.second) { +int +IsEqualCal( + const struct calendar *expected, + const struct calendar *actual) +{ + if (expected->year == actual->year && + (!expected->yearday || expected->yearday == actual->yearday) && + expected->month == actual->month && + expected->monthday == actual->monthday && + expected->hour == actual->hour && + expected->minute == actual->minute && + expected->second == actual->second) { return TRUE; } else { - printf("expected: %s but was %s", CalendarFromCalToString(expected) , CalendarFromCalToString(actual)); + printf("expected: %s but was %s", + CalendarFromCalToString(expected), + CalendarFromCalToString(actual)); return FALSE; } } -int IsEqualIso(const struct isodate expected, const struct isodate actual) { //&& - if (expected.year == actual.year && - expected.week == actual.week && - expected.weekday == actual.weekday && - expected.hour == actual.hour && - expected.minute == actual.minute && - expected.second == actual.second) { +int +IsEqualIso( + const struct isodate *expected, + const struct isodate *actual) +{ + if (expected->year == actual->year && + expected->week == actual->week && + expected->weekday == actual->weekday && + expected->hour == actual->hour && + expected->minute == actual->minute && + expected->second == actual->second) { return TRUE; } else { - printf("expected: %s but was %s", CalendarFromIsoToString(expected) , CalendarFromIsoToString(actual)); + printf("expected: %s but was %s", + CalendarFromIsoToString(expected), + CalendarFromIsoToString(actual)); return FALSE; } } -char * DateFromCalToString(const struct calendar cal) { //& - - char * ss = malloc (sizeof (char) * 100); - - char buffer[100] =""; - sprintf(buffer, "%u", cal.year); - strcat(ss,buffer); - strcat(ss,"-"); - sprintf(buffer, "%u", (u_int)cal.month); - strcat(ss,buffer); - strcat(ss,"-"); - sprintf(buffer, "%u", (u_int)cal.monthday); - strcat(ss,buffer); - strcat(ss," ("); - sprintf(buffer, "%u", cal.yearday); - strcat(ss,buffer); - strcat(ss,")"); - - return ss; - //ss << cal.year << "-" << (u_int)cal.month << "-" << (u_int)cal.monthday << " (" << cal.yearday << ")"; +char * +DateFromCalToString( + const struct calendar *cal) +{ + + char * str = emalloc (sizeof (char) * 100); + snprintf(str, 100, "%u-%02u-%02u (%u)", + cal->year, (u_int)cal->month, (u_int)cal->monthday, + cal->yearday); + str[99] = '\0'; /* paranoia rulez! */ + return str; } -char * DateFromIsoToString(const struct isodate iso) { //& - - char * ss = malloc (sizeof (char) * 100); - - char buffer[100] =""; - sprintf(buffer, "%u", iso.year); - strcat(ss,buffer); - strcat(ss,"-"); - sprintf(buffer, "%u", (u_int)iso.week); - strcat(ss,buffer); - strcat(ss,"-"); - sprintf(buffer, "%u", (u_int)iso.weekday); - strcat(ss,buffer); - - return ss; - //ss << iso.year << "-" << (u_int)iso.week << "-" << (u_int)iso.weekday; - +char * +DateFromIsoToString( + const struct isodate *iso) +{ + + char * str = emalloc (sizeof (char) * 100); + snprintf(str, 100, "%u-W%02u-%02u", + iso->year, (u_int)iso->week, (u_int)iso->weekday); + str[99] = '\0'; /* paranoia rulez! */ + return str; } -//boolean -int IsEqualDateCal(const struct calendar expected, const struct calendar actual) { //&& - if (expected.year == actual.year && - (!expected.yearday || expected.yearday == actual.yearday) && - expected.month == actual.month && - expected.monthday == actual.monthday) { +int/*BOOL*/ +IsEqualDateCal( + const struct calendar *expected, + const struct calendar *actual) +{ + if (expected->year == actual->year && + (!expected->yearday || expected->yearday == actual->yearday) && + expected->month == actual->month && + expected->monthday == actual->monthday) { return TRUE; } else { - printf("expected: %s but was %s", DateFromCalToString(expected) ,DateFromCalToString(actual)); + printf("expected: %s but was %s", + DateFromCalToString(expected), + DateFromCalToString(actual)); return FALSE; } } -//boolean -int IsEqualDateIso(const struct isodate expected, const struct isodate actual) { //&& - if (expected.year == actual.year && - expected.week == actual.week && - expected.weekday == actual.weekday) { +int/*BOOL*/ +IsEqualDateIso( + const struct isodate *expected, + const struct isodate *actual) +{ + if (expected->year == actual->year && + expected->week == actual->week && + expected->weekday == actual->weekday) { return TRUE; } else { - printf("expected: %s but was %s", DateFromIsoToString(expected) ,DateFromIsoToString(actual)); + printf("expected: %s but was %s", + DateFromIsoToString(expected), + DateFromIsoToString(actual)); return FALSE; } } -// --------------------------------------------------------------------- -// test cases -// --------------------------------------------------------------------- +/* + * --------------------------------------------------------------------- + * test cases + * --------------------------------------------------------------------- + */ + +/* days before month, with a full-year pad at the upper end */ static const u_short real_month_table[2][13] = { /* -*- table for regular years -*- */ { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, @@ -216,7 +202,7 @@ static const u_short real_month_table[2][13] = { { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } }; -// days in month, with one month wrap-around at both ends +/* days in month, with one month wrap-around at both ends */ static const u_short real_month_days[2][14] = { /* -*- table for regular years -*- */ { 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 }, @@ -224,17 +210,24 @@ static const u_short real_month_days[2][14] = { { 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 } }; -// test the day/sec join & split ops, making sure that 32bit -// intermediate results would definitely overflow and the hi DWORD of -// the 'vint64' is definitely needed. -void test_DaySplitMerge() { +/* test the day/sec join & split ops, making sure that 32bit + * intermediate results would definitely overflow and the hi DWORD of + * the 'vint64' is definitely needed. + */ +void +test_DaySplitMerge(void) { int32 day,sec; for (day = -1000000; day <= 1000000; day += 100) { for (sec = -100000; sec <= 186400; sec += 10000) { - vint64 merge = ntpcal_dayjoin(day, sec); - ntpcal_split split = ntpcal_daysplit(&merge); - int32 eday = day; - int32 esec = sec; + vint64 merge; + ntpcal_split split; + int32 eday; + int32 esec; + + merge = ntpcal_dayjoin(day, sec); + split = ntpcal_daysplit(&merge); + eday = day; + esec = sec; while (esec >= 86400) { eday += 1; @@ -251,13 +244,14 @@ void test_DaySplitMerge() { } } -void test_SplitYearDays1() { +void +test_SplitYearDays1(void) { int32 eyd; for (eyd = -1; eyd <= 365; eyd++) { ntpcal_split split = ntpcal_split_yeardays(eyd, 0); if (split.lo >= 0 && split.hi >= 0) { - TEST_ASSERT_TRUE(isGT(12,split.hi));//EXPECT_GT(12, split.hi); - TEST_ASSERT_TRUE(isGT(real_month_days[0][split.hi+1], split.lo));//EXPECT_GT(real_month_days[0][split.hi+1], split.lo); + TEST_ASSERT_TRUE(isGT(12,split.hi)); + TEST_ASSERT_TRUE(isGT(real_month_days[0][split.hi+1], split.lo)); int32 tyd = real_month_table[0][split.hi] + split.lo; TEST_ASSERT_EQUAL(eyd, tyd); } else @@ -265,14 +259,16 @@ void test_SplitYearDays1() { } } -void test_SplitYearDays2() { +void +test_SplitYearDays2(void) { int32 eyd; for (eyd = -1; eyd <= 366; eyd++) { ntpcal_split split = ntpcal_split_yeardays(eyd, 1); if (split.lo >= 0 && split.hi >= 0) { - //TEST_ASSERT_TRUE(12 > split.hi); //simpler version, works for basic types, doesn't work for complex structs - TEST_ASSERT_TRUE(isGT(12,split.hi));//EXPECT_GT(12, split.hi); - TEST_ASSERT_TRUE(isGT(real_month_days[1][split.hi+1], split.lo));//EXPECT_GT(real_month_days[1][split.hi+1], split.lo); + /* basic checks do not work on compunds :( */ + /* would like: TEST_ASSERT_TRUE(12 > split.hi); */ + TEST_ASSERT_TRUE(isGT(12,split.hi)); + TEST_ASSERT_TRUE(isGT(real_month_days[1][split.hi+1], split.lo)); int32 tyd = real_month_table[1][split.hi] + split.lo; TEST_ASSERT_EQUAL(eyd, tyd); } else @@ -280,17 +276,19 @@ void test_SplitYearDays2() { } } -void test_RataDie1() { - int32 testDate = 1; // 0001-01-01 (proleptic date) +void +test_RataDie1(void) { + int32 testDate = 1; /* 0001-01-01 (proleptic date) */ struct calendar expected = { 1, 1, 1, 1 }; struct calendar actual; ntpcal_rd_to_date(&actual, testDate); - TEST_ASSERT_TRUE(IsEqualDateCal(expected, actual)); + TEST_ASSERT_TRUE(IsEqualDateCal(&expected, &actual)); } -// check last day of february for first 10000 years -void test_LeapYears1() { +/* check last day of february for first 10000 years */ +void +test_LeapYears1(void) { struct calendar dateIn, dateOut; for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) { @@ -300,12 +298,13 @@ void test_LeapYears1() { ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn)); - TEST_ASSERT_TRUE(IsEqualDateCal(dateIn, dateOut)); + TEST_ASSERT_TRUE(IsEqualDateCal(&dateIn, &dateOut)); } } -// check first day of march for first 10000 years -void test_LeapYears2() { +/* check first day of march for first 10000 years */ +void +test_LeapYears2(void) { struct calendar dateIn, dateOut; for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) { @@ -314,19 +313,21 @@ void test_LeapYears2() { dateIn.yearday = 60 + leapdays(dateIn.year); ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn)); - TEST_ASSERT_TRUE(IsEqualDateCal(dateIn, dateOut)); + TEST_ASSERT_TRUE(IsEqualDateCal(&dateIn, &dateOut)); } } -// Full roundtrip for 1601-01-01 to 2400-12-31 -// checks sequence of rata die numbers and validates date output -// (since the input is all nominal days of the calendar in that range -// and the result of the inverse calculation must match the input no -// invalid output can occur.) -void test_RoundTripDate() { +/* Full roundtrip from 1601-01-01 to 2400-12-31 + * checks sequence of rata die numbers and validates date output + * (since the input is all nominal days of the calendar in that range + * and the result of the inverse calculation must match the input no + * invalid output can occur.) + */ +void +test_RoundTripDate(void) { struct calendar truDate, expDate = { 1600, 0, 12, 31 };; - int32 truRdn, expRdn = ntpcal_date_to_rd(&expDate); int leaps; + int32 truRdn, expRdn = ntpcal_date_to_rd(&expDate); while (expDate.year < 2400) { expDate.year++; @@ -345,14 +346,15 @@ void test_RoundTripDate() { TEST_ASSERT_EQUAL(expRdn, truRdn); ntpcal_rd_to_date(&truDate, truRdn); - TEST_ASSERT_TRUE(IsEqualDateCal(expDate, truDate)); + TEST_ASSERT_TRUE(IsEqualDateCal(&expDate, &truDate)); } } } } -// Roundtrip testing on calyearstart -void test_RoundTripYearStart() { +/* Roundtrip testing on calyearstart */ +void +test_RoundTripYearStart(void) { static const time_t pivot = 0; u_int32 ntp, expys, truys; struct calendar date; @@ -367,8 +369,9 @@ void test_RoundTripYearStart() { } } -// Roundtrip testing on calymonthstart -void test_RoundTripMonthStart() { +/* Roundtrip testing on calmonthstart */ +void +test_RoundTripMonthStart(void) { static const time_t pivot = 0; u_int32 ntp, expms, trums; struct calendar date; @@ -383,8 +386,9 @@ void test_RoundTripMonthStart() { } } -// Roundtrip testing on calweekstart -void test_RoundTripWeekStart() { +/* Roundtrip testing on calweekstart */ +void +test_RoundTripWeekStart(void) { static const time_t pivot = 0; u_int32 ntp, expws, truws; struct isodate date; @@ -399,8 +403,9 @@ void test_RoundTripWeekStart() { } } -// Roundtrip testing on caldaystart -void test_RoundTripDayStart() { +/* Roundtrip testing on caldaystart */ +void +test_RoundTripDayStart(void) { static const time_t pivot = 0; u_int32 ntp, expds, truds; struct calendar date; @@ -412,5 +417,125 @@ void test_RoundTripDayStart() { expds = ntpcal_date_to_ntp(&date); TEST_ASSERT_EQUAL(expds, truds); } -} +} + +/* --------------------------------------------------------------------- + * ISO8601 week calendar internals + * + * The ISO8601 week calendar implementation is simple in the terms of + * the math involved, but the implementation of the calculations must + * take care of a few things like overflow, floor division, and sign + * corrections. + * + * Most of the functions are straight forward, but converting from years + * to weeks and from weeks to years warrants some extra tests. These use + * an independent reference implementation of the conversion from years + * to weeks. + * --------------------------------------------------------------------- + */ + +/* helper / reference implementation for the first week of year in the + * ISO8601 week calendar. This is based on the reference definition of + * the ISO week calendar start: The Monday closest to January,1st of the + * corresponding year in the Gregorian calendar. + */ +static int32_t +refimpl_WeeksInIsoYears( + int32_t years) +{ + int32_t days, weeks; + days = ntpcal_weekday_close( + ntpcal_days_in_years(years) + 1, + CAL_MONDAY) - 1; + /* the weekday functions operate on RDN, while we want elapsed + * units here -- we have to add / sub 1 in the midlle / at the + * end of the operation that gets us the first day of the ISO + * week calendar day. + */ + weeks = days / 7; + days = days % 7; + TEST_ASSERT_EQUAL(0, days); /* paranoia check... */ + return weeks; +} + +/* The next tests loop over 5000yrs, but should still be very fast. If + * they are not, the calendar needs a better implementation... + */ +void +test_IsoCalYearsToWeeks(void) { + int32_t years; + int32_t wref, wcal; + for (years = -1000; years < 4000; ++years) { + /* get number of weeks before years (reference) */ + wref = refimpl_WeeksInIsoYears(years); + /* get number of weeks before years (object-under-test) */ + wcal = isocal_weeks_in_years(years); + TEST_ASSERT_EQUAL(wref, wcal); + } +} + +void +test_IsoCalWeeksToYearStart(void) { + int32_t years; + int32_t wref; + ntpcal_split ysplit; + for (years = -1000; years < 4000; ++years) { + /* get number of weeks before years (reference) */ + wref = refimpl_WeeksInIsoYears(years); + /* reverse split */ + ysplit = isocal_split_eraweeks(wref); + /* check invariants: same year, week 0 */ + TEST_ASSERT_EQUAL(years, ysplit.hi); + TEST_ASSERT_EQUAL(0, ysplit.lo); + } +} + +void +test_IsoCalWeeksToYearEnd(void) { + int32_t years; + int32_t wref; + ntpcal_split ysplit; + for (years = -1000; years < 4000; ++years) { + /* get last week of previous year */ + wref = refimpl_WeeksInIsoYears(years) - 1; + /* reverse split */ + ysplit = isocal_split_eraweeks(wref); + /* check invariants: previous year, week 51 or 52 */ + TEST_ASSERT_EQUAL(years-1, ysplit.hi); + TEST_ASSERT(ysplit.lo == 51 || ysplit.lo == 52); + } +} + +void +test_DaySecToDate(void) { + struct calendar cal; + int32_t days; + + days = ntpcal_daysec_to_date(&cal, -86400); + TEST_ASSERT_MESSAGE((days==-1 && cal.hour==0 && cal.minute==0 && cal.second==0), + "failed for -86400"); + days = ntpcal_daysec_to_date(&cal, -86399); + TEST_ASSERT_MESSAGE((days==-1 && cal.hour==0 && cal.minute==0 && cal.second==1), + "failed for -86399"); + + days = ntpcal_daysec_to_date(&cal, -1); + TEST_ASSERT_MESSAGE((days==-1 && cal.hour==23 && cal.minute==59 && cal.second==59), + "failed for -1"); + + days = ntpcal_daysec_to_date(&cal, 0); + TEST_ASSERT_MESSAGE((days==0 && cal.hour==0 && cal.minute==0 && cal.second==0), + "failed for 0"); + + days = ntpcal_daysec_to_date(&cal, 1); + TEST_ASSERT_MESSAGE((days==0 && cal.hour==0 && cal.minute==0 && cal.second==1), + "failed for 1"); + + days = ntpcal_daysec_to_date(&cal, 86399); + TEST_ASSERT_MESSAGE((days==0 && cal.hour==23 && cal.minute==59 && cal.second==59), + "failed for 86399"); + + days = ntpcal_daysec_to_date(&cal, 86400); + TEST_ASSERT_MESSAGE((days==1 && cal.hour==0 && cal.minute==0 && cal.second==0), + "failed for 86400"); +} |