]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/locale/test/test_winapi_formatting.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / locale / test / test_winapi_formatting.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
9 #ifdef BOOST_LOCALE_NO_WINAPI_BACKEND
10 #include <iostream>
11 int main()
12 {
13 std::cout << "WinAPI Backend is not build... Skipping" << std::endl;
14 }
15 #else
16
17 #include <boost/locale/formatting.hpp>
18 #include <boost/locale/localization_backend.hpp>
19 #include <boost/locale/generator.hpp>
20 #include <boost/locale/encoding.hpp>
21 #include <boost/locale/info.hpp>
22 #include <iomanip>
23 #include "test_locale.hpp"
24 #include "test_locale_tools.hpp"
25 #include "../src/win32/lcid.hpp"
26 #include <iostream>
27
28 #include <time.h>
29 #include <assert.h>
30
31 #ifndef NOMINMAX
32 #define NOMINMAX
33 #endif
34
35 #include <windows.h>
36
37 #define DEBUG_FMT
38
39 bool equal(std::string const &s1,std::wstring const &s2)
40 {
41 bool res = s1 == boost::locale::conv::from_utf(s2,"UTF-8");
42 #ifdef DEBUG_FMT
43 if(!res)
44 std::cout << "[" << s1 << "]!=["<<boost::locale::conv::from_utf(s2,"UTF-8")<<"]"<<std::endl;
45 #endif
46 return res;
47 }
48
49 bool equal(std::wstring const &s1,std::wstring const &s2)
50 {
51 bool res = s1 == s2;
52 #ifdef DEBUG_FMT
53 if(!res)
54 std::cout << "[" << boost::locale::conv::from_utf(s1,"UTF-8") << "]!=["<<boost::locale::conv::from_utf(s2,"UTF-8")<<"]"<<std::endl;
55 #endif
56 return res;
57 }
58
59
60 bool equal(std::string const &s1,std::string const &s2)
61 {
62 bool res = s1 == s2;
63 #ifdef DEBUG_FMT
64 if(!res)
65 std::cout << "[" << s1 << "]!=["<<s2<<"]"<<std::endl;
66 #endif
67 return res;
68 }
69
70 bool equal(std::wstring const &s1,std::string const &s2)
71 {
72 bool res = s1 == boost::locale::conv::to_utf<wchar_t>(s2,"UTF-8");
73 #ifdef DEBUG_FMT
74 if(!res)
75 std::cout << "[" << boost::locale::conv::from_utf(s1,"UTF-8") << "]!=["<<s2<<"]"<<std::endl;
76 #endif
77 return res;
78
79 }
80
81 template<typename CharType>
82 std::basic_string<CharType> conv_to_char(char const *p)
83 {
84 std::basic_string<CharType> r;
85 while(*p)
86 r+=CharType(*p++);
87 return r;
88 }
89
90
91 template<typename CharType>
92 void test_by_char(std::locale const &l,std::string name,int lcid)
93 {
94 typedef std::basic_stringstream<CharType> ss_type;
95 typedef std::basic_string<CharType> string_type;
96
97 using namespace boost::locale;
98
99 {
100 std::cout << "--- Testing as::posix" << std::endl;
101 ss_type ss;
102 ss.imbue(l);
103
104 ss << 1045.45;
105 TEST(ss);
106 double n;
107 ss >> n;
108 TEST(ss);
109 TEST(n == 1045.45);
110 TEST(equal(ss.str(),"1045.45"));
111 }
112
113 {
114 std::cout << "--- Testing as::number" << std::endl;
115 ss_type ss;
116 ss.imbue(l);
117
118 ss << as::number;
119 ss << 1045.45;
120 TEST(ss);
121 double n;
122 ss >> n;
123 TEST(ss);
124 TEST(n == 1045.45);
125
126 if(name == "ru_RU.UTF-8") {
127 if(sizeof(CharType)==1)
128 TEST(equal(ss.str(),"1 045,45")); // SP
129 else
130 TEST(equal(ss.str(),"1\xC2\xA0" "045,45")); // NBSP
131 }
132 else
133 TEST(equal(ss.str(),"1,045.45"));
134 }
135
136 {
137 std::cout << "--- Testing as::currency " << std::endl;
138
139 ss_type ss;
140 ss.imbue(l);
141
142 ss << as::currency;
143 ss << 1043.34;
144 TEST(ss);
145
146 wchar_t buf[256];
147 GetCurrencyFormatW(lcid,0,L"1043.34",0,buf,256);
148
149 TEST(equal(ss.str(),buf));
150 }
151
152 {
153 std::cout << "--- Testing as::date/time" << std::endl;
154 ss_type ss;
155 ss.imbue(l);
156
157 time_t a_date = 3600*24*(31+4); // Feb 5th
158 time_t a_time = 3600*15+60*33; // 15:33:13
159 time_t a_timesec = 13;
160 time_t a_datetime = a_date + a_time + a_timesec;
161
162 ss << as::time_zone("GMT");
163
164 ss << as::date << a_datetime << CharType('\n');
165 ss << as::time << a_datetime << CharType('\n');
166 ss << as::datetime << a_datetime << CharType('\n');
167 ss << as::time_zone("GMT+01:00");
168 ss << as::ftime(conv_to_char<CharType>("%H")) << a_datetime << CharType('\n');
169 ss << as::time_zone("GMT+00:15");
170 ss << as::ftime(conv_to_char<CharType>("%M")) << a_datetime << CharType('\n');
171
172 wchar_t time_buf[256];
173 wchar_t date_buf[256];
174
175 SYSTEMTIME st= { 1970, 2,5, 5,15,33,13,0 };
176 GetTimeFormatW(lcid,0,&st,0,time_buf,256);
177 GetDateFormatW(lcid,0,&st,0,date_buf,256);
178 TEST(equal(ss.str(),std::wstring(date_buf)+L"\n" + time_buf +L"\n" + date_buf + L" " + time_buf + L"\n16\n48\n"));
179
180 }
181
182 }
183
184
185 void test_date_time(std::locale l)
186 {
187 std::ostringstream ss;
188 ss.imbue(l);
189
190 ss << boost::locale::as::time_zone("GMT");
191
192 time_t a_date = 3600*24*(31+4); // Feb 5th
193 time_t a_time = 3600*15+60*33; // 15:33:13
194 time_t a_timesec = 13;
195 time_t a_datetime = a_date + a_time + a_timesec;
196
197 std::string pat[] = {
198 "a", "Thu",
199 "A", "Thursday",
200 "b", "Feb",
201 "B", "February",
202 "d", "05",
203 "D", "02/05/70",
204 "e", "5",
205 "h", "Feb",
206 "H", "15",
207 "I", "03",
208 "m", "02",
209 "M", "33",
210 "n", "\n",
211 "p", "PM",
212 "r", "03:33:13 PM",
213 "R", "15:33",
214 "S", "13",
215 "t", "\t",
216 "y", "70",
217 "Y", "1970",
218 "%", "%"
219 };
220
221 for(unsigned i=0;i<sizeof(pat)/sizeof(pat[0]);i+=2) {
222 ss.str("");
223 ss << boost::locale::as::ftime("%" + pat[i]) << a_datetime;
224 TEST(equal(ss.str(),pat[i+1]));
225 }
226 }
227
228 int main()
229 {
230 try {
231 boost::locale::localization_backend_manager mgr = boost::locale::localization_backend_manager::global();
232 mgr.select("winapi");
233 boost::locale::localization_backend_manager::global(mgr);
234 boost::locale::generator gen;
235 std::string name;
236 std::string names[] = { "en_US.UTF-8", "he_IL.UTF-8", "ru_RU.UTF-8" };
237 int lcids[] = { 0x0409, 0x040D ,0x0419 };
238
239 for(unsigned i=0;i<sizeof(names)/sizeof(names[9]);i++) {
240 name = names[i];
241 std::cout << "- " << name << " locale" << std::endl;
242 if(boost::locale::impl_win::locale_to_lcid(name) == 0) {
243 std::cout << "-- not supported, skipping" << std::endl;
244 continue;
245 }
246 std::locale l1=gen(name);
247 std::cout << "-- UTF-8" << std::endl;
248 test_by_char<char>(l1,name,lcids[i]);
249 std::cout << "-- UTF-16" << std::endl;
250 test_by_char<wchar_t>(l1,name,lcids[i]);
251 }
252 std::cout << "- Testing strftime" <<std::endl;
253 test_date_time(gen("en_US.UTF-8"));
254 }
255 catch(std::exception const &e) {
256 std::cerr << "Failed " << e.what() << std::endl;
257 return EXIT_FAILURE;
258 }
259 FINALIZE();
260
261 }
262
263 #endif // no winapi
264
265 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
266
267 // boostinspect:noascii