1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
|
#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN
/* We are testing some deprecated APIs here */
#ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
#define GLIB_DISABLE_DEPRECATION_WARNINGS
#endif
#include "config.h"
#include "glib.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
#include <time.h>
#ifdef G_OS_WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
/* mingw defines it while msvc doesn't */
#ifndef SUBLANG_LITHUANIAN_LITHUANIA
#define SUBLANG_LITHUANIAN_LITHUANIA 0x01
#endif
#endif
static void
test_basic (void)
{
g_assert_cmpint (sizeof (GDate), <, 9);
g_assert (!g_date_valid_month (G_DATE_BAD_MONTH));
g_assert (!g_date_valid_month (13));
g_assert (!g_date_valid_day (G_DATE_BAD_DAY));
g_assert (!g_date_valid_day (32));
g_assert (!g_date_valid_year (G_DATE_BAD_YEAR));
g_assert (!g_date_valid_julian (G_DATE_BAD_JULIAN));
g_assert (!g_date_valid_weekday (G_DATE_BAD_WEEKDAY));
g_assert (g_date_is_leap_year (2000));
g_assert (!g_date_is_leap_year (1999));
g_assert (g_date_is_leap_year (1996));
g_assert (g_date_is_leap_year (1600));
g_assert (!g_date_is_leap_year (2100));
g_assert (!g_date_is_leap_year (1800));
}
static void
test_empty_constructor (void)
{
GDate *d;
d = g_date_new ();
g_assert (!g_date_valid (d));
g_date_free (d);
}
static void
test_dmy_constructor (void)
{
GDate *d;
guint32 j;
d = g_date_new_dmy (1, 1, 1);
g_assert (g_date_valid (d));
j = g_date_get_julian (d);
g_assert_cmpint (j, ==, 1);
g_assert_cmpint (g_date_get_month (d), ==, G_DATE_JANUARY);
g_assert_cmpint (g_date_get_day (d), ==, 1);
g_assert_cmpint (g_date_get_year (d), ==, 1);
g_date_free (d);
}
static void
test_julian_constructor (void)
{
GDate *d1;
GDate *d2;
d1 = g_date_new_julian (4000);
d2 = g_date_new_julian (5000);
g_assert_cmpint (g_date_get_julian (d1), ==, 4000);
g_assert_cmpint (g_date_days_between (d1, d2), ==, 1000);
g_assert_cmpint (g_date_get_year (d1), ==, 11);
g_assert_cmpint (g_date_get_day (d2), ==, 9);
g_date_free (d1);
g_date_free (d2);
}
static void
test_dates (void)
{
GDate *d;
GTimeVal tv;
time_t now;
d = g_date_new ();
/* today */
now = time (NULL);
g_assert_cmpint (now, !=, (time_t) -1);
g_date_set_time (d, now);
g_assert (g_date_valid (d));
/* Unix epoch */
g_date_set_time (d, 1);
g_assert (g_date_valid (d));
tv.tv_sec = 0;
tv.tv_usec = 0;
g_date_set_time_val (d, &tv);
g_assert (g_date_valid (d));
/* Julian day 1 */
g_date_set_julian (d, 1);
g_assert (g_date_valid (d));
g_date_set_year (d, 3);
g_date_set_day (d, 3);
g_date_set_month (d, 3);
g_assert (g_date_valid (d));
g_assert_cmpint (g_date_get_year (d), ==, 3);
g_assert_cmpint (g_date_get_month (d), ==, 3);
g_assert_cmpint (g_date_get_day (d), ==, 3);
g_assert (!g_date_is_first_of_month (d));
g_assert (!g_date_is_last_of_month (d));
g_date_set_day (d, 1);
g_assert (g_date_is_first_of_month (d));
g_date_subtract_days (d, 1);
g_assert (g_date_is_last_of_month (d));
g_date_free (d);
}
static void
test_parse (void)
{
GDate *d;
gchar buf[101];
d = g_date_new ();
g_date_set_dmy (d, 10, 1, 2000);
g_date_strftime (buf, 100, "%x", d);
g_date_set_parse (d, buf);
g_assert (g_date_valid (d));
g_assert_cmpint (g_date_get_month (d), ==, 1);
g_assert_cmpint (g_date_get_day (d), ==, 10);
g_assert_cmpint (g_date_get_year (d), ==, 2000);
g_date_set_parse (d, "2001 10 1");
g_assert (g_date_valid (d));
g_assert_cmpint (g_date_get_month (d), ==, 10);
g_assert_cmpint (g_date_get_day (d), ==, 1);
g_assert_cmpint (g_date_get_year (d), ==, 2001);
g_date_set_parse (d, "2001 10");
g_assert (!g_date_valid (d));
g_date_set_parse (d, "2001 10 1 1");
g_assert (!g_date_valid (d));
g_date_set_parse (d, "March 1999");
g_assert (g_date_valid (d));
g_assert_cmpint (g_date_get_month (d), ==, 3);
g_assert_cmpint (g_date_get_day (d), ==, 1);
g_assert_cmpint (g_date_get_year (d), ==, 1999);
g_date_set_parse (d, "10 Sep 1087");
g_assert (g_date_valid (d));
g_assert_cmpint (g_date_get_month (d), ==, 9);
g_assert_cmpint (g_date_get_day (d), ==, 10);
g_assert_cmpint (g_date_get_year (d), ==, 1087);
g_date_set_parse (d, "19990301");
g_assert (g_date_valid (d));
g_assert_cmpint (g_date_get_month (d), ==, 3);
g_assert_cmpint (g_date_get_day (d), ==, 1);
g_assert_cmpint (g_date_get_year (d), ==, 1999);
g_date_set_parse (d, "20011320");
g_assert (!g_date_valid (d));
g_date_free (d);
}
static void
test_parse_invalid (void)
{
const gchar * const strs[] =
{
/* Incomplete UTF-8 sequence */
"\xfd",
/* Ridiculously long input */
"12345678901234567890123456789012345678901234567890123456789012345678901234567890"
"12345678901234567890123456789012345678901234567890123456789012345678901234567890"
"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
};
gsize i;
for (i = 0; i < G_N_ELEMENTS (strs); i++)
{
GDate *d = g_date_new ();
g_test_message ("Test %" G_GSIZE_FORMAT, i);
g_date_set_parse (d, strs[i]);
g_assert_false (g_date_valid (d));
g_date_free (d);
}
}
static void
test_parse_locale_change (void)
{
/* Checks that g_date_set_parse correctly changes locale specific data as
* necessary. In this particular case year adjustment, as Thai calendar is
* 543 years ahead of the Gregorian calendar. */
GDate date;
if (setlocale (LC_ALL, "th_TH") == NULL)
{
g_test_skip ("locale th_TH not available");
return;
}
g_date_set_parse (&date, "04/07/2519");
setlocale (LC_ALL, "C");
g_date_set_parse (&date, "07/04/76");
g_assert_cmpint (g_date_get_day (&date), ==, 4);
g_assert_cmpint (g_date_get_month (&date), ==, 7);
g_assert_cmpint (g_date_get_year (&date), ==, 1976);
setlocale (LC_ALL, "");
}
static void
test_month_substring (void)
{
GDate date;
g_test_bug ("793550");
if (setlocale (LC_ALL, "pl_PL") == NULL)
{
g_test_skip ("pl_PL locale not available");
return;
}
/* In Polish language September is "wrzesień" and August is "sierpień"
* abbreviated as "sie". The former used to be confused with the latter
* because "sie" is a substring of "wrzesień" and was matched first. */
g_date_set_parse (&date, "wrzesień 2018");
g_assert_true (g_date_valid (&date));
g_assert_cmpint (g_date_get_month (&date), ==, G_DATE_SEPTEMBER);
g_date_set_parse (&date, "sie 2018");
g_assert_true (g_date_valid (&date));
g_assert_cmpint (g_date_get_month (&date), ==, G_DATE_AUGUST);
g_date_set_parse (&date, "sierpień 2018");
g_assert_true (g_date_valid (&date));
g_assert_cmpint (g_date_get_month (&date), ==, G_DATE_AUGUST);
setlocale (LC_ALL, "");
}
static void
test_month_names (void)
{
#if defined(HAVE_LANGINFO_ABALTMON) || defined(G_OS_WIN32)
GDate *gdate;
gchar buf[101];
gchar *oldlocale;
#ifdef G_OS_WIN32
LCID old_lcid;
#endif
#endif /* defined(HAVE_LANGINFO_ABALTMON) || defined(G_OS_WIN32) */
g_test_bug ("749206");
/* If running uninstalled (G_TEST_BUILDDIR is set), skip this test, since we
* need the translations to be installed. We can’t mess around with
* bindtextdomain() here, as the compiled .gmo files in po/ are not in the
* right installed directory hierarchy to be successfully loaded by gettext. */
if (g_getenv ("G_TEST_BUILDDIR") != NULL)
{
g_test_skip ("Skipping due to running uninstalled. "
"This test can only be run when the translations are installed.");
return;
}
/* This test can only work (on non-Windows platforms) if libc supports
* the %OB (etc.) format placeholders. If it doesn’t, strftime() (and hence
* g_date_strftime()) will return the placeholder unsubstituted.
* g_date_strftime() explicitly documents that it doesn’t provide any more
* format placeholders than the system strftime(), so we should skip the test
* in that case. If people need %OB support, they should depend on a suitable
* version of libc, or use g_date_time_format(). Note: a test for a support
* of _NL_ABALTMON_* is not strictly the same as checking for %OB support.
* Some platforms (BSD, OS X) support %OB while _NL_ABALTMON_* and %Ob
* are supported only by glibc 2.27 and newer. But we don’t care about BSD
* here, the aim of this test is to make sure that our custom implementation
* for Windows works the same as glibc 2.27 native implementation. */
#if !defined(HAVE_LANGINFO_ABALTMON) && !defined(G_OS_WIN32)
g_test_skip ("libc doesn’t support all alternative month names");
#else
#define TEST_DATE(d,m,y,f,o) G_STMT_START { \
gchar *o_casefold, *buf_casefold; \
g_date_set_dmy (gdate, d, m, y); \
g_date_strftime (buf, 100, f, gdate); \
buf_casefold = g_utf8_casefold (buf, -1); \
o_casefold = g_utf8_casefold ((o), -1); \
g_assert_cmpstr (buf_casefold, ==, o_casefold); \
g_free (buf_casefold); \
g_free (o_casefold); \
g_date_set_parse (gdate, buf); \
g_assert (g_date_valid (gdate)); \
g_assert_cmpint (g_date_get_day (gdate), ==, d); \
g_assert_cmpint (g_date_get_month (gdate), ==, m); \
g_assert_cmpint (g_date_get_year (gdate), ==, y); \
} G_STMT_END
oldlocale = g_strdup (setlocale (LC_ALL, NULL));
#ifdef G_OS_WIN32
old_lcid = GetThreadLocale ();
#endif
gdate = g_date_new ();
/* Note: Windows implementation of g_date_strftime() does not support
* "-" format modifier (e.g., "%-d", "%-e") so we will not use it.
*/
/* Make sure that nothing has been changed in western European languages. */
setlocale (LC_ALL, "en_GB.utf-8");
#ifdef G_OS_WIN32
SetThreadLocale (MAKELCID (MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_UK), SORT_DEFAULT));
#endif
if (strstr (setlocale (LC_ALL, NULL), "en_GB") != NULL)
{
TEST_DATE (1, 1, 2018, "%B %d, %Y", "January 01, 2018");
TEST_DATE (1, 2, 2018, "%OB %Y", "February 2018");
TEST_DATE (1, 3, 2018, "%e %b %Y", " 1 Mar 2018");
TEST_DATE (1, 4, 2018, "%Ob %Y", "Apr 2018");
TEST_DATE (1, 5, 2018, "%d %h %Y", "01 May 2018");
TEST_DATE (1, 6, 2018, "%Oh %Y", "Jun 2018");
}
else
g_test_skip ("locale en_GB not available, skipping English month names test");
setlocale (LC_ALL, "de_DE.utf-8");
#ifdef G_OS_WIN32
SetThreadLocale (MAKELCID (MAKELANGID (LANG_GERMAN, SUBLANG_GERMAN), SORT_DEFAULT));
#endif
if (strstr (setlocale (LC_ALL, NULL), "de_DE") != NULL)
{
TEST_DATE (16, 7, 2018, "%d. %B %Y", "16. Juli 2018");
TEST_DATE ( 1, 8, 2018, "%OB %Y", "August 2018");
TEST_DATE (18, 9, 2018, "%e. %b %Y", "18. Sep 2018");
TEST_DATE ( 1, 10, 2018, "%Ob %Y", "Okt 2018");
TEST_DATE (20, 11, 2018, "%d. %h %Y", "20. Nov 2018");
TEST_DATE ( 1, 12, 2018, "%Oh %Y", "Dez 2018");
}
else
g_test_skip ("locale de_DE not available, skipping German month names test");
setlocale (LC_ALL, "es_ES.utf-8");
#ifdef G_OS_WIN32
SetThreadLocale (MAKELCID (MAKELANGID (LANG_SPANISH, SUBLANG_SPANISH_MODERN), SORT_DEFAULT));
#endif
if (strstr (setlocale (LC_ALL, NULL), "es_ES") != NULL)
{
TEST_DATE ( 9, 1, 2018, "%d de %B de %Y", "09 de enero de 2018");
TEST_DATE ( 1, 2, 2018, "%OB de %Y", "febrero de 2018");
TEST_DATE (10, 3, 2018, "%e de %b de %Y", "10 de mar de 2018");
TEST_DATE ( 1, 4, 2018, "%Ob de %Y", "abr de 2018");
TEST_DATE (11, 5, 2018, "%d de %h de %Y", "11 de may de 2018");
TEST_DATE ( 1, 6, 2018, "%Oh de %Y", "jun de 2018");
}
else
g_test_skip ("locale es_ES not available, skipping Spanish month names test");
setlocale (LC_ALL, "fr_FR.utf-8");
#ifdef G_OS_WIN32
SetThreadLocale (MAKELCID (MAKELANGID (LANG_FRENCH, SUBLANG_FRENCH), SORT_DEFAULT));
#endif
if (strstr (setlocale (LC_ALL, NULL), "fr_FR") != NULL)
{
TEST_DATE (31, 7, 2018, "%d %B %Y", "31 juillet 2018");
TEST_DATE ( 1, 8, 2018, "%OB %Y", "août 2018");
TEST_DATE (30, 9, 2018, "%e %b %Y", "30 sept. 2018");
TEST_DATE ( 1, 10, 2018, "%Ob %Y", "oct. 2018");
TEST_DATE (29, 11, 2018, "%d %h %Y", "29 nov. 2018");
TEST_DATE ( 1, 12, 2018, "%Oh %Y", "déc. 2018");
}
else
g_test_skip ("locale fr_FR not available, skipping French month names test");
/* Make sure that there are visible changes in some European languages. */
setlocale (LC_ALL, "el_GR.utf-8");
#ifdef G_OS_WIN32
SetThreadLocale (MAKELCID (MAKELANGID (LANG_GREEK, SUBLANG_GREEK_GREECE), SORT_DEFAULT));
#endif
if (strstr (setlocale (LC_ALL, NULL), "el_GR") != NULL)
{
TEST_DATE ( 2, 1, 2018, "%d %B %Y", "02 Ιανουαρίου 2018");
TEST_DATE ( 4, 2, 2018, "%e %B %Y", " 4 Φεβρουαρίου 2018");
TEST_DATE (15, 3, 2018, "%d %B %Y", "15 Μαρτίου 2018");
TEST_DATE ( 1, 4, 2018, "%OB %Y", "Απρίλιος 2018");
TEST_DATE ( 1, 5, 2018, "%OB %Y", "Μάιος 2018");
TEST_DATE ( 1, 6, 2018, "%OB %Y", "Ιούνιος 2018");
TEST_DATE (16, 7, 2018, "%e %b %Y", "16 Ιουλ 2018");
TEST_DATE ( 1, 8, 2018, "%Ob %Y", "Αύγ 2018");
}
else
g_test_skip ("locale el_GR not available, skipping Greek month names test");
setlocale (LC_ALL, "hr_HR.utf-8");
#ifdef G_OS_WIN32
SetThreadLocale (MAKELCID (MAKELANGID (LANG_CROATIAN, SUBLANG_CROATIAN_CROATIA), SORT_DEFAULT));
#endif
if (strstr (setlocale (LC_ALL, NULL), "hr_HR") != NULL)
{
TEST_DATE ( 8, 5, 2018, "%d. %B %Y", "08. svibnja 2018");
TEST_DATE ( 9, 6, 2018, "%e. %B %Y", " 9. lipnja 2018");
TEST_DATE (10, 7, 2018, "%d. %B %Y", "10. srpnja 2018");
TEST_DATE ( 1, 8, 2018, "%OB %Y", "Kolovoz 2018");
TEST_DATE ( 1, 9, 2018, "%OB %Y", "Rujan 2018");
TEST_DATE ( 1, 10, 2018, "%OB %Y", "Listopad 2018");
TEST_DATE (11, 11, 2018, "%e. %b %Y", "11. Stu 2018");
TEST_DATE ( 1, 12, 2018, "%Ob %Y", "Pro 2018");
}
else
g_test_skip ("locale hr_HR not available, skipping Croatian month names test");
setlocale (LC_ALL, "lt_LT.utf-8");
#ifdef G_OS_WIN32
SetThreadLocale (MAKELCID (MAKELANGID (LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA), SORT_DEFAULT));
#endif
if (strstr (setlocale (LC_ALL, NULL), "lt_LT") != NULL)
{
TEST_DATE ( 1, 1, 2018, "%Y m. %B %d d.", "2018 m. sausio 01 d.");
TEST_DATE ( 2, 2, 2018, "%Y m. %B %e d.", "2018 m. vasario 2 d.");
TEST_DATE ( 3, 3, 2018, "%Y m. %B %d d.", "2018 m. kovo 03 d.");
TEST_DATE ( 1, 4, 2018, "%Y m. %OB", "2018 m. balandis");
TEST_DATE ( 1, 5, 2018, "%Y m. %OB", "2018 m. gegužė");
TEST_DATE ( 1, 6, 2018, "%Y m. %OB", "2018 m. birželis");
TEST_DATE (17, 7, 2018, "%Y m. %b %e d.", "2018 m. liep. 17 d.");
TEST_DATE ( 1, 8, 2018, "%Y m. %Ob", "2018 m. rugp.");
}
else
g_test_skip ("locale lt_LT not available, skipping Lithuanian month names test");
setlocale (LC_ALL, "pl_PL.utf-8");
#ifdef G_OS_WIN32
SetThreadLocale (MAKELCID (MAKELANGID (LANG_POLISH, SUBLANG_POLISH_POLAND), SORT_DEFAULT));
#endif
if (strstr (setlocale (LC_ALL, NULL), "pl_PL") != NULL)
{
TEST_DATE ( 3, 5, 2018, "%d %B %Y", "03 maja 2018");
TEST_DATE ( 4, 6, 2018, "%e %B %Y", " 4 czerwca 2018");
TEST_DATE (20, 7, 2018, "%d %B %Y", "20 lipca 2018");
TEST_DATE ( 1, 8, 2018, "%OB %Y", "sierpień 2018");
TEST_DATE ( 1, 9, 2018, "%OB %Y", "wrzesień 2018");
TEST_DATE ( 1, 10, 2018, "%OB %Y", "październik 2018");
TEST_DATE (25, 11, 2018, "%e %b %Y", "25 lis 2018");
TEST_DATE ( 1, 12, 2018, "%Ob %Y", "gru 2018");
}
else
g_test_skip ("locale pl_PL not available, skipping Polish month names test");
setlocale (LC_ALL, "ru_RU.utf-8");
#ifdef G_OS_WIN32
SetThreadLocale (MAKELCID (MAKELANGID (LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA), SORT_DEFAULT));
#endif
if (strstr (setlocale (LC_ALL, NULL), "ru_RU") != NULL)
{
TEST_DATE ( 3, 1, 2018, "%d %B %Y", "03 января 2018");
TEST_DATE ( 4, 2, 2018, "%e %B %Y", " 4 февраля 2018");
TEST_DATE (23, 3, 2018, "%d %B %Y", "23 марта 2018");
TEST_DATE ( 1, 4, 2018, "%OB %Y", "Апрель 2018");
TEST_DATE ( 1, 5, 2018, "%OB %Y", "Май 2018");
TEST_DATE ( 1, 6, 2018, "%OB %Y", "Июнь 2018");
TEST_DATE (24, 7, 2018, "%e %b %Y", "24 июл 2018");
TEST_DATE ( 1, 8, 2018, "%Ob %Y", "авг 2018");
/* This difference is very important in Russian: */
TEST_DATE (19, 5, 2018, "%e %b %Y", "19 мая 2018");
TEST_DATE (20, 5, 2018, "%Ob, %d-е, %Y", "май, 20-е, 2018");
}
else
g_test_skip ("locale ru_RU not available, skipping Russian month names test");
g_date_free (gdate);
setlocale (LC_ALL, oldlocale);
#ifdef G_OS_WIN32
SetThreadLocale (old_lcid);
#endif
g_free (oldlocale);
#endif /* defined(HAVE_LANGINFO_ABALTMON) || defined(G_OS_WIN32) */
}
static void
test_year (gconstpointer t)
{
GDateYear y = GPOINTER_TO_INT (t);
GDateMonth m;
GDateDay day;
guint32 j;
GDate *d;
gint i;
GDate tmp;
guint32 first_day_of_year = G_DATE_BAD_JULIAN;
guint16 days_in_year = g_date_is_leap_year (y) ? 366 : 365;
guint sunday_week_of_year = 0;
guint sunday_weeks_in_year = g_date_get_sunday_weeks_in_year (y);
guint monday_week_of_year = 0;
guint monday_weeks_in_year = g_date_get_monday_weeks_in_year (y);
guint iso8601_week_of_year = 0;
g_assert (g_date_valid_year (y));
/* Years ought to have roundabout 52 weeks */
g_assert (sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53);
g_assert (monday_weeks_in_year == 52 || monday_weeks_in_year == 53);
m = 1;
while (m < 13)
{
guint8 dim = g_date_get_days_in_month (m, y);
GDate days[31];
g_date_clear (days, 31);
g_assert (dim > 0 && dim < 32);
g_assert (g_date_valid_month (m));
day = 1;
while (day <= dim)
{
g_assert (g_date_valid_dmy (day, m, y));
d = &days[day - 1];
//g_assert (!g_date_valid (d));
g_date_set_dmy (d, day, m, y);
g_assert (g_date_valid (d));
if (m == G_DATE_JANUARY && day == 1)
first_day_of_year = g_date_get_julian (d);
g_assert (first_day_of_year != G_DATE_BAD_JULIAN);
g_assert_cmpint (g_date_get_month (d), ==, m);
g_assert_cmpint (g_date_get_year (d), ==, y);
g_assert_cmpint (g_date_get_day (d), ==, day);
g_assert (g_date_get_julian (d) + 1 - first_day_of_year ==
g_date_get_day_of_year (d));
if (m == G_DATE_DECEMBER && day == 31)
g_assert_cmpint (g_date_get_day_of_year (d), ==, days_in_year);
g_assert_cmpint (g_date_get_day_of_year (d), <=, days_in_year);
g_assert_cmpint (g_date_get_monday_week_of_year (d), <=, monday_weeks_in_year);
g_assert_cmpint (g_date_get_monday_week_of_year (d), >=, monday_week_of_year);
if (g_date_get_weekday(d) == G_DATE_MONDAY)
{
g_assert_cmpint (g_date_get_monday_week_of_year (d) - monday_week_of_year, ==, 1);
if ((m == G_DATE_JANUARY && day <= 4) ||
(m == G_DATE_DECEMBER && day >= 29))
g_assert_cmpint (g_date_get_iso8601_week_of_year (d), ==, 1);
else
g_assert_cmpint (g_date_get_iso8601_week_of_year (d) - iso8601_week_of_year, ==, 1);
}
else
{
g_assert_cmpint (g_date_get_monday_week_of_year(d) - monday_week_of_year, ==, 0);
if (!(day == 1 && m == G_DATE_JANUARY))
g_assert_cmpint (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year, ==, 0);
}
monday_week_of_year = g_date_get_monday_week_of_year (d);
iso8601_week_of_year = g_date_get_iso8601_week_of_year (d);
g_assert_cmpint (g_date_get_sunday_week_of_year (d), <=, sunday_weeks_in_year);
g_assert_cmpint (g_date_get_sunday_week_of_year (d), >=, sunday_week_of_year);
if (g_date_get_weekday(d) == G_DATE_SUNDAY)
g_assert_cmpint (g_date_get_sunday_week_of_year (d) - sunday_week_of_year, ==, 1);
else
g_assert_cmpint (g_date_get_sunday_week_of_year (d) - sunday_week_of_year, ==, 0);
sunday_week_of_year = g_date_get_sunday_week_of_year (d);
g_assert_cmpint (g_date_compare (d, d), ==, 0);
i = 1;
while (i < 402) /* Need to get 400 year increments in */
{
tmp = *d;
g_date_add_days (d, i);
g_assert_cmpint (g_date_compare (d, &tmp), >, 0);
g_date_subtract_days (d, i);
g_assert_cmpint (g_date_get_day (d), ==, day);
g_assert_cmpint (g_date_get_month (d), ==, m);
g_assert_cmpint (g_date_get_year (d), ==, y);
tmp = *d;
g_date_add_months (d, i);
g_assert_cmpint (g_date_compare (d, &tmp), >, 0);
g_date_subtract_months (d, i);
g_assert_cmpint (g_date_get_month (d), ==, m);
g_assert_cmpint (g_date_get_year (d), ==, y);
if (day < 29)
g_assert_cmpint (g_date_get_day (d), ==, day);
else
g_date_set_day (d, day);
tmp = *d;
g_date_add_years (d, i);
g_assert_cmpint (g_date_compare (d, &tmp), >, 0);
g_date_subtract_years (d, i);
g_assert_cmpint (g_date_get_month (d), ==, m);
g_assert_cmpint (g_date_get_year (d), ==, y);
if (m != 2 && day != 29)
g_assert_cmpint (g_date_get_day (d), ==, day);
else
g_date_set_day (d, day); /* reset */
i += 10;
}
j = g_date_get_julian (d);
++day;
}
++m;
}
/* at this point, d is the last day of year y */
g_date_set_dmy (&tmp, 1, 1, y + 1);
g_assert_cmpint (j + 1, ==, g_date_get_julian (&tmp));
g_date_add_days (&tmp, 1);
g_assert_cmpint (j + 2, ==, g_date_get_julian (&tmp));
}
static void
test_clamp (void)
{
GDate d1, d2, d, o;
g_date_set_dmy (&d1, 1, 1, 1970);
g_date_set_dmy (&d2, 1, 1, 1980);
g_date_set_dmy (&d, 1, 1, 1);
o = d;
g_date_clamp (&o, NULL, NULL);
g_assert (g_date_compare (&o, &d) == 0);
g_date_clamp (&o, &d1, &d2);
g_assert (g_date_compare (&o, &d1) == 0);
g_date_set_dmy (&o, 1, 1, 2000);
g_date_clamp (&o, &d1, &d2);
g_assert (g_date_compare (&o, &d2) == 0);
}
static void
test_order (void)
{
GDate d1, d2;
g_date_set_dmy (&d1, 1, 1, 1970);
g_date_set_dmy (&d2, 1, 1, 1980);
g_assert (g_date_compare (&d1, &d2) == -1);
g_date_order (&d2, &d1);
g_assert (g_date_compare (&d1, &d2) == 1);
}
static void
test_copy (void)
{
GDate *d;
GDate *c;
d = g_date_new ();
g_assert_false (g_date_valid (d));
c = g_date_copy (d);
g_assert_nonnull (c);
g_assert_false (g_date_valid (c));
g_date_free (c);
g_date_set_day (d, 10);
c = g_date_copy (d);
g_date_set_month (c, 1);
g_date_set_year (c, 2015);
g_assert_true (g_date_valid (c));
g_assert_cmpuint (g_date_get_day (c), ==, 10);
g_date_free (c);
g_date_free (d);
}
/* Check the results of g_date_valid_dmy() for various inputs. */
static void
test_valid_dmy (void)
{
const struct
{
GDateDay day;
GDateMonth month;
GDateYear year;
gboolean expected_valid;
}
vectors[] =
{
/* Lower bounds */
{ 0, 0, 0, FALSE },
{ 1, 1, 1, TRUE },
{ 1, 1, 0, FALSE },
/* Leap year month lengths */
{ 30, 2, 2000, FALSE },
{ 29, 2, 2000, TRUE },
{ 29, 2, 2001, FALSE },
/* Maximum year */
{ 1, 1, G_MAXUINT16, TRUE },
};
gsize i;
for (i = 0; i < G_N_ELEMENTS (vectors); i++)
{
gboolean valid;
g_test_message ("Vector %" G_GSIZE_FORMAT ": %04u-%02u-%02u, %s",
i, vectors[i].year, vectors[i].month, vectors[i].day,
vectors[i].expected_valid ? "valid" : "invalid");
valid = g_date_valid_dmy (vectors[i].day, vectors[i].month, vectors[i].year);
if (vectors[i].expected_valid)
g_assert_true (valid);
else
g_assert_false (valid);
}
}
int
main (int argc, char** argv)
{
gchar *path;
gsize i;
/* Try to get all the leap year cases. */
int check_years[] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 98, 99, 100, 101, 102, 103, 397,
398, 399, 400, 401, 402, 403, 404, 405, 406,
1598, 1599, 1600, 1601, 1602, 1650, 1651,
1897, 1898, 1899, 1900, 1901, 1902, 1903,
1961, 1962, 1963, 1964, 1965, 1967,
1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976,
1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985,
1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
3000, 3001, 3002, 3998, 3999, 4000, 4001, 4002, 4003
};
g_setenv ("LC_ALL", "en_US.utf-8", TRUE);
setlocale (LC_ALL, "");
#ifdef G_OS_WIN32
SetThreadLocale (MAKELCID (MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT));
#endif
g_test_init (&argc, &argv, NULL);
g_test_bug_base ("http://bugzilla.gnome.org/");
g_test_add_func ("/date/basic", test_basic);
g_test_add_func ("/date/empty", test_empty_constructor);
g_test_add_func ("/date/dmy", test_dmy_constructor);
g_test_add_func ("/date/julian", test_julian_constructor);
g_test_add_func ("/date/dates", test_dates);
g_test_add_func ("/date/parse", test_parse);
g_test_add_func ("/date/parse/invalid", test_parse_invalid);
g_test_add_func ("/date/parse_locale_change", test_parse_locale_change);
g_test_add_func ("/date/month_substring", test_month_substring);
g_test_add_func ("/date/month_names", test_month_names);
g_test_add_func ("/date/clamp", test_clamp);
g_test_add_func ("/date/order", test_order);
for (i = 0; i < G_N_ELEMENTS (check_years); i++)
{
path = g_strdup_printf ("/date/year/%d", check_years[i]);
g_test_add_data_func (path, GINT_TO_POINTER(check_years[i]), test_year);
g_free (path);
}
g_test_add_func ("/date/copy", test_copy);
g_test_add_func ("/date/valid-dmy", test_valid_dmy);
return g_test_run ();
}
|