]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/spirit/test/x3/optional.cpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / libs / spirit / test / x3 / optional.cpp
CommitLineData
7c673cae
FG
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
15struct adata
16{
17 int a;
18 boost::optional<int> b;
19};
20
21BOOST_FUSION_ADAPT_STRUCT(adata,
22 a, b
23)
24
25struct 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
34int
35main()
36{
11fdf7f2
TL
37 using boost::spirit::x3::traits::is_optional;
38
39 static_assert(is_optional<boost::optional<int>>(), "is_optional problem");
40
7c673cae
FG
41 using spirit_test::test;
42 using spirit_test::test_attr;
43
44 using boost::spirit::x3::int_;
45 using boost::spirit::x3::omit;
46 using boost::spirit::x3::ascii::char_;
47
48 {
49 BOOST_TEST((test("1234", -int_)));
50 BOOST_TEST((test("abcd", -int_, false)));
51 }
52
53 { // test propagation of unused
54 using boost::fusion::at_c;
55 using boost::fusion::vector;
56
57 vector<char, char> v;
58 BOOST_TEST((test_attr("a1234c", char_ >> -omit[int_] >> char_, v)));
59 BOOST_TEST((at_c<0>(v) == 'a'));
60 BOOST_TEST((at_c<1>(v) == 'c'));
61
62 v = boost::fusion::vector<char, char>();
63 BOOST_TEST((test_attr("a1234c", char_ >> omit[-int_] >> char_, v)));
64 BOOST_TEST((at_c<0>(v) == 'a'));
65 BOOST_TEST((at_c<1>(v) == 'c'));
66
67 char ch;
68 BOOST_TEST((test_attr(",c", -(',' >> char_), ch)));
69 BOOST_TEST((ch == 'c'));
70 }
71
72 { // test action
73 boost::optional<int> n = 0;
74 BOOST_TEST((test_attr("1234", (-int_)[test_attribute_type()], n)));
75 BOOST_TEST((n.get() == 1234));
76 }
77
78 {
79 std::string s;
80 BOOST_TEST((test_attr("abc", char_ >> -(char_ >> char_), s)));
81 BOOST_TEST(s == "abc");
82 }
83
84 {
85 boost::optional<int> n = 0;
86 auto f = [&](auto& ctx){ n = _attr(ctx); };
87
88 BOOST_TEST((test("1234", (-int_)[f])));
89 BOOST_TEST(n.get() == 1234);
90
91 n = boost::optional<int>();
92 BOOST_TEST((test("abcd", (-int_)[f], false)));
93 BOOST_TEST(!n);
94 }
95
96 {
97 std::vector<adata> v;
98 BOOST_TEST((test_attr("a 1 2 a 2", *('a' >> int_ >> -int_), v
99 , char_(' '))));
100 BOOST_TEST(2 == v.size() &&
101 1 == v[0].a && v[0].b && 2 == *(v[0].b) &&
102 2 == v[1].a && !v[1].b);
103 }
104
105 return boost::report_errors();
106}