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>
main()10 int main()
11 {
12 std::cout << "POSIX Backend is not build... Skipping" << std::endl;
13 }
14 #else
15 #include <boost/locale/formatting.hpp>
16 #include <boost/locale/localization_backend.hpp>
17 #include <boost/locale/generator.hpp>
18 #include <boost/locale/encoding.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 #include <time.h>
27 #include <monetary.h>
28 #include <assert.h>
29 #include <langinfo.h>
30
31 //#define DEBUG_FMT
32
equal(std::string const & s1,std::string const & s2,locale_t)33 bool equal(std::string const &s1,std::string const &s2,locale_t /*lc*/)
34 {
35 return s1 == s2;
36 }
37
equal(std::wstring const & s1,std::string const & s2,locale_t lc)38 bool equal(std::wstring const &s1,std::string const &s2,locale_t lc)
39 {
40 return s1 == boost::locale::conv::to_utf<wchar_t>(s2,nl_langinfo_l(CODESET,lc));
41 }
42
43 template<typename CharType>
conv_to_char(char const * p)44 std::basic_string<CharType> conv_to_char(char const *p)
45 {
46 std::basic_string<CharType> r;
47 while(*p)
48 r+=CharType(*p++);
49 return r;
50 }
51
52
53 template<typename CharType,typename RefCharType>
test_by_char(std::locale const & l,locale_t lreal)54 void test_by_char(std::locale const &l,locale_t lreal)
55 {
56 typedef std::basic_stringstream<CharType> ss_type;
57
58 using namespace boost::locale;
59
60 {
61 std::cout << "- Testing as::posix" << std::endl;
62 ss_type ss;
63 ss.imbue(l);
64
65 ss << 1045.45;
66 TEST(ss);
67 double n;
68 ss >> n;
69 TEST(ss);
70 TEST(n == 1045.45);
71 TEST(ss.str()==to_correct_string<CharType>("1045.45",l));
72 #ifdef DEBUG_FMT
73 std::cout << "[" << boost::locale::conv::from_utf(ss.str(),"UTF-8") << "]=\n" ;
74 #endif
75 }
76
77 {
78 std::cout << "- Testing as::number" << std::endl;
79 ss_type ss;
80 ss.imbue(l);
81
82 ss << as::number;
83 ss << 1045.45;
84 TEST(ss);
85 double n;
86 ss >> n;
87 TEST(ss);
88 TEST(n == 1045.45);
89
90 if(std::use_facet<boost::locale::info>(l).country()=="US")
91 TEST(equal(ss.str(),"1,045.45",lreal));
92 }
93
94 {
95 std::cout << "- Testing as::currency national " << std::endl;
96
97 char buf[256];
98 strfmon_l(buf,256,lreal,"%n",1043.34);
99
100 ss_type ss;
101 ss.imbue(l);
102
103 ss << as::currency;
104 ss << 1043.34;
105 TEST(ss);
106
107 TEST(equal(ss.str(),buf,lreal));
108 #ifdef DEBUG_FMT
109 std::cout << "[" << boost::locale::conv::from_utf(ss.str(),"UTF-8") << "]=\n" ;
110 std::cout << "[" << boost::locale::conv::from_utf(buf,"UTF-8") << "]\n" ;
111 #endif
112
113 }
114
115 {
116 std::cout << "- Testing as::currency iso" << std::endl;
117 char buf[256];
118 strfmon_l(buf,256,lreal,"%i",1043.34);
119 ss_type ss;
120 ss.imbue(l);
121
122 ss << as::currency << as::currency_iso;
123 ss << 1043.34;
124 TEST(ss);
125
126 TEST(equal(ss.str(),buf,lreal));
127 #ifdef DEBUG_FMT
128 std::cout << "[" << boost::locale::conv::from_utf(ss.str(),"UTF-8") << "]=\n" ;
129 std::cout << "[" << boost::locale::conv::from_utf(buf,"UTF-8") << "]\n" ;
130 #endif
131 }
132
133
134 {
135 std::cout << "- Testing as::date/time" << std::endl;
136 ss_type ss;
137 ss.imbue(l);
138
139 time_t a_date = 3600*24*(31+4); // Feb 5th
140 time_t a_time = 3600*15+60*33; // 15:33:05
141 time_t a_timesec = 13;
142 time_t a_datetime = a_date + a_time + a_timesec;
143
144 ss << as::time_zone("GMT");
145
146 ss << as::date << a_datetime << CharType('\n');
147 ss << as::time << a_datetime << CharType('\n');
148 ss << as::datetime << a_datetime << CharType('\n');
149 ss << as::time_zone("GMT+01:00");
150 ss << as::ftime(conv_to_char<CharType>("%H")) << a_datetime << CharType('\n');
151 ss << as::time_zone("GMT+00:15");
152 ss << as::ftime(conv_to_char<CharType>("%M")) << a_datetime << CharType('\n');
153
154 std::tm tm=*gmtime(&a_datetime);
155 char buf[256];
156 strftime_l(buf,sizeof(buf),"%x\n%X\n%c\n16\n48\n",&tm,lreal);
157
158 TEST(equal(ss.str(),buf,lreal));
159 #ifdef DEBUG_FMT
160 std::cout << "[" << boost::locale::conv::from_utf(ss.str(),"UTF-8") << "]=\n" ;
161 std::cout << "[" << boost::locale::conv::from_utf(buf,"UTF-8") << "]\n" ;
162 #endif
163 }
164
165 }
166
main()167 int main()
168 {
169 locale_t lreal = 0;
170 try {
171 boost::locale::localization_backend_manager mgr = boost::locale::localization_backend_manager::global();
172 mgr.select("posix");
173 boost::locale::localization_backend_manager::global(mgr);
174 boost::locale::generator gen;
175 std::string name;
176
177 {
178 std::cout << "en_US.UTF locale" << std::endl;
179 name="en_US.UTF-8";
180 if(!have_locale(name)) {
181 std::cout << "en_US.UTF-8 not supported" << std::endl;
182 }
183 else {
184 std::locale l1=gen(name);
185 lreal=newlocale(LC_ALL_MASK,name.c_str(),0);
186 assert(lreal);
187 std::cout << "UTF-8" << std::endl;
188
189 test_by_char<char,char>(l1,lreal);
190
191 std::cout << "Wide UTF-" << sizeof(wchar_t) * 8 << std::endl;
192 test_by_char<wchar_t,char>(l1,lreal);
193 freelocale(lreal);
194 lreal = 0;
195 }
196 }
197 {
198 std::cout << "en_US.Latin-1 locale" << std::endl;
199 std::string name = "en_US.ISO8859-1";
200 if(!have_locale(name)) {
201 std::cout << "en_US.ISO8859-8 not supported" << std::endl;
202 }
203 else {
204 std::locale l1=gen(name);
205 lreal=newlocale(LC_ALL_MASK,name.c_str(),0);
206 assert(lreal);
207 test_by_char<char,char>(l1,lreal);
208 std::cout << "Wide UTF-" << sizeof(wchar_t) * 8 << std::endl;
209 test_by_char<wchar_t,char>(l1,lreal);
210 freelocale(lreal);
211 lreal = 0;
212 }
213 }
214 {
215 std::cout << "he_IL.UTF locale" << std::endl;
216 name="he_IL.UTF-8";
217 if(!have_locale(name)) {
218 std::cout << name << " not supported" << std::endl;
219 }
220 else {
221 std::locale l1=gen(name);
222 lreal=newlocale(LC_ALL_MASK,name.c_str(),0);
223 assert(lreal);
224 std::cout << "UTF-8" << std::endl;
225
226 test_by_char<char,char>(l1,lreal);
227
228 std::cout << "Wide UTF-" << sizeof(wchar_t) * 8 << std::endl;
229 test_by_char<wchar_t,char>(l1,lreal);
230 freelocale(lreal);
231 lreal = 0;
232 }
233 }
234 {
235 std::cout << "he_IL.ISO locale" << std::endl;
236 std::string name = "he_IL.ISO8859-8";
237 if(!have_locale(name)) {
238 std::cout << name <<" not supported" << std::endl;
239 }
240 else {
241 std::locale l1=gen(name);
242 lreal=newlocale(LC_ALL_MASK,name.c_str(),0);
243 assert(lreal);
244 test_by_char<char,char>(l1,lreal);
245 std::cout << "Wide UTF-" << sizeof(wchar_t) * 8 << std::endl;
246 test_by_char<wchar_t,char>(l1,lreal);
247 freelocale(lreal);
248 lreal = 0;
249 }
250 }
251 {
252 std::cout << "Testing UTF-8 punct issues" << std::endl;
253 std::string name = "ru_RU.UTF-8";
254 if(!have_locale(name)) {
255 std::cout << "- No russian locale" << std::endl;
256 }
257 else {
258 std::locale l1=gen(name);
259 std::ostringstream ss;
260 ss.imbue(l1);
261 ss << std::setprecision(10) ;
262 ss << boost::locale::as::number << 12345.45;
263 std::string v=ss.str();
264 TEST(v == "12345,45" || v == "12 345,45" || v=="12.345,45");
265 }
266 }
267 }
268 catch(std::exception const &e) {
269 std::cerr << "Failed " << e.what() << std::endl;
270 if(lreal)
271 freelocale(lreal);
272 return EXIT_FAILURE;
273 }
274 FINALIZE();
275
276 }
277
278 #endif // posix
279 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
280
281 // boostinspect:noascii
282