]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/calendar.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / calendar.c
1 #include "config.h"
2
3 #include "ntp_stdlib.h" //test fail without this include, for some reason
4 #include "ntp_calendar.h"
5 #include "unity.h"
6
7 //#include "test-libntp.h"
8
9
10 #include <string.h>
11 //#include <sstream>
12
13 static int leapdays(int year);
14
15 char * CalendarFromCalToString(const struct calendar cal); //&
16 char * CalendarFromIsoToString(const struct isodate iso); //&
17
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); //&&
21
22 char * DateFromCalToStringCal(const struct calendar cal); //&
23 char * DateFromIsoToStringIso(const struct isodate iso); //&
24
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); //&&
28
29
30
31 // ---------------------------------------------------------------------
32 // test support stuff
33 // ---------------------------------------------------------------------
34
35 //function which, in combination with TEST_ASSERT_TRUE replaces google test framework's EXPECT_GT(a,b); -> GT means Greather Than
36 //boolean
37 int isGT(int first,int second){
38         if(first > second){
39         
40         return TRUE;
41         }
42
43         else return FALSE;
44 }
45
46
47 int leapdays(int year)
48 {
49         if (year % 400 == 0)
50                 return 1;
51         if (year % 100 == 0)
52                 return 0;
53         if (year % 4 == 0)
54                 return 1;
55         return 0;
56 }
57
58 char * CalendarFromCalToString(const struct calendar cal) { //&
59         char * ss = malloc (sizeof (char) * 100);
60         
61         char buffer[100] ="";
62         sprintf(buffer, "%u", cal.year);
63         strcat(ss,buffer);
64         strcat(ss,"-");
65         sprintf(buffer, "%u", (u_int)cal.month);
66         strcat(ss,buffer);
67         strcat(ss,"-");
68         sprintf(buffer, "%u", (u_int)cal.monthday);
69         strcat(ss,buffer);
70         strcat(ss," (");
71         sprintf(buffer, "%u", cal.yearday);
72         strcat(ss,buffer);
73         strcat(ss,") ");
74         sprintf(buffer, "%u", (u_int)cal.hour);
75         strcat(ss,buffer);
76         strcat(ss,":");
77         sprintf(buffer, "%u", (u_int)cal.minute);
78         strcat(ss,buffer);
79         strcat(ss,":");
80         sprintf(buffer, "%u", (u_int)cal.second);
81         strcat(ss,buffer);
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;
83         return ss;      
84
85 }
86
87 char * CalendarFromIsoToString(const struct isodate iso) { //&
88
89         char * ss = malloc (sizeof (char) * 100);
90         
91         char buffer[100] ="";
92         sprintf(buffer, "%u", iso.year);
93         strcat(ss,buffer);
94         strcat(ss,"-");
95         sprintf(buffer, "%u", (u_int)iso.week);
96         strcat(ss,buffer);
97         strcat(ss,"-");
98         sprintf(buffer, "%u", (u_int)iso.weekday);
99         strcat(ss,buffer);
100         sprintf(buffer, "%u", (u_int)iso.hour);
101         strcat(ss,buffer);
102         strcat(ss,":");
103         sprintf(buffer, "%u", (u_int)iso.minute);
104         strcat(ss,buffer);
105         strcat(ss,":");
106         sprintf(buffer, "%u", (u_int)iso.second);
107         strcat(ss,buffer);
108         //ss << iso.year << "-" << (u_int)iso.week << "-" << (u_int)iso.weekday << (u_int)iso.hour << ":" << (u_int)iso.minute << ":" << (u_int)iso.second;     
109         return ss;      
110
111 }
112
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) {
121                 return TRUE;
122         } else {
123                 printf("expected: %s but was %s", CalendarFromCalToString(expected) , CalendarFromCalToString(actual));
124                 return FALSE;             
125         }
126 }
127
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) {
135                 return TRUE;
136         } else {
137                 printf("expected: %s but was %s", CalendarFromIsoToString(expected) , CalendarFromIsoToString(actual));
138                 return FALSE;      
139         }
140 }
141
142 char * DateFromCalToString(const struct calendar cal) { //&
143
144         char * ss = malloc (sizeof (char) * 100);
145         
146         char buffer[100] ="";
147         sprintf(buffer, "%u", cal.year);
148         strcat(ss,buffer);
149         strcat(ss,"-");
150         sprintf(buffer, "%u", (u_int)cal.month);
151         strcat(ss,buffer);
152         strcat(ss,"-");
153         sprintf(buffer, "%u", (u_int)cal.monthday);
154         strcat(ss,buffer);
155         strcat(ss," (");
156         sprintf(buffer, "%u", cal.yearday);
157         strcat(ss,buffer);
158         strcat(ss,")");
159         
160         return ss;
161         //ss << cal.year << "-" << (u_int)cal.month << "-" << (u_int)cal.monthday << " (" << cal.yearday << ")";
162 }
163
164 char * DateFromIsoToString(const struct isodate iso) { //&
165
166         char * ss = malloc (sizeof (char) * 100);
167         
168         char buffer[100] ="";
169         sprintf(buffer, "%u", iso.year);
170         strcat(ss,buffer);
171         strcat(ss,"-");
172         sprintf(buffer, "%u", (u_int)iso.week);
173         strcat(ss,buffer);
174         strcat(ss,"-");
175         sprintf(buffer, "%u", (u_int)iso.weekday);
176         strcat(ss,buffer);
177
178         return ss;
179         //ss << iso.year << "-" << (u_int)iso.week << "-" << (u_int)iso.weekday;
180         
181 }
182
183 //boolean 
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) {
189                 return TRUE;
190         } else {
191                 printf("expected: %s but was %s", DateFromCalToString(expected) ,DateFromCalToString(actual));
192                 return FALSE;
193         }
194 }
195
196 //boolean
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) {
201                 return TRUE;
202         } else {
203                 printf("expected: %s but was %s", DateFromIsoToString(expected) ,DateFromIsoToString(actual));
204                 return FALSE;       
205         }
206 }
207
208
209 // ---------------------------------------------------------------------
210 // test cases
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 }
217 };
218
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 }
225 };
226
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() {
231         int32 day,sec;
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);
236                         int32        eday  = day;
237                         int32        esec  = sec;
238
239                         while (esec >= 86400) {
240                                 eday += 1;
241                                 esec -= 86400;
242                         }
243                         while (esec < 0) {
244                                 eday -= 1;
245                                 esec += 86400;
246                         }
247
248                         TEST_ASSERT_EQUAL(eday, split.hi);
249                         TEST_ASSERT_EQUAL(esec, split.lo);
250                 }
251         }
252 }
253
254 void test_SplitYearDays1() {
255         int32 eyd;
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);
263                 } else
264                         TEST_ASSERT_TRUE(eyd < 0 || eyd > 364);
265         }
266 }
267                 
268 void test_SplitYearDays2() {
269         int32 eyd;
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);
278                 } else
279                         TEST_ASSERT_TRUE(eyd < 0 || eyd > 365);
280                 }
281 }
282                 
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;
287
288         ntpcal_rd_to_date(&actual, testDate);
289         TEST_ASSERT_TRUE(IsEqualDateCal(expected, actual));
290 }
291
292 // check last day of february for first 10000 years
293 void test_LeapYears1() {
294         struct calendar dateIn, dateOut;
295
296         for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) {
297                 dateIn.month    = 2;
298                 dateIn.monthday = 28 + leapdays(dateIn.year);
299                 dateIn.yearday  = 31 + dateIn.monthday;
300
301                 ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn));
302
303                 TEST_ASSERT_TRUE(IsEqualDateCal(dateIn, dateOut));
304         }
305 }
306
307 // check first day of march for first 10000 years
308 void test_LeapYears2() {
309         struct calendar dateIn, dateOut;
310
311         for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) {
312                 dateIn.month    = 3;
313                 dateIn.monthday = 1;
314                 dateIn.yearday  = 60 + leapdays(dateIn.year);
315
316                 ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn));
317                 TEST_ASSERT_TRUE(IsEqualDateCal(dateIn, dateOut));
318         }
319 }
320
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);
329         int      leaps;
330
331         while (expDate.year < 2400) {
332                 expDate.year++;
333                 expDate.month   = 0;
334                 expDate.yearday = 0;
335                 leaps = leapdays(expDate.year);
336                 while (expDate.month < 12) {
337                         expDate.month++;                        
338                         expDate.monthday = 0;
339                         while (expDate.monthday < real_month_days[leaps][expDate.month]) {
340                                 expDate.monthday++;
341                                 expDate.yearday++;
342                                 expRdn++;
343
344                                 truRdn = ntpcal_date_to_rd(&expDate);
345                                 TEST_ASSERT_EQUAL(expRdn, truRdn);
346
347                                 ntpcal_rd_to_date(&truDate, truRdn);
348                                 TEST_ASSERT_TRUE(IsEqualDateCal(expDate, truDate));
349                         }
350                 }
351         }
352 }
353
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;
359
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);
367         }
368 }       
369
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;
375
376         for (ntp = 0; ntp < 0xFFFFFFFFu - 2000000u; ntp += 2000000u) {
377                 trums = calmonthstart(ntp, &pivot);
378                 ntpcal_ntp_to_date(&date, ntp, &pivot);
379                 date.monthday = 1;
380                 date.hour = date.minute = date.second = 0;
381                 expms = ntpcal_date_to_ntp(&date);
382                 TEST_ASSERT_EQUAL(expms, trums);
383         }
384 }       
385
386 // Roundtrip testing on calweekstart
387 void test_RoundTripWeekStart() {
388         static const time_t pivot = 0;
389         u_int32 ntp, expws, truws;
390         struct isodate date;
391
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;
396                 date.weekday = 1;
397                 expws = isocal_date_to_ntp(&date);
398                 TEST_ASSERT_EQUAL(expws, truws);
399         }
400 }       
401
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;
407
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);
414         }
415 }       
416