]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/karma/bool.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / spirit / test / karma / bool.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2011 Hartmut Kaiser
3 Copyright (c) 2001-2011 Joel de Guzman
4
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8
9 #include <boost/config/warning_disable.hpp>
10 #include <boost/detail/lightweight_test.hpp>
11
12 #include <boost/spirit/include/karma_string.hpp>
13 #include <boost/spirit/include/karma_numeric.hpp>
14 #include <boost/spirit/include/karma_directive.hpp>
15 #include <boost/spirit/include/karma_operator.hpp>
16 #include <boost/spirit/include/karma_phoenix_attributes.hpp>
17
18 #include <boost/spirit/include/phoenix_core.hpp>
19 #include <boost/spirit/include/phoenix_operator.hpp>
20 #include <boost/spirit/include/phoenix_statement.hpp>
21
22 #include "test.hpp"
23
24 using namespace spirit_test;
25
26 ///////////////////////////////////////////////////////////////////////////////
27 // special bool output policy allowing to spell false as true backwards (eurt)
28 struct special_bool_policy : boost::spirit::karma::bool_policies<>
29 {
30 template <typename CharEncoding, typename Tag
31 , typename OutputIterator>
32 static bool generate_false(OutputIterator& sink, bool)
33 {
34 // we want to spell the names of true and false backwards
35 return boost::spirit::karma::string_inserter<CharEncoding, Tag>::
36 call(sink, "eurt");
37 }
38 };
39
40 ///////////////////////////////////////////////////////////////////////////////
41 // special policy allowing to use any type as a boolean
42 struct test_bool_data
43 {
44 explicit test_bool_data(bool b) : b(b) {}
45
46 bool b;
47
48 // we need to provide (safe) convertibility to bool
49 private:
50 struct dummy { void true_() {} };
51 typedef void (dummy::*safe_bool)();
52
53 public:
54 operator safe_bool () const { return b ? &dummy::true_ : 0; }
55 };
56
57 struct test_bool_policy : boost::spirit::karma::bool_policies<>
58 {
59 template <typename Inserter, typename OutputIterator, typename Policies>
60 static bool
61 call (OutputIterator& sink, test_bool_data b, Policies const& p)
62 {
63 // call the predefined inserter to do the job
64 return Inserter::call_n(sink, bool(b), p);
65 }
66 };
67
68
69 ///////////////////////////////////////////////////////////////////////////////
70 int main()
71 {
72 using boost::spirit::karma::bool_;
73 using boost::spirit::karma::false_;
74 using boost::spirit::karma::true_;
75 using boost::spirit::karma::lit;
76 using boost::spirit::karma::lower;
77 using boost::spirit::karma::upper;
78
79 // testing plain bool
80 {
81 BOOST_TEST(test("false", bool_, false));
82 BOOST_TEST(test("true", bool_, true));
83 BOOST_TEST(test("false", false_, false));
84 BOOST_TEST(test("true", true_, true));
85 BOOST_TEST(test("false", bool_(false)));
86 BOOST_TEST(test("true", bool_(true)));
87 BOOST_TEST(test("false", bool_(false), false));
88 BOOST_TEST(test("true", bool_(true), true));
89 BOOST_TEST(!test("", bool_(false), true));
90 BOOST_TEST(!test("", bool_(true), false));
91 BOOST_TEST(test("false", lit(false)));
92 BOOST_TEST(test("true", lit(true)));
93 }
94
95 // test optional attributes
96 {
97 boost::optional<bool> optbool;
98
99 BOOST_TEST(!test("", bool_, optbool));
100 BOOST_TEST(test("", -bool_, optbool));
101 optbool = false;
102 BOOST_TEST(test("false", bool_, optbool));
103 BOOST_TEST(test("false", -bool_, optbool));
104 optbool = true;
105 BOOST_TEST(test("true", bool_, optbool));
106 BOOST_TEST(test("true", -bool_, optbool));
107 }
108
109 // we support Phoenix attributes only starting with V2.2
110 #if SPIRIT_VERSION >= 0x2020
111 // test Phoenix expression attributes (requires to include
112 // karma_phoenix_attributes.hpp)
113 {
114 namespace phoenix = boost::phoenix;
115
116 BOOST_TEST(test("true", bool_, phoenix::val(true)));
117
118 bool b = false;
119 BOOST_TEST(test("false", bool_, phoenix::ref(b)));
120 BOOST_TEST(test("true", bool_, !phoenix::ref(b)));
121 }
122 #endif
123
124 {
125 BOOST_TEST(test("false", lower[bool_], false));
126 BOOST_TEST(test("true", lower[bool_], true));
127 BOOST_TEST(test("false", lower[bool_(false)]));
128 BOOST_TEST(test("true", lower[bool_(true)]));
129 BOOST_TEST(test("false", lower[bool_(false)], false));
130 BOOST_TEST(test("true", lower[bool_(true)], true));
131 BOOST_TEST(!test("", lower[bool_(false)], true));
132 BOOST_TEST(!test("", lower[bool_(true)], false));
133 BOOST_TEST(test("false", lower[lit(false)]));
134 BOOST_TEST(test("true", lower[lit(true)]));
135 }
136
137 {
138 BOOST_TEST(test("FALSE", upper[bool_], false));
139 BOOST_TEST(test("TRUE", upper[bool_], true));
140 BOOST_TEST(test("FALSE", upper[bool_(false)]));
141 BOOST_TEST(test("TRUE", upper[bool_(true)]));
142 BOOST_TEST(test("FALSE", upper[bool_(false)], false));
143 BOOST_TEST(test("TRUE", upper[bool_(true)], true));
144 BOOST_TEST(!test("", upper[bool_(false)], true));
145 BOOST_TEST(!test("", upper[bool_(true)], false));
146 BOOST_TEST(test("FALSE", upper[lit(false)]));
147 BOOST_TEST(test("TRUE", upper[lit(true)]));
148 }
149
150 {
151 typedef boost::spirit::karma::bool_generator<bool, special_bool_policy>
152 backwards_bool_type;
153 backwards_bool_type const backwards_bool = backwards_bool_type();
154
155 BOOST_TEST(test("eurt", backwards_bool, false));
156 BOOST_TEST(test("true", backwards_bool, true));
157 BOOST_TEST(test("eurt", backwards_bool(false)));
158 BOOST_TEST(test("true", backwards_bool(true)));
159 BOOST_TEST(test("eurt", backwards_bool(false), false));
160 BOOST_TEST(test("true", backwards_bool(true), true));
161 BOOST_TEST(!test("", backwards_bool(false), true));
162 BOOST_TEST(!test("", backwards_bool(true), false));
163 }
164
165 {
166 typedef boost::spirit::karma::bool_generator<
167 test_bool_data, test_bool_policy> test_bool_type;
168 test_bool_type const test_bool = test_bool_type();
169
170 test_bool_data const test_false = test_bool_data(false);
171 test_bool_data const test_true = test_bool_data(true);
172
173 BOOST_TEST(test("false", test_bool, test_false));
174 BOOST_TEST(test("true", test_bool, test_true));
175 BOOST_TEST(test("false", test_bool(test_false)));
176 BOOST_TEST(test("true", test_bool(test_true)));
177 BOOST_TEST(test("false", test_bool(test_false), test_false));
178 BOOST_TEST(test("true", test_bool(test_true), test_true));
179 BOOST_TEST(!test("", test_bool(test_false), test_true));
180 BOOST_TEST(!test("", test_bool(test_true), test_false));
181 }
182
183 return boost::report_errors();
184 }
185