| 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 | |
|---|
| 19 | #ifdef BOOST_MSVC |
|---|
| 20 | #pragma warning(disable:4127) |
|---|
| 21 | #endif |
|---|
| 22 | |
|---|
| 23 | #ifdef BOOST_NO_STDC_NAMESPACE |
|---|
| 24 | namespace std{ using ::setlocale; } |
|---|
| 25 | #endif |
|---|
| 26 | |
|---|
| 27 | test_locale::test_locale(const char* c_name, boost::uint32_t lcid) |
|---|
| 28 | { |
|---|
| 29 | // store the name: |
|---|
| 30 | m_old_name = m_name; |
|---|
| 31 | m_name = c_name; |
|---|
| 32 | // back up C locale and then set it's new name: |
|---|
| 33 | const char* pl = std::setlocale(LC_ALL, 0); |
|---|
| 34 | m_old_c_locale = pl ? pl : ""; |
|---|
| 35 | m_old_c_state = s_c_locale; |
|---|
| 36 | if(std::setlocale(LC_ALL, c_name)) |
|---|
| 37 | { |
|---|
| 38 | s_c_locale = test_with_locale; |
|---|
| 39 | std::cout << "Testing the global C locale: " << c_name << std::endl; |
|---|
| 40 | } |
|---|
| 41 | else |
|---|
| 42 | { |
|---|
| 43 | s_c_locale = no_test; |
|---|
| 44 | std::cout << "The global C locale: " << c_name << " is not available and will not be tested." << std::endl; |
|---|
| 45 | } |
|---|
| 46 | #ifndef BOOST_NO_STD_LOCALE |
|---|
| 47 | // back up the C++ locale and create the new one: |
|---|
| 48 | m_old_cpp_locale = s_cpp_locale_inst; |
|---|
| 49 | m_old_cpp_state = s_cpp_locale; |
|---|
| 50 | try{ |
|---|
| 51 | s_cpp_locale_inst = std::locale(c_name); |
|---|
| 52 | s_cpp_locale = test_with_locale; |
|---|
| 53 | std::cout << "Testing the C++ locale: " << c_name << std::endl; |
|---|
| 54 | }catch(std::runtime_error const &) |
|---|
| 55 | { |
|---|
| 56 | s_cpp_locale = no_test; |
|---|
| 57 | std::cout << "The C++ locale: " << c_name << " is not available and will not be tested." << std::endl; |
|---|
| 58 | } |
|---|
| 59 | #else |
|---|
| 60 | s_cpp_locale = no_test; |
|---|
| 61 | #endif |
|---|
| 62 | |
|---|
| 63 | // back up win locale and create the new one: |
|---|
| 64 | m_old_win_locale = s_win_locale_inst; |
|---|
| 65 | m_old_win_state = s_win_locale; |
|---|
| 66 | s_win_locale_inst = lcid; |
|---|
| 67 | #if defined(BOOST_WINDOWS) && !defined(BOOST_DISABLE_WIN32) |
|---|
| 68 | // |
|---|
| 69 | // Start by geting the printable name of the locale. |
|---|
| 70 | // We use this for debugging purposes only: |
|---|
| 71 | // |
|---|
| 72 | boost::scoped_array<char> p; |
|---|
| 73 | int r = ::GetLocaleInfoA( |
|---|
| 74 | lcid, // locale identifier |
|---|
| 75 | LOCALE_SCOUNTRY, // information type |
|---|
| 76 | 0, // information buffer |
|---|
| 77 | 0 // size of buffer |
|---|
| 78 | ); |
|---|
| 79 | p.reset(new char[r+1]); |
|---|
| 80 | r = ::GetLocaleInfoA( |
|---|
| 81 | lcid, // locale identifier |
|---|
| 82 | LOCALE_SCOUNTRY, // information type |
|---|
| 83 | p.get(), // information buffer |
|---|
| 84 | r+1 // size of buffer |
|---|
| 85 | ); |
|---|
| 86 | // |
|---|
| 87 | // now see if locale is installed and behave accordingly: |
|---|
| 88 | // |
|---|
| 89 | if(::IsValidLocale(lcid, LCID_INSTALLED)) |
|---|
| 90 | { |
|---|
| 91 | s_win_locale = test_with_locale; |
|---|
| 92 | std::cout << "Testing the Win32 locale: \"" << p.get() << "\" (0x" << std::hex << lcid << ")" << std::endl; |
|---|
| 93 | } |
|---|
| 94 | else |
|---|
| 95 | { |
|---|
| 96 | s_win_locale = no_test; |
|---|
| 97 | std::cout << "The Win32 locale: \"" << p.get() << "\" (0x" << std::hex << lcid << ") is not available and will not be tested." << std::endl; |
|---|
| 98 | } |
|---|
| 99 | #else |
|---|
| 100 | s_win_locale = no_test; |
|---|
| 101 | #endif |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | test_locale::~test_locale() |
|---|
| 105 | { |
|---|
| 106 | // restore to previous state: |
|---|
| 107 | std::setlocale(LC_ALL, m_old_c_locale.c_str()); |
|---|
| 108 | s_c_locale = m_old_c_state; |
|---|
| 109 | #ifndef BOOST_NO_STD_LOCALE |
|---|
| 110 | s_cpp_locale_inst = m_old_cpp_locale; |
|---|
| 111 | #endif |
|---|
| 112 | s_cpp_locale = m_old_cpp_state; |
|---|
| 113 | s_win_locale_inst = m_old_win_locale; |
|---|
| 114 | s_win_locale = m_old_win_state; |
|---|
| 115 | m_name = m_old_name; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | int test_locale::s_c_locale = test_no_locale; |
|---|
| 119 | int test_locale::s_cpp_locale = test_no_locale; |
|---|
| 120 | int test_locale::s_win_locale = test_no_locale; |
|---|
| 121 | #ifndef BOOST_NO_STD_LOCALE |
|---|
| 122 | std::locale test_locale::s_cpp_locale_inst; |
|---|
| 123 | #endif |
|---|
| 124 | boost::uint32_t test_locale::s_win_locale_inst = 0; |
|---|
| 125 | std::string test_locale::m_name; |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | void test_en_locale(const char* name, boost::uint32_t lcid) |
|---|
| 129 | { |
|---|
| 130 | using namespace boost::regex_constants; |
|---|
| 131 | errors_as_warnings w; |
|---|
| 132 | test_locale l(name, lcid); |
|---|
| 133 | 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)); |
|---|
| 134 | 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)); |
|---|
| 135 | // 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)); |
|---|
| 136 | 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)); |
|---|
| 137 | 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)); |
|---|
| 138 | 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)); |
|---|
| 139 | // collation sensitive ranges: |
|---|
| 140 | #if !BOOST_WORKAROUND(__BORLANDC__, < 0x600) |
|---|
| 141 | // these tests are disabled for Borland C++: a bug in std::collate<wchar_t> |
|---|
| 142 | // causes these tests to crash (pointer overrun in std::collate<wchar_t>::do_transform). |
|---|
| 143 | 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)); |
|---|
| 144 | 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)); |
|---|
| 145 | // and equivalence classes: |
|---|
| 146 | 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)); |
|---|
| 147 | // case mapping: |
|---|
| 148 | 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)); |
|---|
| 149 | 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)); |
|---|
| 150 | 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)); |
|---|
| 151 | #endif |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | void test_en_locale() |
|---|
| 155 | { |
|---|
| 156 | // VC6 seems to have problems with std::setlocale, I've never |
|---|
| 157 | // gotten to the bottem of this as the program runs fine under the |
|---|
| 158 | // debugger, but hangs when run from bjam: |
|---|
| 159 | #if !BOOST_WORKAROUND(BOOST_MSVC, <1300) && !(defined(__ICL) && defined(_MSC_VER) && (_MSC_VER == 1200)) |
|---|
| 160 | test_en_locale("en_US", 0x09 | 0x01 << 10); |
|---|
| 161 | test_en_locale("en_UK", 0x09 | 0x02 << 10); |
|---|
| 162 | test_en_locale("en", 0x09); |
|---|
| 163 | #endif |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | |
|---|