]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/iostreams/test/execute_test.cpp
2 * Distributed under the Boost Software License, Version 1.0.(See accompanying
3 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
5 * See http://www.boost.org/libs/iostreams for documentation.
7 * Tests the function templates boost::iostreams::detail::execute_all and
8 * boost::iostreams::detail::execute_foreach
10 * File: libs/iostreams/test/execute_test.cpp
11 * Date: Thu Dec 06 13:21:54 MST 2007
12 * Copyright: 2007-2008 CodeRage, LLC
13 * Author: Jonathan Turkanis
14 * Contact: turkanis at coderage dot com
17 #include <boost/iostreams/detail/execute.hpp>
18 #include <boost/test/test_tools.hpp>
19 #include <boost/test/unit_test.hpp>
22 using namespace boost
;
23 using namespace boost::iostreams
;
24 using namespace boost::iostreams::detail
;
25 using boost::unit_test::test_suite
;
27 // Function object that sets a boolean flag and returns a value
28 // specified at construction
29 template<typename Result
>
32 typedef Result result_type
;
33 explicit operation(Result r
, bool& executed
)
34 : r_(r
), executed_(executed
)
36 Result
operator()() const
42 operation
& operator=(const operation
&);
47 // Specialization for void return
49 class operation
<void> {
51 typedef void result_type
;
52 explicit operation(bool& executed
) : executed_(executed
) { }
53 void operator()() const { executed_
= true; }
55 operation
& operator=(const operation
&);
59 // Simple exception class with error code built in to type
63 // Function object that sets a boolean flag and throws an exception
67 typedef void result_type
;
68 explicit thrower(bool& executed
) : executed_(executed
) { }
69 void operator()() const
75 thrower
& operator=(const thrower
&);
79 // Function object for use by foreach_test
82 typedef void result_type
;
83 explicit foreach_func(int& count
) : count_(count
) { }
84 void operator()(int x
) const
88 case 0: throw error
<0>();
89 case 1: throw error
<1>();
90 case 2: throw error
<2>();
91 case 3: throw error
<3>();
92 case 4: throw error
<4>();
93 case 5: throw error
<5>();
94 case 6: throw error
<6>();
95 case 7: throw error
<7>();
96 case 8: throw error
<8>();
97 case 9: throw error
<9>();
103 foreach_func
& operator=(const foreach_func
&);
104 int& count_
; // Number of times operator() has been called
109 // Test returning an int
111 bool executed
= false;
112 BOOST_CHECK(execute_all(operation
<int>(9, executed
)) == 9);
113 BOOST_CHECK(executed
);
116 // Test returning void
118 bool executed
= false;
119 execute_all(operation
<void>(executed
));
120 BOOST_CHECK(executed
);
123 // Test returning an int with one cleanup operation
125 bool executed
= false, cleaned_up
= false;
128 operation
<int>(9, executed
),
129 operation
<void>(cleaned_up
)
132 BOOST_CHECK(executed
&& cleaned_up
);
135 // Test returning void with one cleanup operation
137 bool executed
= false, cleaned_up
= false;
139 operation
<void>(executed
),
140 operation
<void>(cleaned_up
)
142 BOOST_CHECK(executed
&& cleaned_up
);
145 // Test returning an int with two cleanup operations
147 bool executed
= false, cleaned_up1
= false, cleaned_up2
= false;
150 operation
<int>(9, executed
),
151 operation
<void>(cleaned_up1
),
152 operation
<void>(cleaned_up2
)
155 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
);
158 // Test returning void with two cleanup operations
160 bool executed
= false, cleaned_up1
= false, cleaned_up2
= false;
162 operation
<void>(executed
),
163 operation
<void>(cleaned_up1
),
164 operation
<void>(cleaned_up2
)
166 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
);
169 // Test returning an int with three cleanup operations
171 bool executed
= false, cleaned_up1
= false,
172 cleaned_up2
= false, cleaned_up3
= false;
175 operation
<int>(9, executed
),
176 operation
<void>(cleaned_up1
),
177 operation
<void>(cleaned_up2
),
178 operation
<void>(cleaned_up3
)
181 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
&& cleaned_up3
);
184 // Test returning void with three cleanup operations
186 bool executed
= false, cleaned_up1
= false,
187 cleaned_up2
= false, cleaned_up3
= false;
189 operation
<void>(executed
),
190 operation
<void>(cleaned_up1
),
191 operation
<void>(cleaned_up2
),
192 operation
<void>(cleaned_up3
)
194 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
&& cleaned_up3
);
198 void operation_throws_test()
200 // Test primary operation throwing with no cleanup operations
202 bool executed
= false;
204 execute_all(thrower
<0>(executed
)),
207 BOOST_CHECK(executed
);
210 // Test primary operation throwing with one cleanup operation
212 bool executed
= false, cleaned_up
= false;
215 thrower
<0>(executed
),
216 operation
<void>(cleaned_up
)
220 BOOST_CHECK(executed
&& cleaned_up
);
223 // Test primary operation throwing with two cleanup operations
225 bool executed
= false, cleaned_up1
= false, cleaned_up2
= false;
228 thrower
<0>(executed
),
229 operation
<void>(cleaned_up1
),
230 operation
<void>(cleaned_up2
)
234 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
);
237 // Test primary operation throwing with three cleanup operations
239 bool executed
= false, cleaned_up1
= false,
240 cleaned_up2
= false, cleaned_up3
= false;
243 thrower
<0>(executed
),
244 operation
<void>(cleaned_up1
),
245 operation
<void>(cleaned_up2
),
246 operation
<void>(cleaned_up3
)
250 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
&& cleaned_up3
);
254 void cleanup_throws_test()
256 // Test single cleanup operation that throws
258 bool executed
= false, cleaned_up
= false;
261 operation
<void>(executed
),
262 thrower
<1>(cleaned_up
)
266 BOOST_CHECK(executed
&& cleaned_up
);
269 // Test fist of two cleanup operations throwing
271 bool executed
= false, cleaned_up1
= false, cleaned_up2
= false;
274 operation
<void>(executed
),
275 thrower
<1>(cleaned_up1
),
276 operation
<void>(cleaned_up2
)
280 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
);
283 // Test second of two cleanup operations throwing
285 bool executed
= false, cleaned_up1
= false, cleaned_up2
= false;
288 operation
<void>(executed
),
289 operation
<void>(cleaned_up1
),
290 thrower
<2>(cleaned_up2
)
294 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
);
297 // Test first of three cleanup operations throwing
299 bool executed
= false, cleaned_up1
= false,
300 cleaned_up2
= false, cleaned_up3
= false;
303 operation
<void>(executed
),
304 thrower
<1>(cleaned_up1
),
305 operation
<void>(cleaned_up2
),
306 operation
<void>(cleaned_up3
)
310 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
&& cleaned_up3
);
313 // Test second of three cleanup operations throwing
315 bool executed
= false, cleaned_up1
= false,
316 cleaned_up2
= false, cleaned_up3
= false;
319 operation
<void>(executed
),
320 operation
<void>(cleaned_up1
),
321 thrower
<2>(cleaned_up2
),
322 operation
<void>(cleaned_up3
)
326 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
&& cleaned_up3
);
329 // Test third of three cleanup operations throwing
331 bool executed
= false, cleaned_up1
= false,
332 cleaned_up2
= false, cleaned_up3
= false;
335 operation
<void>(executed
),
336 operation
<void>(cleaned_up1
),
337 operation
<void>(cleaned_up2
),
338 thrower
<3>(cleaned_up3
)
342 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
&& cleaned_up3
);
346 void multiple_exceptions_test()
348 // Test primary operation and cleanup operation throwing
350 bool executed
= false, cleaned_up
= false;
353 thrower
<0>(executed
),
354 thrower
<1>(cleaned_up
)
358 BOOST_CHECK(executed
&& cleaned_up
);
361 // Test primary operation and first of two cleanup operations throwing
363 bool executed
= false, cleaned_up1
= false, cleaned_up2
= false;
366 thrower
<0>(executed
),
367 thrower
<1>(cleaned_up1
),
368 operation
<void>(cleaned_up2
)
372 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
);
375 // Test primary operation and second of two cleanup operations throwing
377 bool executed
= false, cleaned_up1
= false, cleaned_up2
= false;
380 thrower
<0>(executed
),
381 operation
<void>(cleaned_up1
),
382 thrower
<2>(cleaned_up2
)
386 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
);
389 // Test two cleanup operations throwing
391 bool executed
= false, cleaned_up1
= false, cleaned_up2
= false;
394 operation
<void>(executed
),
395 thrower
<1>(cleaned_up1
),
396 thrower
<2>(cleaned_up2
)
400 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
);
403 // Test primary operation and first of three cleanup operations throwing
405 bool executed
= false, cleaned_up1
= false,
406 cleaned_up2
= false, cleaned_up3
= false;
409 thrower
<0>(executed
),
410 thrower
<1>(cleaned_up1
),
411 operation
<void>(cleaned_up2
),
412 operation
<void>(cleaned_up3
)
416 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
&& cleaned_up3
);
419 // Test primary operation and second of three cleanup operations throwing
421 bool executed
= false, cleaned_up1
= false,
422 cleaned_up2
= false, cleaned_up3
= false;
425 thrower
<0>(executed
),
426 operation
<void>(cleaned_up1
),
427 thrower
<2>(cleaned_up2
),
428 operation
<void>(cleaned_up3
)
432 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
&& cleaned_up3
);
435 // Test primary operation and third of three cleanup operations throwing
437 bool executed
= false, cleaned_up1
= false,
438 cleaned_up2
= false, cleaned_up3
= false;
441 thrower
<0>(executed
),
442 operation
<void>(cleaned_up1
),
443 operation
<void>(cleaned_up2
),
444 thrower
<3>(cleaned_up3
)
448 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
&& cleaned_up3
);
451 // Test first and second of three cleanup operations throwing
453 bool executed
= false, cleaned_up1
= false,
454 cleaned_up2
= false, cleaned_up3
= false;
457 operation
<void>(executed
),
458 thrower
<1>(cleaned_up1
),
459 thrower
<2>(cleaned_up2
),
460 operation
<void>(cleaned_up3
)
464 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
&& cleaned_up3
);
467 // Test first and third of three cleanup operations throwing
469 bool executed
= false, cleaned_up1
= false,
470 cleaned_up2
= false, cleaned_up3
= false;
473 operation
<void>(executed
),
474 thrower
<1>(cleaned_up1
),
475 operation
<void>(cleaned_up2
),
476 thrower
<3>(cleaned_up3
)
480 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
&& cleaned_up3
);
483 // Test second and third of three cleanup operations throwing
485 bool executed
= false, cleaned_up1
= false,
486 cleaned_up2
= false, cleaned_up3
= false;
489 operation
<void>(executed
),
490 operation
<void>(cleaned_up1
),
491 thrower
<2>(cleaned_up2
),
492 thrower
<3>(cleaned_up3
)
496 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
&& cleaned_up3
);
499 // Test three cleanup operations throwing
501 bool executed
= false, cleaned_up1
= false,
502 cleaned_up2
= false, cleaned_up3
= false;
505 operation
<void>(executed
),
506 thrower
<1>(cleaned_up1
),
507 thrower
<2>(cleaned_up2
),
508 thrower
<3>(cleaned_up3
)
512 BOOST_CHECK(executed
&& cleaned_up1
&& cleaned_up2
&& cleaned_up3
);
516 #define ARRAY_SIZE(ar) (sizeof(ar) / sizeof(ar[0]))
520 // Test case where neither of two operations throws
523 int seq
[] = {-1, -1};
524 BOOST_CHECK_NO_THROW(
525 execute_foreach(seq
, seq
+ ARRAY_SIZE(seq
), foreach_func(count
))
527 BOOST_CHECK(count
== ARRAY_SIZE(seq
));
530 // Test case where first of two operations throws
535 execute_foreach(seq
, seq
+ ARRAY_SIZE(seq
), foreach_func(count
)),
538 BOOST_CHECK(count
== ARRAY_SIZE(seq
));
541 // Test case where second of two operations throws
546 execute_foreach(seq
, seq
+ ARRAY_SIZE(seq
), foreach_func(count
)),
549 BOOST_CHECK(count
== ARRAY_SIZE(seq
));
552 // Test case where both of two operations throw
557 execute_foreach(seq
, seq
+ ARRAY_SIZE(seq
), foreach_func(count
)),
560 BOOST_CHECK(count
== ARRAY_SIZE(seq
));
563 // Test case where none of three operations throws
566 int seq
[] = {-1, -1, -1};
567 BOOST_CHECK_NO_THROW(
568 execute_foreach(seq
, seq
+ ARRAY_SIZE(seq
), foreach_func(count
))
570 BOOST_CHECK(count
== ARRAY_SIZE(seq
));
573 // Test case where first of three operations throw
576 int seq
[] = {0, -1, -1};
578 execute_foreach(seq
, seq
+ ARRAY_SIZE(seq
), foreach_func(count
)),
581 BOOST_CHECK(count
== ARRAY_SIZE(seq
));
584 // Test case where second of three operations throw
587 int seq
[] = {-1, 1, -1};
589 execute_foreach(seq
, seq
+ ARRAY_SIZE(seq
), foreach_func(count
)),
592 BOOST_CHECK(count
== ARRAY_SIZE(seq
));
595 // Test case where third of three operations throw
598 int seq
[] = {-1, -1, 2};
600 execute_foreach(seq
, seq
+ ARRAY_SIZE(seq
), foreach_func(count
)),
603 BOOST_CHECK(count
== ARRAY_SIZE(seq
));
606 // Test case where first and second of three operations throw
609 int seq
[] = {0, 1, -1};
611 execute_foreach(seq
, seq
+ ARRAY_SIZE(seq
), foreach_func(count
)),
614 BOOST_CHECK(count
== ARRAY_SIZE(seq
));
617 // Test case where first and third of three operations throw
620 int seq
[] = {0, -1, 2};
622 execute_foreach(seq
, seq
+ ARRAY_SIZE(seq
), foreach_func(count
)),
625 BOOST_CHECK(count
== ARRAY_SIZE(seq
));
628 // Test case where second and third of three operations throw
631 int seq
[] = {-1, 1, 2};
633 execute_foreach(seq
, seq
+ ARRAY_SIZE(seq
), foreach_func(count
)),
636 BOOST_CHECK(count
== ARRAY_SIZE(seq
));
639 // Test case where three of three operations throw
642 int seq
[] = {0, 1, 2};
644 execute_foreach(seq
, seq
+ ARRAY_SIZE(seq
), foreach_func(count
)),
647 BOOST_CHECK(count
== ARRAY_SIZE(seq
));
651 test_suite
* init_unit_test_suite(int, char* [])
653 test_suite
* test
= BOOST_TEST_SUITE("execute test");
654 test
->add(BOOST_TEST_CASE(&success_test
));
655 test
->add(BOOST_TEST_CASE(&operation_throws_test
));
656 test
->add(BOOST_TEST_CASE(&cleanup_throws_test
));
657 test
->add(BOOST_TEST_CASE(&multiple_exceptions_test
));
658 test
->add(BOOST_TEST_CASE(&foreach_test
));