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