]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/serialization/performance/performance_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)
17 #include <cstddef> // size_t
18 #include <boost/config.hpp>
19 #if defined(BOOST_NO_STDC_NAMESPACE)
26 #ifdef BOOST_NO_STDC_NAMESPACE
27 namespace std
{ using ::wcslen
; }
30 #include "../test/test_tools.hpp"
31 #include <boost/archive/iterators/istream_iterator.hpp>
32 #include <boost/archive/iterators/ostream_iterator.hpp>
34 #include <boost/archive/add_facet.hpp>
35 #include <boost/archive/detail/utf8_codecvt_facet.hpp>
37 template<std::size_t s
>
40 static unsigned char utf8_encoding
[];
41 static wchar_t wchar_encoding
[];
45 unsigned char test_data
<2>::utf8_encoding
[] = {
55 wchar_t test_data
<2>::wchar_encoding
[] = {
65 unsigned char test_data
<4>::utf8_encoding
[] = {
72 0xf0, 0x90, 0x80, 0x80,
73 0xf4, 0x8f, 0xbf, 0xbf,
74 0xf7, 0xbf, 0xbf, 0xbf,
75 0xf8, 0x88, 0x80, 0x80, 0x80,
76 0xfb, 0xbf, 0xbf, 0xbf, 0xbf,
77 0xfc, 0x84, 0x80, 0x80, 0x80, 0x80,
78 0xfd, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf
82 wchar_t test_data
<4>::wchar_encoding
[] = {
99 test_main(int /* argc */, char * /* argv */[]) {
101 std::locale
* utf8_locale
102 = boost::archive::add_facet(
104 new boost::archive::detail::utf8_codecvt_facet
108 typedef test_data
<sizeof(wchar_t)> td
;
110 // Send our test UTF-8 data to file
113 ofs
.open("test.dat", std::ios::binary
);
116 #if ! defined(BOOST_BORLANDC)
117 // borland 5.60 complains about this
118 td::utf8_encoding
+ sizeof(td::utf8_encoding
) / sizeof(unsigned char),
120 // so use this instead
121 td::utf8_encoding
+ 12,
123 boost::archive::iterators::ostream_iterator
<utf8_t
>(ofs
)
127 // Read the test data back in, converting to UCS-4 on the way in
128 std::vector
<wchar_t> from_file
;
131 ifs
.imbue(*utf8_locale
);
132 ifs
.open("test.dat");
135 // note can't use normal vector from iterator constructor because
136 // dinkumware doesn't have it.
144 from_file
.push_back(item
);
148 // compare the data read back in with the orginal
149 #if ! defined(BOOST_BORLANDC)
150 // borland 5.60 complains about this
151 BOOST_CHECK(from_file
.size() == sizeof(td::wchar_encoding
)/sizeof(wchar_t));
153 // so use this instead
154 BOOST_CHECK(from_file
.size() == 6);
157 BOOST_CHECK(std::equal(from_file
.begin(), from_file
.end(), td::wchar_encoding
));
159 // Send the UCS4_data back out, converting to UTF-8
162 ofs
.imbue(*utf8_locale
);
163 ofs
.open("test2.dat");
167 boost::archive::iterators::ostream_iterator
<wchar_t>(ofs
)
171 // Make sure that both files are the same
173 typedef boost::archive::iterators::istream_iterator
<utf8_t
> is_iter
;
176 std::ifstream
ifs1("test.dat");
178 std::vector
<utf8_t
> data1
;
179 std::copy(it1
, end_iter
, std::back_inserter(data1
));
181 std::ifstream
ifs2("test2.dat");
183 std::vector
<utf8_t
> data2
;
184 std::copy(it2
, end_iter
, std::back_inserter(data2
));
186 BOOST_CHECK(data1
== data2
);
189 // some libraries have trouble that only shows up with longer strings
191 wchar_t * test3_data
= L
"\
192 <?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\
193 <!DOCTYPE boost_serialization>\
194 <boost_serialization signature=\"serialization::archive\" version=\"3\">\
195 <a class_id=\"0\" tracking_level=\"0\">\
212 <x>1.0170664757130923</x>\
214 <z>cuwjentqpkejp</z>\
216 </boost_serialization>\
219 // Send the UCS4_data back out, converting to UTF-8
220 std::size_t l
= std::wcslen(test3_data
);
223 ofs
.imbue(*utf8_locale
);
224 ofs
.open("test3.dat");
228 boost::archive::iterators::ostream_iterator
<wchar_t>(ofs
)
232 // Make sure that both files are the same
235 ifs
.imbue(*utf8_locale
);
236 ifs
.open("test3.dat");
241 boost::archive::iterators::istream_iterator
<wchar_t>(ifs
)