]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/x3/container_support.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2015 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
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 #include <boost/spirit/home/x3.hpp>
9 #include <boost/fusion/include/std_pair.hpp>
13 #include <unordered_map>
14 #include <boost/unordered_map.hpp>
19 #include <unordered_set>
20 #include <boost/unordered_set.hpp>
24 namespace x3
= boost::spirit::x3
;
27 // check if we did not break user defined specializations
28 namespace check_substitute
{
29 template <typename T
> struct foo
{};
30 template <typename T
> struct bar
{ using type
= T
; };
31 template <typename T
> struct is_bar
: std::false_type
{};
32 template <typename T
> struct is_bar
<bar
<T
>> : std::true_type
{};
35 namespace boost
{ namespace spirit
{ namespace x3
{ namespace traits
{
36 using namespace check_substitute
;
38 template <typename T
, typename U
>
39 struct is_substitute
<foo
<T
>, foo
<U
>> : is_substitute
<T
, U
> {};
41 template <typename T
, typename U
>
42 struct is_substitute
<T
, U
, std::enable_if_t
<is_bar
<T
>::value
&& is_bar
<U
>::value
>>
43 : is_substitute
<typename
T::type
, typename
U::type
> {};
46 namespace check_substitute
{
47 using x3::traits::is_substitute
;
48 static_assert(is_substitute
<foo
<int>, foo
<int>>::value
, "is_substitute problem");
49 static_assert(!is_substitute
<foo
<int>, foo
<long>>::value
, "is_substitute problem");
50 static_assert(is_substitute
<bar
<int>, bar
<int>>::value
, "is_substitute problem");
51 static_assert(!is_substitute
<bar
<int>, bar
<long>>::value
, "is_substitute problem");
55 x3::rule
<class pair_rule
, std::pair
<std::string
,std::string
>> const pair_rule("pair");
56 x3::rule
<class string_rule
, std::string
> const string_rule("string");
58 auto const pair_rule_def
= string_rule
> x3::lit('=') > string_rule
;
59 auto const string_rule_def
= x3::lexeme
[*x3::alnum
];
61 BOOST_SPIRIT_DEFINE(pair_rule
, string_rule
)
63 template <typename Container
>
64 void test_map_support()
66 using spirit_test::test_attr
;
69 Container
const compare
{{"k1", "v1"}, {"k2", "v2"}};
70 auto const rule
= pair_rule
% x3::lit(',');
72 BOOST_TEST(test_attr("k1=v1,k2=v2,k2=v3", rule
, container
));
73 BOOST_TEST(container
.size() == 2);
74 BOOST_TEST(container
== compare
);
76 // test sequences parsing into containers
77 auto const seq_rule
= pair_rule
>> ',' >> pair_rule
>> ',' >> pair_rule
;
79 BOOST_TEST(test_attr("k1=v1,k2=v2,k2=v3", seq_rule
, container
));
81 // test parsing container into container
82 auto const cic_rule
= pair_rule
>> +(',' >> pair_rule
);
84 BOOST_TEST(test_attr("k1=v1,k2=v2,k2=v3", cic_rule
, container
));
87 template <typename Container
>
88 void test_multimap_support()
90 using spirit_test::test_attr
;
93 Container
const compare
{{"k1", "v1"}, {"k2", "v2"}, {"k2", "v3"}};
94 auto const rule
= pair_rule
% x3::lit(',');
96 BOOST_TEST(test_attr("k1=v1,k2=v2,k2=v3", rule
, container
));
97 BOOST_TEST(container
.size() == 3);
98 BOOST_TEST(container
== compare
);
100 // test sequences parsing into containers
101 auto const seq_rule
= pair_rule
>> ',' >> pair_rule
>> ',' >> pair_rule
;
103 BOOST_TEST(test_attr("k1=v1,k2=v2,k2=v3", seq_rule
, container
));
105 // test parsing container into container
106 auto const cic_rule
= pair_rule
>> +(',' >> pair_rule
);
108 BOOST_TEST(test_attr("k1=v1,k2=v2,k2=v3", cic_rule
, container
));
111 template <typename Container
>
112 void test_sequence_support()
114 using spirit_test::test_attr
;
117 Container
const compare
{"e1", "e2", "e2"};
118 auto const rule
= string_rule
% x3::lit(',');
120 BOOST_TEST(test_attr("e1,e2,e2", rule
, container
));
121 BOOST_TEST(container
.size() == 3);
122 BOOST_TEST(container
== compare
);
124 // test sequences parsing into containers
125 auto const seq_rule
= string_rule
>> ',' >> string_rule
>> ',' >> string_rule
;
127 BOOST_TEST(test_attr("e1,e2,e2", seq_rule
, container
));
129 // test parsing container into container
130 auto const cic_rule
= string_rule
>> +(',' >> string_rule
);
132 BOOST_TEST(test_attr("e1,e2,e2", cic_rule
, container
));
135 template <typename Container
>
136 void test_set_support()
138 using spirit_test::test_attr
;
141 Container
const compare
{"e1", "e2"};
142 auto const rule
= string_rule
% x3::lit(',');
144 BOOST_TEST(test_attr("e1,e2,e2", rule
, container
));
145 BOOST_TEST(container
.size() == 2);
146 BOOST_TEST(container
== compare
);
148 // test sequences parsing into containers
149 auto const seq_rule
= string_rule
>> ',' >> string_rule
>> ',' >> string_rule
;
151 BOOST_TEST(test_attr("e1,e2,e2", seq_rule
, container
));
153 // test parsing container into container
154 auto const cic_rule
= string_rule
>> +(',' >> string_rule
);
156 BOOST_TEST(test_attr("e1,e2,e2", cic_rule
, container
));
159 template <typename Container
>
160 void test_multiset_support()
162 using spirit_test::test_attr
;
165 Container
const compare
{"e1", "e2", "e2"};
166 auto const rule
= string_rule
% x3::lit(',');
168 BOOST_TEST(test_attr("e1,e2,e2", rule
, container
));
169 BOOST_TEST(container
.size() == 3);
170 BOOST_TEST(container
== compare
);
172 // test sequences parsing into containers
173 auto const seq_rule
= string_rule
>> ',' >> string_rule
>> ',' >> string_rule
;
175 BOOST_TEST(test_attr("e1,e2,e2", seq_rule
, container
));
177 // test parsing container into container
178 auto const cic_rule
= string_rule
>> +(',' >> string_rule
);
180 BOOST_TEST(test_attr("e1,e2,e2", cic_rule
, container
));
183 template <typename Container
>
184 void test_string_support()
186 using spirit_test::test_attr
;
189 Container
const compare
{"e1e2e2"};
190 auto const rule
= string_rule
% x3::lit(',');
192 BOOST_TEST(test_attr("e1,e2,e2", rule
, container
));
193 BOOST_TEST(container
.size() == 6);
194 BOOST_TEST(container
== compare
);
196 // test sequences parsing into containers
197 auto const seq_rule
= string_rule
>> ',' >> string_rule
>> ',' >> string_rule
;
199 BOOST_TEST(test_attr("e1,e2,e2", seq_rule
, container
));
201 // test parsing container into container
202 auto const cic_rule
= string_rule
>> +(',' >> string_rule
);
204 BOOST_TEST(test_attr("e1,e2,e2", cic_rule
, container
));
210 using x3::traits::is_associative
;
212 // ------------------------------------------------------------------
214 static_assert(is_associative
<std::set
<int>>::value
, "is_associative problem");
215 static_assert(is_associative
<std::unordered_set
<int>>::value
, "is_associative problem");
216 static_assert(is_associative
<boost::unordered_set
<int>>::value
, "is_associative problem");
217 static_assert(is_associative
<std::multiset
<int>>::value
, "is_associative problem");
218 static_assert(is_associative
<std::unordered_multiset
<int>>::value
, "is_associative problem");
219 static_assert(is_associative
<boost::unordered_multiset
<int>>::value
, "is_associative problem");
220 static_assert(is_associative
<std::map
<int,int>>::value
, "is_associative problem");
221 static_assert(is_associative
<std::unordered_map
<int,int>>::value
, "is_associative problem");
222 static_assert(is_associative
<boost::unordered_map
<int,int>>::value
, "is_associative problem");
223 static_assert(is_associative
<std::multimap
<int,int>>::value
, "is_associative problem");
224 static_assert(is_associative
<std::unordered_multimap
<int,int>>::value
, "is_associative problem");
225 static_assert(is_associative
<boost::unordered_multimap
<int,int>>::value
, "is_associative problem");
227 static_assert(!is_associative
<std::vector
<int>>::value
, "is_associative problem");
228 static_assert(!is_associative
<std::string
>::value
, "is_associative problem");
229 static_assert(!is_associative
<std::deque
<int>>::value
, "is_associative problem");
230 static_assert(!is_associative
<std::list
<int>>::value
, "is_associative problem");
232 // ------------------------------------------------------------------
234 test_string_support
<std::string
>();
236 test_sequence_support
<std::vector
<std::string
>>();
237 test_sequence_support
<std::list
<std::string
>>();
238 test_sequence_support
<std::deque
<std::string
>>();
240 test_set_support
<std::set
<std::string
>>();
241 test_set_support
<std::unordered_set
<std::string
>>();
242 test_set_support
<boost::unordered_set
<std::string
>>();
244 test_multiset_support
<std::multiset
<std::string
>>();
245 test_multiset_support
<std::unordered_multiset
<std::string
>>();
246 test_multiset_support
<boost::unordered_multiset
<std::string
>>();
248 test_map_support
<std::map
<std::string
,std::string
>>();
249 test_map_support
<std::unordered_map
<std::string
,std::string
>>();
250 test_map_support
<boost::unordered_map
<std::string
,std::string
>>();
252 test_multimap_support
<std::multimap
<std::string
,std::string
>>();
253 test_multimap_support
<std::unordered_multimap
<std::string
,std::string
>>();
254 test_multimap_support
<boost::unordered_multimap
<std::string
,std::string
>>();
256 return boost::report_errors();