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 
9 #ifndef BOOST_LOCALE_WITH_ICU
10 #include <iostream>
main()11 int main()
12 {
13         std::cout << "ICU is not build... Skipping" << std::endl;
14 }
15 #else
16 
17 #include <boost/locale/collator.hpp>
18 #include <boost/locale/generator.hpp>
19 #include <iomanip>
20 #include "test_locale.hpp"
21 
22 
23 template<typename Char>
test_comp(std::locale l,std::basic_string<Char> left,std::basic_string<Char> right,int ilevel,int expected)24 void test_comp(std::locale l,std::basic_string<Char> left,std::basic_string<Char> right,int ilevel,int expected)
25 {
26     typedef std::basic_string<Char> string_type;
27     boost::locale::collator_base::level_type level = static_cast<boost::locale::collator_base::level_type>(ilevel);
28     TEST(boost::locale::comparator<Char>(l,level)(left,right) == (expected < 0));
29     if(ilevel==4) {
30         std::collate<Char> const &coll=std::use_facet<std::collate<Char> >(l);
31         string_type lt=coll.transform(left.c_str(),left.c_str()+left.size());
32         string_type rt=coll.transform(right.c_str(),right.c_str()+right.size());
33         if(expected < 0)
34             TEST(lt<rt);
35         else if(expected == 0) {
36             TEST(lt==rt);
37         }
38         else
39             TEST(lt > rt);
40         long lh=coll.hash(left.c_str(),left.c_str()+left.size());
41         long rh=coll.hash(right.c_str(),right.c_str()+right.size());
42         if(expected == 0)
43             TEST(lh==rh);
44         else
45             TEST(lh!=rh);
46     }
47     boost::locale::collator<Char> const &coll=std::use_facet<boost::locale::collator<Char> >(l);
48     string_type lt=coll.transform(level,left.c_str(),left.c_str()+left.size());
49     TEST(lt==coll.transform(level,left));
50     string_type rt=coll.transform(level,right.c_str(),right.c_str()+right.size());
51     TEST(rt==coll.transform(level,right));
52     if(expected < 0)
53         TEST(lt<rt);
54     else if(expected == 0)
55         TEST(lt==rt);
56     else
57         TEST(lt > rt);
58     long lh=coll.hash(level,left.c_str(),left.c_str()+left.size());
59     TEST(lh==coll.hash(level,left));
60     long rh=coll.hash(level,right.c_str(),right.c_str()+right.size());
61     TEST(rh==coll.hash(level,right));
62     if(expected == 0)
63         TEST(lh==rh);
64     else
65         TEST(lh!=rh);
66 
67 }
68 
69 #define TEST_COMP(c,_l,_r) test_comp<c>(l,_l,_r,level,expected)
70 
71 
compare(std::string left,std::string right,int level,int expected)72 void compare(std::string left,std::string right,int level,int expected)
73 {
74     boost::locale::generator gen;
75     std::locale l=gen("en_US.UTF-8");
76     if(level == 4)
77         TEST(l(left,right) == (expected < 0));
78     TEST_COMP(char,left,right);
79     TEST_COMP(wchar_t,to<wchar_t>(left),to<wchar_t>(right));
80     #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
81     TEST_COMP(char16_t,to<char16_t>(left),to<char16_t>(right));
82     #endif
83     #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
84     TEST_COMP(char32_t,to<char32_t>(left),to<char32_t>(right));
85     #endif
86     l=gen("en_US.ISO8859-1");
87     if(level == 4)
88         TEST(l(to<char>(left),to<char>(right)) == (expected < 0));
89     TEST_COMP(char,to<char>(left),to<char>(right));
90 }
91 
92 
test_collate()93 void test_collate()
94 {
95     int
96         primary     = 0,
97         secondary   = 1,
98         tertiary    = 2,
99         quaternary  = 3,
100         identical   = 4;
101     int     le = -1,gt = 1,eq = 0;
102 
103 
104     compare("a","A",primary,eq);
105     compare("a","A",secondary,eq);
106     compare("A","a",tertiary,gt);
107     compare("a","A",tertiary,le);
108     compare("a","A",quaternary,le);
109     compare("A","a",quaternary,gt);
110     compare("a","A",identical,le);
111     compare("A","a",identical,gt);
112     compare("a","ä",primary,eq); //  a , ä
113     compare("a","ä",secondary,le); //  a , ä
114     compare("ä","a",secondary,gt); //  a , ä
115     compare("a","ä",quaternary,le); //  a , ä
116     compare("ä","a",quaternary,gt); //  a , ä
117     compare("a","ä",identical,le); //  a , ä
118     compare("ä","a",identical,gt); //  a , ä
119 }
120 
121 
122 
123 
main()124 int main()
125 {
126     try {
127         test_collate();
128     }
129     catch(std::exception const &e) {
130         std::cerr << "Failed " << e.what() << std::endl;
131         return EXIT_FAILURE;
132     }
133     FINALIZE();
134 
135 }
136 
137 #endif // NOICU
138 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
139 // boostinspect:noascii
140