1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_iterators.cpp
3
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)
8
9 #include <algorithm>
10 #include <vector>
11 #include <cstdlib> // for rand
12 #include <functional>
13 #include <sstream> // used to test stream iterators
14
15 #include <boost/config.hpp>
16 #ifdef BOOST_NO_STDC_NAMESPACE
17 namespace std{
18 using ::rand;
19 }
20 #endif
21
22 #include <boost/detail/workaround.hpp>
23 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
24 #include <boost/archive/dinkumware.hpp>
25 #endif
26
27 #ifndef BOOST_NO_CWCHAR
28 #include <boost/archive/iterators/mb_from_wchar.hpp>
29 #include <boost/archive/iterators/wchar_from_mb.hpp>
30 #endif
31 #include <boost/archive/iterators/xml_escape.hpp>
32 #include <boost/archive/iterators/xml_unescape.hpp>
33 #include <boost/archive/iterators/transform_width.hpp>
34 #include <boost/archive/iterators/istream_iterator.hpp>
35 #include <boost/archive/iterators/ostream_iterator.hpp>
36
37 #include "../test/test_tools.hpp"
38
39 #ifndef BOOST_NO_CWCHAR
40
test_wchar_from_mb(const wchar_t * la,const char * a,const unsigned int size)41 void test_wchar_from_mb(const wchar_t *la, const char * a, const unsigned int size){
42 typedef boost::archive::iterators::wchar_from_mb<const char *> translator;
43 BOOST_CHECK((
44 std::equal(
45 translator(BOOST_MAKE_PFTO_WRAPPER(a)),
46 translator(BOOST_MAKE_PFTO_WRAPPER(a + size)),
47 la
48 )
49 ));
50 }
51
test_mb_from_wchar(const char * a,const wchar_t * la,const unsigned int size)52 void test_mb_from_wchar(const char * a, const wchar_t *la, const unsigned int size){
53 typedef boost::archive::iterators::mb_from_wchar<const wchar_t *> translator;
54 BOOST_CHECK(
55 std::equal(
56 translator(BOOST_MAKE_PFTO_WRAPPER(la)),
57 translator(BOOST_MAKE_PFTO_WRAPPER(la + size)),
58 a
59 )
60 );
61 }
62
63 #endif
64
65 template<class CharType>
test_xml_escape(const CharType * xml_escaped,const CharType * xml,unsigned int size)66 void test_xml_escape(
67 const CharType * xml_escaped,
68 const CharType * xml,
69 unsigned int size
70 ){
71 typedef boost::archive::iterators::xml_escape<const CharType *> translator;
72
73 BOOST_CHECK(
74 std::equal(
75 translator(BOOST_MAKE_PFTO_WRAPPER(xml)),
76 translator(BOOST_MAKE_PFTO_WRAPPER(xml + size)),
77 xml_escaped
78 )
79 );
80 }
81
82 template<class CharType>
test_xml_unescape(const CharType * xml,const CharType * xml_escaped,unsigned int size)83 void test_xml_unescape(
84 const CharType * xml,
85 const CharType * xml_escaped,
86 unsigned int size
87 ){
88
89 // test xml_unescape
90 typedef boost::archive::iterators::xml_unescape<const CharType *> translator;
91
92 BOOST_CHECK(
93 std::equal(
94 translator(BOOST_MAKE_PFTO_WRAPPER(xml_escaped)),
95 translator(BOOST_MAKE_PFTO_WRAPPER(xml_escaped + size)),
96 xml
97 )
98 );
99 }
100
101 template<int BitsOut, int BitsIn>
test_transform_width(unsigned int size)102 void test_transform_width(unsigned int size){
103 // test transform_width
104 char rawdata[8];
105
106 char * rptr;
107 for(rptr = rawdata + 6; rptr-- > rawdata;)
108 *rptr = std::rand();
109
110 // convert 8 to 6 bit characters
111 typedef boost::archive::iterators::transform_width<
112 char *, BitsOut, BitsIn
113 > translator1;
114
115 std::vector<char> v6;
116
117 std::copy(
118 translator1(BOOST_MAKE_PFTO_WRAPPER(static_cast<char *>(rawdata))),
119 translator1(BOOST_MAKE_PFTO_WRAPPER(rawdata + size)),
120 std::back_inserter(v6)
121 );
122
123 // check to see we got the expected # of characters out
124 if(0 == size)
125 BOOST_CHECK(v6.size() == 0);
126 else
127 BOOST_CHECK(v6.size() == (size * BitsIn - 1 ) / BitsOut + 1);
128
129 typedef boost::archive::iterators::transform_width<
130 std::vector<char>::iterator, BitsIn, BitsOut
131 > translator2;
132
133 BOOST_CHECK(
134 std::equal(
135 rawdata,
136 rawdata + size,
137 translator2(BOOST_MAKE_PFTO_WRAPPER(v6.begin()))
138 )
139 );
140
141 }
142
143 template<class CharType>
test_stream_iterators(const CharType * test_data,unsigned int size)144 void test_stream_iterators(
145 const CharType * test_data,
146 unsigned int size
147 ){
148 std::basic_stringstream<CharType> ss;
149 boost::archive::iterators::ostream_iterator<CharType> osi =
150 boost::archive::iterators::ostream_iterator<CharType>(ss);
151 std::copy(test_data, test_data + size, osi);
152
153 BOOST_CHECK(size == ss.str().size());
154
155 boost::archive::iterators::istream_iterator<CharType> isi =
156 boost::archive::iterators::istream_iterator<CharType>(ss);
157 BOOST_CHECK(std::equal(test_data, test_data + size,isi));
158 }
159
160 int
test_main(int,char * [])161 test_main(int /* argc */, char* /* argv */ [] )
162 {
163 const char xml[] = "<+>+&+\"+'";
164 const char xml_escaped[] = "<+>+&+"+'";
165 test_xml_escape<const char>(
166 xml_escaped,
167 xml,
168 sizeof(xml) / sizeof(char) - 1
169 );
170 test_xml_unescape<const char>(
171 xml,
172 xml_escaped,
173 sizeof(xml_escaped) / sizeof(char) - 1
174 );
175
176 const char a[] = "abcdefghijklmnopqrstuvwxyz";
177
178 #ifndef BOOST_NO_CWCHAR
179 const wchar_t wxml[] = L"<+>+&+\"+'";
180 const wchar_t wxml_escaped[] = L"<+>+&+"+'";
181 test_xml_escape<const wchar_t>(
182 wxml_escaped,
183 wxml,
184 sizeof(wxml) / sizeof(wchar_t) - 1
185 );
186 test_xml_unescape<const wchar_t>(
187 wxml,
188 wxml_escaped,
189 sizeof(wxml_escaped) / sizeof(wchar_t) - 1
190 );
191
192 const wchar_t la[] = L"abcdefghijklmnopqrstuvwxyz";
193
194 test_wchar_from_mb(la, a, sizeof(a) / sizeof(char) - 1);
195 test_mb_from_wchar(a, la, sizeof(la) / sizeof(wchar_t) - 1);
196
197 test_stream_iterators<wchar_t>(la, sizeof(la)/sizeof(wchar_t) - 1);
198 #endif
199
200 test_stream_iterators<char>(a, sizeof(a) - 1);
201
202 test_transform_width<6, 8>(0);
203 test_transform_width<6, 8>(1);
204 test_transform_width<6, 8>(2);
205 test_transform_width<6, 8>(3);
206 test_transform_width<6, 8>(4);
207 test_transform_width<6, 8>(5);
208 test_transform_width<6, 8>(6);
209 test_transform_width<6, 8>(7);
210 test_transform_width<6, 8>(8);
211
212 return EXIT_SUCCESS;
213 }
214