]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/serialization/test/test_utf8_codecvt.cpp
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_utf8_codecvt.cpp
4 // (C) Copyright 2002-4 Robert Ramey - http://www.rrsd.com .
5 // Use, modification and distribution is subject to the Boost Software
6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 #include <algorithm> // std::copy
17 #include <cstddef> // size_t
19 #include <boost/config.hpp>
21 #include <boost/archive/detail/utf8_codecvt_facet.hpp>
23 #if defined(BOOST_NO_STDC_NAMESPACE)
27 #if !defined(UNDER_CE) && !defined(__PGIC__)
33 // Note: copied from boost/iostreams/char_traits.hpp
35 // Dinkumware that comes with QNX Momentics 6.3.0, 4.0.2, incorrectly defines
36 // the EOF and WEOF macros to not std:: qualify the wint_t type (and so does
37 // Sun C++ 5.8 + STLport 4). Fix by placing the def in this scope.
38 // NOTE: Use BOOST_WORKAROUND?
39 #if (defined(__QNX__) && defined(BOOST_DINKUMWARE_STDLIB)) \
40 || defined(__SUNPRO_CC)
44 #include "test_tools.hpp"
46 template<std::size_t s
>
49 static unsigned char utf8_encoding
[];
50 static wchar_t wchar_encoding
[];
54 unsigned char test_data
<2>::utf8_encoding
[] = {
64 wchar_t test_data
<2>::wchar_encoding
[] = {
74 unsigned char test_data
<4>::utf8_encoding
[] = {
81 0xf0, 0x90, 0x80, 0x80,
82 0xf4, 0x8f, 0xbf, 0xbf,
83 /* codecvt implementations for clang and gcc don't handle more than 21 bits and
84 * return eof accordlingly. So don't test the whole 32 range
87 0xf7, 0xbf, 0xbf, 0xbf,
88 0xf8, 0x88, 0x80, 0x80, 0x80,
89 0xfb, 0xbf, 0xbf, 0xbf, 0xbf,
90 0xfc, 0x84, 0x80, 0x80, 0x80, 0x80,
91 0xfd, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf
96 wchar_t test_data
<4>::wchar_encoding
[] = {
105 /* codecvt implementations for clang and gcc don't handle more than 21 bits and
106 * return eof accordlingly. So don't test the whole 32 range
118 test_main(int /* argc */, char * /* argv */[]) {
119 std::locale utf8_locale
121 std::locale::classic(),
122 new boost::archive::detail::utf8_codecvt_facet
126 // define test data compatible with the wchar_t implementation
127 // as either ucs-2 or ucs-4 depending on the compiler/library.
128 typedef test_data
<sizeof(wchar_t)> td
;
130 // Send our test UTF-8 data to file
133 ofs
.open("test.dat");
136 td::utf8_encoding
+ sizeof(td::utf8_encoding
) / sizeof(unsigned char),
137 std::ostream_iterator
<utf8_t
>(ofs
)
141 // Read the test data back in, converting to UCS-4 on the way in
142 std::vector
<wchar_t> from_file
;
145 ifs
.imbue(utf8_locale
);
146 ifs
.open("test.dat");
148 std::wint_t item
= 0;
149 // note can't use normal vector from iterator constructor because
150 // dinkumware doesn't have it.
158 from_file
.push_back(item
);
162 BOOST_CHECK(std::equal(from_file
.begin(), from_file
.end(), td::wchar_encoding
));
164 // Send the UCS4_data back out, converting to UTF-8
167 ofs
.imbue(utf8_locale
);
168 ofs
.open("test2.dat");
172 std::ostream_iterator
<wchar_t, wchar_t>(ofs
)
176 // Make sure that both files are the same
178 typedef std::istream_iterator
<utf8_t
> is_iter
;
181 std::ifstream
ifs1("test.dat");
183 std::vector
<utf8_t
> data1
;
184 std::copy(it1
, end_iter
, std::back_inserter(data1
));
186 std::ifstream
ifs2("test2.dat");
188 std::vector
<utf8_t
> data2
;
189 std::copy(it2
, end_iter
, std::back_inserter(data2
));
191 BOOST_CHECK(data1
== data2
);
194 // some libraries have trouble that only shows up with longer strings
196 const wchar_t * test3_data
= L
"\
197 <?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\
198 <!DOCTYPE boost_serialization>\
199 <boost_serialization signature=\"serialization::archive\" version=\"3\">\
200 <a class_id=\"0\" tracking_level=\"0\">\
217 <x>1.0170664757130923</x>\
219 <z>cuwjentqpkejp</z>\
221 </boost_serialization>\
224 // Send the UCS4_data back out, converting to UTF-8
225 std::size_t l
= std::wcslen(test3_data
);
228 ofs
.imbue(utf8_locale
);
229 ofs
.open("test3.dat");
233 std::ostream_iterator
<wchar_t, wchar_t>(ofs
)
237 // Make sure that both files are the same
240 ifs
.imbue(utf8_locale
);
241 ifs
.open("test3.dat");
242 ifs
>> std::noskipws
;
247 std::istream_iterator
<wchar_t, wchar_t>(ifs
)