]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/locale/test/test_codecvt.cpp
2 // Copyright (c) 2015 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)
8 #include <boost/locale/utf8_codecvt.hpp>
14 #define BOOST_LOCALE_ERROR_LIMIT -1
15 #include "test_locale.hpp"
17 static char const *utf8_name
= "\xf0\x9d\x92\x9e-\xD0\xBF\xD1\x80\xD0\xB8\xD0\xB2\xD0\xB5\xD1\x82-\xE3\x82\x84\xE3\x81\x82.txt";
18 static wchar_t const *wide_name
= L
"\U0001D49E-\u043F\u0440\u0438\u0432\u0435\u0442-\u3084\u3042.txt";
20 char const *res(std::codecvt_base::result r
)
23 case std::codecvt_base::ok
: return "ok";
24 case std::codecvt_base::partial
: return "partial";
25 case std::codecvt_base::error
: return "error";
26 case std::codecvt_base::noconv
: return "noconv";
32 typedef std::codecvt
<wchar_t,char,std::mbstate_t> cvt_type
;
34 void test_codecvt_in_n_m(cvt_type
const &cvt
,int n
,int m
)
36 wchar_t const *wptr
= wide_name
;
37 int wlen
= wcslen(wide_name
);
38 int u8len
= strlen(utf8_name
);
39 char const *from
= utf8_name
;
40 char const *end
= from
;
41 char const *real_end
= utf8_name
+ u8len
;
42 char const *from_next
= from
;
43 std::mbstate_t mb
=std::mbstate_t();
44 while(from_next
< real_end
) {
53 wchar_t *to_end
= to
+ m
;
54 wchar_t *to_next
= to
;
57 std::mbstate_t mb2
= mb
;
58 std::codecvt_base::result r
= cvt
.in(mb
,from
,end
,from_next
,to
,to_end
,to_next
);
59 //std::cout << "In from_size=" << (end-from) << " from move=" << (from_next - from) << " to move= " << to_next - to << " state = " << res(r) << std::endl;
61 int count
= cvt
.length(mb2
,from
,end
,to_end
- to
);
62 #ifndef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
63 TEST(memcmp(&mb
,&mb2
,sizeof(mb
))==0);
64 if(count
!= from_next
- from
) {
65 std::cout
<< count
<< " " << from_next
- from
<< std::endl
;
67 TEST(count
== from_next
- from
);
69 TEST(count
== to_next
- to
);
73 if(r
== cvt_type::partial
) {
79 TEST(r
== cvt_type::ok
);
88 TEST(wptr
== wide_name
+ wlen
);
89 TEST(from
== real_end
);
93 void test_codecvt_out_n_m(cvt_type
const &cvt
,int n
,int m
)
95 char const *nptr
= utf8_name
;
96 int wlen
= wcslen(wide_name
);
97 int u8len
= strlen(utf8_name
);
99 std::mbstate_t mb
=std::mbstate_t();
101 wchar_t const *from_next
= wide_name
;
102 wchar_t const *real_from_end
= wide_name
+ wlen
;
107 char *to_end
= to
+ n
;
108 char *real_to_end
= buf
+ sizeof(buf
);
110 while(from_next
< real_from_end
) {
111 wchar_t const *from
= from_next
;
112 wchar_t const *from_end
= from
+ m
;
113 if(from_end
> real_from_end
)
114 from_end
= real_from_end
;
119 std::codecvt_base::result r
= cvt
.out(mb
,from
,from_end
,from_next
,to
,to_end
,to_next
);
120 //std::cout << "In from_size=" << (end-from) << " from move=" << (from_next - from) << " to move= " << to_next - to << " state = " << res(r) << std::endl;
121 if(r
== cvt_type::partial
) {
122 TEST(to_end
- to_next
< cvt
.max_length());
124 if(to_end
> real_to_end
)
125 to_end
= real_to_end
;
128 TEST(r
== cvt_type::ok
);
138 TEST(nptr
== utf8_name
+ u8len
);
139 TEST(from_next
== real_from_end
);
140 TEST(cvt
.unshift(mb
,to
,to
+n
,to_next
)==cvt_type::ok
);
146 void test_codecvt_conv()
148 std::cout
<< "Conversions " << std::endl
;
149 std::locale
l(std::locale::classic(),new boost::locale::utf8_codecvt
<wchar_t>());
151 cvt_type
const &cvt
= std::use_facet
<cvt_type
>(l
);
153 TEST(cvt
.max_length()==4);
155 for(int i
=1;i
<=(int)strlen(utf8_name
)+1;i
++) {
156 for(int j
=1;j
<=(int)wcslen(wide_name
)+1;j
++) {
158 test_codecvt_in_n_m(cvt
,i
,j
);
159 test_codecvt_out_n_m(cvt
,i
,j
);
162 std::cerr
<< "Wlen=" <<j
<< " Nlen=" << i
<< std::endl
;
169 void test_codecvt_err()
171 std::cout
<< "Errors " << std::endl
;
172 std::locale
l(std::locale::classic(),new boost::locale::utf8_codecvt
<wchar_t>());
174 cvt_type
const &cvt
= std::use_facet
<cvt_type
>(l
);
176 std::cout
<< "- UTF-8" << std::endl
;
181 wchar_t *to_end
= buf
+2;
182 wchar_t *to_next
= to
;
183 char const *err_utf
="1\xFF\xFF";
185 std::mbstate_t mb
=std::mbstate_t();
186 char const *from
=err_utf
;
187 char const *from_end
= from
+ strlen(from
);
188 char const *from_next
= from
;
190 TEST(cvt
.in(mb
,from
,from_end
,from_next
,to
,to_end
,to_next
)==cvt_type::error
);
191 TEST(from_next
== from
+1);
192 TEST(to_next
== to
+ 1);
197 std::mbstate_t mb
=std::mbstate_t();
198 char const *from
=err_utf
;
199 char const *from_end
= from
+ strlen(from
);
200 char const *from_next
= from
;
201 TEST(cvt
.in(mb
,from
,from_end
,from_next
,to
,to_end
,to_next
)==cvt_type::error
);
202 TEST(from_next
== from
);
207 std::cout
<< "- UTF-16/32" << std::endl
;
212 char *to_end
= buf
+32;
214 wchar_t err_buf
[3] = { '1' , 0xDC9E }; // second surrogate not works both for UTF-16 and 32
215 wchar_t const *err_utf
= err_buf
;
217 std::mbstate_t mb
=std::mbstate_t();
218 wchar_t const *from
=err_utf
;
219 wchar_t const *from_end
= from
+ wcslen(from
);
220 wchar_t const *from_next
= from
;
221 TEST(cvt
.out(mb
,from
,from_end
,from_next
,to
,to_end
,to_next
)==cvt_type::error
);
222 TEST(from_next
== from
+1);
223 TEST(to_next
== to
+ 1);
228 std::mbstate_t mb
=std::mbstate_t();
229 wchar_t const *from
=err_utf
;
230 wchar_t const *from_end
= from
+ wcslen(from
);
231 wchar_t const *from_next
= from
;
233 TEST(cvt
.out(mb
,from
,from_end
,from_next
,to
,to_end
,to_next
)==cvt_type::error
);
234 TEST(from_next
== from
);
242 void test_char_char()
244 std::cout
<< "Char-char specialization"<<std::endl
;
245 std::locale
l(std::locale::classic(),new boost::locale::utf8_codecvt
<char>());
246 std::codecvt
<char,char,std::mbstate_t> const &cvt
=std::use_facet
<std::codecvt
<char,char,std::mbstate_t> >(l
);
247 std::mbstate_t mb
=std::mbstate_t();
248 char const *from
= "a";
249 char const *from_end
= from
+1;
250 char const *from_next
= from
;
253 char *to_end
= buf
+1;
255 TEST(cvt
.always_noconv()==true);
256 TEST(cvt
.in(mb
,from
,from_end
,from_next
,to
,to_end
,to_next
)==cvt_type::noconv
);
257 TEST(from_next
== from
);
259 TEST(cvt
.out(mb
,from
,from_end
,from_next
,to
,to_end
,to_next
)==cvt_type::noconv
);
260 TEST(from_next
== from
);
262 TEST(cvt
.encoding()==1);
263 TEST(cvt
.max_length()==1);
274 catch(std::exception
const &e
) {
275 std::cerr
<< "Failed : " << e
.what() << std::endl
;
278 std::cout
<< "Ok" << std::endl
;
282 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4