]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/spirit/test/x3/optional.cpp
update ceph source to reef 18.1.2
[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=============================================================================*/
7c673cae
FG
7#include <boost/spirit/home/x3.hpp>
8#include <boost/fusion/adapted/struct.hpp>
9#include <boost/fusion/include/vector.hpp>
10
11#include <iostream>
12#include "test.hpp"
92f5a8d4 13#include "utils.hpp"
7c673cae 14
1e59de90
TL
15#ifdef _MSC_VER
16// bogus https://developercommunity.visualstudio.com/t/buggy-warning-c4709/471956
17# pragma warning(disable: 4709) // comma operator within array index expression
18#endif
19
7c673cae
FG
20struct adata
21{
22 int a;
23 boost::optional<int> b;
24};
25
26BOOST_FUSION_ADAPT_STRUCT(adata,
27 a, b
28)
29
30struct test_attribute_type
31{
32 template <typename Context>
33 void operator()(Context& ctx) const
34 {
35 BOOST_TEST(typeid(decltype(_attr(ctx))).name() == typeid(boost::optional<int>).name());
36 }
37};
38
39int
40main()
41{
11fdf7f2
TL
42 using boost::spirit::x3::traits::is_optional;
43
44 static_assert(is_optional<boost::optional<int>>(), "is_optional problem");
45
7c673cae
FG
46 using spirit_test::test;
47 using spirit_test::test_attr;
48
49 using boost::spirit::x3::int_;
50 using boost::spirit::x3::omit;
51 using boost::spirit::x3::ascii::char_;
52
f67539c2
TL
53 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(-int_);
54
7c673cae
FG
55 {
56 BOOST_TEST((test("1234", -int_)));
57 BOOST_TEST((test("abcd", -int_, false)));
20effc67
TL
58
59 boost::optional<int> n;
60 BOOST_TEST(test_attr("", -int_, n))
61 && BOOST_TEST(!n);
62 BOOST_TEST(test_attr("123", -int_, n))
63 && BOOST_TEST(n) && BOOST_TEST_EQ(*n, 123);
64
65 boost::optional<std::string> s;
66 BOOST_TEST(test_attr("", -+char_, s))
67 && BOOST_TEST(!s);
68 BOOST_TEST(test_attr("abc", -+char_, s))
69 && BOOST_TEST(s) && BOOST_TEST_EQ(*s, "abc");
7c673cae
FG
70 }
71
72 { // test propagation of unused
73 using boost::fusion::at_c;
74 using boost::fusion::vector;
75
76 vector<char, char> v;
77 BOOST_TEST((test_attr("a1234c", char_ >> -omit[int_] >> char_, v)));
78 BOOST_TEST((at_c<0>(v) == 'a'));
79 BOOST_TEST((at_c<1>(v) == 'c'));
80
81 v = boost::fusion::vector<char, char>();
82 BOOST_TEST((test_attr("a1234c", char_ >> omit[-int_] >> char_, v)));
83 BOOST_TEST((at_c<0>(v) == 'a'));
84 BOOST_TEST((at_c<1>(v) == 'c'));
85
86 char ch;
87 BOOST_TEST((test_attr(",c", -(',' >> char_), ch)));
88 BOOST_TEST((ch == 'c'));
89 }
90
91 { // test action
92 boost::optional<int> n = 0;
93 BOOST_TEST((test_attr("1234", (-int_)[test_attribute_type()], n)));
94 BOOST_TEST((n.get() == 1234));
95 }
96
97 {
98 std::string s;
99 BOOST_TEST((test_attr("abc", char_ >> -(char_ >> char_), s)));
100 BOOST_TEST(s == "abc");
101 }
102
103 {
104 boost::optional<int> n = 0;
105 auto f = [&](auto& ctx){ n = _attr(ctx); };
106
107 BOOST_TEST((test("1234", (-int_)[f])));
108 BOOST_TEST(n.get() == 1234);
109
110 n = boost::optional<int>();
111 BOOST_TEST((test("abcd", (-int_)[f], false)));
112 BOOST_TEST(!n);
113 }
114
115 {
116 std::vector<adata> v;
117 BOOST_TEST((test_attr("a 1 2 a 2", *('a' >> int_ >> -int_), v
118 , char_(' '))));
119 BOOST_TEST(2 == v.size() &&
120 1 == v[0].a && v[0].b && 2 == *(v[0].b) &&
121 2 == v[1].a && !v[1].b);
122 }
123
92f5a8d4
TL
124 { // test move only types
125 boost::optional<move_only> o;
126 BOOST_TEST(test_attr("s", -synth_move_only, o));
127 BOOST_TEST(o);
128 }
129
7c673cae
FG
130 return boost::report_errors();
131}