]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2001-2015 Joel de Guzman | |
3 | Copyright (c) 2001-2011 Hartmut Kaiser | |
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 | #include <boost/detail/lightweight_test.hpp> | |
9 | #include <boost/spirit/home/x3.hpp> | |
10 | #include <boost/fusion/include/std_pair.hpp> | |
11 | ||
12 | #include <iostream> | |
13 | #include <map> | |
14 | #include <unordered_map> | |
15 | #include <boost/unordered_map.hpp> | |
16 | #include <vector> | |
17 | #include <list> | |
18 | #include <deque> | |
19 | #include <set> | |
20 | #include <unordered_set> | |
21 | #include <boost/unordered_set.hpp> | |
22 | #include <string> | |
23 | #include "test.hpp" | |
24 | ||
25 | namespace x3 = boost::spirit::x3; | |
26 | ||
27 | x3::rule<class pair_rule, std::pair<std::string,std::string>> const pair_rule("pair"); | |
28 | x3::rule<class string_rule, std::string> const string_rule("string"); | |
29 | ||
30 | auto const pair_rule_def = string_rule > x3::lit('=') > string_rule; | |
31 | auto const string_rule_def = x3::lexeme[*x3::alnum]; | |
32 | ||
33 | BOOST_SPIRIT_DEFINE(pair_rule, string_rule); | |
34 | ||
35 | template <typename Container> | |
36 | void test_map_support(Container&& container) | |
37 | { | |
38 | using spirit_test::test_attr; | |
39 | ||
40 | Container const compare {{"k1", "v1"}, {"k2", "v2"}}; | |
41 | auto const rule = pair_rule % x3::lit(','); | |
42 | ||
43 | BOOST_TEST(test_attr("k1=v1,k2=v2,k2=v3", rule, container)); | |
44 | BOOST_TEST(container.size() == 2); | |
45 | BOOST_TEST(container == compare); | |
46 | ||
47 | // test sequences parsing into containers | |
48 | auto const seq_rule = pair_rule >> ',' >> pair_rule >> ',' >> pair_rule; | |
49 | container.clear(); | |
50 | BOOST_TEST(test_attr("k1=v1,k2=v2,k2=v3", seq_rule, container)); | |
11fdf7f2 TL |
51 | |
52 | // test parsing container into container | |
53 | auto const cic_rule = pair_rule >> +(',' >> pair_rule); | |
54 | container.clear(); | |
55 | BOOST_TEST(test_attr("k1=v1,k2=v2,k2=v3", cic_rule, container)); | |
7c673cae FG |
56 | } |
57 | ||
58 | template <typename Container> | |
59 | void test_multimap_support(Container&& container) | |
60 | { | |
61 | using spirit_test::test_attr; | |
62 | ||
63 | Container const compare {{"k1", "v1"}, {"k2", "v2"}, {"k2", "v3"}}; | |
64 | auto const rule = pair_rule % x3::lit(','); | |
65 | ||
66 | BOOST_TEST(test_attr("k1=v1,k2=v2,k2=v3", rule, container)); | |
67 | BOOST_TEST(container.size() == 3); | |
68 | BOOST_TEST(container == compare); | |
69 | ||
70 | // test sequences parsing into containers | |
71 | auto const seq_rule = pair_rule >> ',' >> pair_rule >> ',' >> pair_rule; | |
72 | container.clear(); | |
73 | BOOST_TEST(test_attr("k1=v1,k2=v2,k2=v3", seq_rule, container)); | |
11fdf7f2 TL |
74 | |
75 | // test parsing container into container | |
76 | auto const cic_rule = pair_rule >> +(',' >> pair_rule); | |
77 | container.clear(); | |
78 | BOOST_TEST(test_attr("k1=v1,k2=v2,k2=v3", cic_rule, container)); | |
7c673cae FG |
79 | } |
80 | ||
81 | template <typename Container> | |
82 | void test_sequence_support(Container&& container) | |
83 | { | |
84 | using spirit_test::test_attr; | |
85 | ||
86 | Container const compare {"e1", "e2", "e2"}; | |
87 | auto const rule = string_rule % x3::lit(','); | |
88 | ||
89 | BOOST_TEST(test_attr("e1,e2,e2", rule, container)); | |
90 | BOOST_TEST(container.size() == 3); | |
91 | BOOST_TEST(container == compare); | |
92 | ||
93 | // test sequences parsing into containers | |
94 | auto const seq_rule = string_rule >> ',' >> string_rule >> ',' >> string_rule; | |
95 | container.clear(); | |
96 | BOOST_TEST(test_attr("e1,e2,e2", seq_rule, container)); | |
11fdf7f2 TL |
97 | |
98 | // test parsing container into container | |
99 | auto const cic_rule = string_rule >> +(',' >> string_rule); | |
100 | container.clear(); | |
101 | BOOST_TEST(test_attr("e1,e2,e2", cic_rule, container)); | |
7c673cae FG |
102 | } |
103 | ||
104 | template <typename Container> | |
105 | void test_set_support(Container&& container) | |
106 | { | |
107 | using spirit_test::test_attr; | |
108 | ||
109 | Container const compare {"e1", "e2"}; | |
110 | auto const rule = string_rule % x3::lit(','); | |
111 | ||
112 | BOOST_TEST(test_attr("e1,e2,e2", rule, container)); | |
113 | BOOST_TEST(container.size() == 2); | |
114 | BOOST_TEST(container == compare); | |
115 | ||
116 | // test sequences parsing into containers | |
117 | auto const seq_rule = string_rule >> ',' >> string_rule >> ',' >> string_rule; | |
118 | container.clear(); | |
119 | BOOST_TEST(test_attr("e1,e2,e2", seq_rule, container)); | |
11fdf7f2 TL |
120 | |
121 | // test parsing container into container | |
122 | auto const cic_rule = string_rule >> +(',' >> string_rule); | |
123 | container.clear(); | |
124 | BOOST_TEST(test_attr("e1,e2,e2", cic_rule, container)); | |
7c673cae FG |
125 | } |
126 | ||
127 | template <typename Container> | |
128 | void test_multiset_support(Container&& container) | |
129 | { | |
130 | using spirit_test::test_attr; | |
131 | ||
132 | Container const compare {"e1", "e2", "e2"}; | |
133 | auto const rule = string_rule % x3::lit(','); | |
134 | ||
135 | BOOST_TEST(test_attr("e1,e2,e2", rule, container)); | |
136 | BOOST_TEST(container.size() == 3); | |
137 | BOOST_TEST(container == compare); | |
138 | ||
139 | // test sequences parsing into containers | |
140 | auto const seq_rule = string_rule >> ',' >> string_rule >> ',' >> string_rule; | |
141 | container.clear(); | |
142 | BOOST_TEST(test_attr("e1,e2,e2", seq_rule, container)); | |
11fdf7f2 TL |
143 | |
144 | // test parsing container into container | |
145 | auto const cic_rule = string_rule >> +(',' >> string_rule); | |
146 | container.clear(); | |
147 | BOOST_TEST(test_attr("e1,e2,e2", cic_rule, container)); | |
7c673cae FG |
148 | } |
149 | ||
150 | template <typename Container> | |
151 | void test_string_support(Container&& container) | |
152 | { | |
153 | using spirit_test::test_attr; | |
154 | ||
155 | Container const compare {"e1e2e2"}; | |
156 | auto const rule = string_rule % x3::lit(','); | |
157 | ||
158 | BOOST_TEST(test_attr("e1,e2,e2", rule, container)); | |
159 | BOOST_TEST(container.size() == 6); | |
160 | BOOST_TEST(container == compare); | |
161 | ||
162 | // test sequences parsing into containers | |
163 | auto const seq_rule = string_rule >> ',' >> string_rule >> ',' >> string_rule; | |
164 | container.clear(); | |
165 | BOOST_TEST(test_attr("e1,e2,e2", seq_rule, container)); | |
11fdf7f2 TL |
166 | |
167 | // test parsing container into container | |
168 | auto const cic_rule = string_rule >> +(',' >> string_rule); | |
169 | container.clear(); | |
170 | BOOST_TEST(test_attr("e1,e2,e2", cic_rule, container)); | |
7c673cae FG |
171 | } |
172 | ||
173 | int | |
174 | main() | |
175 | { | |
176 | using x3::traits::is_associative; | |
177 | using x3::traits::is_reservable; | |
178 | ||
179 | static_assert(is_reservable<std::vector<int>>::value, "is_reservable problem"); | |
180 | static_assert(is_reservable<std::string>::value, "is_reservable problem"); | |
181 | static_assert(is_reservable<std::unordered_set<int>>::value, "is_reservable problem"); | |
182 | static_assert(is_reservable<boost::unordered_set<int>>::value, "is_reservable problem"); | |
183 | static_assert(is_reservable<std::unordered_multiset<int>>::value, "is_reservable problem"); | |
184 | static_assert(is_reservable<boost::unordered_multiset<int>>::value, "is_reservable problem"); | |
185 | static_assert(is_reservable<std::unordered_map<int,int>>::value, "is_reservable problem"); | |
186 | static_assert(is_reservable<boost::unordered_map<int,int>>::value, "is_reservable problem"); | |
187 | static_assert(is_reservable<std::unordered_multimap<int,int>>::value, "is_reservable problem"); | |
188 | static_assert(is_reservable<boost::unordered_multimap<int,int>>::value, "is_reservable problem"); | |
189 | ||
190 | static_assert(!is_reservable<std::deque<int>>::value, "is_reservable problem"); | |
191 | static_assert(!is_reservable<std::list<int>>::value, "is_reservable problem"); | |
192 | static_assert(!is_reservable<std::set<int>>::value, "is_reservable problem"); | |
193 | static_assert(!is_reservable<std::multiset<int>>::value, "is_reservable problem"); | |
194 | static_assert(!is_reservable<std::map<int,int>>::value, "is_reservable problem"); | |
195 | static_assert(!is_reservable<std::multimap<int,int>>::value, "is_reservable problem"); | |
196 | ||
197 | // ------------------------------------------------------------------ | |
198 | ||
199 | static_assert(is_associative<std::set<int>>::value, "is_associative problem"); | |
200 | static_assert(is_associative<std::unordered_set<int>>::value, "is_associative problem"); | |
201 | static_assert(is_associative<boost::unordered_set<int>>::value, "is_associative problem"); | |
202 | static_assert(is_associative<std::multiset<int>>::value, "is_associative problem"); | |
203 | static_assert(is_associative<std::unordered_multiset<int>>::value, "is_associative problem"); | |
204 | static_assert(is_associative<boost::unordered_multiset<int>>::value, "is_associative problem"); | |
205 | static_assert(is_associative<std::map<int,int>>::value, "is_associative problem"); | |
206 | static_assert(is_associative<std::unordered_map<int,int>>::value, "is_associative problem"); | |
207 | static_assert(is_associative<boost::unordered_map<int,int>>::value, "is_associative problem"); | |
208 | static_assert(is_associative<std::multimap<int,int>>::value, "is_associative problem"); | |
209 | static_assert(is_associative<std::unordered_multimap<int,int>>::value, "is_associative problem"); | |
210 | static_assert(is_associative<boost::unordered_multimap<int,int>>::value, "is_associative problem"); | |
211 | ||
212 | static_assert(!is_associative<std::vector<int>>::value, "is_associative problem"); | |
213 | static_assert(!is_associative<std::string>::value, "is_associative problem"); | |
214 | static_assert(!is_associative<std::deque<int>>::value, "is_associative problem"); | |
215 | static_assert(!is_associative<std::list<int>>::value, "is_associative problem"); | |
216 | ||
217 | // ------------------------------------------------------------------ | |
218 | ||
219 | test_string_support(std::string()); | |
220 | ||
221 | test_sequence_support(std::vector<std::string>()); | |
222 | test_sequence_support(std::list<std::string>()); | |
223 | test_sequence_support(std::deque<std::string>()); | |
224 | ||
225 | test_set_support(std::set<std::string>()); | |
226 | test_set_support(std::unordered_set<std::string>()); | |
227 | test_set_support(boost::unordered_set<std::string>()); | |
228 | ||
229 | test_multiset_support(std::multiset<std::string>()); | |
230 | test_multiset_support(std::unordered_multiset<std::string>()); | |
231 | test_multiset_support(boost::unordered_multiset<std::string>()); | |
232 | ||
233 | test_map_support(std::map<std::string,std::string>()); | |
234 | test_map_support(std::unordered_map<std::string,std::string>()); | |
235 | test_map_support(boost::unordered_map<std::string,std::string>()); | |
236 | ||
237 | test_multimap_support(std::multimap<std::string,std::string>()); | |
238 | test_multimap_support(std::unordered_multimap<std::string,std::string>()); | |
239 | test_multimap_support(boost::unordered_multimap<std::string,std::string>()); | |
240 | ||
241 | return boost::report_errors(); | |
242 | } |