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