1 | |
---|
2 | #include "glib.h" |
---|
3 | |
---|
4 | #include <stdio.h> |
---|
5 | #include <string.h> |
---|
6 | #include <locale.h> |
---|
7 | #include <time.h> |
---|
8 | |
---|
9 | gboolean failed = FALSE; |
---|
10 | guint32 passed = 0; |
---|
11 | guint32 notpassed = 0; |
---|
12 | |
---|
13 | #define TEST(m,cond) G_STMT_START { failed = !(cond); \ |
---|
14 | if (failed) \ |
---|
15 | { ++notpassed; \ |
---|
16 | if (!m) \ |
---|
17 | g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \ |
---|
18 | else \ |
---|
19 | g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \ |
---|
20 | } \ |
---|
21 | else \ |
---|
22 | ++passed; \ |
---|
23 | if ((passed+notpassed) % 10000 == 0) g_print ("."); fflush (stdout); \ |
---|
24 | } G_STMT_END |
---|
25 | |
---|
26 | void g_date_debug_print(GDate* d) |
---|
27 | { |
---|
28 | if (!d) g_print("NULL!\n"); |
---|
29 | else |
---|
30 | g_print("julian: %u (%s) DMY: %u %u %u (%s)\n", |
---|
31 | d->julian_days, |
---|
32 | d->julian ? "valid" : "invalid", |
---|
33 | d->day, |
---|
34 | d->month, |
---|
35 | d->year, |
---|
36 | d->dmy ? "valid" : "invalid"); |
---|
37 | |
---|
38 | fflush(stdout); |
---|
39 | } |
---|
40 | |
---|
41 | int main(int argc, char** argv) |
---|
42 | { |
---|
43 | GDate* d; |
---|
44 | guint32 j; |
---|
45 | GDateMonth m; |
---|
46 | GDateYear y, prev_y; |
---|
47 | GDateDay day; |
---|
48 | gchar buf[101]; |
---|
49 | gchar* loc; |
---|
50 | /* Try to get all the leap year cases. */ |
---|
51 | GDateYear check_years[] = { |
---|
52 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
---|
53 | 11, 12, 13, 14, 98, 99, 100, 101, 102, 103, 397, |
---|
54 | 398, 399, 400, 401, 402, 403, 404, 405, 406, |
---|
55 | 1598, 1599, 1600, 1601, 1602, 1650, 1651, |
---|
56 | 1897, 1898, 1899, 1900, 1901, 1902, 1903, |
---|
57 | 1961, 1962, 1963, 1964, 1965, 1967, |
---|
58 | 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, |
---|
59 | 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, |
---|
60 | 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, |
---|
61 | 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, |
---|
62 | 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, |
---|
63 | 3000, 3001, 3002, 3998, 3999, 4000, 4001, 4002, 4003 |
---|
64 | }; |
---|
65 | guint n_check_years = sizeof(check_years)/sizeof(GDateYear); |
---|
66 | guint i; |
---|
67 | gboolean discontinuity; |
---|
68 | |
---|
69 | g_print("checking GDate..."); |
---|
70 | |
---|
71 | TEST("sizeof(GDate) is not more than 8 bytes on this platform", sizeof(GDate) < 9); |
---|
72 | |
---|
73 | d = g_date_new(); |
---|
74 | |
---|
75 | TEST("Empty constructor produces invalid date", !g_date_valid(d)); |
---|
76 | |
---|
77 | g_date_free(d); |
---|
78 | |
---|
79 | d = g_date_new_dmy(1,1,1); |
---|
80 | |
---|
81 | TEST("January 1, Year 1 created and valid", g_date_valid(d)); |
---|
82 | |
---|
83 | j = g_date_julian(d); |
---|
84 | |
---|
85 | TEST("January 1, Year 1 is Julian date 1", j == 1); |
---|
86 | |
---|
87 | TEST("Returned month is January", g_date_month(d) == G_DATE_JANUARY); |
---|
88 | TEST("Returned day is 1", g_date_day(d) == 1); |
---|
89 | TEST("Returned year is 1", g_date_year(d) == 1); |
---|
90 | |
---|
91 | TEST("Bad month is invalid", !g_date_valid_month(G_DATE_BAD_MONTH)); |
---|
92 | TEST("Month 13 is invalid", !g_date_valid_month(13)); |
---|
93 | TEST("Bad day is invalid", !g_date_valid_day(G_DATE_BAD_DAY)); |
---|
94 | TEST("Day 32 is invalid", !g_date_valid_day(32)); |
---|
95 | TEST("Bad year is invalid", !g_date_valid_year(G_DATE_BAD_YEAR)); |
---|
96 | TEST("Bad julian is invalid", !g_date_valid_julian(G_DATE_BAD_JULIAN)); |
---|
97 | TEST("Bad weekday is invalid", !g_date_valid_weekday(G_DATE_BAD_WEEKDAY)); |
---|
98 | TEST("Year 2000 is a leap year", g_date_is_leap_year(2000)); |
---|
99 | TEST("Year 1999 is not a leap year", !g_date_is_leap_year(1999)); |
---|
100 | TEST("Year 1996 is a leap year", g_date_is_leap_year(1996)); |
---|
101 | TEST("Year 1600 is a leap year", g_date_is_leap_year(1600)); |
---|
102 | TEST("Year 2100 is not a leap year", !g_date_is_leap_year(2100)); |
---|
103 | TEST("Year 1800 is not a leap year", !g_date_is_leap_year(1800)); |
---|
104 | |
---|
105 | g_date_free(d); |
---|
106 | |
---|
107 | loc = setlocale(LC_ALL,""); |
---|
108 | if (loc) |
---|
109 | g_print("\nLocale set to %s\n", loc); |
---|
110 | else |
---|
111 | g_print("\nLocale unchanged\n"); |
---|
112 | |
---|
113 | d = g_date_new(); |
---|
114 | g_date_set_time(d, time(NULL)); |
---|
115 | TEST("Today is valid", g_date_valid(d)); |
---|
116 | |
---|
117 | g_date_strftime(buf,100,"Today is a %A, %x\n", d); |
---|
118 | g_print("%s", buf); |
---|
119 | |
---|
120 | g_date_set_time(d, 1); |
---|
121 | TEST("Beginning of Unix epoch is valid", g_date_valid(d)); |
---|
122 | |
---|
123 | g_date_strftime(buf,100,"1 second into the Unix epoch it was a %A, in the month of %B, %x\n", d); |
---|
124 | g_print("%s", buf); |
---|
125 | |
---|
126 | g_date_set_julian(d, 1); |
---|
127 | TEST("GDate's \"Julian\" epoch's first day is valid", g_date_valid(d)); |
---|
128 | |
---|
129 | g_date_strftime(buf,100,"Our \"Julian\" epoch begins on a %A, in the month of %B, %x\n", |
---|
130 | d); |
---|
131 | g_print("%s", buf); |
---|
132 | |
---|
133 | g_date_set_dmy(d, 10, 1, 2000); |
---|
134 | |
---|
135 | g_date_strftime(buf,100,"%x", d); |
---|
136 | |
---|
137 | g_date_set_parse(d, buf); |
---|
138 | /* Note: this test will hopefully work, but no promises. */ |
---|
139 | TEST("Successfully parsed a %x-formatted string", |
---|
140 | g_date_valid(d) && |
---|
141 | g_date_month(d) == 1 && |
---|
142 | g_date_day(d) == 10 && |
---|
143 | g_date_year(d) == 2000); |
---|
144 | if (failed) |
---|
145 | g_date_debug_print(d); |
---|
146 | |
---|
147 | g_date_free(d); |
---|
148 | |
---|
149 | j = G_DATE_BAD_JULIAN; |
---|
150 | |
---|
151 | i = 0; |
---|
152 | discontinuity = TRUE; |
---|
153 | y = check_years[0]; |
---|
154 | prev_y = G_DATE_BAD_YEAR; |
---|
155 | while (i < n_check_years) |
---|
156 | { |
---|
157 | guint32 first_day_of_year = G_DATE_BAD_JULIAN; |
---|
158 | guint16 days_in_year = g_date_is_leap_year(y) ? 366 : 365; |
---|
159 | guint sunday_week_of_year = 0; |
---|
160 | guint sunday_weeks_in_year = g_date_sunday_weeks_in_year(y); |
---|
161 | guint monday_week_of_year = 0; |
---|
162 | guint monday_weeks_in_year = g_date_monday_weeks_in_year(y); |
---|
163 | |
---|
164 | if (discontinuity) |
---|
165 | g_print(" (Break in sequence of requested years to check)\n"); |
---|
166 | |
---|
167 | g_print("Checking year %u", y); |
---|
168 | |
---|
169 | TEST("Year is valid", g_date_valid_year(y)); |
---|
170 | |
---|
171 | TEST("Number of Sunday weeks in year is 52 or 53", |
---|
172 | sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53); |
---|
173 | |
---|
174 | TEST("Number of Monday weeks in year is 52 or 53", |
---|
175 | monday_weeks_in_year == 52 || monday_weeks_in_year == 53); |
---|
176 | |
---|
177 | m = 1; |
---|
178 | while (m < 13) |
---|
179 | { |
---|
180 | guint8 dim = g_date_days_in_month(m,y); |
---|
181 | GDate days[31]; /* This is the fast way, no allocation */ |
---|
182 | |
---|
183 | TEST("Sensible number of days in month", (dim > 0 && dim < 32)); |
---|
184 | |
---|
185 | TEST("Month between 1 and 12 is valid", g_date_valid_month(m)); |
---|
186 | |
---|
187 | day = 1; |
---|
188 | |
---|
189 | g_date_clear(days, 31); |
---|
190 | |
---|
191 | while (day <= dim) |
---|
192 | { |
---|
193 | guint i; |
---|
194 | GDate tmp; |
---|
195 | |
---|
196 | TEST("DMY triplet is valid", g_date_valid_dmy(day,m,y)); |
---|
197 | |
---|
198 | /* Create GDate with triplet */ |
---|
199 | |
---|
200 | d = &days[day-1]; |
---|
201 | |
---|
202 | TEST("Cleared day is invalid", !g_date_valid(d)); |
---|
203 | |
---|
204 | g_date_set_dmy(d,day,m,y); |
---|
205 | |
---|
206 | TEST("Set day is valid", g_date_valid(d)); |
---|
207 | |
---|
208 | if (m == G_DATE_JANUARY && day == 1) |
---|
209 | { |
---|
210 | first_day_of_year = g_date_julian(d); |
---|
211 | } |
---|
212 | |
---|
213 | g_assert(first_day_of_year != G_DATE_BAD_JULIAN); |
---|
214 | |
---|
215 | TEST("Date with DMY triplet is valid", g_date_valid(d)); |
---|
216 | TEST("Month accessor works", g_date_month(d) == m); |
---|
217 | TEST("Year accessor works", g_date_year(d) == y); |
---|
218 | TEST("Day of month accessor works", g_date_day(d) == day); |
---|
219 | |
---|
220 | TEST("Day of year is consistent with Julian dates", |
---|
221 | ((g_date_julian(d) + 1 - first_day_of_year) == |
---|
222 | (g_date_day_of_year(d)))); |
---|
223 | |
---|
224 | if (failed) |
---|
225 | { |
---|
226 | g_print("first day: %u this day: %u day of year: %u\n", |
---|
227 | first_day_of_year, |
---|
228 | g_date_julian(d), |
---|
229 | g_date_day_of_year(d)); |
---|
230 | } |
---|
231 | |
---|
232 | if (m == G_DATE_DECEMBER && day == 31) |
---|
233 | { |
---|
234 | TEST("Last day of year equals number of days in year", |
---|
235 | g_date_day_of_year(d) == days_in_year); |
---|
236 | if (failed) |
---|
237 | { |
---|
238 | g_print("last day: %u days in year: %u\n", |
---|
239 | g_date_day_of_year(d), days_in_year); |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | TEST("Day of year is not more than number of days in the year", |
---|
244 | g_date_day_of_year(d) <= days_in_year); |
---|
245 | |
---|
246 | TEST("Monday week of year is not more than number of weeks in the year", |
---|
247 | g_date_monday_week_of_year(d) <= monday_weeks_in_year); |
---|
248 | if (failed) |
---|
249 | { |
---|
250 | g_print("Weeks in year: %u\n", monday_weeks_in_year); |
---|
251 | g_date_debug_print(d); |
---|
252 | } |
---|
253 | TEST("Monday week of year is >= than last week of year", |
---|
254 | g_date_monday_week_of_year(d) >= monday_week_of_year); |
---|
255 | |
---|
256 | if (g_date_weekday(d) == G_DATE_MONDAY) |
---|
257 | { |
---|
258 | |
---|
259 | TEST("Monday week of year on Monday 1 more than previous day's week of year", |
---|
260 | (g_date_monday_week_of_year(d) - monday_week_of_year) == 1); |
---|
261 | } |
---|
262 | else |
---|
263 | { |
---|
264 | TEST("Monday week of year on non-Monday 0 more than previous day's week of year", |
---|
265 | (g_date_monday_week_of_year(d) - monday_week_of_year) == 0); |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | monday_week_of_year = g_date_monday_week_of_year(d); |
---|
270 | |
---|
271 | |
---|
272 | TEST("Sunday week of year is not more than number of weeks in the year", |
---|
273 | g_date_sunday_week_of_year(d) <= sunday_weeks_in_year); |
---|
274 | if (failed) |
---|
275 | { |
---|
276 | g_date_debug_print(d); |
---|
277 | } |
---|
278 | TEST("Sunday week of year is >= than last week of year", |
---|
279 | g_date_sunday_week_of_year(d) >= sunday_week_of_year); |
---|
280 | |
---|
281 | if (g_date_weekday(d) == G_DATE_SUNDAY) |
---|
282 | { |
---|
283 | TEST("Sunday week of year on Sunday 1 more than previous day's week of year", |
---|
284 | (g_date_sunday_week_of_year(d) - sunday_week_of_year) == 1); |
---|
285 | } |
---|
286 | else |
---|
287 | { |
---|
288 | TEST("Sunday week of year on non-Sunday 0 more than previous day's week of year", |
---|
289 | (g_date_sunday_week_of_year(d) - sunday_week_of_year) == 0); |
---|
290 | } |
---|
291 | |
---|
292 | sunday_week_of_year = g_date_sunday_week_of_year(d); |
---|
293 | |
---|
294 | TEST("Date is equal to itself", |
---|
295 | g_date_compare(d,d) == 0); |
---|
296 | |
---|
297 | |
---|
298 | /*************** Increments ***********/ |
---|
299 | |
---|
300 | i = 1; |
---|
301 | while (i < 402) /* Need to get 400 year increments in */ |
---|
302 | { |
---|
303 | |
---|
304 | /***** Days ******/ |
---|
305 | tmp = *d; |
---|
306 | g_date_add_days(d, i); |
---|
307 | |
---|
308 | TEST("Adding days gives a value greater than previous", |
---|
309 | g_date_compare(d, &tmp) > 0); |
---|
310 | |
---|
311 | g_date_subtract_days(d, i); |
---|
312 | TEST("Forward days then backward days returns us to current day", |
---|
313 | g_date_day(d) == day); |
---|
314 | |
---|
315 | if (failed) |
---|
316 | { |
---|
317 | g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); |
---|
318 | g_date_debug_print(d); |
---|
319 | } |
---|
320 | |
---|
321 | TEST("Forward days then backward days returns us to current month", |
---|
322 | g_date_month(d) == m); |
---|
323 | |
---|
324 | if (failed) |
---|
325 | { |
---|
326 | g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); |
---|
327 | g_date_debug_print(d); |
---|
328 | } |
---|
329 | |
---|
330 | TEST("Forward days then backward days returns us to current year", |
---|
331 | g_date_year(d) == y); |
---|
332 | |
---|
333 | if (failed) |
---|
334 | { |
---|
335 | g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); |
---|
336 | g_date_debug_print(d); |
---|
337 | } |
---|
338 | |
---|
339 | /******* Months ********/ |
---|
340 | |
---|
341 | tmp = *d; |
---|
342 | g_date_add_months(d, i); |
---|
343 | TEST("Adding months gives a larger value", |
---|
344 | g_date_compare(d, &tmp) > 0); |
---|
345 | g_date_subtract_months(d, i); |
---|
346 | |
---|
347 | TEST("Forward months then backward months returns us to current month", |
---|
348 | g_date_month(d) == m); |
---|
349 | |
---|
350 | if (failed) |
---|
351 | { |
---|
352 | g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); |
---|
353 | g_date_debug_print(d); |
---|
354 | } |
---|
355 | |
---|
356 | TEST("Forward months then backward months returns us to current year", |
---|
357 | g_date_year(d) == y); |
---|
358 | |
---|
359 | if (failed) |
---|
360 | { |
---|
361 | g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); |
---|
362 | g_date_debug_print(d); |
---|
363 | } |
---|
364 | |
---|
365 | |
---|
366 | if (day < 29) |
---|
367 | { |
---|
368 | /* Day should be unchanged */ |
---|
369 | |
---|
370 | TEST("Forward months then backward months returns us to current day", |
---|
371 | g_date_day(d) == day); |
---|
372 | |
---|
373 | if (failed) |
---|
374 | { |
---|
375 | g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); |
---|
376 | g_date_debug_print(d); |
---|
377 | } |
---|
378 | } |
---|
379 | else |
---|
380 | { |
---|
381 | /* reset the day for later tests */ |
---|
382 | g_date_set_day(d, day); |
---|
383 | } |
---|
384 | |
---|
385 | /******* Years ********/ |
---|
386 | |
---|
387 | tmp = *d; |
---|
388 | g_date_add_years(d, i); |
---|
389 | |
---|
390 | TEST("Adding years gives a larger value", |
---|
391 | g_date_compare(d,&tmp) > 0); |
---|
392 | |
---|
393 | g_date_subtract_years(d, i); |
---|
394 | |
---|
395 | TEST("Forward years then backward years returns us to current month", |
---|
396 | g_date_month(d) == m); |
---|
397 | |
---|
398 | if (failed) |
---|
399 | { |
---|
400 | g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); |
---|
401 | g_date_debug_print(d); |
---|
402 | } |
---|
403 | |
---|
404 | TEST("Forward years then backward years returns us to current year", |
---|
405 | g_date_year(d) == y); |
---|
406 | |
---|
407 | if (failed) |
---|
408 | { |
---|
409 | g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); |
---|
410 | g_date_debug_print(d); |
---|
411 | } |
---|
412 | |
---|
413 | if (m != 2 && day != 29) |
---|
414 | { |
---|
415 | TEST("Forward years then backward years returns us to current day", |
---|
416 | g_date_day(d) == day); |
---|
417 | |
---|
418 | if (failed) |
---|
419 | { |
---|
420 | g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); |
---|
421 | g_date_debug_print(d); |
---|
422 | } |
---|
423 | } |
---|
424 | else |
---|
425 | { |
---|
426 | g_date_set_day(d, day); /* reset */ |
---|
427 | } |
---|
428 | |
---|
429 | i += 10; |
---|
430 | } |
---|
431 | |
---|
432 | /***** increment test relative to our local Julian count */ |
---|
433 | |
---|
434 | if (!discontinuity) { |
---|
435 | |
---|
436 | /* We can only run sequence tests between sequential years */ |
---|
437 | |
---|
438 | TEST("Julians are sequential with increment 1", |
---|
439 | j+1 == g_date_julian(d)); |
---|
440 | if (failed) |
---|
441 | { |
---|
442 | g_print("Out of sequence, prev: %u expected: %u got: %u\n", |
---|
443 | j, j+1, g_date_julian(d)); |
---|
444 | } |
---|
445 | |
---|
446 | g_date_add_days(d,1); |
---|
447 | TEST("Next day has julian 1 higher", |
---|
448 | g_date_julian(d) == j + 2); |
---|
449 | g_date_subtract_days(d, 1); |
---|
450 | |
---|
451 | if (j != G_DATE_BAD_JULIAN) |
---|
452 | { |
---|
453 | g_date_subtract_days(d, 1); |
---|
454 | |
---|
455 | TEST("Previous day has julian 1 lower", |
---|
456 | g_date_julian(d) == j); |
---|
457 | |
---|
458 | g_date_add_days(d, 1); /* back to original */ |
---|
459 | } |
---|
460 | } |
---|
461 | discontinuity = FALSE; /* goes away now */ |
---|
462 | |
---|
463 | fflush(stdout); |
---|
464 | fflush(stderr); |
---|
465 | |
---|
466 | j = g_date_julian(d); /* inc current julian */ |
---|
467 | |
---|
468 | ++day; |
---|
469 | } |
---|
470 | ++m; |
---|
471 | } |
---|
472 | g_print(" done\n"); |
---|
473 | ++i; |
---|
474 | prev_y = y; |
---|
475 | y = check_years[i]; |
---|
476 | if (prev_y == G_DATE_BAD_YEAR || |
---|
477 | (prev_y + 1) != y) discontinuity = TRUE; |
---|
478 | } |
---|
479 | |
---|
480 | |
---|
481 | g_print("\n%u tests passed, %u failed\n",passed, notpassed); |
---|
482 | |
---|
483 | return 0; |
---|
484 | } |
---|
485 | |
---|
486 | |
---|