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
|
/* Test of gl_locale_name function and its variants.
Copyright (C) 2007-2019 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2007. */
#include <config.h>
#include "localename.h"
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include "macros.h"
#if HAVE_NEWLOCALE && HAVE_WORKING_USELOCALE && !HAVE_FAKE_LOCALES
# define HAVE_GOOD_USELOCALE 1
#endif
#if HAVE_GOOD_USELOCALE
static struct { int cat; int mask; const char *string; } const categories[] =
{
{ LC_CTYPE, LC_CTYPE_MASK, "LC_CTYPE" },
{ LC_NUMERIC, LC_NUMERIC_MASK, "LC_NUMERIC" },
{ LC_TIME, LC_TIME_MASK, "LC_TIME" },
{ LC_COLLATE, LC_COLLATE_MASK, "LC_COLLATE" },
{ LC_MONETARY, LC_MONETARY_MASK, "LC_MONETARY" },
{ LC_MESSAGES, LC_MESSAGES_MASK, "LC_MESSAGES" }
# ifdef LC_PAPER
, { LC_PAPER, LC_PAPER_MASK, "LC_PAPER" }
# endif
# ifdef LC_NAME
, { LC_NAME, LC_NAME_MASK, "LC_NAME" }
# endif
# ifdef LC_ADDRESS
, { LC_ADDRESS, LC_ADDRESS_MASK, "LC_ADDRESS" }
# endif
# ifdef LC_TELEPHONE
, { LC_TELEPHONE, LC_TELEPHONE_MASK, "LC_TELEPHONE" }
# endif
# ifdef LC_MEASUREMENT
, { LC_MEASUREMENT, LC_MEASUREMENT_MASK, "LC_MEASUREMENT" }
# endif
# ifdef LC_IDENTIFICATION
, { LC_IDENTIFICATION, LC_IDENTIFICATION_MASK, "LC_IDENTIFICATION" }
# endif
};
#endif
/* Test the gl_locale_name() function. */
static void
test_locale_name (void)
{
const char *ret;
const char *name;
/* Check that gl_locale_name returns non-NULL. */
ASSERT (gl_locale_name (LC_MESSAGES, "LC_MESSAGES") != NULL);
/* Get into a defined state, */
setlocale (LC_ALL, "en_US.UTF-8");
#if HAVE_GOOD_USELOCALE
uselocale (LC_GLOBAL_LOCALE);
#endif
/* Check that when all environment variables are unset,
gl_locale_name returns the default locale. */
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
unsetenv ("LC_MESSAGES");
unsetenv ("LC_NUMERIC");
unsetenv ("LANG");
/* Need also to unset all environment variables that specify standard or
non-standard locale categories. Otherwise, on glibc systems, when some
of these variables are set and reference a nonexistent locale, the
setlocale (LC_ALL, "") call below would fail. */
unsetenv ("LC_COLLATE");
unsetenv ("LC_MONETARY");
unsetenv ("LC_TIME");
unsetenv ("LC_ADDRESS");
unsetenv ("LC_IDENTIFICATION");
unsetenv ("LC_MEASUREMENT");
unsetenv ("LC_NAME");
unsetenv ("LC_PAPER");
unsetenv ("LC_TELEPHONE");
ret = setlocale (LC_ALL, "");
ASSERT (ret != NULL);
ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
gl_locale_name_default ()) == 0);
ASSERT (strcmp (gl_locale_name (LC_NUMERIC, "LC_NUMERIC"),
gl_locale_name_default ()) == 0);
/* Check that an empty environment variable is treated like an unset
environment variable. */
setenv ("LC_ALL", "", 1);
unsetenv ("LC_CTYPE");
unsetenv ("LC_MESSAGES");
unsetenv ("LANG");
setlocale (LC_ALL, "");
ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
gl_locale_name_default ()) == 0);
unsetenv ("LC_ALL");
setenv ("LC_CTYPE", "", 1);
unsetenv ("LC_MESSAGES");
unsetenv ("LANG");
setlocale (LC_ALL, "");
ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
gl_locale_name_default ()) == 0);
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
setenv ("LC_MESSAGES", "", 1);
unsetenv ("LANG");
setlocale (LC_ALL, "");
ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
gl_locale_name_default ()) == 0);
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
unsetenv ("LC_MESSAGES");
setenv ("LANG", "", 1);
setlocale (LC_ALL, "");
ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
gl_locale_name_default ()) == 0);
/* Check that LC_ALL overrides the others, and LANG is overridden by the
others. */
setenv ("LC_ALL", "C", 1);
unsetenv ("LC_CTYPE");
unsetenv ("LC_MESSAGES");
unsetenv ("LANG");
setlocale (LC_ALL, "");
ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
unsetenv ("LC_ALL");
setenv ("LC_CTYPE", "C", 1);
setenv ("LC_MESSAGES", "C", 1);
unsetenv ("LANG");
setlocale (LC_ALL, "");
ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
unsetenv ("LC_MESSAGES");
setenv ("LANG", "C", 1);
setlocale (LC_ALL, "");
ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
/* Check mixed situations. */
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
setenv ("LANG", "de_DE.UTF-8", 1);
if (setlocale (LC_ALL, "") != NULL)
{
name = gl_locale_name (LC_CTYPE, "LC_CTYPE");
#if defined _WIN32 && !defined __CYGWIN__
/* On native Windows, here,
gl_locale_name_thread (LC_CTYPE, "LC_CTYPE")
returns NULL and
gl_locale_name_posix (LC_CTYPE, "LC_CTYPE")
returns either "de_DE" or "de_DE.UTF-8". */
ASSERT (strcmp (name, "de_DE") == 0 || strcmp (name, "de_DE.UTF-8") == 0);
#else
ASSERT (strcmp (name, "de_DE.UTF-8") == 0);
#endif
name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES");
ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
}
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
unsetenv ("LANG");
if (setlocale (LC_ALL, "") != NULL)
{
name = gl_locale_name (LC_CTYPE, "LC_CTYPE");
ASSERT (strcmp (name, gl_locale_name_default ()) == 0);
name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES");
ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
}
#if HAVE_GOOD_USELOCALE
/* Check that gl_locale_name considers the thread locale. */
{
locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
if (locale != NULL)
{
uselocale (locale);
name = gl_locale_name (LC_CTYPE, "LC_CTYPE");
ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES");
ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
uselocale (LC_GLOBAL_LOCALE);
freelocale (locale);
}
}
/* Check that gl_locale_name distinguishes different categories of the
thread locale, and that the name is the right one for each. */
{
unsigned int i;
for (i = 0; i < SIZEOF (categories); i++)
{
int category_mask = categories[i].mask;
locale_t loc = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
if (loc != NULL)
{
locale_t locale = newlocale (category_mask, "de_DE.UTF-8", loc);
if (locale == NULL)
freelocale (loc);
else
{
unsigned int j;
uselocale (locale);
for (j = 0; j < SIZEOF (categories); j++)
{
const char *name_j =
gl_locale_name (categories[j].cat, categories[j].string);
if (j == i)
ASSERT (strcmp (name_j, "de_DE.UTF-8") == 0);
else
ASSERT (strcmp (name_j, "fr_FR.UTF-8") == 0);
}
uselocale (LC_GLOBAL_LOCALE);
freelocale (locale);
}
}
}
}
#endif
}
/* Test the gl_locale_name_thread() function. */
static void
test_locale_name_thread (void)
{
/* Get into a defined state, */
setlocale (LC_ALL, "en_US.UTF-8");
#if HAVE_GOOD_USELOCALE
/* Check that gl_locale_name_thread returns NULL when no thread locale is
set. */
uselocale (LC_GLOBAL_LOCALE);
ASSERT (gl_locale_name_thread (LC_CTYPE, "LC_CTYPE") == NULL);
ASSERT (gl_locale_name_thread (LC_MESSAGES, "LC_MESSAGES") == NULL);
/* Check that gl_locale_name_thread considers the thread locale. */
{
locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
if (locale != NULL)
{
const char *name;
uselocale (locale);
name = gl_locale_name_thread (LC_CTYPE, "LC_CTYPE");
ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
name = gl_locale_name_thread (LC_MESSAGES, "LC_MESSAGES");
ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
uselocale (LC_GLOBAL_LOCALE);
freelocale (locale);
}
}
/* Check that gl_locale_name_thread distinguishes different categories of the
thread locale, and that the name is the right one for each. */
{
unsigned int i;
for (i = 0; i < SIZEOF (categories); i++)
{
int category_mask = categories[i].mask;
locale_t loc = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
if (loc != NULL)
{
locale_t locale = newlocale (category_mask, "de_DE.UTF-8", loc);
if (locale == NULL)
freelocale (loc);
else
{
unsigned int j;
uselocale (locale);
for (j = 0; j < SIZEOF (categories); j++)
{
const char *name_j =
gl_locale_name_thread (categories[j].cat,
categories[j].string);
if (j == i)
ASSERT (strcmp (name_j, "de_DE.UTF-8") == 0);
else
ASSERT (strcmp (name_j, "fr_FR.UTF-8") == 0);
}
uselocale (LC_GLOBAL_LOCALE);
freelocale (locale);
}
}
}
}
/* Check that gl_locale_name_thread returns a string that is allocated with
indefinite extent. */
{
/* Try many locale names in turn, in order to defeat possible caches. */
static const char * const choices[] =
{
"C",
"POSIX",
"af_ZA",
"af_ZA.UTF-8",
"am_ET",
"am_ET.UTF-8",
"be_BY",
"be_BY.UTF-8",
"bg_BG",
"bg_BG.UTF-8",
"ca_ES",
"ca_ES.UTF-8",
"cs_CZ",
"cs_CZ.UTF-8",
"da_DK",
"da_DK.UTF-8",
"de_AT",
"de_AT.UTF-8",
"de_CH",
"de_CH.UTF-8",
"de_DE",
"de_DE.UTF-8",
"el_GR",
"el_GR.UTF-8",
"en_AU",
"en_AU.UTF-8",
"en_CA",
"en_CA.UTF-8",
"en_GB",
"en_GB.UTF-8",
"en_IE",
"en_IE.UTF-8",
"en_NZ",
"en_NZ.UTF-8",
"en_US",
"en_US.UTF-8",
"es_ES",
"es_ES.UTF-8",
"et_EE",
"et_EE.UTF-8",
"eu_ES",
"eu_ES.UTF-8",
"fi_FI",
"fi_FI.UTF-8",
"fr_BE",
"fr_BE.UTF-8",
"fr_CA",
"fr_CA.UTF-8",
"fr_CH",
"fr_CH.UTF-8",
"fr_FR",
"fr_FR.UTF-8",
"he_IL",
"he_IL.UTF-8",
"hr_HR",
"hr_HR.UTF-8",
"hu_HU",
"hu_HU.UTF-8",
"hy_AM",
"is_IS",
"is_IS.UTF-8",
"it_CH",
"it_CH.UTF-8",
"it_IT",
"it_IT.UTF-8",
"ja_JP.UTF-8",
"kk_KZ",
"kk_KZ.UTF-8",
"ko_KR.UTF-8",
"lt_LT",
"lt_LT.UTF-8",
"nl_BE",
"nl_BE.UTF-8",
"nl_NL",
"nl_NL.UTF-8",
"no_NO",
"no_NO.UTF-8",
"pl_PL",
"pl_PL.UTF-8",
"pt_BR",
"pt_BR.UTF-8",
"pt_PT",
"pt_PT.UTF-8",
"ro_RO",
"ro_RO.UTF-8",
"ru_RU",
"ru_RU.UTF-8",
"sk_SK",
"sk_SK.UTF-8",
"sl_SI",
"sl_SI.UTF-8",
"sv_SE",
"sv_SE.UTF-8",
"tr_TR",
"tr_TR.UTF-8",
"uk_UA",
"uk_UA.UTF-8",
"zh_CN",
"zh_CN.UTF-8",
"zh_HK",
"zh_HK.UTF-8",
"zh_TW",
"zh_TW.UTF-8"
};
/* Remember which locales are available. */
unsigned char /* bool */ available[SIZEOF (choices)];
/* Array of remembered results of gl_locale_name_thread. */
const char *unsaved_names[SIZEOF (choices)][SIZEOF (categories)];
/* Array of remembered results of gl_locale_name_thread, stored in safe
memory. */
char *saved_names[SIZEOF (choices)][SIZEOF (categories)];
unsigned int j;
for (j = 0; j < SIZEOF (choices); j++)
{
locale_t locale = newlocale (LC_ALL_MASK, choices[j], NULL);
available[j] = (locale != NULL);
if (locale != NULL)
{
unsigned int i;
uselocale (locale);
for (i = 0; i < SIZEOF (categories); i++)
{
unsaved_names[j][i] = gl_locale_name_thread (categories[i].cat, categories[i].string);
saved_names[j][i] = strdup (unsaved_names[j][i]);
}
uselocale (LC_GLOBAL_LOCALE);
freelocale (locale);
}
}
/* Verify the unsaved_names are still valid. */
for (j = 0; j < SIZEOF (choices); j++)
if (available[j])
{
unsigned int i;
for (i = 0; i < SIZEOF (categories); i++)
ASSERT (strcmp (unsaved_names[j][i], saved_names[j][i]) == 0);
}
/* Allocate many locales, without freeing them. This is an attempt at
overwriting as much of the previously allocated memory as possible. */
for (j = SIZEOF (choices); j > 0; )
{
j--;
if (available[j])
{
locale_t locale = newlocale (LC_ALL_MASK, choices[j], NULL);
unsigned int i;
ASSERT (locale != NULL);
uselocale (locale);
for (i = 0; i < SIZEOF (categories); i++)
{
const char *name = gl_locale_name_thread (categories[i].cat, categories[i].string);
ASSERT (strcmp (unsaved_names[j][i], name) == 0);
}
uselocale (LC_GLOBAL_LOCALE);
freelocale (locale);
}
}
/* Verify the unsaved_names are still valid. */
for (j = 0; j < SIZEOF (choices); j++)
if (available[j])
{
unsigned int i;
for (i = 0; i < SIZEOF (categories); i++)
{
ASSERT (strcmp (unsaved_names[j][i], saved_names[j][i]) == 0);
free (saved_names[j][i]);
}
}
}
#else
/* Check that gl_locale_name_thread always returns NULL. */
ASSERT (gl_locale_name_thread (LC_CTYPE, "LC_CTYPE") == NULL);
ASSERT (gl_locale_name_thread (LC_MESSAGES, "LC_MESSAGES") == NULL);
#endif
}
/* Test the gl_locale_name_posix() function. */
static void
test_locale_name_posix (void)
{
const char *ret;
const char *name;
/* Get into a defined state, */
setlocale (LC_ALL, "en_US.UTF-8");
#if HAVE_GOOD_USELOCALE
uselocale (LC_GLOBAL_LOCALE);
#endif
/* Check that when all environment variables are unset,
gl_locale_name_posix returns either NULL or the default locale. */
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
unsetenv ("LC_MESSAGES");
unsetenv ("LC_NUMERIC");
unsetenv ("LANG");
/* Need also to unset all environment variables that specify standard or
non-standard locale categories. Otherwise, on glibc systems, when some
of these variables are set and reference a nonexistent locale, the
setlocale (LC_ALL, "") call below would fail. */
unsetenv ("LC_COLLATE");
unsetenv ("LC_MONETARY");
unsetenv ("LC_TIME");
unsetenv ("LC_ADDRESS");
unsetenv ("LC_IDENTIFICATION");
unsetenv ("LC_MEASUREMENT");
unsetenv ("LC_NAME");
unsetenv ("LC_PAPER");
unsetenv ("LC_TELEPHONE");
ret = setlocale (LC_ALL, "");
ASSERT (ret != NULL);
name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
name = gl_locale_name_posix (LC_NUMERIC, "LC_NUMERIC");
ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
/* Check that an empty environment variable is treated like an unset
environment variable. */
setenv ("LC_ALL", "", 1);
unsetenv ("LC_CTYPE");
unsetenv ("LC_MESSAGES");
unsetenv ("LANG");
setlocale (LC_ALL, "");
name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
unsetenv ("LC_ALL");
setenv ("LC_CTYPE", "", 1);
unsetenv ("LC_MESSAGES");
unsetenv ("LANG");
setlocale (LC_ALL, "");
name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
setenv ("LC_MESSAGES", "", 1);
unsetenv ("LANG");
setlocale (LC_ALL, "");
name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
unsetenv ("LC_MESSAGES");
setenv ("LANG", "", 1);
setlocale (LC_ALL, "");
name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
/* Check that LC_ALL overrides the others, and LANG is overridden by the
others. */
setenv ("LC_ALL", "C", 1);
unsetenv ("LC_CTYPE");
unsetenv ("LC_MESSAGES");
unsetenv ("LANG");
setlocale (LC_ALL, "");
name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
ASSERT (strcmp (name, "C") == 0);
unsetenv ("LC_ALL");
setenv ("LC_CTYPE", "C", 1);
setenv ("LC_MESSAGES", "C", 1);
unsetenv ("LANG");
setlocale (LC_ALL, "");
name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
ASSERT (strcmp (name, "C") == 0);
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
unsetenv ("LC_MESSAGES");
setenv ("LANG", "C", 1);
setlocale (LC_ALL, "");
name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
ASSERT (strcmp (name, "C") == 0);
/* Check mixed situations. */
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
setenv ("LANG", "de_DE.UTF-8", 1);
if (setlocale (LC_ALL, "") != NULL)
{
name = gl_locale_name_posix (LC_CTYPE, "LC_CTYPE");
#if defined _WIN32 && !defined __CYGWIN__
ASSERT (strcmp (name, "de_DE") == 0 || strcmp (name, "de_DE.UTF-8") == 0);
#else
ASSERT (strcmp (name, "de_DE.UTF-8") == 0);
#endif
name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
}
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
unsetenv ("LANG");
if (setlocale (LC_ALL, "") != NULL)
{
name = gl_locale_name_posix (LC_CTYPE, "LC_CTYPE");
ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
}
#if HAVE_GOOD_USELOCALE
/* Check that gl_locale_name_posix ignores the thread locale. */
{
locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
if (locale != NULL)
{
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
unsetenv ("LC_MESSAGES");
setenv ("LANG", "C", 1);
setlocale (LC_ALL, "");
uselocale (locale);
name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
ASSERT (strcmp (name, "C") == 0);
uselocale (LC_GLOBAL_LOCALE);
freelocale (locale);
}
}
#endif
}
/* Test the gl_locale_name_environ() function. */
static void
test_locale_name_environ (void)
{
const char *name;
/* Get into a defined state, */
setlocale (LC_ALL, "en_US.UTF-8");
#if HAVE_GOOD_USELOCALE
uselocale (LC_GLOBAL_LOCALE);
#endif
/* Check that when all environment variables are unset,
gl_locale_name_environ returns NULL. */
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
unsetenv ("LC_MESSAGES");
unsetenv ("LC_NUMERIC");
unsetenv ("LANG");
ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
ASSERT (gl_locale_name_environ (LC_NUMERIC, "LC_NUMERIC") == NULL);
/* Check that an empty environment variable is treated like an unset
environment variable. */
setenv ("LC_ALL", "", 1);
unsetenv ("LC_CTYPE");
unsetenv ("LC_MESSAGES");
unsetenv ("LANG");
ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
unsetenv ("LC_ALL");
setenv ("LC_CTYPE", "", 1);
unsetenv ("LC_MESSAGES");
unsetenv ("LANG");
ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
setenv ("LC_MESSAGES", "", 1);
unsetenv ("LANG");
ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
unsetenv ("LC_MESSAGES");
setenv ("LANG", "", 1);
ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
/* Check that LC_ALL overrides the others, and LANG is overridden by the
others. */
setenv ("LC_ALL", "C", 1);
unsetenv ("LC_CTYPE");
unsetenv ("LC_MESSAGES");
unsetenv ("LANG");
name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
ASSERT (strcmp (name, "C") == 0);
unsetenv ("LC_ALL");
setenv ("LC_CTYPE", "C", 1);
setenv ("LC_MESSAGES", "C", 1);
unsetenv ("LANG");
name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
ASSERT (strcmp (name, "C") == 0);
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
unsetenv ("LC_MESSAGES");
setenv ("LANG", "C", 1);
name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
ASSERT (strcmp (name, "C") == 0);
/* Check mixed situations. */
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
setenv ("LANG", "de_DE.UTF-8", 1);
name = gl_locale_name_environ (LC_CTYPE, "LC_CTYPE");
ASSERT (strcmp (name, "de_DE.UTF-8") == 0);
name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
unsetenv ("LANG");
name = gl_locale_name_environ (LC_CTYPE, "LC_CTYPE");
ASSERT (name == NULL);
name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
#if HAVE_GOOD_USELOCALE
/* Check that gl_locale_name_environ ignores the thread locale. */
{
locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
if (locale != NULL)
{
unsetenv ("LC_ALL");
unsetenv ("LC_CTYPE");
unsetenv ("LC_MESSAGES");
setenv ("LANG", "C", 1);
setlocale (LC_ALL, "");
uselocale (locale);
name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
ASSERT (strcmp (name, "C") == 0);
uselocale (LC_GLOBAL_LOCALE);
freelocale (locale);
}
}
#endif
}
/* Test the gl_locale_name_default() function. */
static void
test_locale_name_default (void)
{
const char *name = gl_locale_name_default ();
ASSERT (name != NULL);
/* Only Mac OS X and Windows have a facility for the user to set the default
locale. */
#if !((defined __APPLE__ && defined __MACH__) || (defined _WIN32 || defined __CYGWIN__))
ASSERT (strcmp (name, "C") == 0);
#endif
#if HAVE_GOOD_USELOCALE
/* Check that gl_locale_name_default ignores the thread locale. */
{
locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.UTF-8", NULL);
if (locale != NULL)
{
uselocale (locale);
ASSERT (strcmp (gl_locale_name_default (), name) == 0);
uselocale (LC_GLOBAL_LOCALE);
freelocale (locale);
}
}
#endif
}
int
main ()
{
test_locale_name ();
test_locale_name_thread ();
test_locale_name_posix ();
test_locale_name_environ ();
test_locale_name_default ();
return 0;
}
|