]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/x3/optional.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / test / x3 / optional.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2015 Joel de Guzman
3
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #include <boost/detail/lightweight_test.hpp>
8 #include <boost/spirit/home/x3.hpp>
9 #include <boost/fusion/adapted/struct.hpp>
10 #include <boost/fusion/include/vector.hpp>
11
12 #include <iostream>
13 #include "test.hpp"
14
15 struct adata
16 {
17 int a;
18 boost::optional<int> b;
19 };
20
21 BOOST_FUSION_ADAPT_STRUCT(adata,
22 a, b
23 )
24
25 struct test_attribute_type
26 {
27 template <typename Context>
28 void operator()(Context& ctx) const
29 {
30 BOOST_TEST(typeid(decltype(_attr(ctx))).name() == typeid(boost::optional<int>).name());
31 }
32 };
33
34 int
35 main()
36 {
37 using spirit_test::test;
38 using spirit_test::test_attr;
39
40 using boost::spirit::x3::int_;
41 using boost::spirit::x3::omit;
42 using boost::spirit::x3::ascii::char_;
43
44 {
45 BOOST_TEST((test("1234", -int_)));
46 BOOST_TEST((test("abcd", -int_, false)));
47 }
48
49 { // test propagation of unused
50 using boost::fusion::at_c;
51 using boost::fusion::vector;
52
53 vector<char, char> v;
54 BOOST_TEST((test_attr("a1234c", char_ >> -omit[int_] >> char_, v)));
55 BOOST_TEST((at_c<0>(v) == 'a'));
56 BOOST_TEST((at_c<1>(v) == 'c'));
57
58 v = boost::fusion::vector<char, char>();
59 BOOST_TEST((test_attr("a1234c", char_ >> omit[-int_] >> char_, v)));
60 BOOST_TEST((at_c<0>(v) == 'a'));
61 BOOST_TEST((at_c<1>(v) == 'c'));
62
63 char ch;
64 BOOST_TEST((test_attr(",c", -(',' >> char_), ch)));
65 BOOST_TEST((ch == 'c'));
66 }
67
68 { // test action
69 boost::optional<int> n = 0;
70 BOOST_TEST((test_attr("1234", (-int_)[test_attribute_type()], n)));
71 BOOST_TEST((n.get() == 1234));
72 }
73
74 {
75 std::string s;
76 BOOST_TEST((test_attr("abc", char_ >> -(char_ >> char_), s)));
77 BOOST_TEST(s == "abc");
78 }
79
80 {
81 boost::optional<int> n = 0;
82 auto f = [&](auto& ctx){ n = _attr(ctx); };
83
84 BOOST_TEST((test("1234", (-int_)[f])));
85 BOOST_TEST(n.get() == 1234);
86
87 n = boost::optional<int>();
88 BOOST_TEST((test("abcd", (-int_)[f], false)));
89 BOOST_TEST(!n);
90 }
91
92 {
93 std::vector<adata> v;
94 BOOST_TEST((test_attr("a 1 2 a 2", *('a' >> int_ >> -int_), v
95 , char_(' '))));
96 BOOST_TEST(2 == v.size() &&
97 1 == v[0].a && v[0].b && 2 == *(v[0].b) &&
98 2 == v[1].a && !v[1].b);
99 }
100
101 return boost::report_errors();
102 }