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