]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2005 Dan Marsden | |
3 | Copyright (c) 2005-2007 Joel de Guzman | |
4 | Copyright (c) 2007 Hartmut Kaiser | |
5 | Copyright (c) 2015 John Fletcher | |
6 | ||
7 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
8 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | ==============================================================================*/ | |
10 | ||
11 | #include <boost/phoenix/core.hpp> | |
12 | #include <boost/phoenix/stl/algorithm/querying.hpp> | |
13 | #include <boost/detail/lightweight_test.hpp> | |
14 | #include <boost/assign/list_of.hpp> | |
15 | ||
16 | #include <boost/config.hpp> | |
17 | ||
18 | #ifdef BOOST_PHOENIX_HAS_HASH | |
19 | #define _GLIBCXX_PERMIT_BACKWARD_HASH | |
20 | #include BOOST_PHOENIX_HASH_SET_HEADER | |
21 | #include BOOST_PHOENIX_HASH_MAP_HEADER | |
22 | #endif | |
23 | ||
24 | #include <set> | |
25 | #include <map> | |
26 | #include <functional> | |
27 | ||
28 | namespace | |
29 | { | |
30 | struct even | |
31 | { | |
32 | bool operator()(const int i) const | |
33 | { | |
34 | return i % 2 == 0; | |
35 | } | |
36 | }; | |
37 | ||
38 | struct mod_2_comparison | |
39 | { | |
40 | bool operator()( | |
41 | const int lhs, | |
42 | const int rhs) | |
43 | { | |
44 | return lhs % 2 == rhs % 2; | |
45 | } | |
46 | }; | |
47 | ||
48 | void find_test() | |
49 | { | |
50 | using boost::phoenix::arg_names::arg1; | |
51 | int array[] = {1,2,3}; | |
52 | BOOST_TEST(boost::phoenix::find(arg1,2)(array) == array + 1); | |
53 | ||
54 | std::set<int> s(array, array + 3); | |
55 | BOOST_TEST(boost::phoenix::find(arg1, 2)(s) == s.find(2)); | |
56 | ||
57 | #if !(defined(BOOST_MSVC) && (BOOST_MSVC >= 1900)) | |
58 | std::map<int, int> m = boost::assign::map_list_of(0, 1)(2, 3)(4, 5); | |
59 | BOOST_TEST(boost::phoenix::find(arg1, 2)(m) == m.find(2)); | |
60 | #endif | |
61 | ||
62 | #ifdef BOOST_PHOENIX_HAS_HASH | |
63 | ||
64 | BOOST_PHOENIX_HASH_NAMESPACE::hash_set<int> hs(array, array + 3); | |
65 | BOOST_TEST(boost::phoenix::find(arg1, 2)(hs) == hs.find(2)); | |
66 | ||
67 | BOOST_PHOENIX_HASH_NAMESPACE::hash_map<int, int> hm = boost::assign::map_list_of(0, 1)(2, 3)(4, 5); | |
68 | BOOST_TEST(boost::phoenix::find(arg1, 2)(hm) == hm.find(2)); | |
69 | ||
70 | #endif | |
71 | ||
72 | return; | |
73 | } | |
74 | ||
75 | ||
76 | void find_if_test() | |
77 | { | |
78 | using boost::phoenix::arg_names::arg1; | |
79 | int array[] = {1,2,3}; | |
80 | BOOST_TEST(boost::phoenix::find_if(arg1, even())(array) == array + 1); | |
81 | return; | |
82 | } | |
83 | ||
84 | void find_end_test() | |
85 | { | |
86 | using boost::phoenix::arg_names::arg1; | |
87 | using boost::phoenix::arg_names::arg2; | |
88 | int array[] = {1,2,3,1,2,3,1}; | |
89 | int pattern[] = {1,2,3}; | |
90 | BOOST_TEST(boost::phoenix::find_end(arg1, arg2)(array, pattern) == array + 3); | |
91 | int pattern2[] = {5,6,5}; | |
92 | BOOST_TEST(boost::phoenix::find_end(arg1, arg2, mod_2_comparison())(array, pattern2) == array + 3); | |
93 | return; | |
94 | } | |
95 | ||
96 | void find_first_of_test() | |
97 | { | |
98 | using boost::phoenix::arg_names::arg1; | |
99 | using boost::phoenix::arg_names::arg2; | |
100 | int array[] = {1,2,3}; | |
101 | int search_for[] = {2,3,4}; | |
102 | BOOST_TEST(boost::phoenix::find_first_of(arg1, arg2)(array, search_for) == array + 1); | |
103 | ||
104 | int search_for2[] = {0}; | |
105 | BOOST_TEST(boost::phoenix::find_first_of(arg1, arg2, mod_2_comparison())(array, search_for2) == array + 1); | |
106 | return; | |
107 | } | |
108 | ||
109 | void adjacent_find_test() | |
110 | { | |
111 | using boost::phoenix::arg_names::arg1; | |
112 | int array[] = {0,1,3,4,4}; | |
113 | BOOST_TEST(boost::phoenix::adjacent_find(arg1)(array) == array + 3); | |
114 | BOOST_TEST(boost::phoenix::adjacent_find(arg1, mod_2_comparison())(array) == array + 1); | |
115 | return; | |
116 | } | |
117 | ||
118 | void count_test() | |
119 | { | |
120 | using boost::phoenix::arg_names::arg1; | |
121 | int array[] = {1,1,0,1,1}; | |
122 | BOOST_TEST(boost::phoenix::count(arg1, 1)(array) == 4); | |
123 | return; | |
124 | } | |
125 | ||
126 | void count_if_test() | |
127 | { | |
128 | using boost::phoenix::arg_names::arg1; | |
129 | int array[] = {1,2,3,4,5}; | |
130 | BOOST_TEST(boost::phoenix::count_if(arg1, even())(array) == 2); | |
131 | return; | |
132 | } | |
133 | ||
134 | void distance_test() | |
135 | { | |
136 | using boost::phoenix::arg_names::arg1; | |
137 | int array[] = {1,1,0,1,1}; | |
138 | BOOST_TEST(boost::phoenix::distance(arg1)(array) == 5); | |
139 | return; | |
140 | } | |
141 | ||
142 | void mismatch_test() | |
143 | { | |
144 | using boost::phoenix::arg_names::arg1; | |
145 | using boost::phoenix::arg_names::arg2; | |
146 | int array[] = {1,2,3,4,5}; | |
147 | int search[] = {1,2,4}; | |
148 | ||
149 | BOOST_TEST( | |
150 | boost::phoenix::mismatch(arg1, arg2)(array, search) == | |
151 | std::make_pair(array + 2, search + 2)); | |
152 | int search2[] = {1,2,1,1}; | |
153 | BOOST_TEST( | |
154 | boost::phoenix::mismatch(arg1, arg2, mod_2_comparison())(array, search2) | |
155 | == std::make_pair(array + 3, search2 + 3)); | |
156 | ||
157 | return; | |
158 | } | |
159 | ||
160 | void equal_test() | |
161 | { | |
162 | using boost::phoenix::arg_names::arg1; | |
163 | using boost::phoenix::arg_names::arg2; | |
164 | int array[] = {1,2,3}; | |
165 | int array2[] = {1,2,3}; | |
166 | int array3[] = {1,2,4}; | |
167 | BOOST_TEST( | |
168 | boost::phoenix::equal(arg1, arg2)(array, array2)); | |
169 | BOOST_TEST( | |
170 | !boost::phoenix::equal(arg1, arg2)(array, array3)); | |
171 | ||
172 | BOOST_TEST( | |
173 | boost::phoenix::equal(arg1, arg2, mod_2_comparison())(array, array2)); | |
174 | BOOST_TEST( | |
175 | !boost::phoenix::equal(arg1, arg2, mod_2_comparison())(array, array3)); | |
176 | return; | |
177 | } | |
178 | ||
179 | void search_test() | |
180 | { | |
181 | using boost::phoenix::arg_names::arg1; | |
182 | using boost::phoenix::arg_names::arg2; | |
183 | int array[] = {1,2,3,1,2,3}; | |
184 | int pattern[] = {2,3}; | |
185 | BOOST_TEST( | |
186 | boost::phoenix::search(arg1, arg2)(array, pattern) == array + 1); | |
187 | int pattern2[] = {1,1}; | |
188 | BOOST_TEST( | |
189 | boost::phoenix::search(arg1, arg2, mod_2_comparison())(array, pattern2) == array + 2); | |
190 | return; | |
191 | } | |
192 | ||
193 | void lower_bound_test() | |
194 | { | |
195 | using boost::phoenix::arg_names::arg1; | |
196 | int array[] = {1,2,3}; | |
197 | const std::set<int> test_set(array, array + 3); | |
198 | BOOST_TEST(boost::phoenix::lower_bound(arg1, 2)(array) == array + 1); | |
199 | BOOST_TEST(boost::phoenix::lower_bound(arg1, 2)(test_set) == test_set.lower_bound(2)); | |
200 | ||
201 | int array2[] = {3,2,1}; | |
202 | const std::set<int, std::greater<int> > test_set2(array2, array2 + 3); | |
203 | BOOST_TEST(boost::phoenix::lower_bound(arg1, 2, std::greater<int>())(array2) == | |
204 | array2 + 1); | |
205 | BOOST_TEST(boost::phoenix::lower_bound(arg1, 2, std::greater<int>())(test_set2) == | |
206 | test_set2.lower_bound(2)); | |
207 | return; | |
208 | } | |
209 | ||
210 | void upper_bound_test() | |
211 | { | |
212 | using boost::phoenix::arg_names::arg1; | |
213 | int array[] = {1,2,3}; | |
214 | const std::set<int> test_set(array, array + 3); | |
215 | BOOST_TEST(upper_bound(arg1, 2)(array) == array + 2); | |
216 | BOOST_TEST(upper_bound(arg1, 2)(test_set) == test_set.upper_bound(2)); | |
217 | ||
218 | int array2[] = {3,2,1}; | |
219 | const std::set<int, std::greater<int> > test_set2(array2, array2 + 3); | |
220 | BOOST_TEST(boost::phoenix::upper_bound(arg1, 2, std::greater<int>())(array2) == | |
221 | array2 + 2); | |
222 | BOOST_TEST(boost::phoenix::upper_bound(arg1, 2, std::greater<int>())(test_set2) == | |
223 | test_set2.upper_bound(2)); | |
224 | return; | |
225 | } | |
226 | ||
227 | void equal_range_test() | |
228 | { | |
229 | using boost::phoenix::arg_names::arg1; | |
230 | int array[] = {1,2,2,3}; | |
231 | const std::set<int> test_set(array, array + 4); | |
232 | BOOST_TEST(boost::phoenix::equal_range(arg1, 2)(array).first == | |
233 | array + 1); | |
234 | BOOST_TEST(boost::phoenix::equal_range(arg1, 2)(array).second == | |
235 | array + 3); | |
236 | ||
237 | BOOST_TEST(boost::phoenix::equal_range(arg1, 2)(test_set).first == | |
238 | test_set.equal_range(2).first); | |
239 | BOOST_TEST(boost::phoenix::equal_range(arg1, 2)(test_set).second == | |
240 | test_set.equal_range(2).second); | |
241 | ||
242 | int array2[] = {3,2,2,1}; | |
243 | const std::set<int, std::greater<int> > test_set2(array2, array2 + 4); | |
244 | BOOST_TEST(boost::phoenix::equal_range(arg1, 2, std::greater<int>())(array2).first == | |
245 | array2 + 1); | |
246 | BOOST_TEST(boost::phoenix::equal_range(arg1, 2, std::greater<int>())(array2).second == | |
247 | array2 + 3); | |
248 | ||
249 | BOOST_TEST(boost::phoenix::equal_range(arg1, 2, std::greater<int>())(test_set2).first == | |
250 | test_set2.equal_range(2).first); | |
251 | BOOST_TEST(boost::phoenix::equal_range(arg1, 2, std::greater<int>())(test_set2).second == | |
252 | test_set2.equal_range(2).second); | |
253 | ||
254 | return; | |
255 | } | |
256 | ||
257 | void binary_search_test() | |
258 | { | |
259 | using boost::phoenix::arg_names::arg1; | |
260 | int array[] = {1,2,3}; | |
261 | BOOST_TEST(boost::phoenix::binary_search(arg1, 2)(array)); | |
262 | BOOST_TEST(!boost::phoenix::binary_search(arg1, 4)(array)); | |
263 | return; | |
264 | } | |
265 | ||
266 | } | |
267 | ||
268 | int main() | |
269 | { | |
270 | find_test(); | |
271 | find_if_test(); | |
272 | find_end_test(); | |
273 | find_first_of_test(); | |
274 | adjacent_find_test(); | |
275 | count_test(); | |
276 | count_if_test(); | |
277 | distance_test(); | |
278 | mismatch_test(); | |
279 | equal_test(); | |
280 | search_test(); | |
281 | lower_bound_test(); | |
282 | upper_bound_test(); | |
283 | equal_range_test(); | |
284 | binary_search_test(); | |
285 | return boost::report_errors(); | |
286 | } |