5.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // { dg-require-namedlocale "de_DE" }
  2. // { dg-require-namedlocale "en_PH" }
  3. // { dg-require-namedlocale "es_MX" }
  4. // { dg-require-namedlocale "fr_FR" }
  5. // { dg-require-namedlocale "it_IT" }
  6. // 2000-09-13 Benjamin Kosnik <bkoz@redhat.com>
  7. // Copyright (C) 2000-2022 Free Software Foundation, Inc.
  8. //
  9. // This file is part of the GNU ISO C++ Library. This library is free
  10. // software; you can redistribute it and/or modify it under the
  11. // terms of the GNU General Public License as published by the
  12. // Free Software Foundation; either version 3, or (at your option)
  13. // any later version.
  14. // This library is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. // You should have received a copy of the GNU General Public License along
  19. // with this library; see the file COPYING3. If not see
  20. // <http://www.gnu.org/licenses/>.
  21. // 22.1.1.2 locale constructors and destructors [lib.locale.cons]
  22. #include <cwchar> // for mbstate_t
  23. #include <cstring>
  24. #include <cstdlib>
  25. #include <locale>
  26. #include <stdexcept>
  27. #include <testsuite_hooks.h>
  28. // More tests for locale("") == POSIX locale::name.
  29. void test04()
  30. {
  31. using namespace std;
  32. #ifdef _GLIBCXX_HAVE_SETENV
  33. char* LANG_orig = strdup(getenv("LANG") ? getenv("LANG") : "");
  34. // Check that a "POSIX" LC_ALL is equivalent to "C".
  35. if (!setenv("LC_ALL", "POSIX", 1))
  36. {
  37. locale loc = locale("");
  38. VERIFY( loc.name() == "C" );
  39. }
  40. setenv("LC_ALL", "", 1);
  41. // Check that a "en_PH" LC_ALL is equivalent to "en_PH".
  42. if (!setenv("LC_ALL", "en_PH", 1))
  43. {
  44. locale loc = locale("");
  45. VERIFY( loc.name() == "en_PH" );
  46. }
  47. setenv("LC_ALL", "", 1);
  48. // Explicit check that LC_ALL sets regardless of LC_* and LANG.
  49. if (!setenv("LANG", "es_MX", 1) && !setenv("LC_COLLATE", "de_DE", 1))
  50. {
  51. if (!setenv("LC_ALL", "en_PH", 1))
  52. {
  53. locale loc = locale("");
  54. VERIFY( loc.name() == "en_PH" );
  55. }
  56. setenv("LC_ALL", "", 1);
  57. setenv("LANG", LANG_orig, 1);
  58. }
  59. // NB: LANG checks all LC_* macro settings. As such, all LC_* macros
  60. // must be cleared for these tests.
  61. setenv("LC_ALL", "", 1);
  62. setenv("LC_CTYPE", "", 1);
  63. setenv("LC_NUMERIC", "", 1);
  64. setenv("LC_TIME", "", 1);
  65. setenv("LC_COLLATE", "", 1);
  66. setenv("LC_MONETARY", "", 1);
  67. setenv("LC_MESSAGES", "", 1);
  68. #if _GLIBCXX_NUM_CATEGORIES
  69. setenv("LC_PAPER", "", 1);
  70. setenv("LC_NAME", "", 1);
  71. setenv("LC_ADDRESS", "", 1);
  72. setenv("LC_TELEPHONE", "", 1);
  73. setenv("LC_MEASUREMENT", "", 1);
  74. setenv("LC_IDENTIFICATION", "", 1);
  75. #endif
  76. // Check the default set by LANG.
  77. if (!setenv("LANG", "fr_FR", 1))
  78. {
  79. locale loc = locale("");
  80. VERIFY( loc.name() == "fr_FR" );
  81. }
  82. // Check that a "POSIX" LANG is equivalent to "C".
  83. if (!setenv("LANG", "POSIX", 1))
  84. {
  85. locale loc("");
  86. VERIFY( loc.name() == "C" );
  87. }
  88. // Setting a category in the "C" default.
  89. if (!setenv("LC_COLLATE", "de_DE", 1))
  90. {
  91. locale loc = locale("");
  92. #if _GLIBCXX_NUM_CATEGORIES
  93. VERIFY( loc.name() == "LC_CTYPE=C;LC_NUMERIC=C;LC_TIME=C;"
  94. "LC_COLLATE=de_DE;LC_MONETARY=C;LC_MESSAGES=C;LC_PAPER=C;"
  95. "LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=C;"
  96. "LC_IDENTIFICATION=C" );
  97. #else
  98. VERIFY( loc.name() == "LC_CTYPE=C;LC_NUMERIC=C;LC_TIME=C;"
  99. "LC_COLLATE=de_DE;LC_MONETARY=C;LC_MESSAGES=C" );
  100. #endif
  101. }
  102. // Changing the LANG default while LC_COLLATE is set.
  103. if (!setenv("LANG", "fr_FR", 1))
  104. {
  105. locale loc = locale("");
  106. #if _GLIBCXX_NUM_CATEGORIES
  107. VERIFY( loc.name() == "LC_CTYPE=fr_FR;LC_NUMERIC=fr_FR;"
  108. "LC_TIME=fr_FR;LC_COLLATE=de_DE;LC_MONETARY=fr_FR;"
  109. "LC_MESSAGES=fr_FR;LC_PAPER=fr_FR;LC_NAME=fr_FR;"
  110. "LC_ADDRESS=fr_FR;LC_TELEPHONE=fr_FR;LC_MEASUREMENT=fr_FR;"
  111. "LC_IDENTIFICATION=fr_FR" );
  112. #else
  113. VERIFY( loc.name() == "LC_CTYPE=fr_FR;LC_NUMERIC=fr_FR;"
  114. "LC_TIME=fr_FR;LC_COLLATE=de_DE;LC_MONETARY=fr_FR;"
  115. "LC_MESSAGES=fr_FR" );
  116. #endif
  117. }
  118. // Changing another (C only) category.
  119. #if _GLIBCXX_NUM_CATEGORIES
  120. if (!setenv("LC_IDENTIFICATION", "it_IT", 1))
  121. {
  122. locale loc = locale("");
  123. VERIFY( loc.name() == "LC_CTYPE=fr_FR;LC_NUMERIC=fr_FR;"
  124. "LC_TIME=fr_FR;LC_COLLATE=de_DE;LC_MONETARY=fr_FR;"
  125. "LC_MESSAGES=fr_FR;LC_PAPER=fr_FR;LC_NAME=fr_FR;"
  126. "LC_ADDRESS=fr_FR;LC_TELEPHONE=fr_FR;LC_MEASUREMENT=fr_FR;"
  127. "LC_IDENTIFICATION=it_IT" );
  128. }
  129. #endif
  130. free(LANG_orig);
  131. #endif // _GLIBCXX_HAVE_SETENV
  132. }
  133. int main()
  134. {
  135. test04();
  136. return 0;
  137. }