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