3 #include "ntp_stdlib.h" //test fail without this include, for some reason
4 #include "ntp_calendar.h"
7 //#include "test-libntp.h"
13 static int leapdays(int year);
15 char * CalendarFromCalToString(const struct calendar cal); //&
16 char * CalendarFromIsoToString(const struct isodate iso); //&
18 //tehnically, booleans
19 int IsEqualCal(const struct calendar expected, const struct calendar actual); //&&
20 int IsEqualIso(const struct isodate expected, const struct isodate actual); //&&
22 char * DateFromCalToStringCal(const struct calendar cal); //&
23 char * DateFromIsoToStringIso(const struct isodate iso); //&
25 //tehnically, booleans
26 int sEqualDateCal(const struct calendar expected, const struct calendar actual); //&&
27 int IsEqualDateIso(const struct isodate expected, const struct isodate actual); //&&
31 // ---------------------------------------------------------------------
33 // ---------------------------------------------------------------------
35 //function which, in combination with TEST_ASSERT_TRUE replaces google test framework's EXPECT_GT(a,b); -> GT means Greather Than
37 int isGT(int first,int second){
47 int leapdays(int year)
58 char * CalendarFromCalToString(const struct calendar cal) { //&
59 char * ss = malloc (sizeof (char) * 100);
62 sprintf(buffer, "%u", cal.year);
65 sprintf(buffer, "%u", (u_int)cal.month);
68 sprintf(buffer, "%u", (u_int)cal.monthday);
71 sprintf(buffer, "%u", cal.yearday);
74 sprintf(buffer, "%u", (u_int)cal.hour);
77 sprintf(buffer, "%u", (u_int)cal.minute);
80 sprintf(buffer, "%u", (u_int)cal.second);
82 //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;
87 char * CalendarFromIsoToString(const struct isodate iso) { //&
89 char * ss = malloc (sizeof (char) * 100);
92 sprintf(buffer, "%u", iso.year);
95 sprintf(buffer, "%u", (u_int)iso.week);
98 sprintf(buffer, "%u", (u_int)iso.weekday);
100 sprintf(buffer, "%u", (u_int)iso.hour);
103 sprintf(buffer, "%u", (u_int)iso.minute);
106 sprintf(buffer, "%u", (u_int)iso.second);
108 //ss << iso.year << "-" << (u_int)iso.week << "-" << (u_int)iso.weekday << (u_int)iso.hour << ":" << (u_int)iso.minute << ":" << (u_int)iso.second;
113 int IsEqualCal(const struct calendar expected, const struct calendar actual) { //&&
114 if (expected.year == actual.year &&
115 (!expected.yearday || expected.yearday == actual.yearday) &&
116 expected.month == actual.month &&
117 expected.monthday == actual.monthday &&
118 expected.hour == actual.hour &&
119 expected.minute == actual.minute &&
120 expected.second == actual.second) {
123 printf("expected: %s but was %s", CalendarFromCalToString(expected) , CalendarFromCalToString(actual));
128 int IsEqualIso(const struct isodate expected, const struct isodate actual) { //&&
129 if (expected.year == actual.year &&
130 expected.week == actual.week &&
131 expected.weekday == actual.weekday &&
132 expected.hour == actual.hour &&
133 expected.minute == actual.minute &&
134 expected.second == actual.second) {
137 printf("expected: %s but was %s", CalendarFromIsoToString(expected) , CalendarFromIsoToString(actual));
142 char * DateFromCalToString(const struct calendar cal) { //&
144 char * ss = malloc (sizeof (char) * 100);
146 char buffer[100] ="";
147 sprintf(buffer, "%u", cal.year);
150 sprintf(buffer, "%u", (u_int)cal.month);
153 sprintf(buffer, "%u", (u_int)cal.monthday);
156 sprintf(buffer, "%u", cal.yearday);
161 //ss << cal.year << "-" << (u_int)cal.month << "-" << (u_int)cal.monthday << " (" << cal.yearday << ")";
164 char * DateFromIsoToString(const struct isodate iso) { //&
166 char * ss = malloc (sizeof (char) * 100);
168 char buffer[100] ="";
169 sprintf(buffer, "%u", iso.year);
172 sprintf(buffer, "%u", (u_int)iso.week);
175 sprintf(buffer, "%u", (u_int)iso.weekday);
179 //ss << iso.year << "-" << (u_int)iso.week << "-" << (u_int)iso.weekday;
184 int IsEqualDateCal(const struct calendar expected, const struct calendar actual) { //&&
185 if (expected.year == actual.year &&
186 (!expected.yearday || expected.yearday == actual.yearday) &&
187 expected.month == actual.month &&
188 expected.monthday == actual.monthday) {
191 printf("expected: %s but was %s", DateFromCalToString(expected) ,DateFromCalToString(actual));
197 int IsEqualDateIso(const struct isodate expected, 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", DateFromIsoToString(expected) ,DateFromIsoToString(actual));
209 // ---------------------------------------------------------------------
211 // ---------------------------------------------------------------------
212 static const u_short real_month_table[2][13] = {
213 /* -*- table for regular years -*- */
214 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
215 /* -*- table for leap years -*- */
216 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
219 // days in month, with one month wrap-around at both ends
220 static const u_short real_month_days[2][14] = {
221 /* -*- table for regular years -*- */
222 { 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 },
223 /* -*- table for leap years -*- */
224 { 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 }
227 // test the day/sec join & split ops, making sure that 32bit
228 // intermediate results would definitely overflow and the hi DWORD of
229 // the 'vint64' is definitely needed.
230 void test_DaySplitMerge() {
232 for (day = -1000000; day <= 1000000; day += 100) {
233 for (sec = -100000; sec <= 186400; sec += 10000) {
234 vint64 merge = ntpcal_dayjoin(day, sec);
235 ntpcal_split split = ntpcal_daysplit(&merge);
239 while (esec >= 86400) {
248 TEST_ASSERT_EQUAL(eday, split.hi);
249 TEST_ASSERT_EQUAL(esec, split.lo);
254 void test_SplitYearDays1() {
256 for (eyd = -1; eyd <= 365; eyd++) {
257 ntpcal_split split = ntpcal_split_yeardays(eyd, 0);
258 if (split.lo >= 0 && split.hi >= 0) {
259 TEST_ASSERT_TRUE(isGT(12,split.hi));//EXPECT_GT(12, split.hi);
260 TEST_ASSERT_TRUE(isGT(real_month_days[0][split.hi+1], split.lo));//EXPECT_GT(real_month_days[0][split.hi+1], split.lo);
261 int32 tyd = real_month_table[0][split.hi] + split.lo;
262 TEST_ASSERT_EQUAL(eyd, tyd);
264 TEST_ASSERT_TRUE(eyd < 0 || eyd > 364);
268 void test_SplitYearDays2() {
270 for (eyd = -1; eyd <= 366; eyd++) {
271 ntpcal_split split = ntpcal_split_yeardays(eyd, 1);
272 if (split.lo >= 0 && split.hi >= 0) {
273 //TEST_ASSERT_TRUE(12 > split.hi); //simpler version, works for basic types, doesn't work for complex structs
274 TEST_ASSERT_TRUE(isGT(12,split.hi));//EXPECT_GT(12, split.hi);
275 TEST_ASSERT_TRUE(isGT(real_month_days[1][split.hi+1], split.lo));//EXPECT_GT(real_month_days[1][split.hi+1], split.lo);
276 int32 tyd = real_month_table[1][split.hi] + split.lo;
277 TEST_ASSERT_EQUAL(eyd, tyd);
279 TEST_ASSERT_TRUE(eyd < 0 || eyd > 365);
283 void test_RataDie1() {
284 int32 testDate = 1; // 0001-01-01 (proleptic date)
285 struct calendar expected = { 1, 1, 1, 1 };
286 struct calendar actual;
288 ntpcal_rd_to_date(&actual, testDate);
289 TEST_ASSERT_TRUE(IsEqualDateCal(expected, actual));
292 // check last day of february for first 10000 years
293 void test_LeapYears1() {
294 struct calendar dateIn, dateOut;
296 for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) {
298 dateIn.monthday = 28 + leapdays(dateIn.year);
299 dateIn.yearday = 31 + dateIn.monthday;
301 ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn));
303 TEST_ASSERT_TRUE(IsEqualDateCal(dateIn, dateOut));
307 // check first day of march for first 10000 years
308 void test_LeapYears2() {
309 struct calendar dateIn, dateOut;
311 for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) {
314 dateIn.yearday = 60 + leapdays(dateIn.year);
316 ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn));
317 TEST_ASSERT_TRUE(IsEqualDateCal(dateIn, dateOut));
321 // Full roundtrip for 1601-01-01 to 2400-12-31
322 // checks sequence of rata die numbers and validates date output
323 // (since the input is all nominal days of the calendar in that range
324 // and the result of the inverse calculation must match the input no
325 // invalid output can occur.)
326 void test_RoundTripDate() {
327 struct calendar truDate, expDate = { 1600, 0, 12, 31 };;
328 int32 truRdn, expRdn = ntpcal_date_to_rd(&expDate);
331 while (expDate.year < 2400) {
335 leaps = leapdays(expDate.year);
336 while (expDate.month < 12) {
338 expDate.monthday = 0;
339 while (expDate.monthday < real_month_days[leaps][expDate.month]) {
344 truRdn = ntpcal_date_to_rd(&expDate);
345 TEST_ASSERT_EQUAL(expRdn, truRdn);
347 ntpcal_rd_to_date(&truDate, truRdn);
348 TEST_ASSERT_TRUE(IsEqualDateCal(expDate, truDate));
354 // Roundtrip testing on calyearstart
355 void test_RoundTripYearStart() {
356 static const time_t pivot = 0;
357 u_int32 ntp, expys, truys;
358 struct calendar date;
360 for (ntp = 0; ntp < 0xFFFFFFFFu - 30000000u; ntp += 30000000u) {
361 truys = calyearstart(ntp, &pivot);
362 ntpcal_ntp_to_date(&date, ntp, &pivot);
363 date.month = date.monthday = 1;
364 date.hour = date.minute = date.second = 0;
365 expys = ntpcal_date_to_ntp(&date);
366 TEST_ASSERT_EQUAL(expys, truys);
370 // Roundtrip testing on calymonthstart
371 void test_RoundTripMonthStart() {
372 static const time_t pivot = 0;
373 u_int32 ntp, expms, trums;
374 struct calendar date;
376 for (ntp = 0; ntp < 0xFFFFFFFFu - 2000000u; ntp += 2000000u) {
377 trums = calmonthstart(ntp, &pivot);
378 ntpcal_ntp_to_date(&date, ntp, &pivot);
380 date.hour = date.minute = date.second = 0;
381 expms = ntpcal_date_to_ntp(&date);
382 TEST_ASSERT_EQUAL(expms, trums);
386 // Roundtrip testing on calweekstart
387 void test_RoundTripWeekStart() {
388 static const time_t pivot = 0;
389 u_int32 ntp, expws, truws;
392 for (ntp = 0; ntp < 0xFFFFFFFFu - 600000u; ntp += 600000u) {
393 truws = calweekstart(ntp, &pivot);
394 isocal_ntp_to_date(&date, ntp, &pivot);
395 date.hour = date.minute = date.second = 0;
397 expws = isocal_date_to_ntp(&date);
398 TEST_ASSERT_EQUAL(expws, truws);
402 // Roundtrip testing on caldaystart
403 void test_RoundTripDayStart() {
404 static const time_t pivot = 0;
405 u_int32 ntp, expds, truds;
406 struct calendar date;
408 for (ntp = 0; ntp < 0xFFFFFFFFu - 80000u; ntp += 80000u) {
409 truds = caldaystart(ntp, &pivot);
410 ntpcal_ntp_to_date(&date, ntp, &pivot);
411 date.hour = date.minute = date.second = 0;
412 expds = ntpcal_date_to_ntp(&date);
413 TEST_ASSERT_EQUAL(expds, truds);