test_locale.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. *
  3. * Copyright (c) 2004
  4. * John Maddock
  5. *
  6. * Use, modification and distribution are subject to the
  7. * Boost Software License, Version 1.0. (See accompanying file
  8. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. */
  11. #include "test.hpp"
  12. #include <clocale>
  13. #if defined(BOOST_WINDOWS) && !defined(BOOST_DISABLE_WIN32)
  14. #include <boost/scoped_array.hpp>
  15. #include <windows.h>
  16. #endif
  17. #include <iostream>
  18. #include <iomanip>
  19. #ifdef BOOST_MSVC
  20. #pragma warning(disable:4127)
  21. #endif
  22. #ifdef BOOST_NO_STDC_NAMESPACE
  23. namespace std{ using ::setlocale; }
  24. #endif
  25. test_locale::test_locale(const char* c_name, boost::uint32_t lcid)
  26. {
  27. #ifndef UNDER_CE
  28. // store the name:
  29. m_old_name = m_name;
  30. m_name = c_name;
  31. // back up C locale and then set it's new name:
  32. const char* pl = std::setlocale(LC_ALL, 0);
  33. m_old_c_locale = pl ? pl : "";
  34. m_old_c_state = s_c_locale;
  35. if(std::setlocale(LC_ALL, c_name))
  36. {
  37. s_c_locale = test_with_locale;
  38. std::cout << "Testing the global C locale: " << c_name << std::endl;
  39. }
  40. else
  41. {
  42. s_c_locale = no_test;
  43. std::cout << "The global C locale: " << c_name << " is not available and will not be tested." << std::endl;
  44. }
  45. #else
  46. s_c_locale = no_test;
  47. #endif
  48. // Disabled for VC15.7 (and later?) as the C runtime asserts if you pass an invalid
  49. // locale name to std::locale, rather than throwing the expected exception.
  50. #if !defined(BOOST_NO_STD_LOCALE) && !defined(BOOST_NO_EXCEPTIONS) && !BOOST_WORKAROUND(BOOST_MSVC, > 1913)
  51. // back up the C++ locale and create the new one:
  52. m_old_cpp_locale = s_cpp_locale_inst;
  53. m_old_cpp_state = s_cpp_locale;
  54. try{
  55. s_cpp_locale_inst = std::locale(c_name);
  56. s_cpp_locale = test_with_locale;
  57. std::cout << "Testing the C++ locale: " << c_name << std::endl;
  58. }
  59. catch(std::runtime_error const &)
  60. {
  61. s_cpp_locale = no_test;
  62. std::cout << "The C++ locale: " << c_name << " is not available and will not be tested." << std::endl;
  63. }
  64. #else
  65. m_old_cpp_locale = s_cpp_locale_inst;
  66. m_old_cpp_state = s_cpp_locale;
  67. s_cpp_locale = no_test;
  68. #endif
  69. // back up win locale and create the new one:
  70. m_old_win_locale = s_win_locale_inst;
  71. m_old_win_state = s_win_locale;
  72. s_win_locale_inst = lcid;
  73. #if defined(BOOST_WINDOWS) && !defined(BOOST_DISABLE_WIN32)
  74. //
  75. // Start by geting the printable name of the locale.
  76. // We use this for debugging purposes only:
  77. //
  78. #ifndef BOOST_NO_ANSI_APIS
  79. boost::scoped_array<char> p;
  80. int r = ::GetLocaleInfoA(
  81. lcid, // locale identifier
  82. LOCALE_SCOUNTRY, // information type
  83. 0, // information buffer
  84. 0 // size of buffer
  85. );
  86. p.reset(new char[r+1]);
  87. r = ::GetLocaleInfoA(
  88. lcid, // locale identifier
  89. LOCALE_SCOUNTRY, // information type
  90. p.get(), // information buffer
  91. r+1 // size of buffer
  92. );
  93. #else
  94. WCHAR code_page_string[7];
  95. int r = ::GetLocaleInfoW(
  96. lcid,
  97. LOCALE_IDEFAULTANSICODEPAGE,
  98. code_page_string,
  99. 7);
  100. BOOST_ASSERT(r != 0);
  101. UINT code_page = static_cast<UINT>(_wtol(code_page_string));
  102. boost::scoped_array<wchar_t> wp;
  103. r = ::GetLocaleInfoW(
  104. lcid, // locale identifier
  105. LOCALE_SCOUNTRY, // information type
  106. 0, // information buffer
  107. 0 // size of buffer
  108. );
  109. wp.reset(new wchar_t[r+1]);
  110. r = ::GetLocaleInfoW(
  111. lcid, // locale identifier
  112. LOCALE_SCOUNTRY, // information type
  113. wp.get(), // information buffer
  114. r+1 // size of buffer
  115. );
  116. int name_size = (r+1) * 2;
  117. boost::scoped_array<char> p(new char[name_size]);
  118. int conv_r = ::WideCharToMultiByte(
  119. code_page,
  120. 0,
  121. wp.get(), r,
  122. p.get(), name_size,
  123. NULL, NULL
  124. );
  125. BOOST_ASSERT(conv_r != 0);
  126. #endif
  127. //
  128. // now see if locale is installed and behave accordingly:
  129. //
  130. if(::IsValidLocale(lcid, LCID_INSTALLED))
  131. {
  132. s_win_locale = test_with_locale;
  133. std::cout << "Testing the Win32 locale: \"" << p.get() << "\" (0x" << std::hex << lcid << ")" << std::endl;
  134. }
  135. else
  136. {
  137. s_win_locale = no_test;
  138. std::cout << "The Win32 locale: \"" << p.get() << "\" (0x" << std::hex << lcid << ") is not available and will not be tested." << std::endl;
  139. }
  140. #else
  141. s_win_locale = no_test;
  142. #endif
  143. }
  144. test_locale::~test_locale()
  145. {
  146. // restore to previous state:
  147. #ifndef UNDER_CE
  148. std::setlocale(LC_ALL, m_old_c_locale.c_str());
  149. s_c_locale = m_old_c_state;
  150. #endif
  151. #ifndef BOOST_NO_STD_LOCALE
  152. s_cpp_locale_inst = m_old_cpp_locale;
  153. #endif
  154. s_cpp_locale = m_old_cpp_state;
  155. s_win_locale_inst = m_old_win_locale;
  156. s_win_locale = m_old_win_state;
  157. m_name = m_old_name;
  158. }
  159. int test_locale::s_c_locale = test_no_locale;
  160. int test_locale::s_cpp_locale = test_no_locale;
  161. int test_locale::s_win_locale = test_no_locale;
  162. #ifndef BOOST_NO_STD_LOCALE
  163. std::locale test_locale::s_cpp_locale_inst;
  164. #endif
  165. boost::uint32_t test_locale::s_win_locale_inst = 0;
  166. std::string test_locale::m_name;
  167. void test_en_locale(const char* name, boost::uint32_t lcid)
  168. {
  169. using namespace boost::regex_constants;
  170. errors_as_warnings w;
  171. test_locale l(name, lcid);
  172. TEST_REGEX_SEARCH_L("[[:lower:]]+", perl, "\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xf7", match_default, make_array(1, 32, -2, -2));
  173. TEST_REGEX_SEARCH_L("[[:upper:]]+", perl, "\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf", match_default, make_array(1, 31, -2, -2));
  174. // TEST_REGEX_SEARCH_L("[[:punct:]]+", perl, "\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0", match_default, make_array(0, 31, -2, -2));
  175. TEST_REGEX_SEARCH_L("[[:print:]]+", perl, "\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe", match_default, make_array(0, 93, -2, -2));
  176. TEST_REGEX_SEARCH_L("[[:graph:]]+", perl, "\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe", match_default, make_array(0, 93, -2, -2));
  177. TEST_REGEX_SEARCH_L("[[:word:]]+", perl, "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe", match_default, make_array(0, 61, -2, -2));
  178. // collation sensitive ranges:
  179. #if !BOOST_WORKAROUND(__BORLANDC__, < 0x600)
  180. // these tests are disabled for Borland C++: a bug in std::collate<wchar_t>
  181. // causes these tests to crash (pointer overrun in std::collate<wchar_t>::do_transform).
  182. TEST_REGEX_SEARCH_L("[a-z]+", perl|::boost::regex_constants::collate, "\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc", match_default, make_array(0, 28, -2, -2));
  183. TEST_REGEX_SEARCH_L("[a-z]+", perl|::boost::regex_constants::collate, "\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc", match_default, make_array(1, 28, -2, -2));
  184. if (lcid != 0x09)
  185. {
  186. // and equivalence classes, these fail for locale "en" but pass for "en_UK" and "en_US":
  187. TEST_REGEX_SEARCH_L("[[=a=]]+", perl, "aA\xe0\xe1\xe2\xe3\xe4\xe5\xc0\xc1\xc2\xc3\xc4\xc5", match_default, make_array(0, 14, -2, -2));
  188. }
  189. // case mapping:
  190. TEST_REGEX_SEARCH_L("[A-Z]+", perl|icase|::boost::regex_constants::collate, "\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc", match_default, make_array(0, 28, -2, -2));
  191. TEST_REGEX_SEARCH_L("[a-z]+", perl|icase|::boost::regex_constants::collate, "\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc", match_default, make_array(1, 28, -2, -2));
  192. TEST_REGEX_SEARCH_L("\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd", perl|icase, "\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe", match_default, make_array(1, 30, -2, -2));
  193. #endif
  194. }
  195. void test_en_locale()
  196. {
  197. // VC6 seems to have problems with std::setlocale, I've never
  198. // gotten to the bottem of this as the program runs fine under the
  199. // debugger, but hangs when run from bjam:
  200. #if !BOOST_WORKAROUND(BOOST_MSVC, <1300) && !(defined(__ICL) && defined(_MSC_VER) && (_MSC_VER == 1200))
  201. test_en_locale("en_US", 0x09 | 0x01 << 10);
  202. test_en_locale("en_UK", 0x09 | 0x02 << 10);
  203. test_en_locale("en", 0x09);
  204. #endif
  205. }