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