• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
12 #include "test.hpp"
13 #include <clocale>
14 #if defined(BOOST_WINDOWS) && !defined(BOOST_DISABLE_WIN32)
15 #include <boost/scoped_array.hpp>
16 #include <windows.h>
17 #endif
18 #include <iostream>
19 #include <iomanip>
20 
21 #ifdef BOOST_MSVC
22 #pragma warning(disable:4127)
23 #endif
24 
25 #ifdef BOOST_NO_STDC_NAMESPACE
26 namespace std{ using ::setlocale; }
27 #endif
28 
test_locale(const char * c_name,boost::uint32_t lcid)29 test_locale::test_locale(const char* c_name, boost::uint32_t lcid)
30 {
31 #ifndef UNDER_CE
32    // store the name:
33    m_old_name = m_name;
34    m_name = c_name;
35    // back up C locale and then set it's new name:
36    const char* pl = std::setlocale(LC_ALL, 0);
37    m_old_c_locale = pl ? pl : "";
38    m_old_c_state = s_c_locale;
39    if(std::setlocale(LC_ALL, c_name))
40    {
41       s_c_locale = test_with_locale;
42       std::cout << "Testing the global C locale: " << c_name << std::endl;
43    }
44    else
45    {
46       s_c_locale = no_test;
47       std::cout << "The global C locale: " << c_name << " is not available and will not be tested." << std::endl;
48    }
49 #else
50   s_c_locale = no_test;
51 #endif
52   // Disabled for VC15.7 (and later?) as the C runtime asserts if you pass an invalid
53   // locale name to std::locale, rather than throwing the expected exception.
54 #if !defined(BOOST_NO_STD_LOCALE) && !defined(BOOST_NO_EXCEPTIONS) && !BOOST_WORKAROUND(BOOST_MSVC, > 1913)
55    // back up the C++ locale and create the new one:
56    m_old_cpp_locale = s_cpp_locale_inst;
57    m_old_cpp_state = s_cpp_locale;
58    try{
59       s_cpp_locale_inst = std::locale(c_name);
60       s_cpp_locale = test_with_locale;
61       std::cout << "Testing the C++ locale: " << c_name << std::endl;
62    }
63    catch(std::runtime_error const &)
64    {
65       s_cpp_locale = no_test;
66       std::cout << "The C++ locale: " << c_name << " is not available and will not be tested." << std::endl;
67    }
68 #else
69    m_old_cpp_locale = s_cpp_locale_inst;
70    m_old_cpp_state = s_cpp_locale;
71    s_cpp_locale = no_test;
72 #endif
73 
74    // back up win locale and create the new one:
75    m_old_win_locale = s_win_locale_inst;
76    m_old_win_state = s_win_locale;
77    s_win_locale_inst = lcid;
78 #if defined(BOOST_WINDOWS) && !defined(BOOST_DISABLE_WIN32)
79    //
80    // Start by geting the printable name of the locale.
81    // We use this for debugging purposes only:
82    //
83 #ifndef BOOST_NO_ANSI_APIS
84    boost::scoped_array<char> p;
85    int r = ::GetLocaleInfoA(
86                lcid,               // locale identifier
87                LOCALE_SCOUNTRY,    // information type
88                0,                  // information buffer
89                0                   // size of buffer
90             );
91    p.reset(new char[r+1]);
92    r = ::GetLocaleInfoA(
93                lcid,               // locale identifier
94                LOCALE_SCOUNTRY,    // information type
95                p.get(),            // information buffer
96                r+1                 // size of buffer
97             );
98 #else
99    WCHAR code_page_string[7];
100    int r = ::GetLocaleInfoW(
101                lcid,
102                LOCALE_IDEFAULTANSICODEPAGE,
103                code_page_string,
104                7);
105    BOOST_ASSERT(r != 0);
106 
107    UINT code_page = static_cast<UINT>(_wtol(code_page_string));
108 
109    boost::scoped_array<wchar_t> wp;
110    r = ::GetLocaleInfoW(
111                lcid,               // locale identifier
112                LOCALE_SCOUNTRY,    // information type
113                0,                  // information buffer
114                0                   // size of buffer
115             );
116    wp.reset(new wchar_t[r+1]);
117    r = ::GetLocaleInfoW(
118                lcid,               // locale identifier
119                LOCALE_SCOUNTRY,    // information type
120                wp.get(),            // information buffer
121                r+1                 // size of buffer
122             );
123 
124    int name_size = (r+1) * 2;
125    boost::scoped_array<char> p(new char[name_size]);
126    int conv_r = ::WideCharToMultiByte(
127                code_page,
128                0,
129                wp.get(), r,
130                p.get(), name_size,
131                NULL, NULL
132             );
133    BOOST_ASSERT(conv_r != 0);
134 #endif
135    //
136    // now see if locale is installed and behave accordingly:
137    //
138    if(::IsValidLocale(lcid, LCID_INSTALLED))
139    {
140       s_win_locale = test_with_locale;
141       std::cout << "Testing the Win32 locale: \"" << p.get() << "\" (0x" << std::hex << lcid << ")" << std::endl;
142    }
143    else
144    {
145       s_win_locale = no_test;
146       std::cout << "The Win32 locale: \"" << p.get() << "\" (0x" << std::hex << lcid << ") is not available and will not be tested." << std::endl;
147    }
148 #else
149    s_win_locale = no_test;
150 #endif
151 }
152 
~test_locale()153 test_locale::~test_locale()
154 {
155    // restore to previous state:
156 #ifndef UNDER_CE
157    std::setlocale(LC_ALL, m_old_c_locale.c_str());
158    s_c_locale = m_old_c_state;
159 #endif
160 #ifndef BOOST_NO_STD_LOCALE
161    s_cpp_locale_inst = m_old_cpp_locale;
162 #endif
163    s_cpp_locale = m_old_cpp_state;
164    s_win_locale_inst = m_old_win_locale;
165    s_win_locale = m_old_win_state;
166    m_name = m_old_name;
167 }
168 
169 int test_locale::s_c_locale = test_no_locale;
170 int test_locale::s_cpp_locale = test_no_locale;
171 int test_locale::s_win_locale = test_no_locale;
172 #ifndef BOOST_NO_STD_LOCALE
173 std::locale test_locale::s_cpp_locale_inst;
174 #endif
175 boost::uint32_t test_locale::s_win_locale_inst = 0;
176 std::string test_locale::m_name;
177 
178 
test_en_locale(const char * name,boost::uint32_t lcid)179 void test_en_locale(const char* name, boost::uint32_t lcid)
180 {
181    using namespace boost::regex_constants;
182    errors_as_warnings w;
183    test_locale l(name, lcid);
184    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));
185    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));
186 //   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));
187    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));
188    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));
189    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));
190    // collation sensitive ranges:
191 #if !BOOST_WORKAROUND(BOOST_BORLANDC, < 0x600)
192    // these tests are disabled for Borland C++: a bug in std::collate<wchar_t>
193    // causes these tests to crash (pointer overrun in std::collate<wchar_t>::do_transform).
194    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));
195    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));
196    if (lcid != 0x09)
197    {
198       // and equivalence classes, these fail for locale "en" but pass for "en_UK" and "en_US":
199       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));
200    }
201    // case mapping:
202    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));
203    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));
204    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));
205 #endif
206 }
207 
test_en_locale()208 void test_en_locale()
209 {
210    // VC6 seems to have problems with std::setlocale, I've never
211    // gotten to the bottem of this as the program runs fine under the
212    // debugger, but hangs when run from bjam:
213 #if !BOOST_WORKAROUND(BOOST_MSVC, <1300) && !(defined(__ICL) && defined(_MSC_VER) && (_MSC_VER == 1200))
214    test_en_locale("en_US", 0x09 | 0x01 << 10);
215    test_en_locale("en_UK", 0x09 | 0x02 << 10);
216    test_en_locale("en", 0x09);
217 #endif
218 }
219 
220 
221