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