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_STD_BACKEND
10 #include <iostream>
main()11 int main()
12 {
13         std::cout << "STD Backend is not build... Skipping" << std::endl;
14 }
15 #else
16 
17 #include <boost/locale/config.hpp>
18 #include <boost/locale/conversion.hpp>
19 #include <boost/locale/localization_backend.hpp>
20 #include <boost/locale/generator.hpp>
21 #include <boost/locale/info.hpp>
22 #include <iomanip>
23 #include "test_locale.hpp"
24 #include "test_locale_tools.hpp"
25 #include <iostream>
26 
get_sign(int x)27 int get_sign(int x)
28 {
29     if(x<0)
30         return -1;
31     else if(x==0)
32         return 0;
33     return 1;
34 }
35 
36 template<typename CharType>
test_one(std::locale const & l,std::string ia,std::string ib,int diff)37 void test_one(std::locale const &l,std::string ia,std::string ib,int diff)
38 {
39     std::basic_string<CharType> a=to_correct_string<CharType>(ia,l);
40     std::basic_string<CharType> b=to_correct_string<CharType>(ib,l);
41     if(diff < 0) {
42         TEST(l(a,b));
43         TEST(!l(b,a));
44     }
45     else if(diff == 0) {
46         TEST(!l(a,b));
47         TEST(!l(b,a));
48     }
49     else {
50         TEST(!l(a,b));
51         TEST(l(b,a));
52     }
53 
54     std::collate<CharType> const &col = std::use_facet<std::collate<CharType> >(l);
55 
56     TEST(diff == col.compare(a.c_str(),a.c_str()+a.size(),b.c_str(),b.c_str()+b.size()));
57     TEST(diff == get_sign(col.transform(a.c_str(),a.c_str()+a.size()).compare(col.transform(b.c_str(),b.c_str()+b.size()))));
58     if(diff == 0) {
59         TEST(col.hash(a.c_str(),a.c_str()+a.size()) == col.hash(b.c_str(),b.c_str()+b.size()));
60     }
61 }
62 
63 template<typename CharType>
test_char()64 void test_char()
65 {
66     boost::locale::generator gen;
67 
68     std::cout << "- Testing at least C" << std::endl;
69 
70     std::locale l = gen("en_US.UTF-8");
71 
72     test_one<CharType>(l,"a","b",-1);
73     test_one<CharType>(l,"a","a",0);
74 
75     std::string name;
76 
77     #if defined(_LIBCPP_VERSION) && (defined(__APPLE__) || defined(__FreeBSD__))
78     std::cout << "- Collation is broken on this OS's standard C++ library, skipping" << std::endl;
79     #else
80     std::string names[] = { "en_US.UTF-8", "en_US.ISO8859-1" };
81     for(unsigned i=0;i<sizeof(names)/sizeof(names[0]);i++) {
82         name = get_std_name(names[i]);
83         if(!name.empty()) {
84             std::cout << "- Testing " << name << std::endl;
85             std::locale l=gen(name);
86             test_one<CharType>(l,"a","ç",-1);
87             test_one<CharType>(l,"ç","d",-1);
88         }
89         else {
90             std::cout << "- " << names[i] << " not supported, skipping" << std::endl;
91         }
92     }
93     #endif
94 }
95 
96 
main()97 int main()
98 {
99     try {
100         boost::locale::localization_backend_manager mgr = boost::locale::localization_backend_manager::global();
101         mgr.select("std");
102         boost::locale::localization_backend_manager::global(mgr);
103 
104         std::cout << "Testing char" << std::endl;
105         test_char<char>();
106         std::cout << "Testing wchar_t" << std::endl;
107         test_char<wchar_t>();
108         #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
109         std::cout << "Testing char16_t" << std::endl;
110         test_char<char16_t>();
111         #endif
112         #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
113         std::cout << "Testing char32_t" << std::endl;
114         test_char<char32_t>();
115         #endif
116     }
117     catch(std::exception const &e) {
118         std::cerr << "Failed " << e.what() << std::endl;
119         return EXIT_FAILURE;
120     }
121     FINALIZE();
122 
123 }
124 
125 #endif // NO STD
126 
127 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
128 
129 // boostinspect:noascii
130