Branch data Line data Source code
1 : : /* We are testing some deprecated APIs here */
2 : : #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
3 : : #define GLIB_DISABLE_DEPRECATION_WARNINGS
4 : : #endif
5 : :
6 : : #include "glib.h"
7 : :
8 : : #include <stdio.h>
9 : : #include <string.h>
10 : : #include <stdlib.h>
11 : : #include <locale.h>
12 : : #include <time.h>
13 : :
14 : : #ifdef G_OS_WIN32
15 : : #define WIN32_LEAN_AND_MEAN
16 : : #include <windows.h>
17 : : /* mingw defines it while msvc doesn't */
18 : : #ifndef SUBLANG_LITHUANIAN_LITHUANIA
19 : : #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
20 : : #endif
21 : : #endif
22 : :
23 : : static void
24 : 1 : test_basic (void)
25 : : {
26 : : GDate *d;
27 : 1 : struct tm tm = { 0 };
28 : :
29 : : /* g_date_valid (d) */
30 [ + - ]: 1 : if (g_test_undefined ())
31 : : {
32 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
33 : : "*assertion *failed*");
34 : 1 : g_assert_false (g_date_valid (NULL));
35 : 1 : g_test_assert_expected_messages ();
36 : : }
37 : :
38 : : /* g_date_new_dmy (d, m, y) */
39 [ + - ]: 1 : if (g_test_undefined ())
40 : : {
41 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
42 : : "*assertion *failed*");
43 : 1 : g_assert_null (g_date_new_dmy (0, 0, 0));
44 : 1 : g_test_assert_expected_messages ();
45 : : }
46 : :
47 : 1 : d = g_date_new ();
48 [ + - ]: 1 : if (g_test_undefined ())
49 : : {
50 : : /* g_date_get_weekday (d) */
51 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
52 : : "*assertion *failed*");
53 : 1 : g_assert_cmpint (g_date_get_weekday (d), ==, G_DATE_BAD_WEEKDAY);
54 : 1 : g_test_assert_expected_messages ();
55 : :
56 : : /* g_date_get_day (d) */
57 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
58 : : "*assertion *failed*");
59 : 1 : g_assert_cmpint (g_date_get_day (d), ==, G_DATE_BAD_DAY);
60 : 1 : g_test_assert_expected_messages ();
61 : :
62 : : /* g_date_get_month (d) */
63 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
64 : : "*assertion *failed*");
65 : 1 : g_assert_cmpint (g_date_get_month (d), ==, G_DATE_BAD_MONTH);
66 : 1 : g_test_assert_expected_messages ();
67 : :
68 : : /* g_date_get_year (d) */
69 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
70 : : "*assertion *failed*");
71 : 1 : g_assert_cmpint (g_date_get_year (d), ==, G_DATE_BAD_YEAR);
72 : 1 : g_test_assert_expected_messages ();
73 : :
74 : : /* g_date_to_struct_tm (d, tm) */
75 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
76 : : "*assertion *failed*");
77 : 1 : g_date_to_struct_tm (d, &tm);
78 : 1 : g_test_assert_expected_messages ();
79 : :
80 : : /* g_is_leap_year (y) */
81 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
82 : : "*assertion *failed*");
83 : 1 : g_assert_false (g_date_is_leap_year (0));
84 : 1 : g_test_assert_expected_messages ();
85 : :
86 : : /* g_date_get_days_in_month (m, y) */
87 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
88 : : "*assertion *failed*");
89 : 1 : g_assert_cmpint (g_date_get_days_in_month (0, 1), ==, 0);
90 : 1 : g_test_assert_expected_messages ();
91 : :
92 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
93 : : "*assertion *failed*");
94 : 1 : g_assert_cmpint (g_date_get_days_in_month (1, 0), ==, 0);
95 : 1 : g_test_assert_expected_messages ();
96 : :
97 : : /* g_date_set_time_t (d, t) */
98 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
99 : : "*assertion *failed*");
100 : 1 : g_date_set_time_t (NULL, 1);
101 : 1 : g_test_assert_expected_messages ();
102 : :
103 : : /* g_date_is_first_of_month (d) */
104 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
105 : : "*assertion *failed*");
106 : 1 : g_assert_false (g_date_is_first_of_month (d));
107 : 1 : g_test_assert_expected_messages ();
108 : :
109 : : /* g_date_is_last_of_month (d) */
110 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
111 : : "*assertion *failed*");
112 : 1 : g_assert_false (g_date_is_last_of_month (d));
113 : 1 : g_test_assert_expected_messages ();
114 : :
115 : : /* g_date_add_days (d, n) */
116 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
117 : : "*assertion *failed*");
118 : 1 : g_date_add_days (d, 1);
119 : 1 : g_test_assert_expected_messages ();
120 : :
121 : : /* g_date_subtract_days (d, n) */
122 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
123 : : "*assertion *failed*");
124 : 1 : g_date_subtract_days (d, 1);
125 : 1 : g_test_assert_expected_messages ();
126 : :
127 : : /* g_date_add_months (d, n) */
128 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
129 : : "*assertion *failed*");
130 : 1 : g_date_add_months (d, 1);
131 : 1 : g_test_assert_expected_messages ();
132 : :
133 : : /* g_date_subtract_months (d, n) */
134 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
135 : : "*assertion *failed*");
136 : 1 : g_date_subtract_months (d, 1);
137 : 1 : g_test_assert_expected_messages ();
138 : :
139 : : /* g_date_add_years (d, y) */
140 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
141 : : "*assertion *failed*");
142 : 1 : g_date_add_years (d, 1);
143 : 1 : g_test_assert_expected_messages ();
144 : :
145 : : /* g_date_subtract_years (d, y) */
146 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
147 : : "*assertion *failed*");
148 : 1 : g_date_subtract_years (d, 1);
149 : 1 : g_test_assert_expected_messages ();
150 : :
151 : : /* g_date_set_month (d, m) */
152 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
153 : : "*assertion *failed*");
154 : 1 : g_date_set_month (NULL, 1);
155 : 1 : g_test_assert_expected_messages ();
156 : :
157 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
158 : : "*assertion *failed*");
159 : 1 : g_date_set_month (d, 13);
160 : 1 : g_test_assert_expected_messages ();
161 : :
162 : : /* g_date_set_day (d, day) */
163 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
164 : : "*assertion *failed*");
165 : 1 : g_date_set_day (NULL, 1);
166 : 1 : g_test_assert_expected_messages ();
167 : :
168 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
169 : : "*assertion *failed*");
170 : 1 : g_date_set_day (d, 32);
171 : 1 : g_test_assert_expected_messages ();
172 : :
173 : : /* g_date_set_year (d, y) */
174 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
175 : : "*assertion *failed*");
176 : 1 : g_date_set_year (NULL, 1);
177 : 1 : g_test_assert_expected_messages ();
178 : :
179 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
180 : : "*assertion *failed*");
181 : 1 : g_date_set_year (d, (GDateYear) (G_MAXUINT16 + 1));
182 : 1 : g_test_assert_expected_messages ();
183 : :
184 : : /* g_date_set_dmy (date, d, m, y) */
185 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
186 : : "*assertion *failed*");
187 : 1 : g_date_set_dmy (NULL, 1, 1, 1);
188 : 1 : g_test_assert_expected_messages ();
189 : :
190 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
191 : : "*assertion *failed*");
192 : 1 : g_date_set_dmy (d, 0, 0, 0);
193 : 1 : g_test_assert_expected_messages ();
194 : :
195 : : /* g_date_set_julian (date, d) */
196 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
197 : : "*assertion *failed*");
198 : 1 : g_date_set_julian (NULL, 1);
199 : 1 : g_test_assert_expected_messages ();
200 : :
201 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
202 : : "*assertion *failed*");
203 : 1 : g_date_set_julian (d, 0);
204 : 1 : g_test_assert_expected_messages ();
205 : :
206 : : /* g_date_clear (d, n) */
207 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
208 : : "*assertion *failed*");
209 : 1 : g_date_clear (d, 0);
210 : 1 : g_test_assert_expected_messages ();
211 : :
212 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
213 : : "*assertion *failed*");
214 : 1 : g_date_clear (NULL, 1);
215 : 1 : g_test_assert_expected_messages ();
216 : : }
217 : :
218 : 1 : g_date_set_dmy (d, 1, 1, 1);
219 [ + - ]: 1 : if (g_test_undefined ())
220 : : {
221 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
222 : : "*assertion *failed*");
223 : 1 : g_date_to_struct_tm (d, NULL);
224 : 1 : g_test_assert_expected_messages ();
225 : : }
226 : 1 : g_date_free (d);
227 : :
228 : 1 : g_assert_cmpint (sizeof (GDate), <, 9);
229 : 1 : g_assert_false (g_date_valid_month (G_DATE_BAD_MONTH));
230 : 1 : g_assert_false (g_date_valid_month (13));
231 : 1 : g_assert_false (g_date_valid_day (G_DATE_BAD_DAY));
232 : 1 : g_assert_false (g_date_valid_day (32));
233 : 1 : g_assert_false (g_date_valid_year (G_DATE_BAD_YEAR));
234 : 1 : g_assert_false (g_date_valid_julian (G_DATE_BAD_JULIAN));
235 : 1 : g_assert_false (g_date_valid_weekday (G_DATE_BAD_WEEKDAY));
236 : 1 : g_assert_true (g_date_valid_weekday ((GDateWeekday) 1));
237 : 1 : g_assert_false (g_date_valid_weekday ((GDateWeekday) 8));
238 : 1 : g_assert_true (g_date_is_leap_year (2000));
239 : 1 : g_assert_false (g_date_is_leap_year (1999));
240 : 1 : g_assert_true (g_date_is_leap_year (1996));
241 : 1 : g_assert_true (g_date_is_leap_year (1600));
242 : 1 : g_assert_false (g_date_is_leap_year (2100));
243 : 1 : g_assert_false (g_date_is_leap_year (1800));
244 : 1 : }
245 : :
246 : : static void
247 : 1 : test_empty_constructor (void)
248 : : {
249 : : GDate *d;
250 : :
251 : 1 : d = g_date_new ();
252 : 1 : g_assert_false (g_date_valid (d));
253 : 1 : g_date_free (d);
254 : :
255 [ + - ]: 1 : if (g_test_undefined ())
256 : : {
257 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
258 : : "*assertion *failed*");
259 : 1 : g_date_free (NULL);
260 : 1 : g_test_assert_expected_messages ();
261 : : }
262 : :
263 : 1 : }
264 : :
265 : : static void
266 : 1 : test_dmy_constructor (void)
267 : : {
268 : : GDate *d;
269 : : guint32 j;
270 : :
271 : 1 : d = g_date_new_dmy (1, 1, 1);
272 : 1 : g_assert_true (g_date_valid (d));
273 : :
274 : 1 : j = g_date_get_julian (d);
275 : 1 : g_assert_cmpint (j, ==, 1);
276 : 1 : g_assert_cmpint (g_date_get_month (d), ==, G_DATE_JANUARY);
277 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 1);
278 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 1);
279 : 1 : g_date_free (d);
280 : 1 : }
281 : :
282 : : static void
283 : 1 : test_date_compare (void)
284 : : {
285 : : GDate *d1;
286 : : GDate *d2;
287 : :
288 : 1 : d1 = g_date_new ();
289 : 1 : d2 = g_date_new ();
290 [ + - ]: 1 : if (g_test_undefined ())
291 : : {
292 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
293 : : "*assertion *failed*");
294 : 1 : g_assert_cmpint (g_date_days_between (d1, d2), ==, 0);
295 : 1 : g_test_assert_expected_messages ();
296 : :
297 : 1 : g_date_set_dmy (d1, 1, 1, 1);
298 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
299 : : "*assertion *failed*");
300 : 1 : g_assert_cmpint (g_date_days_between (d1, d2), ==, 0);
301 : 1 : g_test_assert_expected_messages ();
302 : : }
303 : 1 : g_date_free (d1);
304 : 1 : g_date_free (d2);
305 : :
306 : 1 : d1 = g_date_new ();
307 : 1 : d2 = g_date_new ();
308 [ + - ]: 1 : if (g_test_undefined ())
309 : : {
310 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
311 : : "*assertion *failed*");
312 : 1 : g_assert_cmpint (g_date_compare (NULL, d2), ==, 0);
313 : 1 : g_test_assert_expected_messages ();
314 : :
315 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
316 : : "*assertion *failed*");
317 : 1 : g_assert_cmpint (g_date_compare (d1, NULL), ==, 0);
318 : 1 : g_test_assert_expected_messages ();
319 : :
320 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
321 : : "*assertion *failed*");
322 : 1 : g_assert_cmpint (g_date_compare (d1, d2), ==, 0);
323 : 1 : g_test_assert_expected_messages ();
324 : :
325 : 1 : g_date_set_dmy (d1, 1, 1, 1);
326 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
327 : : "*assertion *failed*");
328 : 1 : g_assert_cmpint (g_date_compare (d1, d2), ==, 0);
329 : 1 : g_test_assert_expected_messages ();
330 : : }
331 : 1 : g_date_free (d1);
332 : 1 : g_date_free (d2);
333 : :
334 : 1 : d1 = g_date_new ();
335 : 1 : d2 = g_date_new ();
336 : :
337 : : /* DMY format */
338 : 1 : g_date_set_dmy (d1, 1, 1, 1);
339 : 1 : g_date_set_dmy (d2, 10, 1, 1);
340 : :
341 : 1 : g_assert_cmpint (g_date_compare (d1, d1), ==, 0);
342 : :
343 : 1 : g_assert_cmpint (g_date_compare (d1, d2), ==, -1);
344 : 1 : g_assert_cmpint (g_date_compare (d2, d1), >, 0);
345 : :
346 : 1 : g_date_set_dmy (d2, 1, 10, 1);
347 : 1 : g_assert_cmpint (g_date_compare (d1, d2), ==, -1);
348 : 1 : g_assert_cmpint (g_date_compare (d2, d1), >, 0);
349 : :
350 : 1 : g_date_set_dmy (d2, 1, 1, 10);
351 : 1 : g_assert_cmpint (g_date_compare (d1, d2), ==, -1);
352 : 1 : g_assert_cmpint (g_date_compare (d2, d1), >, 0);
353 : :
354 : : /* Julian format */
355 : 1 : g_date_set_julian (d1, 1);
356 : 1 : g_date_set_julian (d2, 10);
357 : :
358 : 1 : g_assert_cmpint (g_date_compare (d1, d1), ==, 0);
359 : :
360 : 1 : g_assert_cmpint (g_date_compare (d1, d2), ==, -1);
361 : 1 : g_assert_cmpint (g_date_compare (d2, d1), >, 0);
362 : :
363 : 1 : g_date_set_julian (d2, 32);
364 : 1 : g_assert_cmpint (g_date_compare (d1, d2), ==, -1);
365 : 1 : g_assert_cmpint (g_date_compare (d2, d1), >, 0);
366 : :
367 : 1 : g_date_set_julian (d2, 366);
368 : 1 : g_assert_cmpint (g_date_compare (d1, d2), ==, -1);
369 : 1 : g_assert_cmpint (g_date_compare (d2, d1), >, 0);
370 : :
371 : 1 : g_date_free (d1);
372 : 1 : g_date_free (d2);
373 : 1 : }
374 : :
375 : : static void
376 : 1 : test_julian_constructor (void)
377 : : {
378 : : GDate *d1;
379 : : GDate *d2;
380 : :
381 [ + - ]: 1 : if (g_test_undefined ())
382 : : {
383 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
384 : : "*assertion *failed*");
385 : 1 : g_assert_null (g_date_new_julian (0));
386 : 1 : g_test_assert_expected_messages ();
387 : : }
388 : :
389 : 1 : d1 = g_date_new ();
390 [ + - ]: 1 : if (g_test_undefined ())
391 : : {
392 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
393 : : "*assertion *failed*");
394 : 1 : g_assert_cmpint (g_date_get_julian (d1), ==, G_DATE_BAD_JULIAN);
395 : 1 : g_test_assert_expected_messages ();
396 : : }
397 : 1 : g_date_free (d1);
398 : :
399 : 1 : d1 = g_date_new_julian (4000);
400 : 1 : d2 = g_date_new_julian (5000);
401 : 1 : g_assert_cmpint (g_date_get_julian (d1), ==, 4000);
402 : 1 : g_assert_cmpint (g_date_days_between (d1, d2), ==, 1000);
403 : 1 : g_assert_cmpint (g_date_get_year (d1), ==, 11);
404 : 1 : g_assert_cmpint (g_date_get_day (d2), ==, 9);
405 : 1 : g_date_free (d1);
406 : 1 : g_date_free (d2);
407 : 1 : }
408 : :
409 : : static void
410 : 1 : test_dates (void)
411 : : {
412 : : GDate *d;
413 : : GTimeVal tv;
414 : : time_t now;
415 : :
416 : 1 : d = g_date_new ();
417 : :
418 : : /* getters on empty date */
419 [ + - ]: 1 : if (g_test_undefined ())
420 : : {
421 : : /* g_date_get_day_of_year (d) */
422 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
423 : : "*assertion *failed*");
424 : 1 : g_assert_cmpint (g_date_get_day_of_year (d), ==, 0);
425 : 1 : g_test_assert_expected_messages ();
426 : :
427 : : /* g_date_get_monday_week_of_year (d) */
428 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
429 : : "*assertion *failed*");
430 : 1 : g_assert_cmpint (g_date_get_monday_week_of_year (d), ==, 0);
431 : 1 : g_test_assert_expected_messages ();
432 : :
433 : : /* g_date_get_monday_weeks_in_year (y) */
434 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
435 : : "*assertion *failed*");
436 : 1 : g_assert_cmpint (g_date_get_monday_weeks_in_year (0), ==, 0);
437 : 1 : g_test_assert_expected_messages ();
438 : :
439 : : /* g_date_get_sunday_week_of_year (d) */
440 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
441 : : "*assertion *failed*");
442 : 1 : g_assert_cmpint (g_date_get_sunday_week_of_year (d), ==, 0);
443 : 1 : g_test_assert_expected_messages ();
444 : :
445 : : /* g_date_get_sunday_weeks_in_year (y) */
446 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
447 : : "*assertion *failed*");
448 : 1 : g_assert_cmpint (g_date_get_sunday_weeks_in_year (0), ==, 0);
449 : 1 : g_test_assert_expected_messages ();
450 : :
451 : : /* g_date_get_iso8601_week_of_year (d) */
452 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
453 : : "*assertion *failed*");
454 : 1 : g_assert_cmpint (g_date_get_iso8601_week_of_year (d), ==, 0);
455 : 1 : g_test_assert_expected_messages ();
456 : : }
457 : :
458 : 1 : g_date_free (d);
459 : :
460 : : /* Remove more time than we have */
461 : 1 : d = g_date_new_julian (1);
462 [ + - ]: 1 : if (g_test_undefined ())
463 : : {
464 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
465 : : "*assertion *failed*");
466 : 1 : g_date_subtract_days (d, 103);
467 : 1 : g_test_assert_expected_messages ();
468 : : }
469 : 1 : g_date_free (d);
470 : :
471 : 1 : d = g_date_new_julian (375);
472 [ + - ]: 1 : if (g_test_undefined ())
473 : : {
474 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
475 : : "*assertion *failed*");
476 : 1 : g_date_subtract_months (d, 13);
477 : 1 : g_test_assert_expected_messages ();
478 : : }
479 : 1 : g_date_free (d);
480 : :
481 : 1 : d = g_date_new_julian (375);
482 [ + - ]: 1 : if (g_test_undefined ())
483 : : {
484 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
485 : : "*assertion *failed*");
486 : 1 : g_date_subtract_years (d, 2);
487 : 1 : g_test_assert_expected_messages ();
488 : : }
489 : 1 : g_date_free (d);
490 : :
491 : : /* Test on leap years */
492 : 1 : g_assert_cmpint (g_date_get_monday_weeks_in_year (1764), ==, 53);
493 : 1 : g_assert_cmpint (g_date_get_monday_weeks_in_year (1776), ==, 53);
494 : :
495 : 1 : g_assert_cmpint (g_date_get_sunday_weeks_in_year (1792), ==, 53);
496 : :
497 : : /* Trigger the update of the dmy/julian parts */
498 : 1 : d = g_date_new_julian (1);
499 : 1 : g_assert_cmpint (g_date_get_day_of_year (d), ==, 1);
500 : 1 : g_date_free (d);
501 : :
502 : 1 : d = g_date_new_julian (1);
503 : 1 : g_assert_cmpint (g_date_get_monday_week_of_year (d), ==, 1);
504 : 1 : g_date_free (d);
505 : :
506 : 1 : d = g_date_new_julian (1);
507 : 1 : g_assert_cmpint (g_date_get_sunday_week_of_year (d), ==, 0);
508 : 1 : g_date_free (d);
509 : :
510 : 1 : d = g_date_new_julian (1);
511 : 1 : g_assert_cmpint (g_date_is_first_of_month (d), ==, 1);
512 : 1 : g_date_free (d);
513 : :
514 : 1 : d = g_date_new_dmy (31, 3, 8);
515 : 1 : g_date_subtract_months (d, 1);
516 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 2);
517 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 29);
518 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 8);
519 : 1 : g_date_free (d);
520 : :
521 : 1 : d = g_date_new_julian (375);
522 : 1 : g_date_add_months (d, 1);
523 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 2);
524 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 10);
525 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 2);
526 : 1 : g_date_free (d);
527 : :
528 : 1 : d = g_date_new_julian (375);
529 : 1 : g_date_subtract_months (d, 1);
530 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 12);
531 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 10);
532 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 1);
533 : 1 : g_date_free (d);
534 : :
535 : 1 : d = g_date_new_julian (375);
536 : 1 : g_date_add_years (d, 1);
537 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 1);
538 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 10);
539 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 3);
540 : 1 : g_date_free (d);
541 : :
542 : 1 : d = g_date_new_julian (675);
543 : 1 : g_date_subtract_years (d, 1);
544 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 11);
545 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 6);
546 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 1);
547 : 1 : g_date_free (d);
548 : :
549 : 1 : d = g_date_new_dmy (28, 2, 7);
550 : 1 : g_date_subtract_years (d, 1);
551 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 2);
552 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 28);
553 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 6);
554 : 1 : g_date_free (d);
555 : :
556 : 1 : d = g_date_new_dmy (29, 2, 8);
557 : 1 : g_date_subtract_years (d, 1);
558 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 2);
559 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 28);
560 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 7);
561 : 1 : g_date_free (d);
562 : :
563 : 1 : d = g_date_new_dmy (1, 1, 1);
564 : 1 : g_assert_cmpint (g_date_get_iso8601_week_of_year (d), ==, 1);
565 : 1 : g_date_free (d);
566 : :
567 : 1 : d = g_date_new_julian (1);
568 : 1 : g_date_set_year (d, 6);
569 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 1);
570 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 1);
571 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 6);
572 : 1 : g_date_free (d);
573 : :
574 : 1 : d = g_date_new_dmy (1, 1, 1);
575 : 1 : g_date_set_year (d, 6);
576 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 1);
577 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 1);
578 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 6);
579 : 1 : g_date_free (d);
580 : :
581 : 1 : d = g_date_new_julian (1);
582 : 1 : g_date_set_month (d, 6);
583 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 6);
584 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 1);
585 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 1);
586 : 1 : g_date_free (d);
587 : :
588 : 1 : d = g_date_new_dmy (1, 1, 1);
589 : 1 : g_date_set_month (d, 6);
590 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 6);
591 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 1);
592 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 1);
593 : 1 : g_date_free (d);
594 : :
595 : 1 : d = g_date_new_julian (1);
596 : 1 : g_date_set_day (d, 6);
597 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 1);
598 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 6);
599 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 1);
600 : 1 : g_date_free (d);
601 : :
602 : 1 : d = g_date_new_dmy (1, 1, 1);
603 : 1 : g_date_set_day (d, 6);
604 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 1);
605 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 6);
606 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 1);
607 : 1 : g_date_free (d);
608 : :
609 : 1 : d = g_date_new_julian (1);
610 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 1);
611 : 1 : g_date_free (d);
612 : :
613 : : /* Correct usage */
614 : 1 : d = g_date_new ();
615 : :
616 : : /* today */
617 : 1 : now = time (NULL);
618 : 1 : g_assert_cmpint (now, !=, (time_t) -1);
619 : 1 : g_date_set_time (d, now);
620 : 1 : g_assert_true (g_date_valid (d));
621 : :
622 : : /* Unix epoch */
623 : 1 : g_date_set_time (d, 1);
624 : 1 : g_assert_true (g_date_valid (d));
625 : :
626 : 1 : tv.tv_sec = 0;
627 : 1 : tv.tv_usec = 0;
628 : 1 : g_date_set_time_val (d, &tv);
629 : 1 : g_assert_true (g_date_valid (d));
630 : :
631 : : /* Julian day 1 */
632 : 1 : g_date_set_julian (d, 1);
633 : 1 : g_assert_true (g_date_valid (d));
634 : :
635 : 1 : g_date_set_year (d, 3);
636 : 1 : g_date_set_day (d, 3);
637 : 1 : g_date_set_month (d, 3);
638 : 1 : g_assert_true (g_date_valid (d));
639 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 3);
640 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 3);
641 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 3);
642 : 1 : g_assert_false (g_date_is_first_of_month (d));
643 : 1 : g_assert_false (g_date_is_last_of_month (d));
644 : 1 : g_date_set_day (d, 1);
645 : 1 : g_assert_true (g_date_is_first_of_month (d));
646 : 1 : g_date_subtract_days (d, 1);
647 : 1 : g_assert_true (g_date_is_last_of_month (d));
648 : :
649 : : /* Testing some other corner cases */
650 : 1 : g_date_set_dmy (d, 29, 2, 2000);
651 : 1 : g_date_subtract_months (d, 2);
652 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 12);
653 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 29);
654 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 1999);
655 : :
656 : : /* Attempt to assign a February 29 to a non-leap year */
657 : 1 : g_date_set_month (d, 2);
658 : 1 : g_date_set_day (d, 29);
659 : 1 : g_assert_false (g_date_valid (d));
660 : 1 : g_date_set_year (d, 3);
661 : 1 : g_assert_false (g_date_valid (d));
662 : :
663 : 1 : g_date_free (d);
664 : 1 : }
665 : :
666 : : static void
667 : 1 : test_strftime (void)
668 : : {
669 : : gsize i;
670 : : GDate *d;
671 : : gchar buf[101];
672 : 1 : const gchar invalid[] = "hello\xffworld%x";
673 : : gchar *oldlocale;
674 : : #ifdef G_OS_WIN32
675 : : LCID old_lcid;
676 : : #endif
677 : :
678 : : struct
679 : : {
680 : : const gchar *format;
681 : : const gchar *expect;
682 : 1 : } strftime_checks[] = {
683 : : { "%A", "Monday" },
684 : : { "%a", "Mon" },
685 : : { "%D", "01/01/01" },
686 : : { "%d", "01" },
687 : : { "%e", " 1" },
688 : : { "%H", "00" },
689 : : { "%I", "12" },
690 : : { "%j", "001" },
691 : : { "%M", "00" },
692 : : { "%m", "01" },
693 : : { "%n", "\n" },
694 : : { "%OB", "January" },
695 : : { "%Ob", "Jan" },
696 : : { "%p", "AM" },
697 : : { "%R", "00:00" },
698 : : { "%S", "00" },
699 : : { "%T", "00:00:00" },
700 : : { "%t", "\t" },
701 : : { "%U", "00" },
702 : : { "%u", "1" },
703 : : { "%V", "01" },
704 : : { "%W", "01" },
705 : : { "%w", "1" },
706 : : { "%y", "01" },
707 : : { "%z", "" },
708 : : { "%%", "%" },
709 : : #if defined(G_OS_WIN32)
710 : : { "%C", "00" },
711 : : { "%c", " 12:00:00 AM" },
712 : : { "%E", "" },
713 : : { "%F", "" },
714 : : { "%G", "" },
715 : : { "%g", "" },
716 : : { "%h", "" },
717 : : { "%k", "" },
718 : : { "%l", "" },
719 : : { "%O", "" },
720 : : { "%P", "" },
721 : : { "%r", "12:00:00AM" },
722 : : { "%X", "12:00:00 AM" },
723 : : { "%x", "" },
724 : : { "%Y", "0001" },
725 : : #else
726 : : { "%B", "January" },
727 : : { "%b", "Jan" },
728 : : #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
729 : : { "%C", "00" },
730 : : { "%c", "Mon Jan 1 00:00:00 0001" },
731 : : { "%E", "E" },
732 : : { "%F", "0001-01-01" },
733 : : { "%G", "0001" },
734 : : { "%O", "O" },
735 : : { "%P", "P" },
736 : : { "%Y", "0001" },
737 : : #else
738 : : { "%C", "0" },
739 : : { "%c", "Mon Jan 1 00:00:00 1" },
740 : : { "%E", "%E" },
741 : : { "%F", "1-01-01" },
742 : : { "%G", "1" },
743 : : { "%O", "%O" },
744 : : { "%P", "am" },
745 : : { "%Y", "1" },
746 : : #endif
747 : : { "%g", "01" },
748 : : { "%h", "Jan" },
749 : : { "%k", " 0" },
750 : : { "%l", "12" },
751 : : { "%r", "12:00:00 AM" },
752 : : { "%X", "00:00:00" },
753 : : { "%x", "01/01/01" },
754 : : { "%Z", "" },
755 : : #endif
756 : : };
757 : :
758 : 1 : oldlocale = g_strdup (setlocale (LC_ALL, NULL));
759 : : #ifdef G_OS_WIN32
760 : : old_lcid = GetThreadLocale ();
761 : : #endif
762 : :
763 : : /* Make sure that nothing has been changed in the original locales. */
764 : 1 : setlocale (LC_ALL, "C");
765 : : #ifdef G_OS_WIN32
766 : : SetThreadLocale (MAKELCID (MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT));
767 : : #endif
768 : :
769 : 1 : d = g_date_new ();
770 : :
771 [ + - ]: 1 : if (g_test_undefined ())
772 : : {
773 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
774 : : "*assertion *failed*");
775 : 1 : g_date_strftime (buf, sizeof (buf), "%x", d);
776 : 1 : g_test_assert_expected_messages ();
777 : : }
778 : :
779 : : /* Trying invalid character */
780 : : #ifndef G_OS_WIN32
781 [ + - ]: 1 : if (g_test_undefined ())
782 : : {
783 : 1 : g_date_set_dmy (d, 10, 1, 2000);
784 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "*Error*");
785 : 1 : g_date_strftime (buf, sizeof (buf), invalid, d);
786 : 1 : g_test_assert_expected_messages ();
787 : 1 : g_assert_cmpstr (buf, ==, "");
788 : : }
789 : : #else
790 : : g_date_set_dmy (d, 10, 1, 2000);
791 : : g_date_strftime (buf, sizeof (buf), invalid, d);
792 : : g_assert_cmpstr (buf, ==, "");
793 : : #endif
794 : :
795 : : /* Test positive cases */
796 : 1 : g_date_set_dmy (d, 1, 1, 1);
797 : :
798 [ + + ]: 45 : for (i = 0; i < G_N_ELEMENTS (strftime_checks); i++)
799 : : {
800 : 44 : g_date_strftime (buf, sizeof (buf), strftime_checks[i].format, d);
801 : 44 : g_assert_cmpstr (buf, ==, strftime_checks[i].expect);
802 : : }
803 : :
804 : : #ifdef G_OS_WIN32
805 : : /*
806 : : * Time zone is too versatile on OS_WIN32 to be checked precisely,
807 : : * According to msdn: "Either the locale's time-zone name
808 : : * or time zone abbreviation, depending on registry settings; no characters
809 : : * if time zone is unknown".
810 : : */
811 : : g_assert_cmpint (g_date_strftime (buf, sizeof (buf), "%Z", d), !=, 0);
812 : : #endif
813 : :
814 : 1 : g_date_free (d);
815 : :
816 : 1 : setlocale (LC_ALL, oldlocale);
817 : 1 : g_free (oldlocale);
818 : : #ifdef G_OS_WIN32
819 : : SetThreadLocale (old_lcid);
820 : : #endif
821 : 1 : }
822 : :
823 : : static void
824 : 1 : test_two_digit_years (void)
825 : : {
826 : : GDate *d;
827 : : gchar buf[101];
828 : : gchar *old_locale;
829 : 1 : gboolean use_alternative_format = FALSE;
830 : :
831 : : #ifdef G_OS_WIN32
832 : : LCID old_lcid;
833 : : #endif
834 : :
835 : 1 : old_locale = g_strdup (setlocale (LC_ALL, NULL));
836 : : #ifdef G_OS_WIN32
837 : : old_lcid = GetThreadLocale ();
838 : : #endif
839 : :
840 : : /* Make sure that nothing has been changed in the original locales. */
841 : 1 : setlocale (LC_ALL, "C");
842 : : #ifdef G_OS_WIN32
843 : : SetThreadLocale (MAKELCID (MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), SORT_DEFAULT));
844 : : #endif
845 : :
846 : 1 : d = g_date_new ();
847 : :
848 : : /* Check two digit years */
849 : 1 : g_date_set_dmy (d, 10, 10, 1976);
850 : 1 : g_date_strftime (buf, sizeof (buf), "%D", d);
851 : 1 : g_assert_cmpstr (buf, ==, "10/10/76");
852 : 1 : g_date_set_parse (d, buf);
853 : :
854 : : #ifdef G_OS_WIN32
855 : : /*
856 : : * It depends on the locale setting whether the dd/mm/yy
857 : : * format is allowed for g_date_set_parse() on Windows, which
858 : : * corresponds to whether there is an d/M/YY or d/M/YYYY (or so)
859 : : * option in the "Date and Time Format" setting for the selected
860 : : * locale in the Control Panel settings. If g_date_set_parse()
861 : : * renders the GDate invalid with the dd/mm/yy format, use an
862 : : * alternative format (yy/mm/dd) for g_date_set_parse() for the
863 : : * 2-digit year tests.
864 : : */
865 : : if (!g_date_valid (d))
866 : : use_alternative_format = TRUE;
867 : : #endif
868 : :
869 [ - + ]: 1 : if (use_alternative_format)
870 : 0 : g_date_set_parse (d, "76/10/10");
871 : :
872 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 10);
873 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 10);
874 : 1 : g_assert_true ((g_date_get_year (d) == 1976) ||
875 : : (g_date_get_year (d) == 76));
876 : :
877 : : /* Check two digit years below 100 */
878 : 1 : g_date_set_dmy (d, 10, 10, 29);
879 : 1 : g_date_strftime (buf, sizeof (buf), "%D", d);
880 : 1 : g_assert_cmpstr (buf, ==, "10/10/29");
881 [ + - ]: 1 : g_date_set_parse (d, use_alternative_format ? "29/10/10" : buf);
882 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 10);
883 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 10);
884 : 1 : g_assert_true ((g_date_get_year (d) == 2029) ||
885 : : (g_date_get_year (d) == 29));
886 : :
887 : 1 : g_date_free (d);
888 : :
889 : 1 : setlocale (LC_ALL, old_locale);
890 : 1 : g_free (old_locale);
891 : : #ifdef G_OS_WIN32
892 : : SetThreadLocale (old_lcid);
893 : : #endif
894 : 1 : }
895 : :
896 : : static void
897 : 1 : test_parse (void)
898 : : {
899 : : GDate *d;
900 : : gchar buf[101];
901 : :
902 : 1 : d = g_date_new ();
903 : :
904 [ + - ]: 1 : if (g_test_undefined ())
905 : : {
906 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
907 : : "*assertion *failed*");
908 : 1 : g_date_set_parse (NULL, "");
909 : 1 : g_test_assert_expected_messages ();
910 : : }
911 : :
912 : 1 : g_date_set_time (d, 1);
913 : 1 : g_assert_true (g_date_valid (d));
914 : 1 : g_date_strftime (buf, sizeof (buf), "Today is a %A, in the month of %B, %x", d);
915 : 1 : g_date_set_parse (d, buf);
916 : :
917 [ + - ]: 1 : if (g_test_undefined ())
918 : : {
919 : : /* g_date_strftime() */
920 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
921 : : "*assertion *failed*");
922 : 1 : g_date_strftime (NULL, 100, "%x", d);
923 : 1 : g_test_assert_expected_messages ();
924 : :
925 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
926 : : "*assertion *failed*");
927 : 1 : g_date_strftime (buf, 0, "%x", d);
928 : 1 : g_test_assert_expected_messages ();
929 : :
930 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
931 : : "*assertion *failed*");
932 : 1 : g_date_strftime (buf, sizeof (buf), NULL, d);
933 : 1 : g_test_assert_expected_messages ();
934 : : }
935 : :
936 : 1 : g_date_set_julian (d, 1);
937 : 1 : g_assert_true (g_date_valid (d));
938 : : #ifndef G_OS_WIN32
939 : : /* Windows FILETIME does not support dates before Jan 1 1601,
940 : : so we can't strftime() the beginning of the "Julian" epoch. */
941 : 1 : g_date_strftime (buf, sizeof (buf), "Today is a %A, in the month of %B, %x", d);
942 : 1 : g_date_set_parse (d, buf);
943 : : #endif
944 : :
945 : 1 : g_date_set_dmy (d, 10, 1, 2000);
946 : 1 : g_assert_true (g_date_valid (d));
947 : 1 : g_date_strftime (buf, sizeof (buf), "%x", d);
948 : 1 : g_date_set_parse (d, buf);
949 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 1);
950 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 10);
951 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 2000);
952 : :
953 : 1 : g_date_set_parse (d, "2001 10 1");
954 : 1 : g_assert_true (g_date_valid (d));
955 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 10);
956 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 1);
957 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 2001);
958 : :
959 : 1 : g_date_set_parse (d, "2001 10");
960 : 1 : g_assert_false (g_date_valid (d));
961 : :
962 : 1 : g_date_set_parse (d, "2001 10 1 1");
963 : 1 : g_assert_false (g_date_valid (d));
964 : :
965 : 1 : g_date_set_parse (d, "2001-10-01");
966 : 1 : g_assert_true (g_date_valid (d));
967 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 10);
968 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 1);
969 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 2001);
970 : :
971 : 1 : g_date_set_parse (d, "March 1999");
972 : 1 : g_assert_true (g_date_valid (d));
973 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 3);
974 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 1);
975 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 1999);
976 : :
977 : 1 : g_date_set_parse (d, "October 98");
978 : 1 : g_assert_true (g_date_valid (d));
979 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 10);
980 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 1);
981 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 98);
982 : :
983 : 1 : g_date_set_parse (d, "oCT 98");
984 : 1 : g_assert_true (g_date_valid (d));
985 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 10);
986 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 1);
987 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 98);
988 : :
989 : 1 : g_date_set_parse (d, "10/24/98");
990 : 1 : g_assert_true (g_date_valid (d));
991 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 10);
992 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 24);
993 : 1 : g_assert_true (g_date_get_year (d) == 1998 ||
994 : : g_date_get_year (d) == 98);
995 : :
996 : 1 : g_date_set_parse (d, "10 -- 24 -- 98");
997 : 1 : g_assert_true (g_date_valid (d));
998 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 10);
999 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 24);
1000 : 1 : g_assert_true (g_date_get_year (d) == 1998 ||
1001 : : g_date_get_year (d) == 98);
1002 : :
1003 : 1 : g_date_set_parse (d, "10/24/1998");
1004 : 1 : g_assert_true (g_date_valid (d));
1005 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 10);
1006 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 24);
1007 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 1998);
1008 : :
1009 : 1 : g_date_set_parse (d, "October 24, 1998");
1010 : 1 : g_assert_true (g_date_valid (d));
1011 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 10);
1012 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 24);
1013 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 1998);
1014 : :
1015 : 1 : g_date_set_parse (d, "10 Sep 1087");
1016 : 1 : g_assert_true (g_date_valid (d));
1017 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 9);
1018 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 10);
1019 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 1087);
1020 : :
1021 : 1 : g_date_set_parse (d, "19990301");
1022 : 1 : g_assert_true (g_date_valid (d));
1023 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 3);
1024 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 1);
1025 : 1 : g_assert_cmpint (g_date_get_year (d), ==, 1999);
1026 : :
1027 : 1 : g_date_set_parse (d, "981024");
1028 : 1 : g_assert_true (g_date_valid (d));
1029 : 1 : g_assert_cmpint (g_date_get_month (d), ==, 10);
1030 : 1 : g_assert_cmpint (g_date_get_day (d), ==, 24);
1031 : 1 : g_assert_true (g_date_get_year (d) == 1998 ||
1032 : : g_date_get_year (d) == 98);
1033 : :
1034 : : /* Catching some invalid dates */
1035 : 1 : g_date_set_parse (d, "20011320");
1036 : 1 : g_assert_false (g_date_valid (d));
1037 : :
1038 : 1 : g_date_set_parse (d, "19998 10 1");
1039 : 1 : g_assert_false (g_date_valid (d));
1040 : :
1041 : 1 : g_date_free (d);
1042 : 1 : }
1043 : :
1044 : : static void
1045 : 1 : test_parse_invalid (void)
1046 : : {
1047 : 1 : const gchar * const strs[] =
1048 : : {
1049 : : /* Incomplete UTF-8 sequence */
1050 : : "\xfd",
1051 : : /* Ridiculously long input */
1052 : : "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
1053 : : "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
1054 : : "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
1055 : : };
1056 : : gsize i;
1057 : :
1058 [ + + ]: 3 : for (i = 0; i < G_N_ELEMENTS (strs); i++)
1059 : : {
1060 : 2 : GDate *d = g_date_new ();
1061 : :
1062 : 2 : g_test_message ("Test %" G_GSIZE_FORMAT, i);
1063 : 2 : g_date_set_parse (d, strs[i]);
1064 : :
1065 : 2 : g_assert_false (g_date_valid (d));
1066 : :
1067 : 2 : g_date_free (d);
1068 : : }
1069 : 1 : }
1070 : :
1071 : : static void
1072 : 1 : test_parse_locale_change (void)
1073 : : {
1074 : : /* Checks that g_date_set_parse correctly changes locale specific data as
1075 : : * necessary. In this particular case year adjustment, as Thai calendar is
1076 : : * 543 years ahead of the Gregorian calendar. */
1077 : :
1078 : : GDate date;
1079 : :
1080 [ - + ]: 1 : if (setlocale (LC_ALL, "th_TH") == NULL)
1081 : : {
1082 : 0 : g_test_skip ("locale th_TH not available");
1083 : 0 : return;
1084 : : }
1085 : :
1086 : 1 : g_date_set_parse (&date, "04/07/2519");
1087 : :
1088 : 1 : setlocale (LC_ALL, "C");
1089 : 1 : g_date_set_parse (&date, "07/04/76");
1090 : 1 : g_assert_cmpint (g_date_get_day (&date), ==, 4);
1091 : 1 : g_assert_cmpint (g_date_get_month (&date), ==, 7);
1092 : : #ifdef G_OS_WIN32
1093 : : /* Windows g_date_strftime() implementation doesn't use twodigit_years */
1094 : : /* FIXME: check if the function can be changed to return 4 digit years instead
1095 : : * See https://gitlab.gnome.org/GNOME/glib/-/issues/2604 */
1096 : : g_assert_cmpint (g_date_get_year (&date), ==, 76);
1097 : : #else
1098 : 1 : g_assert_cmpint (g_date_get_year (&date), ==, 1976);
1099 : : #endif
1100 : :
1101 : 1 : setlocale (LC_ALL, "");
1102 : : }
1103 : :
1104 : : static void
1105 : 1 : test_month_substring (void)
1106 : : {
1107 : : GDate date;
1108 : : #ifdef G_OS_WIN32
1109 : : LCID old_lcid;
1110 : : #endif
1111 : :
1112 : 1 : g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=793550");
1113 : :
1114 [ - + ]: 1 : if (setlocale (LC_ALL, "pl_PL") == NULL)
1115 : : {
1116 : 0 : g_test_skip ("pl_PL locale not available");
1117 : 0 : return;
1118 : : }
1119 : :
1120 : : #ifdef G_OS_WIN32
1121 : : /* after the crt, set also the win32 thread locale: */
1122 : : /* https://www.codeproject.com/Articles/9600/Windows-SetThreadLocale-and-CRT-setlocale */
1123 : : old_lcid = GetThreadLocale ();
1124 : : SetThreadLocale (MAKELCID (MAKELANGID (LANG_POLISH, SUBLANG_POLISH_POLAND), SORT_DEFAULT));
1125 : : #endif
1126 : :
1127 : : /* In Polish language September is "wrzesień" and August is "sierpień"
1128 : : * abbreviated as "sie". The former used to be confused with the latter
1129 : : * because "sie" is a substring of "wrzesień" and was matched first. */
1130 : :
1131 : 1 : g_date_set_parse (&date, "wrzesień 2018");
1132 : 1 : g_assert_true (g_date_valid (&date));
1133 : 1 : g_assert_cmpint (g_date_get_month (&date), ==, G_DATE_SEPTEMBER);
1134 : :
1135 : 1 : g_date_set_parse (&date, "sie 2018");
1136 : 1 : g_assert_true (g_date_valid (&date));
1137 : 1 : g_assert_cmpint (g_date_get_month (&date), ==, G_DATE_AUGUST);
1138 : :
1139 : 1 : g_date_set_parse (&date, "sierpień 2018");
1140 : 1 : g_assert_true (g_date_valid (&date));
1141 : 1 : g_assert_cmpint (g_date_get_month (&date), ==, G_DATE_AUGUST);
1142 : :
1143 : : #ifdef G_OS_WIN32
1144 : : SetThreadLocale (old_lcid);
1145 : : #endif
1146 : 1 : setlocale (LC_ALL, "");
1147 : : }
1148 : :
1149 : :
1150 : : static void
1151 : 1 : test_month_names (void)
1152 : : {
1153 : : #if defined(HAVE_LANGINFO_ABALTMON) || defined(G_OS_WIN32)
1154 : : GDate *gdate;
1155 : : gchar buf[101];
1156 : : gchar *oldlocale;
1157 : : #ifdef G_OS_WIN32
1158 : : LCID old_lcid;
1159 : : #endif
1160 : : #endif /* defined(HAVE_LANGINFO_ABALTMON) || defined(G_OS_WIN32) */
1161 : :
1162 : 1 : g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=749206");
1163 : :
1164 : : /* If running uninstalled (G_TEST_BUILDDIR is set), skip this test, since we
1165 : : * need the translations to be installed. We can’t mess around with
1166 : : * bindtextdomain() here, as the compiled .gmo files in po/ are not in the
1167 : : * right installed directory hierarchy to be successfully loaded by gettext. */
1168 [ + - ]: 1 : if (g_getenv ("G_TEST_BUILDDIR") != NULL)
1169 : : {
1170 : 1 : g_test_skip ("Skipping due to running uninstalled. "
1171 : : "This test can only be run when the translations are installed.");
1172 : 1 : return;
1173 : : }
1174 : :
1175 : : /* This test can only work (on non-Windows platforms) if libc supports
1176 : : * the %OB (etc.) format placeholders. If it doesn’t, strftime() (and hence
1177 : : * g_date_strftime()) will return the placeholder unsubstituted.
1178 : : * g_date_strftime() explicitly documents that it doesn’t provide any more
1179 : : * format placeholders than the system strftime(), so we should skip the test
1180 : : * in that case. If people need %OB support, they should depend on a suitable
1181 : : * version of libc, or use g_date_time_format(). Note: a test for a support
1182 : : * of _NL_ABALTMON_* is not strictly the same as checking for %OB support.
1183 : : * Some platforms (BSD, OS X) support %OB while _NL_ABALTMON_* and %Ob
1184 : : * are supported only by glibc 2.27 and newer. But we don’t care about BSD
1185 : : * here, the aim of this test is to make sure that our custom implementation
1186 : : * for Windows works the same as glibc 2.27 native implementation. */
1187 : : #if !defined(HAVE_LANGINFO_ABALTMON) && !defined(G_OS_WIN32)
1188 : 0 : g_test_skip ("libc doesn’t support all alternative month names");
1189 : : #else
1190 : :
1191 : : #define TEST_DATE(d, m, y, f, o) \
1192 : : G_STMT_START \
1193 : : { \
1194 : : gchar *o_casefold, *buf_casefold; \
1195 : : g_date_set_dmy (gdate, d, m, y); \
1196 : : g_date_strftime (buf, sizeof (buf), f, gdate); \
1197 : : buf_casefold = g_utf8_casefold (buf, -1); \
1198 : : o_casefold = g_utf8_casefold ((o), -1); \
1199 : : g_assert_cmpstr (buf_casefold, ==, o_casefold); \
1200 : : g_free (buf_casefold); \
1201 : : g_free (o_casefold); \
1202 : : g_date_set_parse (gdate, buf); \
1203 : : g_assert_true (g_date_valid (gdate)); \
1204 : : g_assert_cmpint (g_date_get_day (gdate), ==, d); \
1205 : : g_assert_cmpint (g_date_get_month (gdate), ==, m); \
1206 : : g_assert_cmpint (g_date_get_year (gdate), ==, y); \
1207 : : } \
1208 : : G_STMT_END
1209 : :
1210 : : oldlocale = g_strdup (setlocale (LC_ALL, NULL));
1211 : : #ifdef G_OS_WIN32
1212 : : old_lcid = GetThreadLocale ();
1213 : : #endif
1214 : :
1215 : : gdate = g_date_new ();
1216 : :
1217 : : /* Note: Windows implementation of g_date_strftime() does not support
1218 : : * "-" format modifier (e.g., "%-d", "%-e") so we will not use it.
1219 : : */
1220 : :
1221 : : /* Make sure that nothing has been changed in western European languages. */
1222 : : setlocale (LC_ALL, "en_GB.utf-8");
1223 : : #ifdef G_OS_WIN32
1224 : : SetThreadLocale (MAKELCID (MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_UK), SORT_DEFAULT));
1225 : : #endif
1226 : : if (strstr (setlocale (LC_ALL, NULL), "en_GB") != NULL)
1227 : : {
1228 : : TEST_DATE (1, 1, 2018, "%B %d, %Y", "January 01, 2018");
1229 : : TEST_DATE (1, 2, 2018, "%OB %Y", "February 2018");
1230 : : TEST_DATE (1, 3, 2018, "%e %b %Y", " 1 Mar 2018");
1231 : : TEST_DATE (1, 4, 2018, "%Ob %Y", "Apr 2018");
1232 : : TEST_DATE (1, 5, 2018, "%d %h %Y", "01 May 2018");
1233 : : TEST_DATE (1, 6, 2018, "%Oh %Y", "Jun 2018");
1234 : : }
1235 : : else
1236 : : g_test_skip ("locale en_GB not available, skipping English month names test");
1237 : :
1238 : : setlocale (LC_ALL, "de_DE.utf-8");
1239 : : #ifdef G_OS_WIN32
1240 : : SetThreadLocale (MAKELCID (MAKELANGID (LANG_GERMAN, SUBLANG_GERMAN), SORT_DEFAULT));
1241 : : #endif
1242 : : if (strstr (setlocale (LC_ALL, NULL), "de_DE") != NULL)
1243 : : {
1244 : : TEST_DATE (16, 7, 2018, "%d. %B %Y", "16. Juli 2018");
1245 : : TEST_DATE ( 1, 8, 2018, "%OB %Y", "August 2018");
1246 : : TEST_DATE (18, 9, 2018, "%e. %b %Y", "18. Sep 2018");
1247 : : TEST_DATE ( 1, 10, 2018, "%Ob %Y", "Okt 2018");
1248 : : TEST_DATE (20, 11, 2018, "%d. %h %Y", "20. Nov 2018");
1249 : : TEST_DATE ( 1, 12, 2018, "%Oh %Y", "Dez 2018");
1250 : : }
1251 : : else
1252 : : g_test_skip ("locale de_DE not available, skipping German month names test");
1253 : :
1254 : :
1255 : : setlocale (LC_ALL, "es_ES.utf-8");
1256 : : #ifdef G_OS_WIN32
1257 : : SetThreadLocale (MAKELCID (MAKELANGID (LANG_SPANISH, SUBLANG_SPANISH_MODERN), SORT_DEFAULT));
1258 : : #endif
1259 : : if (strstr (setlocale (LC_ALL, NULL), "es_ES") != NULL)
1260 : : {
1261 : : TEST_DATE ( 9, 1, 2018, "%d de %B de %Y", "09 de enero de 2018");
1262 : : TEST_DATE ( 1, 2, 2018, "%OB de %Y", "febrero de 2018");
1263 : : #if defined(G_OS_WIN32) && defined(_UCRT)
1264 : : TEST_DATE (10, 3, 2018, "%e de %b de %Y", "10 de mar. de 2018");
1265 : : TEST_DATE ( 1, 4, 2018, "%Ob de %Y", "abr. de 2018");
1266 : : TEST_DATE (11, 5, 2018, "%d de %h de %Y", "11 de may. de 2018");
1267 : : TEST_DATE ( 1, 6, 2018, "%Oh de %Y", "jun. de 2018");
1268 : : #else
1269 : : TEST_DATE (10, 3, 2018, "%e de %b de %Y", "10 de mar de 2018");
1270 : : TEST_DATE ( 1, 4, 2018, "%Ob de %Y", "abr de 2018");
1271 : : TEST_DATE (11, 5, 2018, "%d de %h de %Y", "11 de may de 2018");
1272 : : TEST_DATE ( 1, 6, 2018, "%Oh de %Y", "jun de 2018");
1273 : : #endif
1274 : : }
1275 : : else
1276 : : g_test_skip ("locale es_ES not available, skipping Spanish month names test");
1277 : :
1278 : : setlocale (LC_ALL, "fr_FR.utf-8");
1279 : : #ifdef G_OS_WIN32
1280 : : SetThreadLocale (MAKELCID (MAKELANGID (LANG_FRENCH, SUBLANG_FRENCH), SORT_DEFAULT));
1281 : : #endif
1282 : : if (strstr (setlocale (LC_ALL, NULL), "fr_FR") != NULL)
1283 : : {
1284 : : TEST_DATE (31, 7, 2018, "%d %B %Y", "31 juillet 2018");
1285 : : TEST_DATE ( 1, 8, 2018, "%OB %Y", "août 2018");
1286 : : TEST_DATE (30, 9, 2018, "%e %b %Y", "30 sept. 2018");
1287 : : TEST_DATE ( 1, 10, 2018, "%Ob %Y", "oct. 2018");
1288 : : TEST_DATE (29, 11, 2018, "%d %h %Y", "29 nov. 2018");
1289 : : TEST_DATE ( 1, 12, 2018, "%Oh %Y", "déc. 2018");
1290 : : }
1291 : : else
1292 : : g_test_skip ("locale fr_FR not available, skipping French month names test");
1293 : :
1294 : : /* Make sure that there are visible changes in some European languages. */
1295 : : setlocale (LC_ALL, "el_GR.utf-8");
1296 : : #ifdef G_OS_WIN32
1297 : : SetThreadLocale (MAKELCID (MAKELANGID (LANG_GREEK, SUBLANG_GREEK_GREECE), SORT_DEFAULT));
1298 : : #endif
1299 : : if (strstr (setlocale (LC_ALL, NULL), "el_GR") != NULL)
1300 : : {
1301 : : TEST_DATE ( 2, 1, 2018, "%d %B %Y", "02 Ιανουαρίου 2018");
1302 : : TEST_DATE ( 4, 2, 2018, "%e %B %Y", " 4 Φεβρουαρίου 2018");
1303 : : TEST_DATE (15, 3, 2018, "%d %B %Y", "15 Μαρτίου 2018");
1304 : : TEST_DATE ( 1, 4, 2018, "%OB %Y", "Απρίλιος 2018");
1305 : : TEST_DATE ( 1, 5, 2018, "%OB %Y", "Μάιος 2018");
1306 : : TEST_DATE ( 1, 6, 2018, "%OB %Y", "Ιούνιος 2018");
1307 : : TEST_DATE (16, 7, 2018, "%e %b %Y", "16 Ιουλ 2018");
1308 : : #if defined(G_OS_WIN32) && defined(_UCRT)
1309 : : TEST_DATE ( 1, 8, 2018, "%Ob %Y", "Αυγ 2018");
1310 : : #else
1311 : : TEST_DATE ( 1, 8, 2018, "%Ob %Y", "Αύγ 2018");
1312 : : #endif
1313 : : }
1314 : : else
1315 : : g_test_skip ("locale el_GR not available, skipping Greek month names test");
1316 : :
1317 : : setlocale (LC_ALL, "hr_HR.utf-8");
1318 : : #ifdef G_OS_WIN32
1319 : : SetThreadLocale (MAKELCID (MAKELANGID (LANG_CROATIAN, SUBLANG_CROATIAN_CROATIA), SORT_DEFAULT));
1320 : : #endif
1321 : : if (strstr (setlocale (LC_ALL, NULL), "hr_HR") != NULL)
1322 : : {
1323 : : TEST_DATE ( 8, 5, 2018, "%d. %B %Y", "08. svibnja 2018");
1324 : : TEST_DATE ( 9, 6, 2018, "%e. %B %Y", " 9. lipnja 2018");
1325 : : TEST_DATE (10, 7, 2018, "%d. %B %Y", "10. srpnja 2018");
1326 : : TEST_DATE ( 1, 8, 2018, "%OB %Y", "Kolovoz 2018");
1327 : : TEST_DATE ( 1, 9, 2018, "%OB %Y", "Rujan 2018");
1328 : : TEST_DATE ( 1, 10, 2018, "%OB %Y", "Listopad 2018");
1329 : : TEST_DATE (11, 11, 2018, "%e. %b %Y", "11. Stu 2018");
1330 : : TEST_DATE ( 1, 12, 2018, "%Ob %Y", "Pro 2018");
1331 : : }
1332 : : else
1333 : : g_test_skip ("locale hr_HR not available, skipping Croatian month names test");
1334 : :
1335 : : setlocale (LC_ALL, "lt_LT.utf-8");
1336 : : #ifdef G_OS_WIN32
1337 : : SetThreadLocale (MAKELCID (MAKELANGID (LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA), SORT_DEFAULT));
1338 : : #endif
1339 : : if (strstr (setlocale (LC_ALL, NULL), "lt_LT") != NULL)
1340 : : {
1341 : : TEST_DATE ( 1, 1, 2018, "%Y m. %B %d d.", "2018 m. sausio 01 d.");
1342 : : TEST_DATE ( 2, 2, 2018, "%Y m. %B %e d.", "2018 m. vasario 2 d.");
1343 : : TEST_DATE ( 3, 3, 2018, "%Y m. %B %d d.", "2018 m. kovo 03 d.");
1344 : : TEST_DATE ( 1, 4, 2018, "%Y m. %OB", "2018 m. balandis");
1345 : : TEST_DATE ( 1, 5, 2018, "%Y m. %OB", "2018 m. gegužė");
1346 : : TEST_DATE ( 1, 6, 2018, "%Y m. %OB", "2018 m. birželis");
1347 : : TEST_DATE (17, 7, 2018, "%Y m. %b %e d.", "2018 m. liep. 17 d.");
1348 : : TEST_DATE ( 1, 8, 2018, "%Y m. %Ob", "2018 m. rugp.");
1349 : : }
1350 : : else
1351 : : g_test_skip ("locale lt_LT not available, skipping Lithuanian month names test");
1352 : :
1353 : : setlocale (LC_ALL, "pl_PL.utf-8");
1354 : : #ifdef G_OS_WIN32
1355 : : SetThreadLocale (MAKELCID (MAKELANGID (LANG_POLISH, SUBLANG_POLISH_POLAND), SORT_DEFAULT));
1356 : : #endif
1357 : : if (strstr (setlocale (LC_ALL, NULL), "pl_PL") != NULL)
1358 : : {
1359 : : TEST_DATE ( 3, 5, 2018, "%d %B %Y", "03 maja 2018");
1360 : : TEST_DATE ( 4, 6, 2018, "%e %B %Y", " 4 czerwca 2018");
1361 : : TEST_DATE (20, 7, 2018, "%d %B %Y", "20 lipca 2018");
1362 : : TEST_DATE ( 1, 8, 2018, "%OB %Y", "sierpień 2018");
1363 : : TEST_DATE ( 1, 9, 2018, "%OB %Y", "wrzesień 2018");
1364 : : TEST_DATE ( 1, 10, 2018, "%OB %Y", "październik 2018");
1365 : : TEST_DATE (25, 11, 2018, "%e %b %Y", "25 lis 2018");
1366 : : TEST_DATE ( 1, 12, 2018, "%Ob %Y", "gru 2018");
1367 : : }
1368 : : else
1369 : : g_test_skip ("locale pl_PL not available, skipping Polish month names test");
1370 : :
1371 : : setlocale (LC_ALL, "ru_RU.utf-8");
1372 : : #ifdef G_OS_WIN32
1373 : : SetThreadLocale (MAKELCID (MAKELANGID (LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA), SORT_DEFAULT));
1374 : : #endif
1375 : : if (strstr (setlocale (LC_ALL, NULL), "ru_RU") != NULL)
1376 : : {
1377 : : TEST_DATE ( 3, 1, 2018, "%d %B %Y", "03 января 2018");
1378 : : TEST_DATE ( 4, 2, 2018, "%e %B %Y", " 4 февраля 2018");
1379 : : TEST_DATE (23, 3, 2018, "%d %B %Y", "23 марта 2018");
1380 : : TEST_DATE ( 1, 4, 2018, "%OB %Y", "Апрель 2018");
1381 : : TEST_DATE ( 1, 5, 2018, "%OB %Y", "Май 2018");
1382 : : TEST_DATE ( 1, 6, 2018, "%OB %Y", "Июнь 2018");
1383 : : TEST_DATE (24, 7, 2018, "%e %b %Y", "24 июл 2018");
1384 : : TEST_DATE ( 1, 8, 2018, "%Ob %Y", "авг 2018");
1385 : : /* This difference is very important in Russian: */
1386 : : TEST_DATE (19, 5, 2018, "%e %b %Y", "19 мая 2018");
1387 : : TEST_DATE (20, 5, 2018, "%Ob, %d-е, %Y", "май, 20-е, 2018");
1388 : : }
1389 : : else
1390 : : g_test_skip ("locale ru_RU not available, skipping Russian month names test");
1391 : :
1392 : : g_date_free (gdate);
1393 : :
1394 : : setlocale (LC_ALL, oldlocale);
1395 : : #ifdef G_OS_WIN32
1396 : : SetThreadLocale (old_lcid);
1397 : : #endif
1398 : : g_free (oldlocale);
1399 : : #endif /* defined(HAVE_LANGINFO_ABALTMON) || defined(G_OS_WIN32) */
1400 : : }
1401 : :
1402 : : static void
1403 : 104 : test_year (gconstpointer t)
1404 : : {
1405 : 104 : GDateYear y = GPOINTER_TO_INT (t);
1406 : : GDateMonth m;
1407 : : GDateDay day;
1408 : 104 : guint32 j = 0;
1409 : : GDate *d;
1410 : : gint i;
1411 : : GDate tmp;
1412 : :
1413 : 104 : guint32 first_day_of_year = G_DATE_BAD_JULIAN;
1414 [ + + ]: 104 : guint16 days_in_year = g_date_is_leap_year (y) ? 366 : 365;
1415 : 104 : guint sunday_week_of_year = 0;
1416 : 104 : guint sunday_weeks_in_year = g_date_get_sunday_weeks_in_year (y);
1417 : 104 : guint monday_week_of_year = 0;
1418 : 104 : guint monday_weeks_in_year = g_date_get_monday_weeks_in_year (y);
1419 : 104 : guint iso8601_week_of_year = 0;
1420 : :
1421 : 104 : g_assert_true (g_date_valid_year (y));
1422 : : /* Years ought to have roundabout 52 weeks */
1423 : 104 : g_assert (sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53);
1424 : 104 : g_assert (monday_weeks_in_year == 52 || monday_weeks_in_year == 53);
1425 : :
1426 : 104 : m = 1;
1427 [ + + ]: 1352 : while (m < 13)
1428 : : {
1429 : 1248 : guint8 dim = g_date_get_days_in_month (m, y);
1430 : : GDate days[31];
1431 : :
1432 : 1248 : g_date_clear (days, 31);
1433 : :
1434 : 1248 : g_assert (dim > 0 && dim < 32);
1435 : 1248 : g_assert_true (g_date_valid_month (m));
1436 : :
1437 : 1248 : day = 1;
1438 [ + + ]: 39228 : while (day <= dim)
1439 : : {
1440 : 37980 : g_assert_true (g_date_valid_dmy (day, m, y));
1441 : :
1442 : 37980 : d = &days[day - 1];
1443 : 37980 : g_assert_false (g_date_valid (d));
1444 : :
1445 : 37980 : g_date_set_dmy (d, day, m, y);
1446 : :
1447 : 37980 : g_assert_true (g_date_valid (d));
1448 : :
1449 [ + + + + ]: 37980 : if (m == G_DATE_JANUARY && day == 1)
1450 : 104 : first_day_of_year = g_date_get_julian (d);
1451 : :
1452 : 37980 : g_assert_cmpint (first_day_of_year, !=, G_DATE_BAD_JULIAN);
1453 : :
1454 : 37980 : g_assert_cmpint (g_date_get_month (d), ==, m);
1455 : 37980 : g_assert_cmpint (g_date_get_year (d), ==, y);
1456 : 37980 : g_assert_cmpint (g_date_get_day (d), ==, day);
1457 : :
1458 : 37980 : g_assert_cmpint (g_date_get_julian (d) + 1 - first_day_of_year,
1459 : : ==,
1460 : : g_date_get_day_of_year (d));
1461 : :
1462 [ + + + + ]: 37980 : if (m == G_DATE_DECEMBER && day == 31)
1463 : 104 : g_assert_cmpint (g_date_get_day_of_year (d), ==, days_in_year);
1464 : :
1465 : 37980 : g_assert_cmpint (g_date_get_day_of_year (d), <=, days_in_year);
1466 : 37980 : g_assert_cmpint (g_date_get_monday_week_of_year (d),
1467 : : <=, monday_weeks_in_year);
1468 : 37980 : g_assert_cmpint (g_date_get_monday_week_of_year (d),
1469 : : >=, monday_week_of_year);
1470 : :
1471 [ + + ]: 37980 : if (g_date_get_weekday(d) == G_DATE_MONDAY)
1472 : : {
1473 : 5426 : g_assert_cmpint (g_date_get_monday_week_of_year (d) -
1474 : : monday_week_of_year,
1475 : : ==, 1);
1476 [ + + + + : 5426 : if ((m == G_DATE_JANUARY && day <= 4) ||
+ + ]
1477 [ + + ]: 461 : (m == G_DATE_DECEMBER && day >= 29))
1478 : 104 : g_assert_cmpint (g_date_get_iso8601_week_of_year (d),
1479 : : ==, 1);
1480 : : else
1481 : 5322 : g_assert_cmpint (g_date_get_iso8601_week_of_year (d) -
1482 : : iso8601_week_of_year,
1483 : : ==, 1);
1484 : : }
1485 : : else
1486 : : {
1487 : 32554 : g_assert_cmpint (g_date_get_monday_week_of_year (d) -
1488 : : monday_week_of_year,
1489 : : ==, 0);
1490 [ + + + + ]: 32554 : if (!(day == 1 && m == G_DATE_JANUARY))
1491 : 32465 : g_assert_cmpint (g_date_get_iso8601_week_of_year (d) -
1492 : : iso8601_week_of_year,
1493 : : ==, 0);
1494 : : }
1495 : :
1496 : 37980 : monday_week_of_year = g_date_get_monday_week_of_year (d);
1497 : 37980 : iso8601_week_of_year = g_date_get_iso8601_week_of_year (d);
1498 : :
1499 : 37980 : g_assert_cmpint (g_date_get_sunday_week_of_year (d),
1500 : : <=, sunday_weeks_in_year);
1501 : 37980 : g_assert_cmpint (g_date_get_sunday_week_of_year (d),
1502 : : >=, sunday_week_of_year);
1503 [ + + ]: 37980 : if (g_date_get_weekday(d) == G_DATE_SUNDAY)
1504 : 5427 : g_assert_cmpint (g_date_get_sunday_week_of_year (d) -
1505 : : sunday_week_of_year,
1506 : : ==, 1);
1507 : : else
1508 : 32553 : g_assert_cmpint (g_date_get_sunday_week_of_year (d) -
1509 : : sunday_week_of_year,
1510 : : ==, 0);
1511 : :
1512 : 37980 : sunday_week_of_year = g_date_get_sunday_week_of_year (d);
1513 : :
1514 : 37980 : g_assert_cmpint (g_date_compare (d, d), ==, 0);
1515 : :
1516 : 37980 : i = 1;
1517 [ + + ]: 1595160 : while (i < 402) /* Need to get 400 year increments in */
1518 : : {
1519 : 1557180 : tmp = *d;
1520 : 1557180 : g_date_add_days (d, i);
1521 : 1557180 : g_assert_cmpint (g_date_compare (d, &tmp), >, 0);
1522 : 1557180 : g_date_subtract_days (d, i);
1523 : 1557180 : g_assert_cmpint (g_date_get_day (d), ==, day);
1524 : 1557180 : g_assert_cmpint (g_date_get_month (d), ==, m);
1525 : 1557180 : g_assert_cmpint (g_date_get_year (d), ==, y);
1526 : :
1527 : 1557180 : tmp = *d;
1528 : 1557180 : g_date_add_months (d, i);
1529 : 1557180 : g_assert_cmpint (g_date_compare (d, &tmp), >, 0);
1530 : 1557180 : g_date_subtract_months (d, i);
1531 : 1557180 : g_assert_cmpint (g_date_get_month (d), ==, m);
1532 : 1557180 : g_assert_cmpint (g_date_get_year (d), ==, y);
1533 : :
1534 [ + + ]: 1557180 : if (day < 29)
1535 : 1432704 : g_assert_cmpint (g_date_get_day (d), ==, day);
1536 : : else
1537 : 124476 : g_date_set_day (d, day);
1538 : :
1539 : 1557180 : tmp = *d;
1540 : 1557180 : g_date_add_years (d, i);
1541 : 1557180 : g_assert_cmpint (g_date_compare (d, &tmp), >, 0);
1542 : 1557180 : g_date_subtract_years (d, i);
1543 : 1557180 : g_assert_cmpint (g_date_get_month (d), ==, m);
1544 : 1557180 : g_assert_cmpint (g_date_get_year (d), ==, y);
1545 : :
1546 [ + + + + ]: 1557180 : if (m != 2 && day != 29)
1547 : 1390064 : g_assert_cmpint (g_date_get_day (d), ==, day);
1548 : : else
1549 : 167116 : g_date_set_day (d, day); /* reset */
1550 : :
1551 : 1557180 : i += 10;
1552 : : }
1553 : :
1554 : 37980 : j = g_date_get_julian (d);
1555 : :
1556 : 37980 : ++day;
1557 : : }
1558 : 1248 : ++m;
1559 : : }
1560 : :
1561 : : /* at this point, d is the last day of year y */
1562 : 104 : g_date_set_dmy (&tmp, 1, 1, y + 1);
1563 : 104 : g_assert_cmpint (j + 1, ==, g_date_get_julian (&tmp));
1564 : :
1565 : 104 : g_date_add_days (&tmp, 1);
1566 : 104 : g_assert_cmpint (j + 2, ==, g_date_get_julian (&tmp));
1567 : 104 : }
1568 : :
1569 : : static void
1570 : 1 : test_clamp (void)
1571 : : {
1572 : : GDate *d, *d1, *d2, *o;
1573 : :
1574 : 1 : d = g_date_new ();
1575 : 1 : d1 = g_date_new ();
1576 : 1 : d2 = g_date_new ();
1577 [ + - ]: 1 : if (g_test_undefined ())
1578 : : {
1579 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1580 : : "*assertion *failed*");
1581 : 1 : g_date_clamp (d, d1, d2);
1582 : 1 : g_test_assert_expected_messages ();
1583 : : }
1584 : :
1585 : 1 : g_date_set_dmy (d, 1, 1, 1);
1586 : 1 : g_date_clamp (d, NULL, NULL);
1587 [ + - ]: 1 : if (g_test_undefined ())
1588 : : {
1589 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1590 : : "*assertion *failed*");
1591 : 1 : g_date_clamp (d, d1, NULL);
1592 : 1 : g_test_assert_expected_messages ();
1593 : :
1594 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1595 : : "*assertion *failed*");
1596 : 1 : g_date_clamp (d, d1, d2);
1597 : 1 : g_test_assert_expected_messages ();
1598 : : }
1599 : :
1600 : 1 : g_date_set_dmy (d1, 1, 1, 1970);
1601 [ + - ]: 1 : if (g_test_undefined ())
1602 : : {
1603 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1604 : : "*assertion *failed*");
1605 : 1 : g_date_clamp (d, d1, d2);
1606 : 1 : g_test_assert_expected_messages ();
1607 : :
1608 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1609 : : "*assertion *failed*");
1610 : 1 : g_date_clamp (d, NULL, d2);
1611 : 1 : g_test_assert_expected_messages ();
1612 : : }
1613 : :
1614 : 1 : g_date_set_dmy (d2, 1, 1, 1980);
1615 [ + - ]: 1 : if (g_test_undefined ())
1616 : : {
1617 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1618 : : "*assertion *failed*");
1619 : 1 : g_date_clamp (d, d2, d1);
1620 : 1 : g_test_assert_expected_messages ();
1621 : : }
1622 : :
1623 : 1 : o = g_date_copy (d);
1624 : 1 : g_date_clamp (o, NULL, NULL);
1625 : 1 : g_assert_cmpint (g_date_compare (o, d), ==, 0);
1626 : :
1627 : 1 : g_date_clamp (o, d1, d2);
1628 : 1 : g_assert_cmpint (g_date_compare (o, d1), ==, 0);
1629 : :
1630 : 1 : g_date_set_dmy (o, 1, 1, 2000);
1631 : :
1632 : 1 : g_date_clamp (o, d1, d2);
1633 : 1 : g_assert_cmpint (g_date_compare (o, d2), ==, 0);
1634 : :
1635 : 1 : g_date_free (d);
1636 : 1 : g_date_free (d1);
1637 : 1 : g_date_free (d2);
1638 : 1 : g_date_free (o);
1639 : 1 : }
1640 : :
1641 : : static void
1642 : 1 : test_order (void)
1643 : : {
1644 : : GDate *d1, *d2;
1645 : :
1646 : 1 : d1 = g_date_new ();
1647 : 1 : d2 = g_date_new ();
1648 : :
1649 [ + - ]: 1 : if (g_test_undefined ())
1650 : : {
1651 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1652 : : "*assertion *failed*");
1653 : 1 : g_date_order (d1, d2);
1654 : 1 : g_test_assert_expected_messages ();
1655 : : }
1656 : :
1657 : 1 : g_date_set_dmy (d1, 1, 1, 1970);
1658 : :
1659 [ + - ]: 1 : if (g_test_undefined ())
1660 : : {
1661 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1662 : : "*assertion *failed*");
1663 : 1 : g_date_order (d1, d2);
1664 : 1 : g_test_assert_expected_messages ();
1665 : : }
1666 : :
1667 : 1 : g_date_set_dmy (d2, 1, 1, 1980);
1668 : :
1669 : 1 : g_date_order (d1, d2);
1670 : 1 : g_assert_cmpint (g_date_compare (d1, d2), ==, -1);
1671 : 1 : g_date_order (d2, d1);
1672 : 1 : g_assert_cmpint (g_date_compare (d1, d2), ==, 1);
1673 : :
1674 : 1 : g_date_free (d1);
1675 : 1 : g_date_free (d2);
1676 : 1 : }
1677 : :
1678 : : static void
1679 : 1 : test_copy (void)
1680 : : {
1681 : : GDate *d;
1682 : : GDate *c;
1683 : :
1684 [ + - ]: 1 : if (g_test_undefined ())
1685 : : {
1686 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1687 : : "*assertion *failed*");
1688 : 1 : g_assert_null (g_date_copy (NULL));
1689 : 1 : g_test_assert_expected_messages ();
1690 : : }
1691 : :
1692 : 1 : d = g_date_new ();
1693 : 1 : g_assert_false (g_date_valid (d));
1694 : :
1695 : 1 : c = g_date_copy (d);
1696 : 1 : g_assert_nonnull (c);
1697 : 1 : g_assert_false (g_date_valid (c));
1698 : 1 : g_date_free (c);
1699 : :
1700 : 1 : g_date_set_day (d, 10);
1701 : :
1702 : 1 : c = g_date_copy (d);
1703 : 1 : g_date_set_month (c, 1);
1704 : 1 : g_date_set_year (c, 2015);
1705 : 1 : g_assert_true (g_date_valid (c));
1706 : 1 : g_assert_cmpuint (g_date_get_day (c), ==, 10);
1707 : 1 : g_date_free (c);
1708 : :
1709 : 1 : g_date_free (d);
1710 : 1 : }
1711 : :
1712 : : /* Check the results of g_date_valid_dmy() for various inputs. */
1713 : : static void
1714 : 1 : test_valid_dmy (void)
1715 : : {
1716 : : const struct
1717 : : {
1718 : : GDateDay day;
1719 : : GDateMonth month;
1720 : : GDateYear year;
1721 : : gboolean expected_valid;
1722 : : }
1723 : 1 : vectors[] =
1724 : : {
1725 : : /* Lower bounds */
1726 : : { 0, 0, 0, FALSE },
1727 : : { 1, 1, 1, TRUE },
1728 : : { 1, 1, 0, FALSE },
1729 : : /* Leap year month lengths */
1730 : : { 30, 2, 2000, FALSE },
1731 : : { 29, 2, 2000, TRUE },
1732 : : { 29, 2, 2001, FALSE },
1733 : : /* Maximum year */
1734 : : { 1, 1, G_MAXUINT16, TRUE },
1735 : : };
1736 : : gsize i;
1737 : :
1738 [ + + ]: 8 : for (i = 0; i < G_N_ELEMENTS (vectors); i++)
1739 : : {
1740 : : gboolean valid;
1741 : 7 : g_test_message ("Vector %" G_GSIZE_FORMAT ": %04u-%02u-%02u, %s",
1742 : 7 : i, vectors[i].year, vectors[i].month, vectors[i].day,
1743 [ + + ]: 7 : vectors[i].expected_valid ? "valid" : "invalid");
1744 : :
1745 : 7 : valid = g_date_valid_dmy (vectors[i].day,
1746 : 7 : vectors[i].month,
1747 : 7 : vectors[i].year);
1748 : :
1749 [ + + ]: 7 : if (vectors[i].expected_valid)
1750 : 3 : g_assert_true (valid);
1751 : : else
1752 : 4 : g_assert_false (valid);
1753 : : }
1754 : 1 : }
1755 : :
1756 : : int
1757 : 1 : main (int argc, char** argv)
1758 : : {
1759 : : gchar *path;
1760 : : gsize i;
1761 : :
1762 : : /* Try to get all the leap year cases. */
1763 : 1 : int check_years[] = {
1764 : : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1765 : : 11, 12, 13, 14, 98, 99, 100, 101, 102, 103, 397,
1766 : : 398, 399, 400, 401, 402, 403, 404, 405, 406,
1767 : : 1598, 1599, 1600, 1601, 1602, 1650, 1651,
1768 : : 1897, 1898, 1899, 1900, 1901, 1902, 1903,
1769 : : 1961, 1962, 1963, 1964, 1965, 1967,
1770 : : 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976,
1771 : : 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985,
1772 : : 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
1773 : : 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
1774 : : 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
1775 : : 3000, 3001, 3002, 3998, 3999, 4000, 4001, 4002, 4003
1776 : : };
1777 : :
1778 : 1 : g_setenv ("LC_ALL", "en_US.utf-8", TRUE);
1779 : 1 : setlocale (LC_ALL, "");
1780 : : #ifdef G_OS_WIN32
1781 : : SetThreadLocale (MAKELCID (MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT));
1782 : : #endif
1783 : :
1784 : 1 : g_test_init (&argc, &argv, NULL);
1785 : :
1786 : 1 : g_test_add_func ("/date/basic", test_basic);
1787 : 1 : g_test_add_func ("/date/empty", test_empty_constructor);
1788 : 1 : g_test_add_func ("/date/dmy", test_dmy_constructor);
1789 : 1 : g_test_add_func ("/date/julian", test_julian_constructor);
1790 : 1 : g_test_add_func ("/date/compare", test_date_compare);
1791 : 1 : g_test_add_func ("/date/dates", test_dates);
1792 : 1 : g_test_add_func ("/date/strftime", test_strftime);
1793 : 1 : g_test_add_func ("/date/two-digit-years", test_two_digit_years);
1794 : 1 : g_test_add_func ("/date/parse", test_parse);
1795 : 1 : g_test_add_func ("/date/parse/invalid", test_parse_invalid);
1796 : 1 : g_test_add_func ("/date/parse_locale_change", test_parse_locale_change);
1797 : 1 : g_test_add_func ("/date/month_substring", test_month_substring);
1798 : 1 : g_test_add_func ("/date/month_names", test_month_names);
1799 : 1 : g_test_add_func ("/date/clamp", test_clamp);
1800 : 1 : g_test_add_func ("/date/order", test_order);
1801 [ + + ]: 105 : for (i = 0; i < G_N_ELEMENTS (check_years); i++)
1802 : : {
1803 : 104 : path = g_strdup_printf ("/date/year/%d", check_years[i]);
1804 : 104 : g_test_add_data_func (path, GINT_TO_POINTER(check_years[i]), test_year);
1805 : 104 : g_free (path);
1806 : : }
1807 : 1 : g_test_add_func ("/date/copy", test_copy);
1808 : 1 : g_test_add_func ("/date/valid-dmy", test_valid_dmy);
1809 : :
1810 : 1 : return g_test_run ();
1811 : : }
|