]>
git.proxmox.com Git - ceph.git/blob - 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
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 =============================================================================*/
9 #include <boost/config/warning_disable.hpp>
10 #include <boost/detail/lightweight_test.hpp>
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>
18 #include <boost/spirit/include/phoenix_core.hpp>
19 #include <boost/spirit/include/phoenix_operator.hpp>
20 #include <boost/spirit/include/phoenix_statement.hpp>
24 using namespace spirit_test
;
26 ///////////////////////////////////////////////////////////////////////////////
27 // special bool output policy allowing to spell false as true backwards (eurt)
28 struct special_bool_policy
: boost::spirit::karma::bool_policies
<>
30 template <typename CharEncoding
, typename Tag
31 , typename OutputIterator
>
32 static bool generate_false(OutputIterator
& sink
, bool)
34 // we want to spell the names of true and false backwards
35 return boost::spirit::karma::string_inserter
<CharEncoding
, Tag
>::
40 ///////////////////////////////////////////////////////////////////////////////
41 // special policy allowing to use any type as a boolean
44 explicit test_bool_data(bool b
) : b(b
) {}
48 // we need to provide (safe) convertibility to bool
50 struct dummy
{ void true_() {} };
51 typedef void (dummy::*safe_bool
)();
54 operator safe_bool () const { return b
? &dummy::true_
: 0; }
57 struct test_bool_policy
: boost::spirit::karma::bool_policies
<>
59 template <typename Inserter
, typename OutputIterator
, typename Policies
>
61 call (OutputIterator
& sink
, test_bool_data b
, Policies
const& p
)
63 // call the predefined inserter to do the job
64 return Inserter::call_n(sink
, bool(b
), p
);
69 ///////////////////////////////////////////////////////////////////////////////
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
;
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)));
95 // test optional attributes
97 boost::optional
<bool> optbool
;
99 BOOST_TEST(!test("", bool_
, optbool
));
100 BOOST_TEST(test("", -bool_
, optbool
));
102 BOOST_TEST(test("false", bool_
, optbool
));
103 BOOST_TEST(test("false", -bool_
, optbool
));
105 BOOST_TEST(test("true", bool_
, optbool
));
106 BOOST_TEST(test("true", -bool_
, optbool
));
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)
114 namespace phoenix
= boost::phoenix
;
116 BOOST_TEST(test("true", bool_
, phoenix::val(true)));
119 BOOST_TEST(test("false", bool_
, phoenix::ref(b
)));
120 BOOST_TEST(test("true", bool_
, ++phoenix::ref(b
)));
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)]));
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)]));
151 typedef boost::spirit::karma::bool_generator
<bool, special_bool_policy
>
153 backwards_bool_type
const backwards_bool
= backwards_bool_type();
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));
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();
170 test_bool_data
const test_false
= test_bool_data(false);
171 test_bool_data
const test_true
= test_bool_data(true);
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
));
183 return boost::report_errors();