]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | #ifndef BOOST_LOCALE_WITH_ICU | |
9 | #include <iostream> | |
10 | int main() | |
11 | { | |
12 | std::cout << "ICU is not build... Skipping" << std::endl; | |
13 | } | |
14 | #else | |
15 | ||
16 | #ifdef _MSC_VER | |
17 | #define _CRT_SECURE_NO_WARNINGS | |
18 | // Disable this "security crap" | |
19 | #endif | |
20 | ||
21 | #include <boost/locale/formatting.hpp> | |
22 | #include <boost/locale/format.hpp> | |
23 | #include <boost/locale/date_time.hpp> | |
24 | #include <boost/locale/generator.hpp> | |
25 | #include "test_locale.hpp" | |
26 | #include "test_locale_tools.hpp" | |
27 | #include <sstream> | |
28 | #include <iostream> | |
29 | #include <iomanip> | |
30 | #include <limits> | |
31 | ||
32 | #include <unicode/uversion.h> | |
33 | ||
34 | using namespace boost::locale; | |
35 | ||
36 | //#define TEST_DEBUG | |
37 | #ifdef BOOST_MSVC | |
38 | #define _CRT_SECURE_NO_WARNINGS | |
39 | #endif | |
40 | ||
41 | #ifdef TEST_DEBUG | |
42 | #undef BOOST_LOCALE_ENABLE_CHAR16_T | |
43 | #undef BOOST_LOCALE_ENABLE_CHAR32_T | |
44 | template<typename T> | |
45 | void print_diff(T const &,T const &,int) | |
46 | { | |
47 | } | |
48 | template<> | |
49 | void print_diff(std::string const &l,std::string const &r,int line) | |
50 | { | |
51 | if(l!=r) { | |
52 | std::cerr << "----[" << l <<"]!=\n----["<<r<<"] in " << line << std::endl; | |
53 | } | |
54 | } | |
55 | ||
56 | #define TESTEQ(x,y) do { print_diff((x),(y),__LINE__); TEST((x)==(y)); } while(0) | |
57 | #else | |
58 | #define TESTEQ(x,y) TEST((x)==(y)) | |
59 | #endif | |
60 | ||
61 | #define TEST_FMT(manip,value,expected) \ | |
62 | do{ \ | |
63 | std::basic_ostringstream<CharType> ss; \ | |
64 | ss.imbue(loc); \ | |
65 | ss << manip << value; \ | |
66 | TESTEQ(ss.str(),to_correct_string<CharType>(expected,loc)); \ | |
67 | }while(0) | |
68 | ||
69 | #ifndef _LIBCPP_VERSION | |
70 | static bool parsing_fails() | |
71 | { | |
72 | return true; | |
73 | } | |
74 | #else | |
75 | static bool parsing_fails() | |
76 | { | |
77 | static bool checked=false; | |
78 | static bool fails; | |
79 | if(!checked) { | |
80 | try { | |
81 | std::istringstream ss("x"); | |
82 | ss.exceptions(std::ios_base::failbit); | |
83 | int x; | |
84 | ss>>x; | |
85 | fails =false; | |
86 | } | |
87 | catch(std::ios_base::failure const &) { | |
88 | fails=true; | |
89 | } | |
90 | catch(...) { | |
91 | fails=false; | |
92 | } | |
93 | checked=true; | |
94 | if(!fails) { | |
95 | std::cerr << "!!! Warning: libc++ library does not throw an exception on failbit !!!" << std::endl; | |
96 | } | |
97 | } | |
98 | return fails; | |
99 | } | |
100 | #endif | |
101 | ||
102 | ||
103 | #define TEST_NOPAR(manip,actual,type) \ | |
104 | do{ \ | |
105 | type v; \ | |
106 | std::basic_string<CharType> act= \ | |
107 | to_correct_string<CharType>(actual,loc); \ | |
108 | { \ | |
109 | std::basic_istringstream<CharType> ss; \ | |
110 | ss.imbue(loc); \ | |
111 | ss.str(act); \ | |
112 | ss >> manip >> v ; \ | |
113 | TEST(ss.fail()); \ | |
114 | } \ | |
115 | if(parsing_fails()){ \ | |
116 | std::basic_istringstream<CharType> ss; \ | |
117 | ss.imbue(loc); \ | |
118 | ss.str(act); \ | |
119 | ss.exceptions(std::ios_base::failbit); \ | |
120 | ss >> manip; \ | |
121 | TEST_THROWS(ss >> v,std::ios_base::failure); \ | |
122 | } \ | |
123 | }while(0) | |
124 | ||
125 | #define TEST_PAR(manip,type,actual,expected) \ | |
126 | do{ \ | |
127 | type v; \ | |
128 | {std::basic_istringstream<CharType> ss; \ | |
129 | ss.imbue(loc); \ | |
130 | ss.str(to_correct_string<CharType>(actual,loc)); \ | |
131 | ss >> manip >> v >> std::ws; \ | |
132 | TESTEQ(v,expected); \ | |
133 | TEST(ss.eof()); }\ | |
134 | {std::basic_istringstream<CharType> ss; \ | |
135 | ss.imbue(loc); \ | |
136 | ss.str(to_correct_string<CharType>(std::string(actual)+"@",loc)); \ | |
137 | CharType tmp_c; \ | |
138 | ss >> manip >> v >> std::skipws >> tmp_c; \ | |
139 | TESTEQ(v,expected); \ | |
140 | TEST(tmp_c=='@'); } \ | |
141 | }while(0) | |
142 | ||
143 | #define TEST_FP1(manip,value_in,str,type,value_out) \ | |
144 | do { \ | |
145 | TEST_FMT(manip,value_in,str); \ | |
146 | TEST_PAR(manip,type,str,value_out); \ | |
147 | }while(0) | |
148 | ||
149 | #define TEST_FP2(m1,m2,value_in,str,type,value_out) \ | |
150 | do { \ | |
151 | TEST_FMT(m1<<m2,value_in,str); \ | |
152 | TEST_PAR(m1>>m2,type,str,value_out); \ | |
153 | }while(0) | |
154 | ||
155 | #define TEST_FP3(m1,m2,m3,value_in,str,type,value_out) \ | |
156 | do { \ | |
157 | TEST_FMT(m1<<m2<<m3,value_in,str); \ | |
158 | TEST_PAR(m1>>m2>>m3,type,str,value_out); \ | |
159 | }while(0) | |
160 | ||
161 | #define TEST_FP4(m1,m2,m3,m4,value_in,str,type,value_out) \ | |
162 | do { \ | |
163 | TEST_FMT(m1<<m2<<m3<<m4,value_in,str); \ | |
164 | TEST_PAR(m1>>m2>>m3>>m4,type,str,value_out); \ | |
165 | }while(0) | |
166 | ||
167 | ||
168 | #define FORMAT(f,v,exp) \ | |
169 | do{\ | |
170 | std::basic_ostringstream<CharType> ss; \ | |
171 | ss.imbue(loc); \ | |
172 | std::basic_string<CharType> fmt = to_correct_string<CharType>(f,loc); \ | |
173 | ss << boost::locale::basic_format<CharType>(fmt) % v; \ | |
174 | TESTEQ(ss.str(),to_correct_string<CharType>(exp,loc)); \ | |
175 | ss.str(to_correct_string<CharType>("",loc)); \ | |
176 | ss << boost::locale::basic_format<CharType>(boost::locale::translate(fmt.c_str())) % v; \ | |
177 | /*ss << boost::locale::basic_format<CharType>(fmt) % v; */ \ | |
178 | TESTEQ(ss.str(),to_correct_string<CharType>(exp,loc)); \ | |
179 | TESTEQ( (boost::locale::basic_format<CharType>(fmt) % v).str(loc),to_correct_string<CharType>(exp,loc)); \ | |
180 | } while(0) | |
181 | ||
182 | ||
183 | #define TEST_MIN_MAX_FMT(type,minval,maxval) \ | |
184 | do { \ | |
185 | TEST_FMT(as::number,std::numeric_limits<type>::min(),minval); \ | |
186 | TEST_FMT(as::number,std::numeric_limits<type>::max(),maxval); \ | |
187 | }while(0) | |
188 | ||
189 | #define TEST_MIN_MAX_PAR(type,minval,maxval) \ | |
190 | do {\ | |
191 | TEST_PAR(as::number,type,minval,std::numeric_limits<type>::min()); \ | |
192 | TEST_PAR(as::number,type,maxval,std::numeric_limits<type>::max()); \ | |
193 | }while(0) | |
194 | ||
195 | #define TEST_MIN_MAX(type,minval,maxval) \ | |
196 | do { \ | |
197 | TEST_MIN_MAX_FMT(type,minval,maxval); \ | |
198 | TEST_MIN_MAX_PAR(type,minval,maxval); \ | |
199 | }while(0) | |
200 | ||
201 | ||
202 | #define BOOST_ICU_VER (U_ICU_VERSION_MAJOR_NUM*100 + U_ICU_VERSION_MINOR_NUM) | |
203 | #define BOOST_ICU_EXACT_VER (U_ICU_VERSION_MAJOR_NUM*10000 + U_ICU_VERSION_MINOR_NUM * 100 + U_ICU_VERSION_PATCHLEVEL_NUM) | |
204 | ||
205 | bool short_parsing_fails() | |
206 | { | |
207 | static bool fails = false; | |
208 | static bool get_result = false; | |
209 | if(get_result) | |
210 | return fails; | |
211 | std::stringstream ss("65000"); | |
212 | ss.imbue(std::locale::classic()); | |
213 | short v=0; | |
214 | ss >> v; | |
215 | fails = ss.fail(); | |
216 | get_result = true; | |
217 | return fails; | |
218 | } | |
219 | ||
220 | template<typename CharType> | |
221 | void test_manip(std::string e_charset="UTF-8") | |
222 | { | |
223 | boost::locale::generator g; | |
224 | std::locale loc=g("en_US."+e_charset); | |
225 | ||
226 | TEST_FP1(as::posix,1200.1,"1200.1",double,1200.1); | |
227 | TEST_FP1(as::number,1200.1,"1,200.1",double,1200.1); | |
228 | TEST_FMT(as::number<<std::setfill(CharType('_'))<<std::setw(6),1534,"_1,534"); | |
229 | TEST_FMT(as::number<<std::left<<std::setfill(CharType('_'))<<std::setw(6),1534,"1,534_"); | |
230 | ||
231 | // Ranges | |
232 | if(sizeof(short) == 2) { | |
233 | TEST_MIN_MAX(short,"-32,768","32,767"); | |
234 | TEST_MIN_MAX(unsigned short,"0","65,535"); | |
235 | TEST_NOPAR(as::number,"-1",unsigned short); | |
236 | if(short_parsing_fails()) { | |
237 | TEST_NOPAR(as::number,"65,535",short); | |
238 | } | |
239 | } | |
240 | if(sizeof(int)==4) { | |
241 | TEST_MIN_MAX(int,"-2,147,483,648","2,147,483,647"); | |
242 | TEST_MIN_MAX(unsigned int,"0","4,294,967,295"); | |
243 | TEST_NOPAR(as::number,"-1",unsigned int); | |
244 | TEST_NOPAR(as::number,"4,294,967,295",int); | |
245 | } | |
246 | if(sizeof(long)==4) { | |
247 | TEST_MIN_MAX(long,"-2,147,483,648","2,147,483,647"); | |
248 | TEST_MIN_MAX(unsigned long,"0","4,294,967,295"); | |
249 | TEST_NOPAR(as::number,"-1",unsigned long); | |
250 | TEST_NOPAR(as::number,"4,294,967,295",long); | |
251 | } | |
252 | if(sizeof(long)==8) { | |
253 | TEST_MIN_MAX(long,"-9,223,372,036,854,775,808","9,223,372,036,854,775,807"); | |
254 | TEST_MIN_MAX_FMT(unsigned long,"0","18446744073709551615"); // Unsupported range by icu - ensure fallback | |
255 | TEST_NOPAR(as::number,"-1",unsigned long); | |
256 | } | |
257 | #ifndef BOOST_NO_LONG_LONG | |
258 | if(sizeof(long long)==8) { | |
259 | TEST_MIN_MAX(long long,"-9,223,372,036,854,775,808","9,223,372,036,854,775,807"); | |
260 | // we can't really parse this as ICU does not support this range, only format | |
261 | TEST_MIN_MAX_FMT(unsigned long long,"0","18446744073709551615"); // Unsupported range by icu - ensure fallback | |
262 | TEST_FMT(as::number,9223372036854775807ULL,"9,223,372,036,854,775,807"); | |
263 | TEST_FMT(as::number,9223372036854775808ULL,"9223372036854775808"); // Unsupported range by icu - ensure fallback | |
264 | TEST_NOPAR(as::number,"-1",unsigned long long); | |
265 | } | |
266 | #endif | |
267 | ||
268 | ||
269 | ||
270 | TEST_FP3(as::number,std::left,std::setw(3),15,"15 ",int,15); | |
271 | TEST_FP3(as::number,std::right,std::setw(3),15," 15",int,15); | |
272 | TEST_FP3(as::number,std::setprecision(3),std::fixed,13.1,"13.100",double,13.1); | |
11fdf7f2 TL |
273 | #if BOOST_ICU_VER < 5601 |
274 | // bug #13276 | |
7c673cae | 275 | TEST_FP3(as::number,std::setprecision(3),std::scientific,13.1,"1.310E1",double,13.1); |
11fdf7f2 | 276 | #endif |
7c673cae FG |
277 | |
278 | TEST_NOPAR(as::number,"",int); | |
279 | TEST_NOPAR(as::number,"--3",int); | |
280 | TEST_NOPAR(as::number,"y",int); | |
281 | ||
282 | TEST_FP1(as::percent,0.1,"10%",double,0.1); | |
283 | TEST_FP3(as::percent,std::fixed,std::setprecision(1),0.10,"10.0%",double,0.1); | |
284 | ||
285 | TEST_NOPAR(as::percent,"1",double); | |
286 | ||
287 | TEST_FP1(as::currency,1345,"$1,345.00",int,1345); | |
288 | TEST_FP1(as::currency,1345.34,"$1,345.34",double,1345.34); | |
289 | ||
290 | TEST_NOPAR(as::currency,"$",double); | |
291 | ||
292 | ||
293 | #if BOOST_ICU_VER >= 402 | |
294 | TEST_FP2(as::currency,as::currency_national,1345,"$1,345.00",int,1345); | |
295 | TEST_FP2(as::currency,as::currency_national,1345.34,"$1,345.34",double,1345.34); | |
296 | TEST_FP2(as::currency,as::currency_iso,1345,"USD1,345.00",int,1345); | |
297 | TEST_FP2(as::currency,as::currency_iso,1345.34,"USD1,345.34",double,1345.34); | |
298 | #endif | |
299 | TEST_FP1(as::spellout,10,"ten",int,10); | |
300 | #if 402 <= BOOST_ICU_VER && BOOST_ICU_VER < 408 | |
301 | if(e_charset=="UTF-8") { | |
302 | TEST_FMT(as::ordinal,1,"1\xcb\xa2\xe1\xb5\x97"); // 1st with st as ligatures | |
303 | } | |
304 | #else | |
305 | TEST_FMT(as::ordinal,1,"1st"); | |
306 | #endif | |
307 | ||
308 | time_t a_date = 3600*24*(31+4); // Feb 5th | |
309 | time_t a_time = 3600*15+60*33; // 15:33:05 | |
310 | time_t a_timesec = 13; | |
311 | time_t a_datetime = a_date + a_time + a_timesec; | |
312 | ||
313 | TEST_FP2(as::date, as::gmt,a_datetime,"Feb 5, 1970",time_t,a_date); | |
314 | TEST_FP3(as::date,as::date_short ,as::gmt,a_datetime,"2/5/70",time_t,a_date); | |
315 | TEST_FP3(as::date,as::date_medium,as::gmt,a_datetime,"Feb 5, 1970",time_t,a_date); | |
316 | TEST_FP3(as::date,as::date_long ,as::gmt,a_datetime,"February 5, 1970",time_t,a_date); | |
317 | TEST_FP3(as::date,as::date_full ,as::gmt,a_datetime,"Thursday, February 5, 1970",time_t,a_date); | |
318 | ||
319 | TEST_NOPAR(as::date>>as::date_short,"aa/bb/cc",double); | |
11fdf7f2 TL |
320 | |
321 | #if BOOST_ICU_VER >= 5901 | |
322 | #define GMT_FULL "Greenwich Mean Time" | |
323 | #else | |
324 | #define GMT_FULL "GMT" | |
325 | #endif | |
7c673cae FG |
326 | |
327 | TEST_FP2(as::time, as::gmt,a_datetime,"3:33:13 PM",time_t,a_time+a_timesec); | |
328 | TEST_FP3(as::time,as::time_short ,as::gmt,a_datetime,"3:33 PM",time_t,a_time); | |
329 | TEST_FP3(as::time,as::time_medium,as::gmt,a_datetime,"3:33:13 PM",time_t,a_time+a_timesec); | |
330 | #if BOOST_ICU_VER >= 408 | |
331 | TEST_FP3(as::time,as::time_long ,as::gmt,a_datetime,"3:33:13 PM GMT",time_t,a_time+a_timesec); | |
332 | #if BOOST_ICU_EXACT_VER != 40800 | |
333 | // know bug #8675 | |
11fdf7f2 | 334 | TEST_FP3(as::time,as::time_full ,as::gmt,a_datetime,"3:33:13 PM " GMT_FULL,time_t,a_time+a_timesec); |
7c673cae FG |
335 | #endif |
336 | #else | |
337 | TEST_FP3(as::time,as::time_long ,as::gmt,a_datetime,"3:33:13 PM GMT+00:00",time_t,a_time+a_timesec); | |
338 | TEST_FP3(as::time,as::time_full ,as::gmt,a_datetime,"3:33:13 PM GMT+00:00",time_t,a_time+a_timesec); | |
339 | #endif | |
340 | ||
341 | TEST_NOPAR(as::time,"AM",double); | |
342 | ||
343 | TEST_FP2(as::time, as::time_zone("GMT+01:00"),a_datetime,"4:33:13 PM",time_t,a_time+a_timesec); | |
344 | TEST_FP3(as::time,as::time_short ,as::time_zone("GMT+01:00"),a_datetime,"4:33 PM",time_t,a_time); | |
345 | TEST_FP3(as::time,as::time_medium,as::time_zone("GMT+01:00"),a_datetime,"4:33:13 PM",time_t,a_time+a_timesec); | |
346 | ||
347 | #if U_ICU_VERSION_MAJOR_NUM >= 52 | |
348 | #define GMT_P100 "GMT+1" | |
349 | #else | |
350 | #define GMT_P100 "GMT+01:00" | |
351 | #endif | |
352 | ||
11fdf7f2 | 353 | |
7c673cae FG |
354 | #if U_ICU_VERSION_MAJOR_NUM >= 50 |
355 | #define PERIOD "," | |
356 | #define ICUAT " at" | |
357 | #else | |
358 | #define PERIOD "" | |
359 | #define ICUAT "" | |
360 | #endif | |
361 | ||
362 | TEST_FP3(as::time,as::time_long ,as::time_zone("GMT+01:00"),a_datetime,"4:33:13 PM " GMT_P100,time_t,a_time+a_timesec); | |
363 | #if BOOST_ICU_VER == 308 && defined(__CYGWIN__) | |
364 | // Known faliture ICU issue | |
365 | #else | |
366 | TEST_FP3(as::time,as::time_full ,as::time_zone("GMT+01:00"),a_datetime,"4:33:13 PM GMT+01:00",time_t,a_time+a_timesec); | |
367 | #endif | |
368 | ||
369 | TEST_FP2(as::datetime, as::gmt,a_datetime,"Feb 5, 1970" PERIOD " 3:33:13 PM",time_t,a_datetime); | |
370 | TEST_FP4(as::datetime,as::date_short ,as::time_short ,as::gmt,a_datetime,"2/5/70" PERIOD " 3:33 PM",time_t,a_date+a_time); | |
371 | TEST_FP4(as::datetime,as::date_medium,as::time_medium,as::gmt,a_datetime,"Feb 5, 1970" PERIOD " 3:33:13 PM",time_t,a_datetime); | |
372 | #if BOOST_ICU_VER >= 408 | |
373 | TEST_FP4(as::datetime,as::date_long ,as::time_long ,as::gmt,a_datetime,"February 5, 1970" ICUAT " 3:33:13 PM GMT",time_t,a_datetime); | |
374 | #if BOOST_ICU_EXACT_VER != 40800 | |
375 | // know bug #8675 | |
11fdf7f2 | 376 | TEST_FP4(as::datetime,as::date_full ,as::time_full ,as::gmt,a_datetime,"Thursday, February 5, 1970" ICUAT " 3:33:13 PM " GMT_FULL,time_t,a_datetime); |
7c673cae FG |
377 | #endif |
378 | #else | |
379 | TEST_FP4(as::datetime,as::date_long ,as::time_long ,as::gmt,a_datetime,"February 5, 1970" PERIOD " 3:33:13 PM GMT+00:00",time_t,a_datetime); | |
380 | TEST_FP4(as::datetime,as::date_full ,as::time_full ,as::gmt,a_datetime,"Thursday, February 5, 1970" PERIOD " 3:33:13 PM GMT+00:00",time_t,a_datetime); | |
381 | #endif | |
382 | ||
383 | time_t now=time(0); | |
384 | time_t lnow = now + 3600 * 4; | |
385 | char local_time_str[256]; | |
386 | std::string format="%H:%M:%S"; | |
387 | std::basic_string<CharType> format_string(format.begin(),format.end()); | |
388 | strftime(local_time_str,sizeof(local_time_str),format.c_str(),gmtime(&lnow)); | |
389 | TEST_FMT(as::ftime(format_string),now,local_time_str); | |
390 | TEST_FMT(as::ftime(format_string)<<as::gmt<<as::local_time,now,local_time_str); | |
391 | ||
392 | std::string marks = | |
393 | "aAbB" | |
394 | "cdeh" | |
395 | "HIjm" | |
396 | "Mnpr" | |
397 | "RStT" | |
398 | "xXyY" | |
399 | "Z%"; | |
400 | ||
401 | std::string result[]= { | |
402 | "Thu","Thursday","Feb","February", // aAbB | |
403 | #if BOOST_ICU_VER >= 408 | |
11fdf7f2 | 404 | "Thursday, February 5, 1970" ICUAT " 3:33:13 PM " GMT_FULL, // c |
7c673cae FG |
405 | #else |
406 | "Thursday, February 5, 1970 3:33:13 PM GMT+00:00", // c | |
407 | #endif | |
408 | "05","5","Feb", // deh | |
409 | "15","03","36","02", // HIjm | |
410 | "33","\n","PM", "03:33:13 PM",// Mnpr | |
411 | "15:33","13","\t","15:33:13", // RStT | |
412 | "Feb 5, 1970","3:33:13 PM","70","1970", // xXyY | |
413 | #if BOOST_ICU_VER >= 408 | |
11fdf7f2 | 414 | GMT_FULL // Z |
7c673cae FG |
415 | #else |
416 | "GMT+00:00" // Z | |
417 | #endif | |
418 | ,"%" }; // % | |
419 | ||
420 | for(unsigned i=0;i<marks.size();i++) { | |
421 | format_string.clear(); | |
422 | format_string+=static_cast<CharType>('%'); | |
423 | format_string+=static_cast<CharType>(marks[i]); | |
424 | TEST_FMT(as::ftime(format_string)<<as::gmt,a_datetime,result[i]); | |
425 | } | |
426 | ||
427 | std::string sample_f[]={ | |
428 | "Now is %A, %H o'clo''ck ' or not ' ", | |
429 | "'test %H'", | |
430 | "%H'", | |
431 | "'%H'" | |
432 | }; | |
433 | std::string expected_f[] = { | |
434 | "Now is Thursday, 15 o'clo''ck ' or not ' ", | |
435 | "'test 15'", | |
436 | "15'", | |
437 | "'15'" | |
438 | }; | |
439 | ||
440 | for(unsigned i=0;i<sizeof(sample_f)/sizeof(sample_f[0]);i++) { | |
441 | format_string.assign(sample_f[i].begin(),sample_f[i].end()); | |
442 | TEST_FMT(as::ftime(format_string)<<as::gmt,a_datetime,expected_f[i]); | |
443 | } | |
444 | ||
445 | } | |
446 | ||
447 | template<typename CharType> | |
448 | void test_format(std::string charset="UTF-8") | |
449 | { | |
450 | boost::locale::generator g; | |
451 | std::locale loc=g("en_US."+charset); | |
452 | ||
453 | FORMAT("{3} {1} {2}", 1 % 2 % 3,"3 1 2"); | |
454 | FORMAT("{1} {2}", "hello" % 2,"hello 2"); | |
455 | FORMAT("{1}",1200.1,"1200.1"); | |
456 | FORMAT("Test {1,num}",1200.1,"Test 1,200.1"); | |
457 | FORMAT("{{}} {1,number}",1200.1,"{} 1,200.1"); | |
11fdf7f2 TL |
458 | #if BOOST_ICU_VER < 5601 |
459 | // bug #13276 | |
7c673cae FG |
460 | FORMAT("{1,num=sci,p=3}",13.1,"1.310E1"); |
461 | FORMAT("{1,num=scientific,p=3}",13.1,"1.310E1"); | |
11fdf7f2 | 462 | #endif |
7c673cae FG |
463 | FORMAT("{1,num=fix,p=3}",13.1,"13.100"); |
464 | FORMAT("{1,num=fixed,p=3}",13.1,"13.100"); | |
465 | FORMAT("{1,<,w=3,num}",-1,"-1 "); | |
466 | FORMAT("{1,>,w=3,num}",1," 1"); | |
467 | FORMAT("{per,1}",0.1,"10%"); | |
468 | FORMAT("{percent,1}",0.1,"10%"); | |
469 | FORMAT("{1,cur}",1234,"$1,234.00"); | |
470 | FORMAT("{1,currency}",1234,"$1,234.00"); | |
471 | if(charset=="UTF-8") { | |
472 | if(U_ICU_VERSION_MAJOR_NUM >=4) | |
473 | FORMAT("{1,cur,locale=de_DE}",10,"10,00\xC2\xA0€"); | |
474 | else | |
475 | FORMAT("{1,cur,locale=de_DE}",10,"10,00 €"); | |
476 | } | |
477 | #if BOOST_ICU_VER >= 402 | |
478 | FORMAT("{1,cur=nat}",1234,"$1,234.00"); | |
479 | FORMAT("{1,cur=national}",1234,"$1,234.00"); | |
480 | FORMAT("{1,cur=iso}",1234,"USD1,234.00"); | |
481 | #endif | |
482 | FORMAT("{1,spell}",10,"ten"); | |
483 | FORMAT("{1,spellout}",10,"ten"); | |
484 | #if 402 <= BOOST_ICU_VER && BOOST_ICU_VER < 408 | |
485 | if(charset=="UTF-8") { | |
486 | FORMAT("{1,ord}",1,"1\xcb\xa2\xe1\xb5\x97"); | |
487 | FORMAT("{1,ordinal}",1,"1\xcb\xa2\xe1\xb5\x97"); | |
488 | } | |
489 | #else | |
490 | FORMAT("{1,ord}",1,"1st"); | |
491 | FORMAT("{1,ordinal}",1,"1st"); | |
492 | #endif | |
493 | ||
494 | time_t now=time(0); | |
495 | time_t lnow = now + 3600 * 4; | |
496 | char local_time_str[256]; | |
497 | std::string format="'%H:%M:%S'"; | |
498 | std::basic_string<CharType> format_string(format.begin(),format.end()); | |
499 | strftime(local_time_str,sizeof(local_time_str),format.c_str(),gmtime(&lnow)); | |
500 | ||
501 | FORMAT("{1,ftime='''%H:%M:%S'''}",now,local_time_str); | |
502 | FORMAT("{1,local,ftime='''%H:%M:%S'''}",now,local_time_str); | |
503 | FORMAT("{1,ftime='''%H:%M:%S'''}",now,local_time_str); | |
504 | ||
505 | time_t a_date = 3600*24*(31+4); // Feb 5th | |
506 | time_t a_time = 3600*15+60*33; // 15:33:05 | |
507 | time_t a_timesec = 13; | |
508 | time_t a_datetime = a_date + a_time + a_timesec; | |
509 | FORMAT("{1,date,gmt};{1,time,gmt};{1,datetime,gmt};{1,dt,gmt}",a_datetime, | |
510 | "Feb 5, 1970;3:33:13 PM;Feb 5, 1970" PERIOD " 3:33:13 PM;Feb 5, 1970" PERIOD " 3:33:13 PM"); | |
511 | #if BOOST_ICU_VER >= 408 | |
512 | FORMAT("{1,time=short,gmt};{1,time=medium,gmt};{1,time=long,gmt};{1,date=full,gmt}",a_datetime, | |
513 | "3:33 PM;3:33:13 PM;3:33:13 PM GMT;Thursday, February 5, 1970"); | |
514 | FORMAT("{1,time=s,gmt};{1,time=m,gmt};{1,time=l,gmt};{1,date=f,gmt}",a_datetime, | |
515 | "3:33 PM;3:33:13 PM;3:33:13 PM GMT;Thursday, February 5, 1970"); | |
516 | #else | |
517 | FORMAT("{1,time=short,gmt};{1,time=medium,gmt};{1,time=long,gmt};{1,date=full,gmt}",a_datetime, | |
518 | "3:33 PM;3:33:13 PM;3:33:13 PM GMT+00:00;Thursday, February 5, 1970"); | |
519 | FORMAT("{1,time=s,gmt};{1,time=m,gmt};{1,time=l,gmt};{1,date=f,gmt}",a_datetime, | |
520 | "3:33 PM;3:33:13 PM;3:33:13 PM GMT+00:00;Thursday, February 5, 1970"); | |
521 | #endif | |
522 | FORMAT("{1,time=s,tz=GMT+01:00}",a_datetime,"4:33 PM"); | |
523 | FORMAT("{1,time=s,timezone=GMT+01:00}",a_datetime,"4:33 PM"); | |
524 | ||
525 | FORMAT("{1,gmt,ftime='%H'''}",a_datetime,"15'"); | |
526 | FORMAT("{1,gmt,ftime='''%H'}",a_datetime,"'15"); | |
527 | FORMAT("{1,gmt,ftime='%H o''clock'}",a_datetime,"15 o'clock"); | |
11fdf7f2 TL |
528 | |
529 | // Test not a year of the week | |
530 | a_datetime=1388491200; // 2013-12-31 12:00 - check we don't use week of year | |
531 | ||
532 | FORMAT("{1,gmt,ftime='%Y'}",a_datetime,"2013"); | |
533 | FORMAT("{1,gmt,ftime='%y'}",a_datetime,"13"); | |
534 | FORMAT("{1,gmt,ftime='%D'}",a_datetime,"12/31/13"); | |
7c673cae FG |
535 | } |
536 | ||
537 | ||
538 | int main() | |
539 | { | |
540 | try { | |
541 | boost::locale::time_zone::global("GMT+4:00"); | |
542 | std::cout << "Testing char, UTF-8" << std::endl; | |
543 | test_manip<char>(); | |
544 | test_format<char>(); | |
545 | std::cout << "Testing char, ISO8859-1" << std::endl; | |
546 | test_manip<char>("ISO8859-1"); | |
547 | test_format<char>("ISO8859-1"); | |
548 | ||
549 | std::cout << "Testing wchar_t" << std::endl; | |
550 | test_manip<wchar_t>(); | |
551 | test_format<wchar_t>(); | |
552 | ||
553 | #ifdef BOOST_LOCALE_ENABLE_CHAR16_T | |
554 | std::cout << "Testing char16_t" << std::endl; | |
555 | test_manip<char16_t>(); | |
556 | test_format<char16_t>(); | |
557 | #endif | |
558 | ||
559 | #ifdef BOOST_LOCALE_ENABLE_CHAR32_T | |
560 | std::cout << "Testing char32_t" << std::endl; | |
561 | test_manip<char32_t>(); | |
562 | test_format<char32_t>(); | |
563 | #endif | |
564 | ||
565 | } | |
566 | catch(std::exception const &e) { | |
567 | std::cerr << "Failed " << e.what() << std::endl; | |
568 | return EXIT_FAILURE; | |
569 | } | |
570 | FINALIZE(); | |
571 | ||
572 | } | |
573 | ||
574 | #endif // NOICU | |
575 | // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 | |
576 | // boostinspect:noascii |