| 1 | // |
| 2 | // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh) |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. |
| 5 | // https://www.boost.org/LICENSE_1_0.txt |
| 6 | |
| 7 | #include <boost/locale/conversion.hpp> |
| 8 | #include <boost/locale/generator.hpp> |
| 9 | #include <boost/locale/info.hpp> |
| 10 | #include <boost/locale/localization_backend.hpp> |
| 11 | #include "boostLocale/test/tools.hpp" |
| 12 | #include "boostLocale/test/unit_test.hpp" |
| 13 | #include <iomanip> |
| 14 | #include <iostream> |
| 15 | |
| 16 | int get_sign(int x) |
| 17 | { |
| 18 | if(x < 0) |
| 19 | return -1; |
| 20 | else if(x == 0) |
| 21 | return 0; |
| 22 | return 1; |
| 23 | } |
| 24 | |
| 25 | template<typename CharType> |
| 26 | void test_one(const std::locale& l, std::string ia, std::string ib, int diff) |
| 27 | { |
| 28 | std::basic_string<CharType> a = to_correct_string<CharType>(ia, l); |
| 29 | std::basic_string<CharType> b = to_correct_string<CharType>(ib, l); |
| 30 | if(diff < 0) { |
| 31 | TEST(l(a, b)); |
| 32 | TEST(!l(b, a)); |
| 33 | } else if(diff == 0) { |
| 34 | TEST(!l(a, b)); |
| 35 | TEST(!l(b, a)); |
| 36 | } else { |
| 37 | TEST(!l(a, b)); |
| 38 | TEST(l(b, a)); |
| 39 | } |
| 40 | |
| 41 | const std::collate<CharType>& col = std::use_facet<std::collate<CharType>>(l); |
| 42 | |
| 43 | TEST_EQ(diff, col.compare(a.c_str(), a.c_str() + a.size(), b.c_str(), b.c_str() + b.size())); |
| 44 | TEST_EQ( |
| 45 | diff, |
| 46 | get_sign(col.transform(a.c_str(), a.c_str() + a.size()).compare(col.transform(b.c_str(), b.c_str() + b.size())))); |
| 47 | if(diff == 0) |
| 48 | TEST_EQ(col.hash(a.c_str(), a.c_str() + a.size()), col.hash(b.c_str(), b.c_str() + b.size())); |
| 49 | } |
| 50 | |
| 51 | template<typename CharType> |
| 52 | void test_char() |
| 53 | { |
| 54 | boost::locale::generator gen; |
| 55 | |
| 56 | std::cout << "- Testing at least C" << std::endl; |
| 57 | std::locale l = gen("C.UTF-8" ); |
| 58 | test_one<CharType>(l, "a" , "b" , -1); |
| 59 | test_one<CharType>(l, "b" , "a" , 1); |
| 60 | test_one<CharType>(l, "a" , "a" , 0); |
| 61 | |
| 62 | #if !defined(__APPLE__) && !defined(__FreeBSD__) |
| 63 | for(const std::string locale_name : {"en_US.UTF-8" , "en_US.ISO8859-1" }) { |
| 64 | if(!has_posix_locale(name: locale_name)) |
| 65 | std::cout << "- " << locale_name << " not supported, skipping" << std::endl; // LCOV_EXCL_LINE |
| 66 | else { |
| 67 | std::cout << "- Testing " << locale_name << std::endl; |
| 68 | l = gen(locale_name); |
| 69 | test_one<CharType>(l, "a" , "ç" , -1); |
| 70 | test_one<CharType>(l, "ç" , "d" , -1); |
| 71 | } |
| 72 | } |
| 73 | #else |
| 74 | std::cout << "- Collation is broken on this OS C standard library, skipping\n" ; |
| 75 | #endif |
| 76 | } |
| 77 | |
| 78 | BOOST_LOCALE_DISABLE_UNREACHABLE_CODE_WARNING |
| 79 | void test_main(int /*argc*/, char** /*argv*/) |
| 80 | { |
| 81 | #ifdef BOOST_LOCALE_NO_POSIX_BACKEND |
| 82 | std::cout << "POSIX Backend is not build... Skipping\n" ; |
| 83 | return; |
| 84 | #endif |
| 85 | |
| 86 | boost::locale::localization_backend_manager mgr = boost::locale::localization_backend_manager::global(); |
| 87 | mgr.select(backend_name: "posix" ); |
| 88 | boost::locale::localization_backend_manager::global(mgr); |
| 89 | |
| 90 | std::cout << "Testing char" << std::endl; |
| 91 | test_char<char>(); |
| 92 | std::cout << "Testing wchar_t" << std::endl; |
| 93 | test_char<wchar_t>(); |
| 94 | } |
| 95 | |
| 96 | // boostinspect:noascii |
| 97 | |