]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/support/utree.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / spirit / test / support / utree.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
4 Copyright (c) 2010 Bryce Lelbach
5
6 Distributed under the Boost Software License, Version 1.0. (See accompanying
7 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9
10 #include <boost/config/warning_disable.hpp>
11 #include <boost/detail/lightweight_test.hpp>
12
13 #include <boost/functional/hash.hpp>
14 #include <boost/spirit/include/phoenix.hpp>
15 #include <boost/spirit/include/support_utree.hpp>
16
17 #include <iostream>
18 #include <sstream>
19 #include <cstdlib>
20
21 inline bool check(boost::spirit::utree const& val, std::string expected)
22 {
23 std::stringstream s;
24 s << val;
25 if (s.str() == expected + " ")
26 return true;
27
28 std::cerr << "got result: " << s.str()
29 << ", expected: " << expected << std::endl;
30 return false;
31 }
32
33 struct one_two_three
34 {
35 boost::spirit::utree operator()(boost::spirit::utree) const
36 {
37 return boost::spirit::utree(123);
38 }
39 };
40
41 struct this_
42 {
43 boost::spirit::utree operator()(boost::spirit::utree) const
44 {
45 return boost::spirit::utree(static_cast<int>(boost::hash_value(this)));
46 }
47 };
48
49 int main()
50 {
51 using boost::spirit::utree;
52 using boost::spirit::get;
53 using boost::spirit::utf8_symbol_type;
54 using boost::spirit::binary_string_type;
55
56 {
57 // test the size
58 std::cout << "size of utree is: "
59 << sizeof(utree) << " bytes" << std::endl;
60 BOOST_TEST_EQ(sizeof(utree), sizeof(void*[4]));
61 }
62
63 {
64 using boost::spirit::nil;
65
66 utree val(nil);
67 BOOST_TEST(check(val, "<nil>"));
68 }
69
70 {
71 using boost::spirit::empty_list;
72
73 utree val(empty_list);
74 BOOST_TEST(check(val, "( )"));
75 }
76
77 {
78 utree val(true);
79 BOOST_TEST(check(val, "true"));
80 }
81
82 {
83 utree val(123);
84 BOOST_TEST(check(val, "123"));
85 }
86
87 {
88 // single element string
89 utree val('x');
90 BOOST_TEST(check(val, "\"x\""));
91
92 // empty string
93 utree val1("");
94 BOOST_TEST(check(val1, "\"\""));
95 }
96
97 {
98 utree val(123.456);
99 BOOST_TEST(check(val, "123.456"));
100 }
101
102 { // strings
103 utree val("Hello, World");
104 BOOST_TEST(check(val, "\"Hello, World\""));
105 utree val2;
106 val2 = val;
107 BOOST_TEST(check(val2, "\"Hello, World\""));
108 utree val3("Hello, World. Chuckie is back!!!");
109 val = val3;
110 BOOST_TEST(check(val, "\"Hello, World. Chuckie is back!!!\""));
111
112 utree val4("Apple");
113 utree val5("Apple");
114 BOOST_TEST_EQ(val4, val5);
115
116 utree val6("ApplePie");
117 BOOST_TEST(val4 < val6);
118 }
119
120 { // symbols
121 utree val(utf8_symbol_type("Hello, World"));
122 BOOST_TEST(check(val, "Hello, World"));
123 utree val2;
124 val2 = val;
125 BOOST_TEST(check(val2, "Hello, World"));
126 utree val3(utf8_symbol_type("Hello, World. Chuckie is back!!!"));
127 val = val3;
128 BOOST_TEST(check(val, "Hello, World. Chuckie is back!!!"));
129
130 utree val4(utf8_symbol_type("Apple"));
131 utree val5(utf8_symbol_type("Apple"));
132 BOOST_TEST_EQ(val4, val5);
133
134 utree val6(utf8_symbol_type("ApplePie"));
135 BOOST_TEST(val4 < val6);
136 }
137
138 { // binary_strings
139 utree val(binary_string_type("\xDE#\xAD"));
140 BOOST_TEST(check(val, "#de23ad#" /* FIXME?: "#\xDE#\xAD#" */));
141 utree val2;
142 val2 = val;
143 BOOST_TEST(check(val2, "#de23ad#" /* FIXME?: "#\xDE#\xAD#" */));
144 utree val3(binary_string_type("\xDE\xAD\xBE\xEF"));
145 val = val3;
146 BOOST_TEST(check(val, "#deadbeef#" /* FIXME?: "#\xDE\xAD\xBE\xEF#" */));
147
148 utree val4(binary_string_type("\x01"));
149 utree val5(binary_string_type("\x01"));
150 BOOST_TEST_EQ(val4, val5);
151
152 utree val6(binary_string_type("\x01\x02"));
153 BOOST_TEST(val4 < val6);
154 }
155
156 {
157 using boost::spirit::nil;
158
159 utree val;
160 val.push_back(123);
161 val.push_back("Chuckie");
162 BOOST_TEST_EQ(val.size(), 2U);
163 utree val2;
164 val2.push_back(123.456);
165 val2.push_back("Mah Doggie");
166 val.push_back(val2);
167 BOOST_TEST_EQ(val.size(), 3U);
168 BOOST_TEST(check(val, "( 123 \"Chuckie\" ( 123.456 \"Mah Doggie\" ) )"));
169 BOOST_TEST(check(val.front(), "123"));
170
171 utree val3(nil);
172 val3.swap(val);
173 BOOST_TEST_EQ(val3.size(), 3U);
174 BOOST_TEST(check(val, "<nil>"));
175 val3.swap(val);
176 BOOST_TEST(check(val, "( 123 \"Chuckie\" ( 123.456 \"Mah Doggie\" ) )"));
177 val.push_back("another string");
178 BOOST_TEST_EQ(val.size(), 4U);
179 BOOST_TEST(check(val, "( 123 \"Chuckie\" ( 123.456 \"Mah Doggie\" ) \"another string\" )"));
180 val.pop_front();
181 BOOST_TEST(check(val, "( \"Chuckie\" ( 123.456 \"Mah Doggie\" ) \"another string\" )"));
182 utree::iterator i = val.begin();
183 ++++i;
184 val.insert(i, "Right in the middle");
185 BOOST_TEST_EQ(val.size(), 4U);
186 BOOST_TEST(check(val, "( \"Chuckie\" ( 123.456 \"Mah Doggie\" ) \"Right in the middle\" \"another string\" )"));
187 val.pop_back();
188 BOOST_TEST(check(val, "( \"Chuckie\" ( 123.456 \"Mah Doggie\" ) \"Right in the middle\" )"));
189 BOOST_TEST_EQ(val.size(), 3U);
190 utree::iterator it = val.end(); --it;
191 val.erase(it);
192 BOOST_TEST(check(val, "( \"Chuckie\" ( 123.456 \"Mah Doggie\" ) )"));
193 BOOST_TEST_EQ(val.size(), 2U);
194
195 val.insert(val.begin(), val2.begin(), val2.end());
196 BOOST_TEST(check(val, "( 123.456 \"Mah Doggie\" \"Chuckie\" ( 123.456 \"Mah Doggie\" ) )"));
197 BOOST_TEST_EQ(val.size(), 4U);
198
199 // Regeression Ticket #6714
200 it = val.insert(val.end(), 111);
201 BOOST_TEST(it != val.begin());
202 BOOST_TEST(it == --val.end());
203 BOOST_TEST(*it == 111);
204
205 val.clear();
206 it = val.insert(val.begin(), 222);
207 BOOST_TEST(it == val.begin());
208 BOOST_TEST(it == --val.end());
209 BOOST_TEST(*it == 222);
210 // Regeression Ticket #6714
211 }
212
213 {
214 utree val;
215 val.insert(val.end(), 123);
216 val.insert(val.end(), "Mia");
217 val.insert(val.end(), "Chuckie");
218 val.insert(val.end(), "Poly");
219 val.insert(val.end(), "Mochi");
220 BOOST_TEST(check(val, "( 123 \"Mia\" \"Chuckie\" \"Poly\" \"Mochi\" )"));
221 }
222
223 {
224 using boost::spirit::nil;
225 using boost::spirit::invalid;
226
227 utree a(nil), b(nil);
228 BOOST_TEST_EQ(a, b);
229 a = 123;
230 BOOST_TEST(a != b);
231 b = 123;
232 BOOST_TEST_EQ(a, b);
233 a = 100.00;
234 BOOST_TEST(a < b);
235
236 b = a = utree(invalid);
237 BOOST_TEST_EQ(a, b);
238 a.push_back(1);
239 a.push_back("two");
240 a.push_back(3.0);
241 b.push_back(1);
242 b.push_back("two");
243 b.push_back(3.0);
244 BOOST_TEST_EQ(a, b);
245 b.push_back(4);
246 BOOST_TEST(a != b);
247 BOOST_TEST(a < b);
248 }
249
250 {
251 using boost::spirit::empty_list;
252
253 utree a(empty_list);
254 a.push_back(0);
255 a.push_back(0);
256 a.push_back(0);
257 a.push_back(0);
258 a.push_back(0);
259 a.push_back(0);
260 a.push_back(0);
261 a.push_back(0);
262 a.push_back(0);
263 a.push_back(0);
264 a.push_back(0);
265 a.push_back(0);
266
267 for (utree::size_type i = 0; i < a.size(); ++i)
268 get(a, i) = int(i + 1);
269
270 BOOST_TEST_EQ(get(a, 0), utree(1));
271 BOOST_TEST_EQ(get(a, 1), utree(2));
272 BOOST_TEST_EQ(get(a, 2), utree(3));
273 BOOST_TEST_EQ(get(a, 3), utree(4));
274 BOOST_TEST_EQ(get(a, 4), utree(5));
275 BOOST_TEST_EQ(get(a, 5), utree(6));
276 BOOST_TEST_EQ(get(a, 6), utree(7));
277 BOOST_TEST_EQ(get(a, 7), utree(8));
278 BOOST_TEST_EQ(get(a, 8), utree(9));
279 BOOST_TEST_EQ(get(a, 9), utree(10));
280 BOOST_TEST_EQ(get(a, 10), utree(11));
281 BOOST_TEST_EQ(get(a, 11), utree(12));
282 }
283
284 {
285 // test empty list
286 utree a;
287 a.push_back(1);
288 a.pop_front();
289 BOOST_TEST(check(a, "( )"));
290
291 // the other way around
292 utree b;
293 b.push_front(1);
294 b.pop_back();
295 BOOST_TEST(check(b, "( )"));
296 }
297
298 { // test references
299 utree val(123);
300 utree ref(boost::ref(val));
301 BOOST_TEST(check(ref, "123"));
302 BOOST_TEST_EQ(ref, utree(123));
303
304 val.clear();
305 val.push_back(1);
306 val.push_back(2);
307 val.push_back(3);
308 val.push_back(4);
309 BOOST_TEST(check(ref, "( 1 2 3 4 )"));
310 BOOST_TEST_EQ(get(ref, 0), utree(1));
311 BOOST_TEST_EQ(get(ref, 1), utree(2));
312 BOOST_TEST_EQ(get(ref, 2), utree(3));
313 BOOST_TEST_EQ(get(ref, 3), utree(4));
314 }
315
316 { // put it in an array
317
318 utree vals[] = {
319 utree(123),
320 utree("Hello, World"),
321 utree(123.456)
322 };
323
324 BOOST_TEST(check(vals[0], "123"));
325 BOOST_TEST(check(vals[1], "\"Hello, World\""));
326 BOOST_TEST(check(vals[2], "123.456"));
327 }
328
329 { // operators
330
331 BOOST_TEST((utree(false) && utree(false)) == utree(false));
332 BOOST_TEST((utree(false) && utree(true)) == utree(false));
333 BOOST_TEST((utree(true) && utree(false)) == utree(false));
334 BOOST_TEST((utree(true) && utree(true)) == utree(true));
335
336 BOOST_TEST((utree(0) && utree(0)) == utree(false));
337 BOOST_TEST((utree(0) && utree(1)) == utree(false));
338 BOOST_TEST((utree(1) && utree(0)) == utree(false));
339 BOOST_TEST((utree(1) && utree(1)) == utree(true));
340
341 BOOST_TEST((utree(false) || utree(false)) == utree(false));
342 BOOST_TEST((utree(false) || utree(true)) == utree(true));
343 BOOST_TEST((utree(true) || utree(false)) == utree(true));
344 BOOST_TEST((utree(true) || utree(true)) == utree(true));
345
346 BOOST_TEST((utree(0) || utree(0)) == utree(false));
347 BOOST_TEST((utree(0) || utree(1)) == utree(true));
348 BOOST_TEST((utree(1) || utree(0)) == utree(true));
349 BOOST_TEST((utree(1) || utree(1)) == utree(true));
350
351 BOOST_TEST((!utree(true)) == utree(false));
352 BOOST_TEST((!utree(false)) == utree(true));
353 BOOST_TEST((!utree(1)) == utree(false));
354 BOOST_TEST((!utree(0)) == utree(true));
355
356 BOOST_TEST((utree(456) + utree(123)) == utree(456 + 123));
357 BOOST_TEST((utree(456) + utree(123.456)) == utree(456 + 123.456));
358 BOOST_TEST((utree(456) - utree(123)) == utree(456 - 123));
359 BOOST_TEST((utree(456) - utree(123.456)) == utree(456 - 123.456));
360 BOOST_TEST((utree(456) * utree(123)) == utree(456 * 123));
361 BOOST_TEST((utree(456) * utree(123.456)) == utree(456 * 123.456));
362 BOOST_TEST((utree(456) / utree(123)) == utree(456 / 123));
363 BOOST_TEST((utree(456) / utree(123.456)) == utree(456 / 123.456));
364 BOOST_TEST((utree(456) % utree(123)) == utree(456 % 123));
365 BOOST_TEST(-utree(456) == utree(-456));
366
367 BOOST_TEST((utree(456) & utree(123)) == utree(456 & 123));
368 BOOST_TEST((utree(456) | utree(123)) == utree(456 | 123));
369 BOOST_TEST((utree(456) ^ utree(123)) == utree(456 ^ 123));
370 BOOST_TEST((utree(456) << utree(3)) == utree(456 << 3));
371 BOOST_TEST((utree(456) >> utree(2)) == utree(456 >> 2));
372 BOOST_TEST(~utree(456) == utree(~456));
373 }
374
375 { // test reference iterator
376 utree val;
377 val.push_back(1);
378 val.push_back(2);
379 val.push_back(3);
380 val.push_back(4);
381 BOOST_TEST(check(val, "( 1 2 3 4 )"));
382
383 utree::ref_iterator b = val.ref_begin();
384 utree::ref_iterator e = val.ref_end();
385
386 utree ref(boost::make_iterator_range(b, e));
387 BOOST_TEST_EQ(get(ref, 0), utree(1));
388 BOOST_TEST_EQ(get(ref, 1), utree(2));
389 BOOST_TEST_EQ(get(ref, 2), utree(3));
390 BOOST_TEST_EQ(get(ref, 3), utree(4));
391 BOOST_TEST(check(ref, "( 1 2 3 4 )"));
392 }
393
394 {
395 // check the tag
396 // TODO: test tags on all utree types
397 utree x;
398 x.tag(123);
399 BOOST_TEST_EQ(x.tag(), 123);
400
401 x = "hello world! my name is bob the builder";
402 x.tag(123);
403 BOOST_TEST_EQ(x.tag(), 123);
404
405 x.tag(456);
406 BOOST_TEST_EQ(x.tag(), 456);
407 BOOST_TEST_EQ(x.size(), 39U);
408 BOOST_TEST(check(x, "\"hello world! my name is bob the builder\""));
409
410 x = "hello";
411 x.tag(456);
412 BOOST_TEST_EQ(x.tag(), 456);
413
414 x.tag(789);
415 BOOST_TEST_EQ(x.tag(), 789);
416 BOOST_TEST_EQ(x.size(), 5U);
417 BOOST_TEST(check(x, "\"hello\""));
418 }
419
420 {
421 // test functions
422 using boost::spirit::stored_function;
423
424 utree f = stored_function<one_two_three>();
425 f.eval(utree());
426 }
427
428 {
429 // test referenced functions
430 using boost::spirit::referenced_function;
431
432 one_two_three f;
433 utree ff = referenced_function<one_two_three>(f);
434 BOOST_TEST_EQ(ff.eval(utree()), f(utree()));
435 }
436
437 {
438 // shallow ranges
439 using boost::spirit::shallow;
440
441 utree val;
442 val.push_back(1);
443 val.push_back(2);
444 val.push_back(3);
445 val.push_back(4);
446
447 utree::iterator i = val.begin(); ++i;
448 utree alias(utree::range(i, val.end()), shallow);
449
450 BOOST_TEST(check(alias, "( 2 3 4 )"));
451 BOOST_TEST_EQ(alias.size(), 3U);
452 BOOST_TEST_EQ(alias.front(), 2);
453 BOOST_TEST_EQ(alias.back(), 4);
454 BOOST_TEST(!alias.empty());
455 BOOST_TEST_EQ(get(alias, 1), 3);
456 }
457
458 {
459 // shallow string ranges
460 using boost::spirit::utf8_string_range_type;
461 using boost::spirit::shallow;
462
463 char const* s = "Hello, World";
464 utree val(utf8_string_range_type(s, s + strlen(s)), shallow);
465 BOOST_TEST(check(val, "\"Hello, World\""));
466
467 utf8_string_range_type r = val.get<utf8_string_range_type>();
468 utf8_string_range_type pf(r.begin()+1, r.end()-1);
469 val = utree(pf, shallow);
470 BOOST_TEST(check(val, "\"ello, Worl\""));
471 }
472
473 {
474 // any pointer
475 using boost::spirit::any_ptr;
476
477 int n = 123;
478 utree up = any_ptr(&n);
479 BOOST_TEST(*up.get<int*>() == 123);
480 }
481
482 // tags
483 {
484 short min = (std::numeric_limits<short>::min)();
485 short max = (std::numeric_limits<short>::max)();
486
487 utree::list_type u;
488 utree u2;
489 bool ok = true;
490
491 for (int t = min ; ok && t <= max ; ++t) {
492 u.tag(t);
493 u2 = u;
494 BOOST_TEST_EQ(t, u.tag());
495 BOOST_TEST_EQ(t, u2.tag());
496 ok = t == u.tag() && t == u2.tag();
497 u2 = utree("12");
498 }
499 }
500
501 return boost::report_errors();
502 }