Fix the description of g_string_assign. (#78728)
[glib.git] / tests / date-test.c
blobba405ea94ca3f6c77584c2f513a68236d3ba1ebe
2 #include "glib.h"
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
7 #include <locale.h>
8 #include <time.h>
10 gboolean failed = FALSE;
11 guint32 passed = 0;
12 guint32 notpassed = 0;
14 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
15 if (failed) \
16 exit(1); \
17 } G_STMT_END
19 void g_date_debug_print(GDate* d)
23 void g_print_dummy(const char *format, ...)
27 void fflush_dummy (FILE *f)
32 #define g_print g_print_dummy
33 #define fflush fflush_dummy
35 int main(int argc, char** argv)
37 GDate* d;
38 guint32 j;
39 GDateMonth m;
40 GDateYear y, prev_y;
41 GDateDay day;
42 gchar buf[101];
43 gchar* loc;
45 /* Try to get all the leap year cases. */
46 GDateYear check_years[] = {
47 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
48 11, 12, 13, 14, 98, 99, 100, 101, 102, 103, 397,
49 398, 399, 400, 401, 402, 403, 404, 405, 406,
50 1598, 1599, 1600, 1601, 1602, 1650, 1651,
51 1897, 1898, 1899, 1900, 1901, 1902, 1903,
52 1961, 1962, 1963, 1964, 1965, 1967,
53 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976,
54 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985,
55 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
56 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
57 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
58 3000, 3001, 3002, 3998, 3999, 4000, 4001, 4002, 4003
61 guint n_check_years = sizeof(check_years)/sizeof(GDateYear);
63 guint i = 0;
64 gboolean discontinuity = FALSE;
66 g_print("checking GDate...");
68 TEST("sizeof(GDate) is not more than 8 bytes on this platform", sizeof(GDate) < 9);
70 d = g_date_new();
72 TEST("Empty constructor produces invalid date", !g_date_valid(d));
74 g_date_free(d);
76 d = g_date_new_dmy(1,1,1);
78 TEST("January 1, Year 1 created and valid", g_date_valid(d));
80 j = g_date_get_julian(d);
82 TEST("January 1, Year 1 is Julian date 1", j == 1);
84 TEST("Returned month is January", g_date_get_month(d) == G_DATE_JANUARY);
85 TEST("Returned day is 1", g_date_get_day(d) == 1);
86 TEST("Returned year is 1", g_date_get_year(d) == 1);
88 TEST("Bad month is invalid", !g_date_valid_month(G_DATE_BAD_MONTH));
89 TEST("Month 13 is invalid", !g_date_valid_month(13));
90 TEST("Bad day is invalid", !g_date_valid_day(G_DATE_BAD_DAY));
91 TEST("Day 32 is invalid", !g_date_valid_day(32));
92 TEST("Bad year is invalid", !g_date_valid_year(G_DATE_BAD_YEAR));
93 TEST("Bad julian is invalid", !g_date_valid_julian(G_DATE_BAD_JULIAN));
94 TEST("Bad weekday is invalid", !g_date_valid_weekday(G_DATE_BAD_WEEKDAY));
95 TEST("Year 2000 is a leap year", g_date_is_leap_year(2000));
96 TEST("Year 1999 is not a leap year", !g_date_is_leap_year(1999));
97 TEST("Year 1996 is a leap year", g_date_is_leap_year(1996));
98 TEST("Year 1600 is a leap year", g_date_is_leap_year(1600));
99 TEST("Year 2100 is not a leap year", !g_date_is_leap_year(2100));
100 TEST("Year 1800 is not a leap year", !g_date_is_leap_year(1800));
102 g_date_free(d);
104 loc = setlocale(LC_ALL,"");
105 if (loc)
106 g_print("\nLocale set to %s\n", loc);
107 else
108 g_print("\nLocale unchanged\n");
110 d = g_date_new();
111 g_date_set_time(d, time(NULL));
112 TEST("Today is valid", g_date_valid(d));
114 g_date_strftime(buf,100,"Today is a %A, %x\n", d);
115 g_print("%s", buf);
117 g_date_set_time(d, 1);
118 TEST("Beginning of Unix epoch is valid", g_date_valid(d));
120 g_date_strftime(buf,100,"1 second into the Unix epoch it was a %A, in the month of %B, %x\n", d);
121 g_print("%s", buf);
123 g_date_set_julian(d, 1);
124 TEST("GDate's \"Julian\" epoch's first day is valid", g_date_valid(d));
126 g_date_strftime(buf,100,"Our \"Julian\" epoch begins on a %A, in the month of %B, %x\n",
128 g_print("%s", buf);
130 g_date_set_dmy(d, 10, 1, 2000);
132 g_date_strftime(buf,100,"%x", d);
134 g_date_set_parse(d, buf);
135 /* Note: this test will hopefully work, but no promises. */
136 TEST("Successfully parsed a %x-formatted string",
137 g_date_valid(d) &&
138 g_date_get_month(d) == 1 &&
139 g_date_get_day(d) == 10 &&
140 g_date_get_year(d) == 2000);
141 if (failed)
142 g_date_debug_print(d);
144 g_date_free(d);
146 j = G_DATE_BAD_JULIAN;
148 i = 0;
149 discontinuity = TRUE;
150 y = check_years[0];
151 prev_y = G_DATE_BAD_YEAR;
152 while (i < n_check_years)
154 guint32 first_day_of_year = G_DATE_BAD_JULIAN;
155 guint16 days_in_year = g_date_is_leap_year(y) ? 366 : 365;
156 guint sunday_week_of_year = 0;
157 guint sunday_weeks_in_year = g_date_get_sunday_weeks_in_year(y);
158 guint monday_week_of_year = 0;
159 guint monday_weeks_in_year = g_date_get_monday_weeks_in_year(y);
161 if (discontinuity)
162 g_print(" (Break in sequence of requested years to check)\n");
164 g_print("Checking year %u", y);
166 TEST("Year is valid", g_date_valid_year(y));
168 TEST("Number of Sunday weeks in year is 52 or 53",
169 sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53);
171 TEST("Number of Monday weeks in year is 52 or 53",
172 monday_weeks_in_year == 52 || monday_weeks_in_year == 53);
174 m = 1;
175 while (m < 13)
177 guint8 dim = g_date_get_days_in_month(m,y);
178 GDate days[31]; /* This is the fast way, no allocation */
180 TEST("Sensible number of days in month", (dim > 0 && dim < 32));
182 TEST("Month between 1 and 12 is valid", g_date_valid_month(m));
184 day = 1;
186 g_date_clear(days, 31);
188 while (day <= dim)
190 guint i;
191 GDate tmp;
193 TEST("DMY triplet is valid", g_date_valid_dmy(day,m,y));
195 /* Create GDate with triplet */
197 d = &days[day-1];
199 TEST("Cleared day is invalid", !g_date_valid(d));
201 g_date_set_dmy(d,day,m,y);
203 TEST("Set day is valid", g_date_valid(d));
205 if (m == G_DATE_JANUARY && day == 1)
207 first_day_of_year = g_date_get_julian(d);
210 g_assert(first_day_of_year != G_DATE_BAD_JULIAN);
212 TEST("Date with DMY triplet is valid", g_date_valid(d));
213 TEST("Month accessor works", g_date_get_month(d) == m);
214 TEST("Year accessor works", g_date_get_year(d) == y);
215 TEST("Day of month accessor works", g_date_get_day(d) == day);
217 TEST("Day of year is consistent with Julian dates",
218 ((g_date_get_julian(d) + 1 - first_day_of_year) ==
219 (g_date_get_day_of_year(d))));
221 if (failed)
223 g_print("first day: %u this day: %u day of year: %u\n",
224 first_day_of_year,
225 g_date_get_julian(d),
226 g_date_get_day_of_year(d));
229 if (m == G_DATE_DECEMBER && day == 31)
231 TEST("Last day of year equals number of days in year",
232 g_date_get_day_of_year(d) == days_in_year);
233 if (failed)
235 g_print("last day: %u days in year: %u\n",
236 g_date_get_day_of_year(d), days_in_year);
240 TEST("Day of year is not more than number of days in the year",
241 g_date_get_day_of_year(d) <= days_in_year);
243 TEST("Monday week of year is not more than number of weeks in the year",
244 g_date_get_monday_week_of_year(d) <= monday_weeks_in_year);
245 if (failed)
247 g_print("Weeks in year: %u\n", monday_weeks_in_year);
248 g_date_debug_print(d);
250 TEST("Monday week of year is >= than last week of year",
251 g_date_get_monday_week_of_year(d) >= monday_week_of_year);
253 if (g_date_get_weekday(d) == G_DATE_MONDAY)
256 TEST("Monday week of year on Monday 1 more than previous day's week of year",
257 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 1);
259 else
261 TEST("Monday week of year on non-Monday 0 more than previous day's week of year",
262 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 0);
266 monday_week_of_year = g_date_get_monday_week_of_year(d);
269 TEST("Sunday week of year is not more than number of weeks in the year",
270 g_date_get_sunday_week_of_year(d) <= sunday_weeks_in_year);
271 if (failed)
273 g_date_debug_print(d);
275 TEST("Sunday week of year is >= than last week of year",
276 g_date_get_sunday_week_of_year(d) >= sunday_week_of_year);
278 if (g_date_get_weekday(d) == G_DATE_SUNDAY)
280 TEST("Sunday week of year on Sunday 1 more than previous day's week of year",
281 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 1);
283 else
285 TEST("Sunday week of year on non-Sunday 0 more than previous day's week of year",
286 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 0);
289 sunday_week_of_year = g_date_get_sunday_week_of_year(d);
291 TEST("Date is equal to itself",
292 g_date_compare(d,d) == 0);
295 /*************** Increments ***********/
297 i = 1;
298 while (i < 402) /* Need to get 400 year increments in */
301 /***** Days ******/
302 tmp = *d;
303 g_date_add_days(d, i);
305 TEST("Adding days gives a value greater than previous",
306 g_date_compare(d, &tmp) > 0);
308 g_date_subtract_days(d, i);
309 TEST("Forward days then backward days returns us to current day",
310 g_date_get_day(d) == day);
312 if (failed)
314 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
315 g_date_debug_print(d);
318 TEST("Forward days then backward days returns us to current month",
319 g_date_get_month(d) == m);
321 if (failed)
323 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
324 g_date_debug_print(d);
327 TEST("Forward days then backward days returns us to current year",
328 g_date_get_year(d) == y);
330 if (failed)
332 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
333 g_date_debug_print(d);
336 /******* Months ********/
338 tmp = *d;
339 g_date_add_months(d, i);
340 TEST("Adding months gives a larger value",
341 g_date_compare(d, &tmp) > 0);
342 g_date_subtract_months(d, i);
344 TEST("Forward months then backward months returns us to current month",
345 g_date_get_month(d) == m);
347 if (failed)
349 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
350 g_date_debug_print(d);
353 TEST("Forward months then backward months returns us to current year",
354 g_date_get_year(d) == y);
356 if (failed)
358 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
359 g_date_debug_print(d);
363 if (day < 29)
365 /* Day should be unchanged */
367 TEST("Forward months then backward months returns us to current day",
368 g_date_get_day(d) == day);
370 if (failed)
372 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
373 g_date_debug_print(d);
376 else
378 /* reset the day for later tests */
379 g_date_set_day(d, day);
382 /******* Years ********/
384 tmp = *d;
385 g_date_add_years(d, i);
387 TEST("Adding years gives a larger value",
388 g_date_compare(d,&tmp) > 0);
390 g_date_subtract_years(d, i);
392 TEST("Forward years then backward years returns us to current month",
393 g_date_get_month(d) == m);
395 if (failed)
397 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
398 g_date_debug_print(d);
401 TEST("Forward years then backward years returns us to current year",
402 g_date_get_year(d) == y);
404 if (failed)
406 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
407 g_date_debug_print(d);
410 if (m != 2 && day != 29)
412 TEST("Forward years then backward years returns us to current day",
413 g_date_get_day(d) == day);
415 if (failed)
417 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
418 g_date_debug_print(d);
421 else
423 g_date_set_day(d, day); /* reset */
426 i += 10;
429 /***** increment test relative to our local Julian count */
431 if (!discontinuity) {
433 /* We can only run sequence tests between sequential years */
435 TEST("Julians are sequential with increment 1",
436 j+1 == g_date_get_julian(d));
437 if (failed)
439 g_print("Out of sequence, prev: %u expected: %u got: %u\n",
440 j, j+1, g_date_get_julian(d));
443 g_date_add_days(d,1);
444 TEST("Next day has julian 1 higher",
445 g_date_get_julian(d) == j + 2);
446 g_date_subtract_days(d, 1);
448 if (j != G_DATE_BAD_JULIAN)
450 g_date_subtract_days(d, 1);
452 TEST("Previous day has julian 1 lower",
453 g_date_get_julian(d) == j);
455 g_date_add_days(d, 1); /* back to original */
458 discontinuity = FALSE; /* goes away now */
460 fflush(stdout);
461 fflush(stderr);
463 j = g_date_get_julian(d); /* inc current julian */
465 ++day;
467 ++m;
469 g_print(" done\n");
470 ++i;
471 prev_y = y;
472 y = check_years[i];
473 if (prev_y == G_DATE_BAD_YEAR ||
474 (prev_y + 1) != y) discontinuity = TRUE;
478 g_print("\n%u tests passed, %u failed\n",passed, notpassed);
480 return 0;