2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
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)
9 #ifdef BOOST_LOCALE_NO_WINAPI_BACKEND
13 std::cout
<< "WinAPI Backend is not build... Skipping" << std::endl
;
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>
23 #include "test_locale.hpp"
24 #include "test_locale_tools.hpp"
25 #include "../src/win32/lcid.hpp"
39 bool equal(std::string
const &s1
,std::wstring
const &s2
)
41 bool res
= s1
== boost::locale::conv::from_utf(s2
,"UTF-8");
44 std::cout
<< "[" << s1
<< "]!=["<<boost::locale::conv::from_utf(s2
,"UTF-8")<<"]"<<std::endl
;
49 bool equal(std::wstring
const &s1
,std::wstring
const &s2
)
54 std::cout
<< "[" << boost::locale::conv::from_utf(s1
,"UTF-8") << "]!=["<<boost::locale::conv::from_utf(s2
,"UTF-8")<<"]"<<std::endl
;
60 bool equal(std::string
const &s1
,std::string
const &s2
)
65 std::cout
<< "[" << s1
<< "]!=["<<s2
<<"]"<<std::endl
;
70 bool equal(std::wstring
const &s1
,std::string
const &s2
)
72 bool res
= s1
== boost::locale::conv::to_utf
<wchar_t>(s2
,"UTF-8");
75 std::cout
<< "[" << boost::locale::conv::from_utf(s1
,"UTF-8") << "]!=["<<s2
<<"]"<<std::endl
;
81 template<typename CharType
>
82 std::basic_string
<CharType
> conv_to_char(char const *p
)
84 std::basic_string
<CharType
> r
;
91 template<typename CharType
>
92 void test_by_char(std::locale
const &l
,std::string name
,int lcid
)
94 typedef std::basic_stringstream
<CharType
> ss_type
;
95 typedef std::basic_string
<CharType
> string_type
;
97 using namespace boost::locale
;
100 std::cout
<< "--- Testing as::posix" << std::endl
;
110 TEST(equal(ss
.str(),"1045.45"));
114 std::cout
<< "--- Testing as::number" << std::endl
;
126 if(name
== "ru_RU.UTF-8") {
127 if(sizeof(CharType
)==1)
128 TEST(equal(ss
.str(),"1 045,45")); // SP
130 TEST(equal(ss
.str(),"1\xC2\xA0" "045,45")); // NBSP
133 TEST(equal(ss
.str(),"1,045.45"));
137 std::cout
<< "--- Testing as::currency " << std::endl
;
147 GetCurrencyFormatW(lcid
,0,L
"1043.34",0,buf
,256);
149 TEST(equal(ss
.str(),buf
));
153 std::cout
<< "--- Testing as::date/time" << std::endl
;
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
;
162 ss
<< as::time_zone("GMT");
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');
172 wchar_t time_buf
[256];
173 wchar_t date_buf
[256];
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"));
185 void test_date_time(std::locale l
)
187 std::ostringstream ss
;
190 ss
<< boost::locale::as::time_zone("GMT");
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
;
197 std::string pat
[] = {
221 for(unsigned i
=0;i
<sizeof(pat
)/sizeof(pat
[0]);i
+=2) {
223 ss
<< boost::locale::as::ftime("%" + pat
[i
]) << a_datetime
;
224 TEST(equal(ss
.str(),pat
[i
+1]));
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
;
236 std::string names
[] = { "en_US.UTF-8", "he_IL.UTF-8", "ru_RU.UTF-8" };
237 int lcids
[] = { 0x0409, 0x040D ,0x0419 };
239 for(unsigned i
=0;i
<sizeof(names
)/sizeof(names
[9]);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
;
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
]);
252 std::cout
<< "- Testing strftime" <<std::endl
;
253 test_date_time(gen("en_US.UTF-8"));
255 catch(std::exception
const &e
) {
256 std::cerr
<< "Failed " << e
.what() << std::endl
;
265 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
267 // boostinspect:noascii