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