| 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 "boostLocale/test/tools.hpp" |
| 11 | #include "boostLocale/test/unit_test.hpp" |
| 12 | #include <iomanip> |
| 13 | #include <iostream> |
| 14 | |
| 15 | template<typename Char> |
| 16 | void test_normc(std::basic_string<Char> orig, std::basic_string<Char> normal, boost::locale::norm_type type) |
| 17 | { |
| 18 | std::locale l = boost::locale::generator().generate(id: "en_US.UTF-8" ); |
| 19 | TEST_EQ(normalize(orig, type, l), normal); |
| 20 | TEST_EQ(normalize(orig.c_str(), type, l), normal); |
| 21 | TEST_EQ(normalize(orig.c_str(), orig.c_str() + orig.size(), type, l), normal); |
| 22 | } |
| 23 | |
| 24 | void test_norm(std::string orig, std::string normal, boost::locale::norm_type type) |
| 25 | { |
| 26 | test_normc<char>(orig, normal, type); |
| 27 | test_normc<wchar_t>(orig: to<wchar_t>(utf8: orig), normal: to<wchar_t>(utf8: normal), type); |
| 28 | #ifndef BOOST_LOCALE_NO_CXX20_STRING8 |
| 29 | test_normc<char8_t>(to<char8_t>(orig), to<char8_t>(normal), type); |
| 30 | #endif |
| 31 | #ifdef BOOST_LOCALE_ENABLE_CHAR16_T |
| 32 | test_normc<char16_t>(to<char16_t>(orig), to<char16_t>(normal), type); |
| 33 | #endif |
| 34 | #ifdef BOOST_LOCALE_ENABLE_CHAR32_T |
| 35 | test_normc<char32_t>(to<char32_t>(orig), to<char32_t>(normal), type); |
| 36 | #endif |
| 37 | } |
| 38 | |
| 39 | #define TEST_A(Chr, how, source, dest) \ |
| 40 | do { \ |
| 41 | const boost::locale::info& inf = std::use_facet<boost::locale::info>(std::locale()); \ |
| 42 | std::cout << "Testing " #how " for " #Chr ", lang=" << inf.language(); \ |
| 43 | if(std::string("char") == #Chr) \ |
| 44 | std::cout << " charset=" << inf.encoding(); \ |
| 45 | std::cout << std::endl; \ |
| 46 | std::basic_string<Chr> source_s = (source), dest_s = (dest); \ |
| 47 | TEST_EQ(boost::locale::how(source_s), dest_s); \ |
| 48 | TEST_EQ(boost::locale::how(source_s.c_str()), dest_s); \ |
| 49 | TEST_EQ(boost::locale::how(source_s.c_str(), source_s.c_str() + source_s.size()), dest_s); \ |
| 50 | BOOST_LOCALE_START_CONST_CONDITION \ |
| 51 | } while(0) BOOST_LOCALE_END_CONST_CONDITION |
| 52 | |
| 53 | #define TEST_ALL_CASES \ |
| 54 | do { \ |
| 55 | eight_bit = true; \ |
| 56 | std::locale::global(gen("en_US.UTF-8")); \ |
| 57 | TEST_V(to_upper, "grüßen i", "GRÜSSEN I"); \ |
| 58 | TEST_V(to_lower, "Façade", "façade"); \ |
| 59 | TEST_V(to_title, "façadE world", "Façade World"); \ |
| 60 | TEST_V(fold_case, "Hello World", "hello world"); \ |
| 61 | std::locale::global(gen("tr_TR.UTF-8")); \ |
| 62 | eight_bit = false; \ |
| 63 | TEST_V(to_upper, "i", "İ"); \ |
| 64 | TEST_V(to_lower, "İ", "i"); \ |
| 65 | BOOST_LOCALE_START_CONST_CONDITION \ |
| 66 | } while(0) BOOST_LOCALE_END_CONST_CONDITION |
| 67 | |
| 68 | BOOST_LOCALE_DISABLE_UNREACHABLE_CODE_WARNING |
| 69 | void test_main(int /*argc*/, char** /*argv*/) |
| 70 | { |
| 71 | #ifndef BOOST_LOCALE_WITH_ICU |
| 72 | std::cout << "ICU is not build... Skipping\n" ; |
| 73 | return; |
| 74 | #endif |
| 75 | { |
| 76 | using namespace boost::locale; |
| 77 | std::cout << "Testing Unicode normalization" << std::endl; |
| 78 | test_norm(orig: "\xEF\xAC\x81" , normal: "\xEF\xAC\x81" , type: norm_nfd); // ligature fi |
| 79 | test_norm(orig: "\xEF\xAC\x81" , normal: "\xEF\xAC\x81" , type: norm_nfc); |
| 80 | test_norm(orig: "\xEF\xAC\x81" , normal: "fi" , type: norm_nfkd); |
| 81 | test_norm(orig: "\xEF\xAC\x81" , normal: "fi" , type: norm_nfkc); |
| 82 | test_norm(orig: "ä" , normal: "ä" , type: norm_nfd); // ä to a and accent |
| 83 | test_norm(orig: "ä" , normal: "ä" , type: norm_nfc); |
| 84 | } |
| 85 | |
| 86 | boost::locale::generator gen; |
| 87 | bool eight_bit = true; |
| 88 | |
| 89 | #define TEST_V(how, source_s, dest_s) \ |
| 90 | do { \ |
| 91 | TEST_A(char, how, source_s, dest_s); \ |
| 92 | if(eight_bit) { \ |
| 93 | std::locale tmp = std::locale(); \ |
| 94 | std::locale::global(gen("en_US.ISO8859-1")); \ |
| 95 | TEST_A(char, how, to<char>(source_s), to<char>(dest_s)); \ |
| 96 | std::locale::global(tmp); \ |
| 97 | } \ |
| 98 | BOOST_LOCALE_START_CONST_CONDITION \ |
| 99 | } while(0) BOOST_LOCALE_END_CONST_CONDITION |
| 100 | |
| 101 | TEST_ALL_CASES; |
| 102 | #undef TEST_V |
| 103 | |
| 104 | #define TEST_V(how, source_s, dest_s) TEST_A(wchar_t, how, to<wchar_t>(source_s), to<wchar_t>(dest_s)) |
| 105 | TEST_ALL_CASES; |
| 106 | #undef TEST_V |
| 107 | |
| 108 | #ifndef BOOST_LOCALE_NO_CXX20_STRING8 |
| 109 | # define TEST_V(how, source_s, dest_s) TEST_A(char8_t, how, to<char8_t>(source_s), to<char8_t>(dest_s)) |
| 110 | TEST_ALL_CASES; |
| 111 | # undef TEST_V |
| 112 | #endif |
| 113 | |
| 114 | #ifdef BOOST_LOCALE_ENABLE_CHAR16_T |
| 115 | # define TEST_V(how, source_s, dest_s) TEST_A(char16_t, how, to<char16_t>(source_s), to<char16_t>(dest_s)) |
| 116 | TEST_ALL_CASES; |
| 117 | # undef TEST_V |
| 118 | #endif |
| 119 | |
| 120 | #ifdef BOOST_LOCALE_ENABLE_CHAR32_T |
| 121 | # define TEST_V(how, source_s, dest_s) TEST_A(char32_t, how, to<char32_t>(source_s), to<char32_t>(dest_s)) |
| 122 | TEST_ALL_CASES; |
| 123 | # undef TEST_V |
| 124 | #endif |
| 125 | } |
| 126 | |
| 127 | // boostinspect:noascii |
| 128 | |