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