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