]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/qi/optional.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
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>
24 boost::optional
<int> b
;
27 BOOST_FUSION_ADAPT_STRUCT(
30 (boost::optional
<int>, b
)
33 struct test_attribute_type
35 template <typename Attribute
, typename Context
>
36 void operator()(Attribute
&, Context
&, bool&) const
38 BOOST_TEST(typeid(Attribute
).name() == typeid(boost::optional
<int>).name());
45 using spirit_test::test
;
46 using spirit_test::test_attr
;
48 using boost::spirit::qi::_1
;
49 using boost::spirit::qi::int_
;
50 using boost::spirit::qi::omit
;
51 using boost::spirit::ascii::char_
;
54 BOOST_TEST((test("1234", -int_
)));
55 BOOST_TEST((test("abcd", -int_
, false)));
57 boost::optional
<int> n
;
58 BOOST_TEST(test_attr("", -int_
, n
))
60 BOOST_TEST(test_attr("123", -int_
, n
))
61 && BOOST_TEST(n
) && BOOST_TEST_EQ(*n
, 123);
63 boost::optional
<std::string
> s
;
64 BOOST_TEST(test_attr("", -+char_
, s
))
66 BOOST_TEST(test_attr("abc", -+char_
, s
))
67 && BOOST_TEST(s
) && BOOST_TEST_EQ(*s
, "abc");
70 { // test propagation of unused
71 using boost::fusion::at_c
;
72 using boost::fusion::vector
;
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'));
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'));
85 BOOST_TEST((test_attr(",c", -(',' >> char_
), ch
)));
86 BOOST_TEST((ch
== 'c'));
90 boost::optional
<int> n
= 0;
91 BOOST_TEST((test_attr("1234", (-int_
)[test_attribute_type()], n
)));
92 BOOST_TEST((n
.get() == 1234));
97 BOOST_TEST((test_attr("abc", char_
>> -(char_
>> char_
), s
)));
98 BOOST_TEST(s
== "abc");
102 namespace phx
= boost::phoenix
;
104 boost::optional
<int> n
= 0;
105 BOOST_TEST((test("1234", (-int_
)[phx::ref(n
) = _1
])));
106 BOOST_TEST(n
.get() == 1234);
108 n
= boost::optional
<int>();
109 BOOST_TEST((test("abcd", (-int_
)[phx::ref(n
) = _1
], false)));
114 std::vector
<adata
> v
;
115 BOOST_TEST((test_attr("a 1 2 a 2", *("a" >> int_
>> -int_
), v
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
);
122 return boost::report_errors();