1 //
2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOST_LOCALE_WITH_ICU
9 #include <iostream>
main()10 int main()
11 {
12 std::cout << "ICU is not build... Skipping" << std::endl;
13 }
14 #else
15
16 #include <boost/locale/conversion.hpp>
17 #include <boost/locale/generator.hpp>
18 #include <boost/locale/info.hpp>
19 #include <iomanip>
20 #include "test_locale.hpp"
21
22
23 template<typename Char>
test_normc(std::basic_string<Char> orig,std::basic_string<Char> normal,boost::locale::norm_type type)24 void test_normc(std::basic_string<Char> orig,std::basic_string<Char> normal,boost::locale::norm_type type)
25 {
26 std::locale l = boost::locale::generator().generate("en_US.UTF-8");
27 TEST(normalize(orig,type,l)==normal);
28 TEST(normalize(orig.c_str(),type,l)==normal);
29 TEST(normalize(orig.c_str(),orig.c_str()+orig.size(),type,l)==normal);
30 }
31
test_norm(std::string orig,std::string normal,boost::locale::norm_type type)32 void test_norm(std::string orig,std::string normal,boost::locale::norm_type type)
33 {
34 test_normc<char>(orig,normal,type);
35 test_normc<wchar_t>(to<wchar_t>(orig),to<wchar_t>(normal),type);
36 #ifdef BOOST_HAS_CHAR16_T
37 test_normc<char16_t>(to<char16_t>(orig),to<char16_t>(normal),type);
38 #endif
39 #ifdef BOOST_HAS_CHAR32_T
40 test_normc<char32_t>(to<char32_t>(orig),to<char32_t>(normal),type);
41 #endif
42 }
43
44 #define TEST_A(Chr,how,source,dest) \
45 do { \
46 boost::locale::info const &inf=std::use_facet<boost::locale::info>(std::locale()); \
47 std::cout <<"Testing " #how " for " #Chr ", lang="<<inf.language(); \
48 if(std::string("char")==#Chr) std::cout <<" charset="<< inf.encoding(); \
49 std::cout << std::endl; \
50 std::basic_string<Chr> source_s=(source),dest_s=(dest); \
51 TEST(boost::locale::how(source_s)==dest_s); \
52 TEST(boost::locale::how(source_s.c_str())==dest_s); \
53 TEST(boost::locale::how(source_s.c_str(),source_s.c_str()+source_s.size())==dest_s);\
54 }while(0)
55
56 #define TEST_ALL_CASES \
57 do { \
58 eight_bit=true; \
59 std::locale::global(gen("en_US.UTF-8")); \
60 TEST_V(to_upper,"grüßen i","GRÜSSEN I"); \
61 TEST_V(to_lower,"Façade","façade"); \
62 TEST_V(to_title,"façadE world","Façade World"); \
63 TEST_V(fold_case,"Hello World","hello world"); \
64 std::locale::global(gen("tr_TR.UTF-8")); \
65 eight_bit=false; \
66 TEST_V(to_upper,"i","İ"); \
67 TEST_V(to_lower,"İ","i"); \
68 }while(0)
69
70
main()71 int main()
72 {
73 try {
74 {
75 using namespace boost::locale;
76 std::cout << "Testing Unicode normalization" << std::endl;
77 test_norm("\xEF\xAC\x81","\xEF\xAC\x81",norm_nfd); /// ligature fi
78 test_norm("\xEF\xAC\x81","\xEF\xAC\x81",norm_nfc);
79 test_norm("\xEF\xAC\x81","fi",norm_nfkd);
80 test_norm("\xEF\xAC\x81","fi",norm_nfkc);
81 test_norm("ä","ä",norm_nfd); // ä to a and accent
82 test_norm("ä","ä",norm_nfc);
83 }
84
85 boost::locale::generator gen;
86 bool eight_bit=true;
87
88 #define TEST_V(how,source_s,dest_s) \
89 do { \
90 TEST_A(char,how,source_s,dest_s); \
91 if(eight_bit) { \
92 std::locale tmp=std::locale(); \
93 std::locale::global(gen("en_US.ISO8859-1")); \
94 TEST_A(char,how,to<char>(source_s),to<char>(dest_s)); \
95 std::locale::global(tmp); \
96 } \
97 }while(0)
98
99 TEST_ALL_CASES;
100 #undef TEST_V
101
102 #define TEST_V(how,source_s,dest_s) TEST_A(wchar_t,how,to<wchar_t>(source_s),to<wchar_t>(dest_s))
103 TEST_ALL_CASES;
104 #undef TEST_V
105
106 #ifdef BOOST_HAS_CHAR16_T
107 #define TEST_V(how,source_s,dest_s) TEST_A(char16_t,how,to<char16_t>(source_s),to<char16_t>(dest_s))
108 TEST_ALL_CASES;
109 #undef TEST_V
110 #endif
111
112 #ifdef BOOST_HAS_CHAR32_T
113 #define TEST_V(how,source_s,dest_s) TEST_A(char32_t,how,to<char32_t>(source_s),to<char32_t>(dest_s))
114 TEST_ALL_CASES;
115 #undef TEST_V
116 #endif
117 }
118 catch(std::exception const &e) {
119 std::cerr << "Failed " << e.what() << std::endl;
120 return EXIT_FAILURE;
121 }
122 FINALIZE();
123
124 }
125 #endif // NO ICU
126 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
127
128
129 // boostinspect:noascii
130