]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/repository/test/karma/test.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / spirit / repository / test / karma / test.hpp
1 // Copyright (c) 2001-2010 Hartmut Kaiser
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #if !defined(BOOST_SPIRIT_KARMA_TEST_FEB_23_2007_1221PM)
7 #define BOOST_SPIRIT_KARMA_TEST_FEB_23_2007_1221PM
8
9 #include <cstring>
10 #include <string>
11 #include <iterator>
12 #include <iostream>
13 #include <typeinfo>
14
15 #include <boost/spirit/include/karma_generate.hpp>
16 #include <boost/spirit/include/karma_what.hpp>
17
18 #include <boost/core/lightweight_test.hpp>
19
20 namespace spirit_test
21 {
22 ///////////////////////////////////////////////////////////////////////////
23 struct display_type
24 {
25 template<typename T>
26 void operator()(T const &) const
27 {
28 std::cout << typeid(T).name() << std::endl;
29 }
30
31 template<typename T>
32 static void print()
33 {
34 std::cout << typeid(T).name() << std::endl;
35 }
36 };
37
38 display_type const display = {};
39
40 ///////////////////////////////////////////////////////////////////////////
41 template <typename Char>
42 struct output_iterator
43 {
44 typedef std::basic_string<Char> string_type;
45 typedef std::back_insert_iterator<string_type> type;
46 };
47
48 ///////////////////////////////////////////////////////////////////////////
49 template <typename Char, typename T>
50 void print_if_failed(char const* func, bool result
51 , std::basic_string<Char> const& generated, T const& expected)
52 {
53 if (!result)
54 std::cerr << "in " << func << ": result is false" << std::endl;
55 else if (generated != expected)
56 std::cerr << "in " << func << ": generated \""
57 << std::string(generated.begin(), generated.end())
58 << "\"" << std::endl;
59 }
60
61 ///////////////////////////////////////////////////////////////////////////
62 template <typename Char, typename Generator>
63 inline bool test(Char const *expected, Generator const& g)
64 {
65 namespace karma = boost::spirit::karma;
66 typedef std::basic_string<Char> string_type;
67
68 // we don't care about the result of the "what" function.
69 // we only care that all generators have it:
70 karma::what(g);
71
72 string_type generated;
73 std::back_insert_iterator<string_type> outit(generated);
74 bool result = karma::generate(outit, g);
75
76 print_if_failed("test", result, generated, expected);
77 return result && generated == expected;
78 }
79
80 template <typename Char, typename Generator>
81 inline bool test(std::basic_string<Char> const& expected, Generator const& g)
82 {
83 namespace karma = boost::spirit::karma;
84 typedef std::basic_string<Char> string_type;
85
86 // we don't care about the result of the "what" function.
87 // we only care that all generators have it:
88 karma::what(g);
89
90 string_type generated;
91 std::back_insert_iterator<string_type> outit(generated);
92 bool result = karma::generate(outit, g);
93
94 print_if_failed("test", result, generated, expected);
95 return result && generated == expected;
96 }
97
98 ///////////////////////////////////////////////////////////////////////////
99 template <typename Char, typename Generator, typename Attribute>
100 inline bool test(Char const *expected, Generator const& g,
101 Attribute const &attr)
102 {
103 namespace karma = boost::spirit::karma;
104 typedef std::basic_string<Char> string_type;
105
106 // we don't care about the result of the "what" function.
107 // we only care that all generators have it:
108 karma::what(g);
109
110 string_type generated;
111 std::back_insert_iterator<string_type> outit(generated);
112 bool result = karma::generate(outit, g, attr);
113
114 print_if_failed("test", result, generated, expected);
115 return result && generated == expected;
116 }
117
118 template <typename Char, typename Generator, typename Attribute>
119 inline bool test(std::basic_string<Char> const& expected, Generator const& g,
120 Attribute const &attr)
121 {
122 namespace karma = boost::spirit::karma;
123 typedef std::basic_string<Char> string_type;
124
125 // we don't care about the result of the "what" function.
126 // we only care that all generators have it:
127 karma::what(g);
128
129 string_type generated;
130 std::back_insert_iterator<string_type> outit(generated);
131 bool result = karma::generate(outit, g, attr);
132
133 print_if_failed("test", result, generated, expected);
134 return result && generated == expected;
135 }
136
137 ///////////////////////////////////////////////////////////////////////////
138 template <typename Char, typename Generator, typename Delimiter>
139 inline bool test_delimited(Char const *expected, Generator const& g,
140 Delimiter const& d)
141 {
142 namespace karma = boost::spirit::karma;
143 typedef std::basic_string<Char> string_type;
144
145 // we don't care about the result of the "what" function.
146 // we only care that all generators have it:
147 karma::what(g);
148
149 string_type generated;
150 std::back_insert_iterator<string_type> outit(generated);
151 bool result = karma::generate_delimited(outit, g, d);
152
153 print_if_failed("test_delimited", result, generated, expected);
154 return result && generated == expected;
155 }
156
157 template <typename Char, typename Generator, typename Delimiter>
158 inline bool test_delimited(std::basic_string<Char> const& expected,
159 Generator const& g, Delimiter const& d)
160 {
161 namespace karma = boost::spirit::karma;
162 typedef std::basic_string<Char> string_type;
163
164 // we don't care about the result of the "what" function.
165 // we only care that all generators have it:
166 karma::what(g);
167
168 string_type generated;
169 std::back_insert_iterator<string_type> outit(generated);
170 bool result = karma::generate_delimited(outit, g, d);
171
172 print_if_failed("test_delimited", result, generated, expected);
173 return result && generated == expected;
174 }
175
176 ///////////////////////////////////////////////////////////////////////////
177 template <typename Char, typename Generator, typename Attribute,
178 typename Delimiter>
179 inline bool test_delimited(Char const *expected, Generator const& g,
180 Attribute const &attr, Delimiter const& d)
181 {
182 namespace karma = boost::spirit::karma;
183 typedef std::basic_string<Char> string_type;
184
185 // we don't care about the result of the "what" function.
186 // we only care that all generators have it:
187 karma::what(g);
188
189 string_type generated;
190 std::back_insert_iterator<string_type> outit(generated);
191 bool result = karma::generate_delimited(outit, g, d, attr);
192
193 print_if_failed("test_delimited", result, generated, expected);
194 return result && generated == expected;
195 }
196
197 template <typename Char, typename Generator, typename Attribute,
198 typename Delimiter>
199 inline bool test_delimited(std::basic_string<Char> const& expected,
200 Generator const& g, Attribute const &attr, Delimiter const& d)
201 {
202 namespace karma = boost::spirit::karma;
203 typedef std::basic_string<Char> string_type;
204
205 // we don't care about the result of the "what" function.
206 // we only care that all generators have it:
207 karma::what(g);
208
209 string_type generated;
210 std::back_insert_iterator<string_type> outit(generated);
211 bool result = karma::generate_delimited(outit, g, d, attr);
212
213 print_if_failed("test_delimited", result, generated, expected);
214 return result && generated == expected;
215 }
216
217 ///////////////////////////////////////////////////////////////////////////
218 template <typename Generator>
219 inline bool
220 binary_test(char const *expected, std::size_t size,
221 Generator const& g)
222 {
223 namespace karma = boost::spirit::karma;
224 typedef std::basic_string<char> string_type;
225
226 // we don't care about the result of the "what" function.
227 // we only care that all generators have it:
228 karma::what(g);
229
230 string_type generated;
231 std::back_insert_iterator<string_type> outit(generated);
232 bool result = karma::generate(outit, g);
233
234 return result && !std::memcmp(generated.c_str(), expected, size);
235 }
236
237 ///////////////////////////////////////////////////////////////////////////
238 template <typename Generator, typename Attribute>
239 inline bool
240 binary_test(char const *expected, std::size_t size,
241 Generator const& g, Attribute const &attr)
242 {
243 namespace karma = boost::spirit::karma;
244 typedef std::basic_string<char> string_type;
245
246 // we don't care about the result of the "what" function.
247 // we only care that all generators have it:
248 karma::what(g);
249
250 string_type generated;
251 std::back_insert_iterator<string_type> outit(generated);
252 bool result = karma::generate(outit, g, attr);
253
254 return result && !std::memcmp(generated.c_str(), expected, size);
255 }
256
257 ///////////////////////////////////////////////////////////////////////////
258 template <typename Generator, typename Delimiter>
259 inline bool
260 binary_test_delimited(char const *expected, std::size_t size,
261 Generator const& g, Delimiter const& d)
262 {
263 namespace karma = boost::spirit::karma;
264 typedef std::basic_string<char> string_type;
265
266 // we don't care about the result of the "what" function.
267 // we only care that all generators have it:
268 karma::what(g);
269
270 string_type generated;
271 std::back_insert_iterator<string_type> outit(generated);
272 bool result = karma::generate_delimited(outit, g, d);
273
274 return result && !std::memcmp(generated.c_str(), expected, size);
275 }
276
277 ///////////////////////////////////////////////////////////////////////////
278 template <typename Generator, typename Attribute, typename Delimiter>
279 inline bool
280 binary_test_delimited(char const *expected, std::size_t size,
281 Generator const& g, Attribute const &attr, Delimiter const& d)
282 {
283 namespace karma = boost::spirit::karma;
284 typedef std::basic_string<char> string_type;
285
286 // we don't care about the result of the "what" function.
287 // we only care that all generators have it:
288 karma::what(g);
289
290 string_type generated;
291 std::back_insert_iterator<string_type> outit(generated);
292 bool result = karma::generate_delimited(outit, g, d, attr);
293
294 return result && !std::memcmp(generated.c_str(), expected, size);
295 }
296
297 } // namespace spirit_test
298
299 #endif // !BOOST_SPIRIT_KARMA_TEST_FEB_23_2007_1221PM