]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/qi/optional.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / spirit / test / qi / optional.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2011 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/include/qi_char.hpp>
9 #include <boost/spirit/include/qi_numeric.hpp>
10 #include <boost/spirit/include/qi_operator.hpp>
11 #include <boost/spirit/include/qi_action.hpp>
12 #include <boost/spirit/include/qi_directive.hpp>
13 #include <boost/spirit/include/support_argument.hpp>
14 #include <boost/spirit/include/phoenix_core.hpp>
15 #include <boost/spirit/include/phoenix_operator.hpp>
16 #include <boost/fusion/adapted/struct.hpp>
17
18 #include <iostream>
19 #include "test.hpp"
20
21 struct adata
22 {
23 int a;
24 boost::optional<int> b;
25 };
26
27 BOOST_FUSION_ADAPT_STRUCT(
28 adata,
29 (int, a)
30 (boost::optional<int>, b)
31 )
32
33 struct test_attribute_type
34 {
35 template <typename Attribute, typename Context>
36 void operator()(Attribute&, Context&, bool&) const
37 {
38 BOOST_TEST(typeid(Attribute).name() == typeid(boost::optional<int>).name());
39 }
40 };
41
42 int
43 main()
44 {
45 using spirit_test::test;
46 using spirit_test::test_attr;
47
48 using boost::spirit::qi::_1;
49 using boost::spirit::qi::int_;
50 using boost::spirit::qi::omit;
51 using boost::spirit::ascii::char_;
52
53 {
54 BOOST_TEST((test("1234", -int_)));
55 BOOST_TEST((test("abcd", -int_, false)));
56
57 boost::optional<int> n;
58 BOOST_TEST(test_attr("", -int_, n))
59 && BOOST_TEST(!n);
60 BOOST_TEST(test_attr("123", -int_, n))
61 && BOOST_TEST(n) && BOOST_TEST_EQ(*n, 123);
62
63 boost::optional<std::string> s;
64 BOOST_TEST(test_attr("", -+char_, s))
65 && BOOST_TEST(!s);
66 BOOST_TEST(test_attr("abc", -+char_, s))
67 && BOOST_TEST(s) && BOOST_TEST_EQ(*s, "abc");
68 }
69
70 { // test propagation of unused
71 using boost::fusion::at_c;
72 using boost::fusion::vector;
73
74 vector<char, char> v;
75 BOOST_TEST((test_attr("a1234c", char_ >> -omit[int_] >> char_, v)));
76 BOOST_TEST((at_c<0>(v) == 'a'));
77 BOOST_TEST((at_c<1>(v) == 'c'));
78
79 v = boost::fusion::vector<char, char>();
80 BOOST_TEST((test_attr("a1234c", char_ >> omit[-int_] >> char_, v)));
81 BOOST_TEST((at_c<0>(v) == 'a'));
82 BOOST_TEST((at_c<1>(v) == 'c'));
83
84 char ch;
85 BOOST_TEST((test_attr(",c", -(',' >> char_), ch)));
86 BOOST_TEST((ch == 'c'));
87 }
88
89 { // test action
90 boost::optional<int> n = 0;
91 BOOST_TEST((test_attr("1234", (-int_)[test_attribute_type()], n)));
92 BOOST_TEST((n.get() == 1234));
93 }
94
95 {
96 std::string s;
97 BOOST_TEST((test_attr("abc", char_ >> -(char_ >> char_), s)));
98 BOOST_TEST(s == "abc");
99 }
100
101 {
102 namespace phx = boost::phoenix;
103
104 boost::optional<int> n = 0;
105 BOOST_TEST((test("1234", (-int_)[phx::ref(n) = _1])));
106 BOOST_TEST(n.get() == 1234);
107
108 n = boost::optional<int>();
109 BOOST_TEST((test("abcd", (-int_)[phx::ref(n) = _1], false)));
110 BOOST_TEST(!n);
111 }
112
113 {
114 std::vector<adata> v;
115 BOOST_TEST((test_attr("a 1 2 a 2", *("a" >> int_ >> -int_), v
116 , char_(' '))));
117 BOOST_TEST(2 == v.size() &&
118 1 == v[0].a && v[0].b && 2 == *(v[0].b) &&
119 2 == v[1].a && !v[1].b);
120 }
121
122 return boost::report_errors();
123 }