]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/serialization/test/test_iterators.cpp
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
4 // (C) Copyright 2002 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, std::equal
11 #include <cstdlib> // for rand
13 #include <sstream> // used to test stream iterators
15 #include <iterator> // begin
16 #include <locale> // setlocale
18 #include <boost/config.hpp>
19 #ifdef BOOST_NO_STDC_NAMESPACE
25 #include <boost/detail/workaround.hpp>
26 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
27 #include <boost/archive/dinkumware.hpp>
30 #ifndef BOOST_NO_CWCHAR
31 #include <boost/archive/iterators/mb_from_wchar.hpp>
32 #include <boost/archive/iterators/wchar_from_mb.hpp>
34 #include <boost/archive/iterators/xml_escape.hpp>
35 #include <boost/archive/iterators/xml_unescape.hpp>
36 #include <boost/archive/iterators/transform_width.hpp>
37 #include <boost/archive/iterators/istream_iterator.hpp>
38 #include <boost/archive/iterators/ostream_iterator.hpp>
40 #include "test_tools.hpp"
42 #ifndef BOOST_NO_CWCHAR
44 void test_wchar_from_mb(const wchar_t *la
, const char * a
, const unsigned int size
){
45 typedef boost::archive::iterators::wchar_from_mb
<const char *> translator
;
55 void test_mb_from_wchar(const char * a
, const wchar_t *la
, const unsigned int size
){
56 typedef boost::archive::iterators::mb_from_wchar
<const wchar_t *> translator
;
60 translator(la
+ size
),
66 void test_roundtrip(const wchar_t * la
){
67 std::size_t s
= std::wcslen(la
);
70 typedef boost::archive::iterators::mb_from_wchar
<const wchar_t *> translator
;
76 // note: wchar_from_mb NEEDS a termination null in order to function!
77 a
.push_back(static_cast<char>(0));
79 BOOST_CHECK(a
.size() > 0);
80 std::vector
<wchar_t> la2
;
82 typedef boost::archive::iterators::wchar_from_mb
<std::vector
<char>::const_iterator
> translator
;
84 translator(a
.begin()),
86 std::back_inserter(la2
)
89 BOOST_CHECK(la2
.size() == s
);
90 BOOST_CHECK(std::equal(la
, la
+ s
, la2
.begin()));
94 template<class CharType
>
96 const CharType
* xml_escaped
,
100 typedef boost::archive::iterators::xml_escape
<const CharType
*> translator
;
105 translator(xml
+ size
),
111 template<class CharType
>
112 void test_xml_unescape(
113 const CharType
* xml
,
114 const CharType
* xml_escaped
,
119 typedef boost::archive::iterators::xml_unescape
<const CharType
*> translator
;
123 translator(xml_escaped
),
124 translator(xml_escaped
+ size
),
130 template<int BitsOut
, int BitsIn
>
131 void test_transform_width(unsigned int size
){
132 // test transform_width
136 for(rptr
= rawdata
+ size
; rptr
-- > rawdata
;)
137 *rptr
= static_cast<char>(0xff & std::rand());
139 // convert 8 to 6 bit characters
140 typedef boost::archive::iterators::transform_width
<
141 char *, BitsOut
, BitsIn
144 std::vector
<char> vout
;
147 translator1(static_cast<char *>(rawdata
)),
148 translator1(rawdata
+ size
),
149 std::back_inserter(vout
)
152 // check to see we got the expected # of characters out
154 BOOST_CHECK(vout
.size() == 0);
156 BOOST_CHECK(vout
.size() == (size
* BitsIn
- 1 ) / BitsOut
+ 1);
158 typedef boost::archive::iterators::transform_width
<
159 std::vector
<char>::iterator
, BitsIn
, BitsOut
162 std::vector
<char> vin
;
164 translator2(vout
.begin()),
165 translator2(vout
.end()),
166 std::back_inserter(vin
)
169 // check to see we got the expected # of characters out
170 BOOST_CHECK(vin
.size() == size
);
181 template<class CharType
>
182 void test_stream_iterators(
183 const CharType
* test_data
,
186 std::basic_stringstream
<CharType
> ss
;
187 boost::archive::iterators::ostream_iterator
<CharType
> osi
=
188 boost::archive::iterators::ostream_iterator
<CharType
>(ss
);
189 std::copy(test_data
, test_data
+ size
, osi
);
191 BOOST_CHECK(size
== ss
.str().size());
193 boost::archive::iterators::istream_iterator
<CharType
> isi
=
194 boost::archive::iterators::istream_iterator
<CharType
>(ss
);
195 BOOST_CHECK(std::equal(test_data
, test_data
+ size
,isi
));
199 test_main(int /* argc */, char* /* argv */ [] )
201 const char xml
[] = "<+>+&+\"+'";
202 const char xml_escaped
[] = "<+>+&+"+'";
203 test_xml_escape
<const char>(
206 sizeof(xml
) / sizeof(char) - 1
208 test_xml_unescape
<const char>(
211 sizeof(xml_escaped
) / sizeof(char) - 1
215 #ifndef BOOST_NO_CWCHAR
216 const wchar_t wxml
[] = L
"<+>+&+\"+'";
217 const wchar_t wxml_escaped
[] = L
"<+>+&+"+'";
218 test_xml_escape
<const wchar_t>(
221 sizeof(wxml
) / sizeof(wchar_t) - 1
223 test_xml_unescape
<const wchar_t>(
226 sizeof(wxml_escaped
) / sizeof(wchar_t) - 1
229 const char b
[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
230 const wchar_t lb
[] = L
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
231 test_mb_from_wchar(b
, lb
, sizeof(lb
) / sizeof(wchar_t) - 1);
232 test_wchar_from_mb(lb
, b
, sizeof(b
) / sizeof(char) - 1);
234 const char a
[] = "abcdefghijklmnopqrstuvwxyz";
235 const wchar_t la
[] = L
"abcdefghijklmnopqrstuvwxyz";
236 test_mb_from_wchar(a
, la
, sizeof(la
) / sizeof(wchar_t) - 1);
237 test_wchar_from_mb(la
, a
, sizeof(a
) / sizeof(char) - 1);
239 test_roundtrip(L
"z\u00df\u6c34\U0001f34c");
241 test_stream_iterators
<wchar_t>(la
, sizeof(la
)/sizeof(wchar_t) - 1);
244 test_stream_iterators
<char>(a
, sizeof(a
) - 1);
246 test_transform_width
<6, 8>(0);
247 test_transform_width
<6, 8>(1);
248 test_transform_width
<6, 8>(2);
249 test_transform_width
<6, 8>(3);
250 test_transform_width
<6, 8>(4);
251 test_transform_width
<6, 8>(5);
252 test_transform_width
<6, 8>(6);
253 test_transform_width
<6, 8>(7);
254 test_transform_width
<6, 8>(8);