Increase the timeout for some GLib tests
[glib.git] / glib / tests / date.c
blob4c25852817dc0d654da18c324c44c752072f2399
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
4 /* We are testing some deprecated APIs here */
5 #define GLIB_DISABLE_DEPRECATION_WARNINGS
7 #include "glib.h"
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <locale.h>
13 #include <time.h>
15 #ifdef G_OS_WIN32
16 #define WIN32_LEAN_AND_MEAN
17 #include <windows.h>
18 #endif
20 static void
21 test_basic (void)
23 g_assert_cmpint (sizeof (GDate), <, 9);
24 g_assert (!g_date_valid_month (G_DATE_BAD_MONTH));
25 g_assert (!g_date_valid_month (13));
26 g_assert (!g_date_valid_day (G_DATE_BAD_DAY));
27 g_assert (!g_date_valid_day (32));
28 g_assert (!g_date_valid_year (G_DATE_BAD_YEAR));
29 g_assert (!g_date_valid_julian (G_DATE_BAD_JULIAN));
30 g_assert (!g_date_valid_weekday (G_DATE_BAD_WEEKDAY));
31 g_assert (g_date_is_leap_year (2000));
32 g_assert (!g_date_is_leap_year (1999));
33 g_assert (g_date_is_leap_year (1996));
34 g_assert (g_date_is_leap_year (1600));
35 g_assert (!g_date_is_leap_year (2100));
36 g_assert (!g_date_is_leap_year (1800));
39 static void
40 test_empty_constructor (void)
42 GDate *d;
44 d = g_date_new ();
45 g_assert (!g_date_valid (d));
46 g_date_free (d);
49 static void
50 test_dmy_constructor (void)
52 GDate *d;
53 guint32 j;
55 d = g_date_new_dmy (1, 1, 1);
56 g_assert (g_date_valid (d));
57 j = g_date_get_julian (d);
58 g_assert_cmpint (j, ==, 1);
59 g_assert_cmpint (g_date_get_month (d), ==, G_DATE_JANUARY);
60 g_assert_cmpint (g_date_get_day (d), ==, 1);
61 g_assert_cmpint (g_date_get_year (d), ==, 1);
62 g_date_free (d);
65 static void
66 test_julian_constructor (void)
68 GDate *d1;
69 GDate *d2;
71 d1 = g_date_new_julian (4000);
72 d2 = g_date_new_julian (5000);
73 g_assert_cmpint (g_date_get_julian (d1), ==, 4000);
74 g_assert_cmpint (g_date_days_between (d1, d2), ==, 1000);
75 g_assert_cmpint (g_date_get_year (d1), ==, 11);
76 g_assert_cmpint (g_date_get_day (d2), ==, 9);
77 g_date_free (d1);
78 g_date_free (d2);
81 static void
82 test_dates (void)
84 GDate *d;
85 GTimeVal tv;
87 d = g_date_new ();
89 /* today */
90 g_date_set_time (d, time (NULL));
91 g_assert (g_date_valid (d));
93 /* Unix epoch */
94 g_date_set_time (d, 1);
95 g_assert (g_date_valid (d));
97 tv.tv_sec = 0;
98 tv.tv_usec = 0;
99 g_date_set_time_val (d, &tv);
100 g_assert (g_date_valid (d));
102 /* Julian day 1 */
103 g_date_set_julian (d, 1);
104 g_assert (g_date_valid (d));
106 g_date_set_year (d, 3);
107 g_date_set_day (d, 3);
108 g_date_set_month (d, 3);
109 g_assert (g_date_valid (d));
110 g_assert_cmpint (g_date_get_year (d), ==, 3);
111 g_assert_cmpint (g_date_get_month (d), ==, 3);
112 g_assert_cmpint (g_date_get_day (d), ==, 3);
113 g_assert (!g_date_is_first_of_month (d));
114 g_assert (!g_date_is_last_of_month (d));
115 g_date_set_day (d, 1);
116 g_assert (g_date_is_first_of_month (d));
117 g_date_subtract_days (d, 1);
118 g_assert (g_date_is_last_of_month (d));
120 g_date_free (d);
123 static void
124 test_parse (void)
126 GDate *d;
127 gchar buf[101];
129 d = g_date_new ();
131 g_date_set_dmy (d, 10, 1, 2000);
132 g_date_strftime (buf, 100, "%x", d);
134 g_date_set_parse (d, buf);
135 g_assert (g_date_valid (d));
136 g_assert_cmpint (g_date_get_month (d), ==, 1);
137 g_assert_cmpint (g_date_get_day (d), ==, 10);
138 g_assert_cmpint (g_date_get_year (d), ==, 2000);
140 g_date_set_parse (d, "2001 10 1");
141 g_assert (g_date_valid (d));
142 g_assert_cmpint (g_date_get_month (d), ==, 10);
143 g_assert_cmpint (g_date_get_day (d), ==, 1);
144 g_assert_cmpint (g_date_get_year (d), ==, 2001);
146 g_date_set_parse (d, "2001 10");
147 g_assert (!g_date_valid (d));
149 g_date_set_parse (d, "2001 10 1 1");
150 g_assert (!g_date_valid (d));
152 g_date_set_parse (d, "March 1999");
153 g_assert (g_date_valid (d));
154 g_assert_cmpint (g_date_get_month (d), ==, 3);
155 g_assert_cmpint (g_date_get_day (d), ==, 1);
156 g_assert_cmpint (g_date_get_year (d), ==, 1999);
158 g_date_set_parse (d, "10 Sep 1087");
159 g_assert (g_date_valid (d));
160 g_assert_cmpint (g_date_get_month (d), ==, 9);
161 g_assert_cmpint (g_date_get_day (d), ==, 10);
162 g_assert_cmpint (g_date_get_year (d), ==, 1087);
164 g_date_set_parse (d, "19990301");
165 g_assert (g_date_valid (d));
166 g_assert_cmpint (g_date_get_month (d), ==, 3);
167 g_assert_cmpint (g_date_get_day (d), ==, 1);
168 g_assert_cmpint (g_date_get_year (d), ==, 1999);
170 g_date_set_parse (d, "20011320");
171 g_assert (!g_date_valid (d));
173 g_date_free (d);
176 static void
177 test_month_names (void)
179 #if defined(HAVE_LANGINFO_ABALTMON) || defined(G_OS_WIN32)
180 GDate *gdate;
181 gchar buf[101];
182 gchar *oldlocale;
183 #ifdef G_OS_WIN32
184 LCID old_lcid;
185 #endif
186 #endif /* defined(HAVE_LANGINFO_ABALTMON) || defined(G_OS_WIN32) */
188 g_test_bug ("749206");
190 /* This test can only work (on non-Windows platforms) if libc supports the %OB
191 * (etc.) format placeholders. If it doesn’t, strftime() (and hence
192 * g_date_strftime()) will return the placeholder unsubstituted.
193 * g_date_strftime() explicitly documents that it doesn’t provide any more
194 * format placeholders than the system strftime(), so we should skip the test
195 * in that case. If people need %OB support, they should depend on a suitable
196 * version of libc, or use g_date_time_format(). */
197 #if !defined(HAVE_LANGINFO_ABALTMON) && !defined(G_OS_WIN32)
198 g_test_skip ("libc doesn’t support alternate month names");
199 #else
201 #define TEST_DATE(d,m,y,f,o) \
202 g_date_set_dmy (gdate, d, m, y); \
203 g_date_strftime (buf, 100, f, gdate); \
204 g_assert_cmpstr (buf, ==, (o)); \
205 g_date_set_parse (gdate, buf); \
206 g_assert (g_date_valid (gdate)); \
207 g_assert_cmpint (g_date_get_day (gdate), ==, d); \
208 g_assert_cmpint (g_date_get_month (gdate), ==, m); \
209 g_assert_cmpint (g_date_get_year (gdate), ==, y);
211 oldlocale = g_strdup (setlocale (LC_ALL, NULL));
212 #ifdef G_OS_WIN32
213 old_lcid = GetThreadLocale ();
214 #endif
216 gdate = g_date_new ();
218 /* Note: Windows implementation of g_date_strftime() does not support
219 * "-" format modifier (e.g., "%-d", "%-e") so we will not use it.
222 /* Make sure that nothing has been changed in western European languages. */
223 setlocale (LC_ALL, "en_GB.utf-8");
224 #ifdef G_OS_WIN32
225 SetThreadLocale (MAKELCID (MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_UK), SORT_DEFAULT));
226 #endif
227 if (strstr (setlocale (LC_ALL, NULL), "en_GB") != NULL)
229 TEST_DATE (1, 1, 2018, "%B %d, %Y", "January 01, 2018");
230 TEST_DATE (1, 2, 2018, "%OB %Y", "February 2018");
231 TEST_DATE (1, 3, 2018, "%e %b %Y", " 1 Mar 2018");
232 TEST_DATE (1, 4, 2018, "%Ob %Y", "Apr 2018");
233 TEST_DATE (1, 5, 2018, "%d %h %Y", "01 May 2018");
234 TEST_DATE (1, 6, 2018, "%Oh %Y", "Jun 2018");
236 else
237 g_test_incomplete ("locale en_GB not available, skipping English month names test");
239 setlocale (LC_ALL, "de_DE.utf-8");
240 #ifdef G_OS_WIN32
241 SetThreadLocale (MAKELCID (MAKELANGID (LANG_GERMAN, SUBLANG_GERMAN), SORT_DEFAULT));
242 #endif
243 if (strstr (setlocale (LC_ALL, NULL), "de_DE") != NULL)
245 TEST_DATE (16, 7, 2018, "%d. %B %Y", "16. Juli 2018");
246 TEST_DATE ( 1, 8, 2018, "%OB %Y", "August 2018");
247 TEST_DATE (18, 9, 2018, "%e. %b %Y", "18. Sep 2018");
248 TEST_DATE ( 1, 10, 2018, "%Ob %Y", "Okt 2018");
249 TEST_DATE (20, 11, 2018, "%d. %h %Y", "20. Nov 2018");
250 TEST_DATE ( 1, 12, 2018, "%Oh %Y", "Dez 2018");
252 else
253 g_test_incomplete ("locale de_DE not available, skipping German month names test");
256 setlocale (LC_ALL, "es_ES.utf-8");
257 #ifdef G_OS_WIN32
258 SetThreadLocale (MAKELCID (MAKELANGID (LANG_SPANISH, SUBLANG_SPANISH_MODERN), SORT_DEFAULT));
259 #endif
260 if (strstr (setlocale (LC_ALL, NULL), "es_ES") != NULL)
262 TEST_DATE ( 9, 1, 2018, "%d de %B de %Y", "09 de enero de 2018");
263 TEST_DATE ( 1, 2, 2018, "%OB de %Y", "febrero de 2018");
264 TEST_DATE (10, 3, 2018, "%e de %b de %Y", "10 de mar de 2018");
265 TEST_DATE ( 1, 4, 2018, "%Ob de %Y", "abr de 2018");
266 TEST_DATE (11, 5, 2018, "%d de %h de %Y", "11 de may de 2018");
267 TEST_DATE ( 1, 6, 2018, "%Oh de %Y", "jun de 2018");
269 else
270 g_test_incomplete ("locale es_ES not available, skipping Spanish month names test");
272 setlocale (LC_ALL, "fr_FR.utf-8");
273 #ifdef G_OS_WIN32
274 SetThreadLocale (MAKELCID (MAKELANGID (LANG_FRENCH, SUBLANG_FRENCH), SORT_DEFAULT));
275 #endif
276 if (strstr (setlocale (LC_ALL, NULL), "fr_FR") != NULL)
278 TEST_DATE (31, 7, 2018, "%d %B %Y", "31 juillet 2018");
279 TEST_DATE ( 1, 8, 2018, "%OB %Y", "août 2018");
280 TEST_DATE (30, 9, 2018, "%e %b %Y", "30 sept. 2018");
281 TEST_DATE ( 1, 10, 2018, "%Ob %Y", "oct. 2018");
282 TEST_DATE (29, 11, 2018, "%d %h %Y", "29 nov. 2018");
283 TEST_DATE ( 1, 12, 2018, "%Oh %Y", "déc. 2018");
285 else
286 g_test_incomplete ("locale fr_FR not available, skipping French month names test");
288 /* Make sure that there are visible changes in some European languages. */
289 setlocale (LC_ALL, "el_GR.utf-8");
290 #ifdef G_OS_WIN32
291 SetThreadLocale (MAKELCID (MAKELANGID (LANG_GREEK, SUBLANG_GREEK_GREECE), SORT_DEFAULT));
292 #endif
293 if (strstr (setlocale (LC_ALL, NULL), "el_GR") != NULL)
295 TEST_DATE ( 2, 1, 2018, "%d %B %Y", "02 Ιανουαρίου 2018");
296 TEST_DATE ( 4, 2, 2018, "%e %B %Y", " 4 Φεβρουαρίου 2018");
297 TEST_DATE (15, 3, 2018, "%d %B %Y", "15 Μαρτίου 2018");
298 TEST_DATE ( 1, 4, 2018, "%OB %Y", "Απρίλιος 2018");
299 TEST_DATE ( 1, 5, 2018, "%OB %Y", "Μάιος 2018");
300 TEST_DATE ( 1, 6, 2018, "%OB %Y", "Ιούνιος 2018");
301 TEST_DATE (16, 7, 2018, "%e %b %Y", "16 Ιούλ 2018");
302 TEST_DATE ( 1, 8, 2018, "%Ob %Y", "Αύγ 2018");
304 else
305 g_test_incomplete ("locale el_GR not available, skipping Greek month names test");
307 setlocale (LC_ALL, "hr_HR.utf-8");
308 #ifdef G_OS_WIN32
309 SetThreadLocale (MAKELCID (MAKELANGID (LANG_CROATIAN, SUBLANG_CROATIAN_CROATIA), SORT_DEFAULT));
310 #endif
311 if (strstr (setlocale (LC_ALL, NULL), "hr_HR") != NULL)
313 TEST_DATE ( 8, 5, 2018, "%d. %B %Y", "08. svibnja 2018");
314 TEST_DATE ( 9, 6, 2018, "%e. %B %Y", " 9. lipnja 2018");
315 TEST_DATE (10, 7, 2018, "%d. %B %Y", "10. srpnja 2018");
316 TEST_DATE ( 1, 8, 2018, "%OB %Y", "Kolovoz 2018");
317 TEST_DATE ( 1, 9, 2018, "%OB %Y", "Rujan 2018");
318 TEST_DATE ( 1, 10, 2018, "%OB %Y", "Listopad 2018");
319 TEST_DATE (11, 11, 2018, "%e. %b %Y", "11. Stu 2018");
320 TEST_DATE ( 1, 12, 2018, "%Ob %Y", "Pro 2018");
322 else
323 g_test_incomplete ("locale hr_HR not available, skipping Croatian month names test");
325 setlocale (LC_ALL, "lt_LT.utf-8");
326 #ifdef G_OS_WIN32
327 SetThreadLocale (MAKELCID (MAKELANGID (LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA), SORT_DEFAULT));
328 #endif
329 if (strstr (setlocale (LC_ALL, NULL), "lt_LT") != NULL)
331 TEST_DATE ( 1, 1, 2018, "%Y m. %B %d d.", "2018 m. sausio 01 d.");
332 TEST_DATE ( 2, 2, 2018, "%Y m. %B %e d.", "2018 m. vasario 2 d.");
333 TEST_DATE ( 3, 3, 2018, "%Y m. %B %d d.", "2018 m. kovo 03 d.");
334 TEST_DATE ( 1, 4, 2018, "%Y m. %OB", "2018 m. balandis");
335 TEST_DATE ( 1, 5, 2018, "%Y m. %OB", "2018 m. gegužė");
336 TEST_DATE ( 1, 6, 2018, "%Y m. %OB", "2018 m. birželis");
337 TEST_DATE (17, 7, 2018, "%Y m. %b %e d.", "2018 m. Lie 17 d.");
338 TEST_DATE ( 1, 8, 2018, "%Y m. %Ob", "2018 m. Rgp");
340 else
341 g_test_incomplete ("locale lt_LT not available, skipping Lithuanian month names test");
343 setlocale (LC_ALL, "pl_PL.utf-8");
344 #ifdef G_OS_WIN32
345 SetThreadLocale (MAKELCID (MAKELANGID (LANG_POLISH, SUBLANG_POLISH_POLAND), SORT_DEFAULT));
346 #endif
347 if (strstr (setlocale (LC_ALL, NULL), "pl_PL") != NULL)
349 TEST_DATE ( 3, 5, 2018, "%d %B %Y", "03 maja 2018");
350 TEST_DATE ( 4, 6, 2018, "%e %B %Y", " 4 czerwca 2018");
351 TEST_DATE (20, 7, 2018, "%d %B %Y", "20 lipca 2018");
352 TEST_DATE ( 1, 8, 2018, "%OB %Y", "sierpień 2018");
353 TEST_DATE ( 1, 9, 2018, "%OB %Y", "wrzesień 2018");
354 TEST_DATE ( 1, 10, 2018, "%OB %Y", "październik 2018");
355 TEST_DATE (25, 11, 2018, "%e %b %Y", "25 lis 2018");
356 TEST_DATE ( 1, 12, 2018, "%Ob %Y", "gru 2018");
358 else
359 g_test_incomplete ("locale pl_PL not available, skipping Polish month names test");
361 setlocale (LC_ALL, "ru_RU.utf-8");
362 #ifdef G_OS_WIN32
363 SetThreadLocale (MAKELCID (MAKELANGID (LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA), SORT_DEFAULT));
364 #endif
365 if (strstr (setlocale (LC_ALL, NULL), "ru_RU") != NULL)
367 TEST_DATE ( 3, 1, 2018, "%d %B %Y", "03 января 2018");
368 TEST_DATE ( 4, 2, 2018, "%e %B %Y", " 4 февраля 2018");
369 TEST_DATE (23, 3, 2018, "%d %B %Y", "23 марта 2018");
370 TEST_DATE ( 1, 4, 2018, "%OB %Y", "Апрель 2018");
371 TEST_DATE ( 1, 5, 2018, "%OB %Y", "Май 2018");
372 TEST_DATE ( 1, 6, 2018, "%OB %Y", "Июнь 2018");
373 TEST_DATE (24, 7, 2018, "%e %b %Y", "24 июл 2018");
374 TEST_DATE ( 1, 8, 2018, "%Ob %Y", "авг 2018");
375 /* This difference is very important in Russian: */
376 TEST_DATE (19, 5, 2018, "%e %b %Y", "19 мая 2018");
377 TEST_DATE (20, 5, 2018, "%Ob, %d-е, %Y", "май, 20-е, 2018");
379 else
380 g_test_incomplete ("locale ru_RU not available, skipping Russian month names test");
382 g_date_free (gdate);
384 setlocale (LC_ALL, oldlocale);
385 #ifdef G_OS_WIN32
386 SetThreadLocale (old_lcid);
387 #endif
388 g_free (oldlocale);
389 #endif /* defined(HAVE_LANGINFO_ABALTMON) || defined(G_OS_WIN32) */
392 static void
393 test_year (gconstpointer t)
395 GDateYear y = GPOINTER_TO_INT (t);
396 GDateMonth m;
397 GDateDay day;
398 guint32 j;
399 GDate *d;
400 gint i;
401 GDate tmp;
403 guint32 first_day_of_year = G_DATE_BAD_JULIAN;
404 guint16 days_in_year = g_date_is_leap_year (y) ? 366 : 365;
405 guint sunday_week_of_year = 0;
406 guint sunday_weeks_in_year = g_date_get_sunday_weeks_in_year (y);
407 guint monday_week_of_year = 0;
408 guint monday_weeks_in_year = g_date_get_monday_weeks_in_year (y);
409 guint iso8601_week_of_year = 0;
411 g_assert (g_date_valid_year (y));
412 /* Years ought to have roundabout 52 weeks */
413 g_assert (sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53);
414 g_assert (monday_weeks_in_year == 52 || monday_weeks_in_year == 53);
416 m = 1;
417 while (m < 13)
419 guint8 dim = g_date_get_days_in_month (m, y);
420 GDate days[31];
422 g_date_clear (days, 31);
424 g_assert (dim > 0 && dim < 32);
425 g_assert (g_date_valid_month (m));
427 day = 1;
428 while (day <= dim)
430 g_assert (g_date_valid_dmy (day, m, y));
432 d = &days[day - 1];
433 //g_assert (!g_date_valid (d));
435 g_date_set_dmy (d, day, m, y);
437 g_assert (g_date_valid (d));
439 if (m == G_DATE_JANUARY && day == 1)
440 first_day_of_year = g_date_get_julian (d);
442 g_assert (first_day_of_year != G_DATE_BAD_JULIAN);
444 g_assert_cmpint (g_date_get_month (d), ==, m);
445 g_assert_cmpint (g_date_get_year (d), ==, y);
446 g_assert_cmpint (g_date_get_day (d), ==, day);
448 g_assert (g_date_get_julian (d) + 1 - first_day_of_year ==
449 g_date_get_day_of_year (d));
451 if (m == G_DATE_DECEMBER && day == 31)
452 g_assert_cmpint (g_date_get_day_of_year (d), ==, days_in_year);
454 g_assert_cmpint (g_date_get_day_of_year (d), <=, days_in_year);
455 g_assert_cmpint (g_date_get_monday_week_of_year (d), <=, monday_weeks_in_year);
456 g_assert_cmpint (g_date_get_monday_week_of_year (d), >=, monday_week_of_year);
458 if (g_date_get_weekday(d) == G_DATE_MONDAY)
460 g_assert_cmpint (g_date_get_monday_week_of_year (d) - monday_week_of_year, ==, 1);
461 if ((m == G_DATE_JANUARY && day <= 4) ||
462 (m == G_DATE_DECEMBER && day >= 29))
463 g_assert_cmpint (g_date_get_iso8601_week_of_year (d), ==, 1);
464 else
465 g_assert_cmpint (g_date_get_iso8601_week_of_year (d) - iso8601_week_of_year, ==, 1);
467 else
469 g_assert_cmpint (g_date_get_monday_week_of_year(d) - monday_week_of_year, ==, 0);
470 if (!(day == 1 && m == G_DATE_JANUARY))
471 g_assert_cmpint (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year, ==, 0);
474 monday_week_of_year = g_date_get_monday_week_of_year (d);
475 iso8601_week_of_year = g_date_get_iso8601_week_of_year (d);
477 g_assert_cmpint (g_date_get_sunday_week_of_year (d), <=, sunday_weeks_in_year);
478 g_assert_cmpint (g_date_get_sunday_week_of_year (d), >=, sunday_week_of_year);
479 if (g_date_get_weekday(d) == G_DATE_SUNDAY)
480 g_assert_cmpint (g_date_get_sunday_week_of_year (d) - sunday_week_of_year, ==, 1);
481 else
482 g_assert_cmpint (g_date_get_sunday_week_of_year (d) - sunday_week_of_year, ==, 0);
484 sunday_week_of_year = g_date_get_sunday_week_of_year (d);
486 g_assert_cmpint (g_date_compare (d, d), ==, 0);
488 i = 1;
489 while (i < 402) /* Need to get 400 year increments in */
491 tmp = *d;
492 g_date_add_days (d, i);
493 g_assert_cmpint (g_date_compare (d, &tmp), >, 0);
494 g_date_subtract_days (d, i);
495 g_assert_cmpint (g_date_get_day (d), ==, day);
496 g_assert_cmpint (g_date_get_month (d), ==, m);
497 g_assert_cmpint (g_date_get_year (d), ==, y);
499 tmp = *d;
500 g_date_add_months (d, i);
501 g_assert_cmpint (g_date_compare (d, &tmp), >, 0);
502 g_date_subtract_months (d, i);
503 g_assert_cmpint (g_date_get_month (d), ==, m);
504 g_assert_cmpint (g_date_get_year (d), ==, y);
506 if (day < 29)
507 g_assert_cmpint (g_date_get_day (d), ==, day);
508 else
509 g_date_set_day (d, day);
511 tmp = *d;
512 g_date_add_years (d, i);
513 g_assert_cmpint (g_date_compare (d, &tmp), >, 0);
514 g_date_subtract_years (d, i);
515 g_assert_cmpint (g_date_get_month (d), ==, m);
516 g_assert_cmpint (g_date_get_year (d), ==, y);
518 if (m != 2 && day != 29)
519 g_assert_cmpint (g_date_get_day (d), ==, day);
520 else
521 g_date_set_day (d, day); /* reset */
523 i += 10;
526 j = g_date_get_julian (d);
528 ++day;
530 ++m;
533 /* at this point, d is the last day of year y */
534 g_date_set_dmy (&tmp, 1, 1, y + 1);
535 g_assert_cmpint (j + 1, ==, g_date_get_julian (&tmp));
537 g_date_add_days (&tmp, 1);
538 g_assert_cmpint (j + 2, ==, g_date_get_julian (&tmp));
541 static void
542 test_clamp (void)
544 GDate d1, d2, d, o;
546 g_date_set_dmy (&d1, 1, 1, 1970);
547 g_date_set_dmy (&d2, 1, 1, 1980);
548 g_date_set_dmy (&d, 1, 1, 1);
550 o = d;
551 g_date_clamp (&o, NULL, NULL);
552 g_assert (g_date_compare (&o, &d) == 0);
554 g_date_clamp (&o, &d1, &d2);
555 g_assert (g_date_compare (&o, &d1) == 0);
557 g_date_set_dmy (&o, 1, 1, 2000);
559 g_date_clamp (&o, &d1, &d2);
560 g_assert (g_date_compare (&o, &d2) == 0);
563 static void
564 test_order (void)
566 GDate d1, d2;
568 g_date_set_dmy (&d1, 1, 1, 1970);
569 g_date_set_dmy (&d2, 1, 1, 1980);
571 g_assert (g_date_compare (&d1, &d2) == -1);
572 g_date_order (&d2, &d1);
573 g_assert (g_date_compare (&d1, &d2) == 1);
576 static void
577 test_copy (void)
579 GDate *d;
580 GDate *c;
582 d = g_date_new ();
583 g_assert_false (g_date_valid (d));
585 c = g_date_copy (d);
586 g_assert_nonnull (c);
587 g_assert_false (g_date_valid (c));
588 g_date_free (c);
590 g_date_set_day (d, 10);
592 c = g_date_copy (d);
593 g_date_set_month (c, 1);
594 g_date_set_year (c, 2015);
595 g_assert_true (g_date_valid (c));
596 g_assert_cmpuint (g_date_get_day (c), ==, 10);
597 g_date_free (c);
599 g_date_free (d);
603 main (int argc, char** argv)
605 gchar *path;
606 gint i;
608 /* Try to get all the leap year cases. */
609 int check_years[] = {
610 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
611 11, 12, 13, 14, 98, 99, 100, 101, 102, 103, 397,
612 398, 399, 400, 401, 402, 403, 404, 405, 406,
613 1598, 1599, 1600, 1601, 1602, 1650, 1651,
614 1897, 1898, 1899, 1900, 1901, 1902, 1903,
615 1961, 1962, 1963, 1964, 1965, 1967,
616 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976,
617 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985,
618 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
619 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
620 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
621 3000, 3001, 3002, 3998, 3999, 4000, 4001, 4002, 4003
624 g_setenv ("LC_ALL", "en_US.utf-8", TRUE);
625 setlocale (LC_ALL, "");
626 #ifdef G_OS_WIN32
627 SetThreadLocale (MAKELCID (MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT));
628 #endif
630 g_test_init (&argc, &argv, NULL);
631 g_test_bug_base ("http://bugzilla.gnome.org/");
633 g_test_add_func ("/date/basic", test_basic);
634 g_test_add_func ("/date/empty", test_empty_constructor);
635 g_test_add_func ("/date/dmy", test_dmy_constructor);
636 g_test_add_func ("/date/julian", test_julian_constructor);
637 g_test_add_func ("/date/dates", test_dates);
638 g_test_add_func ("/date/parse", test_parse);
639 g_test_add_func ("/date/month_names", test_month_names);
640 g_test_add_func ("/date/clamp", test_clamp);
641 g_test_add_func ("/date/order", test_order);
642 for (i = 0; i < G_N_ELEMENTS (check_years); i++)
644 path = g_strdup_printf ("/date/year/%d", check_years[i]);
645 g_test_add_data_func (path, GINT_TO_POINTER(check_years[i]), test_year);
646 g_free (path);
648 g_test_add_func ("/date/copy", test_copy);
650 return g_test_run ();