1 // switch_test.cpp -- The Boost Lambda Library --------------------------
2 //
3 // Copyright (C) 2000-2003 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
4 // Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
5 //
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10 // For more information, see www.boost.org
11
12 // -----------------------------------------------------------------------
13
14
15 #include <boost/test/minimal.hpp> // see "Header Implementation Option"
16
17
18 #include "boost/lambda/lambda.hpp"
19 #include "boost/lambda/if.hpp"
20 #include "boost/lambda/switch.hpp"
21
22 #include <iostream>
23 #include <algorithm>
24 #include <vector>
25 #include <string>
26
27
28
29 // Check that elements 0 -- index are 1, and the rest are 0
check(const std::vector<int> & v,int index)30 bool check(const std::vector<int>& v, int index) {
31 using namespace boost::lambda;
32 int counter = 0;
33 std::vector<int>::const_iterator
34 result = std::find_if(v.begin(), v.end(),
35 ! if_then_else_return(
36 var(counter)++ <= index,
37 _1 == 1,
38 _1 == 0)
39 );
40 return result == v.end();
41 }
42
43
44
do_switch_no_defaults_tests()45 void do_switch_no_defaults_tests() {
46
47 using namespace boost::lambda;
48
49 int i = 0;
50 std::vector<int> v,w;
51
52 // elements from 0 to 9
53 std::generate_n(std::back_inserter(v),
54 10,
55 var(i)++);
56 std::fill_n(std::back_inserter(w), 10, 0);
57
58 // ---
59 std::for_each(v.begin(), v.end(),
60 switch_statement(
61 _1,
62 case_statement<0>(++var(w[0]))
63 )
64 );
65
66 BOOST_CHECK(check(w, 0));
67 std::fill_n(w.begin(), 10, 0);
68
69 // ---
70 std::for_each(v.begin(), v.end(),
71 switch_statement(
72 _1,
73 case_statement<0>(++var(w[0])),
74 case_statement<1>(++var(w[1]))
75 )
76 );
77
78 BOOST_CHECK(check(w, 1));
79 std::fill_n(w.begin(), 10, 0);
80
81 // ---
82 std::for_each(v.begin(), v.end(),
83 switch_statement(
84 _1,
85 case_statement<0>(++var(w[0])),
86 case_statement<1>(++var(w[1])),
87 case_statement<2>(++var(w[2]))
88 )
89 );
90
91 BOOST_CHECK(check(w, 2));
92 std::fill_n(w.begin(), 10, 0);
93
94 // ---
95 std::for_each(v.begin(), v.end(),
96 switch_statement(
97 _1,
98 case_statement<0>(++var(w[0])),
99 case_statement<1>(++var(w[1])),
100 case_statement<2>(++var(w[2])),
101 case_statement<3>(++var(w[3]))
102 )
103 );
104
105 BOOST_CHECK(check(w, 3));
106 std::fill_n(w.begin(), 10, 0);
107
108 // ---
109 std::for_each(v.begin(), v.end(),
110 switch_statement(
111 _1,
112 case_statement<0>(++var(w[0])),
113 case_statement<1>(++var(w[1])),
114 case_statement<2>(++var(w[2])),
115 case_statement<3>(++var(w[3])),
116 case_statement<4>(++var(w[4]))
117 )
118 );
119
120 BOOST_CHECK(check(w, 4));
121 std::fill_n(w.begin(), 10, 0);
122
123 // ---
124 std::for_each(v.begin(), v.end(),
125 switch_statement(
126 _1,
127 case_statement<0>(++var(w[0])),
128 case_statement<1>(++var(w[1])),
129 case_statement<2>(++var(w[2])),
130 case_statement<3>(++var(w[3])),
131 case_statement<4>(++var(w[4])),
132 case_statement<5>(++var(w[5]))
133 )
134 );
135
136 BOOST_CHECK(check(w, 5));
137 std::fill_n(w.begin(), 10, 0);
138
139 // ---
140 std::for_each(v.begin(), v.end(),
141 switch_statement(
142 _1,
143 case_statement<0>(++var(w[0])),
144 case_statement<1>(++var(w[1])),
145 case_statement<2>(++var(w[2])),
146 case_statement<3>(++var(w[3])),
147 case_statement<4>(++var(w[4])),
148 case_statement<5>(++var(w[5])),
149 case_statement<6>(++var(w[6]))
150 )
151 );
152
153 BOOST_CHECK(check(w, 6));
154 std::fill_n(w.begin(), 10, 0);
155
156 // ---
157 std::for_each(v.begin(), v.end(),
158 switch_statement(
159 _1,
160 case_statement<0>(++var(w[0])),
161 case_statement<1>(++var(w[1])),
162 case_statement<2>(++var(w[2])),
163 case_statement<3>(++var(w[3])),
164 case_statement<4>(++var(w[4])),
165 case_statement<5>(++var(w[5])),
166 case_statement<6>(++var(w[6])),
167 case_statement<7>(++var(w[7]))
168 )
169 );
170
171 BOOST_CHECK(check(w, 7));
172 std::fill_n(w.begin(), 10, 0);
173
174 // ---
175 std::for_each(v.begin(), v.end(),
176 switch_statement(
177 _1,
178 case_statement<0>(++var(w[0])),
179 case_statement<1>(++var(w[1])),
180 case_statement<2>(++var(w[2])),
181 case_statement<3>(++var(w[3])),
182 case_statement<4>(++var(w[4])),
183 case_statement<5>(++var(w[5])),
184 case_statement<6>(++var(w[6])),
185 case_statement<7>(++var(w[7])),
186 case_statement<8>(++var(w[8]))
187 )
188 );
189
190 BOOST_CHECK(check(w, 8));
191 std::fill_n(w.begin(), 10, 0);
192
193 }
194
195
do_switch_yes_defaults_tests()196 void do_switch_yes_defaults_tests() {
197
198 using namespace boost::lambda;
199
200 int i = 0;
201 std::vector<int> v,w;
202
203 // elements from 0 to 9
204 std::generate_n(std::back_inserter(v),
205 10,
206 var(i)++);
207 std::fill_n(std::back_inserter(w), 10, 0);
208
209 int default_count;
210 // ---
211 default_count = 0;
212 std::for_each(v.begin(), v.end(),
213 switch_statement(
214 _1,
215 default_statement(++var(default_count))
216 )
217 );
218
219 BOOST_CHECK(check(w, -1));
220 BOOST_CHECK(default_count == 10);
221 std::fill_n(w.begin(), 10, 0);
222
223 // ---
224 default_count = 0;
225 std::for_each(v.begin(), v.end(),
226 switch_statement(
227 _1,
228 case_statement<0>(++var(w[0])),
229 default_statement(++var(default_count))
230 )
231 );
232
233 BOOST_CHECK(check(w, 0));
234 BOOST_CHECK(default_count == 9);
235 std::fill_n(w.begin(), 10, 0);
236
237 // ---
238 default_count = 0;
239 std::for_each(v.begin(), v.end(),
240 switch_statement(
241 _1,
242 case_statement<0>(++var(w[0])),
243 case_statement<1>(++var(w[1])),
244 default_statement(++var(default_count))
245 )
246 );
247
248 BOOST_CHECK(check(w, 1));
249 BOOST_CHECK(default_count == 8);
250 std::fill_n(w.begin(), 10, 0);
251
252 // ---
253 default_count = 0;
254 std::for_each(v.begin(), v.end(),
255 switch_statement(
256 _1,
257 case_statement<0>(++var(w[0])),
258 case_statement<1>(++var(w[1])),
259 case_statement<2>(++var(w[2])),
260 default_statement(++var(default_count))
261 )
262 );
263
264 BOOST_CHECK(check(w, 2));
265 BOOST_CHECK(default_count == 7);
266 std::fill_n(w.begin(), 10, 0);
267
268 // ---
269 default_count = 0;
270 std::for_each(v.begin(), v.end(),
271 switch_statement(
272 _1,
273 case_statement<0>(++var(w[0])),
274 case_statement<1>(++var(w[1])),
275 case_statement<2>(++var(w[2])),
276 case_statement<3>(++var(w[3])),
277 default_statement(++var(default_count))
278 )
279 );
280
281 BOOST_CHECK(check(w, 3));
282 BOOST_CHECK(default_count == 6);
283 std::fill_n(w.begin(), 10, 0);
284
285 // ---
286 default_count = 0;
287 std::for_each(v.begin(), v.end(),
288 switch_statement(
289 _1,
290 case_statement<0>(++var(w[0])),
291 case_statement<1>(++var(w[1])),
292 case_statement<2>(++var(w[2])),
293 case_statement<3>(++var(w[3])),
294 case_statement<4>(++var(w[4])),
295 default_statement(++var(default_count))
296 )
297 );
298
299 BOOST_CHECK(check(w, 4));
300 BOOST_CHECK(default_count == 5);
301 std::fill_n(w.begin(), 10, 0);
302
303 // ---
304 default_count = 0;
305 std::for_each(v.begin(), v.end(),
306 switch_statement(
307 _1,
308 case_statement<0>(++var(w[0])),
309 case_statement<1>(++var(w[1])),
310 case_statement<2>(++var(w[2])),
311 case_statement<3>(++var(w[3])),
312 case_statement<4>(++var(w[4])),
313 case_statement<5>(++var(w[5])),
314 default_statement(++var(default_count))
315 )
316 );
317
318 BOOST_CHECK(check(w, 5));
319 BOOST_CHECK(default_count == 4);
320 std::fill_n(w.begin(), 10, 0);
321
322 // ---
323 default_count = 0;
324 std::for_each(v.begin(), v.end(),
325 switch_statement(
326 _1,
327 case_statement<0>(++var(w[0])),
328 case_statement<1>(++var(w[1])),
329 case_statement<2>(++var(w[2])),
330 case_statement<3>(++var(w[3])),
331 case_statement<4>(++var(w[4])),
332 case_statement<5>(++var(w[5])),
333 case_statement<6>(++var(w[6])),
334 default_statement(++var(default_count))
335 )
336 );
337
338 BOOST_CHECK(check(w, 6));
339 BOOST_CHECK(default_count == 3);
340 std::fill_n(w.begin(), 10, 0);
341
342 // ---
343 default_count = 0;
344 std::for_each(v.begin(), v.end(),
345 switch_statement(
346 _1,
347 case_statement<0>(++var(w[0])),
348 case_statement<1>(++var(w[1])),
349 case_statement<2>(++var(w[2])),
350 case_statement<3>(++var(w[3])),
351 case_statement<4>(++var(w[4])),
352 case_statement<5>(++var(w[5])),
353 case_statement<6>(++var(w[6])),
354 case_statement<7>(++var(w[7])),
355 default_statement(++var(default_count))
356 )
357 );
358
359 BOOST_CHECK(check(w, 7));
360 BOOST_CHECK(default_count == 2);
361 std::fill_n(w.begin(), 10, 0);
362
363 }
364
test_empty_cases()365 void test_empty_cases() {
366
367 using namespace boost::lambda;
368
369 // ---
370 switch_statement(
371 _1,
372 default_statement()
373 )(make_const(1));
374
375 switch_statement(
376 _1,
377 case_statement<1>()
378 )(make_const(1));
379
380 }
381
test_main(int,char * [])382 int test_main(int, char* []) {
383
384 do_switch_no_defaults_tests();
385 do_switch_yes_defaults_tests();
386
387 test_empty_cases();
388
389 return EXIT_SUCCESS;
390
391 }
392
393