Release 1.39.0
[boost.git] / Boost_1_39_0 / libs / spirit / phoenix / test / container / container_tests.hpp
blob38ba1bd94d3ffa4995807477106822ef5bd22f57
1 /*=============================================================================
2 Copyright (c) 2004 Angus Leeming
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 #if !defined(CONTAINER_TESTS_HPP)
8 #define CONTAINER_TESTS_HPP
10 #include <boost/detail/lightweight_test.hpp>
11 #include <boost/spirit/include/phoenix_core.hpp>
12 #include <boost/spirit/home/phoenix/stl/container/container.hpp>
14 #include <iostream>
15 #include <typeinfo>
16 #include <deque>
17 #include <list>
18 #include <map>
19 #include <vector>
20 #include <utility>
22 #ifdef BOOST_MSVC
23 #pragma warning(disable : 4800)
24 #endif
26 using std::cerr;
27 namespace phx = boost::phoenix;
29 std::deque<int> const build_deque();
30 std::list<int> const build_list();
31 std::map<int, int> const build_map();
32 std::multimap<int, int> const build_multimap();
33 std::vector<int> const build_vector();
35 inline bool
36 test(bool fail)
38 BOOST_TEST(!fail);
39 return fail;
42 template <typename Container>
43 void test_assign(Container c)
45 using phx::arg_names::arg1;
46 using phx::assign;
48 typename Container::size_type count = 2;
49 typename Container::const_iterator first = c.begin();
50 typename Container::const_iterator second = first;
51 typename Container::value_type value = *first;
53 assign(arg1, count, value)(c);
55 // iterators may be invalidated!
56 first = c.begin();
57 second = first;
59 std::advance(second, 1);
60 if (test(*first != *second)) {
61 cerr << "Failed " << typeid(Container).name() << " test_assign 1\n";
62 return;
64 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
65 // Should not --- does not, Yay! --- compile.
66 Container const const_c = c;
67 assign(const_c, count, value);
68 #endif
71 template <typename Container>
72 void test_assign2(Container c)
74 using phx::arg_names::arg1;
75 using phx::arg_names::arg2;
76 using phx::arg_names::arg3;
77 using phx::assign;
79 Container c2 = c;
80 typename Container::const_iterator first = c2.begin();
81 typename Container::const_iterator last = c2.end();
82 typename Container::size_type size = c2.size();
84 c.clear();
85 assign(arg1, arg2, arg3)(c, first, last);
86 if (test(c.size() != size)) {
87 cerr << "Failed " << typeid(Container).name()
88 << " test_assign2 1\n"
89 << "size == " << c.size() << '\n';
90 return;
93 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
94 // Should not --- does not, Yay! --- compile.
95 Container const const_c = c;
96 assign(const_c, first, second);
97 #endif
100 template <typename Container>
101 void test_at(Container c)
103 using phx::arg_names::arg1;
104 using phx::at;
106 typename Container::reference r1 = at(arg1, 0)(c);
107 if (test(r1 != c.at(0))) {
108 cerr << "Failed " << typeid(Container).name() << " test_at 1\n";
109 return;
112 typename Container::const_reference r2 = at(arg1, 0)(c);
113 if (test(r2 != c.at(0))) {
114 cerr << "Failed " << typeid(Container).name() << " test_at 2\n";
115 return;
118 Container const const_c = c;
119 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
120 // Should not --- does not, Yay! --- compile.
121 typename Container::reference r3 = at(arg1, 0)(const_c);
122 #endif
124 typename Container::const_reference r4 = at(arg1, 0)(const_c);
125 if (test(r4 != c.at(0))) {
126 cerr << "Failed " << typeid(Container).name() << " test_at 4\n";
127 return;
131 template <typename Container>
132 void test_back(Container c)
134 using phx::arg_names::arg1;
135 using phx::back;
137 typename Container::reference r1 = back(arg1)(c);
138 if (test(r1 != c.back())) {
139 cerr << "Failed " << typeid(Container).name() << " test_back 1\n";
140 return;
142 typename Container::const_reference r2 = back(arg1)(c);
143 if (test(r2 != c.back())) {
144 cerr << "Failed " << typeid(Container).name() << " test_back 2\n";
145 return;
148 Container const const_c = c;
149 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
150 // Should not --- does not, Yay! --- compile.
151 typename Container::reference r3 = back(arg1)(const_c);
152 #endif
154 typename Container::const_reference r4 = back(arg1)(const_c);
155 if (test(r4 != c.back())) {
156 cerr << "Failed " << typeid(Container).name() << " test_back 4\n";
157 return;
161 template <typename Container>
162 void test_begin(Container c)
164 using phx::arg_names::arg1;
165 using phx::begin;
167 typename Container::iterator it1 = begin(arg1)(c);
168 if (test(it1 != c.begin())) {
169 cerr << "Failed " << typeid(Container).name() << " test_begin 1\n";
170 return;
172 typename Container::const_iterator it2 = begin(arg1)(c);
173 if (test(it2 != c.begin())) {
174 cerr << "Failed " << typeid(Container).name() << " test_begin 2\n";
175 return;
178 Container const const_c = c;
179 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
180 // Should not --- does not, Yay! --- compile.
181 typename Container::iterator it3 = begin(arg1)(const_c);
182 #endif
184 typename Container::const_iterator it4 = begin(arg1)(const_c);
185 if (test(it4 != const_c.begin())) {
186 cerr << "Failed " << typeid(Container).name() << " test_begin 4\n";
187 return;
191 template <typename Container>
192 void test_capacity(Container c)
194 using phx::arg_names::arg1;
195 using phx::capacity;
197 typename Container::size_type s1 = capacity(arg1)(c);
198 if (test(s1 != c.capacity())) {
199 cerr << "Failed " << typeid(Container).name() << " test_capacity 1\n";
200 return;
203 Container const const_c = c;
204 typename Container::size_type s2 = capacity(arg1)(const_c);
205 if (test(s2 != const_c.capacity())) {
206 cerr << "Failed " << typeid(Container).name() << " test_capacity 2\n";
207 return;
211 template <typename Container>
212 void test_clear(Container c)
214 using phx::arg_names::arg1;
215 using phx::clear;
217 clear(arg1)(c);
218 if (test(!c.empty())) {
219 cerr << "Failed " << typeid(Container).name() << " test_clear 1\n";
220 return;
223 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
224 Container const const_c = c;
225 clear(arg1)(const_c);
226 #endif
229 template <typename Container>
230 void test_empty(Container c)
232 using phx::arg_names::arg1;
233 using phx::empty;
235 typename Container::size_type s1 = empty(arg1)(c);
236 if (test(bool(s1) != c.empty())) {
237 cerr << "Failed " << typeid(Container).name() << " test_empty 1\n";
238 return;
241 Container const const_c = c;
242 typename Container::size_type s2 = empty(arg1)(const_c);
243 if (test(bool(s2) != const_c.empty())) {
244 cerr << "Failed " << typeid(Container).name() << " test_empty 2\n";
245 return;
249 template <typename Container>
250 void test_end(Container c)
252 using phx::arg_names::arg1;
253 using phx::end;
255 typename Container::iterator it1 = end(arg1)(c);
256 if (test(it1 != c.end())) {
257 cerr << "Failed " << typeid(Container).name() << " test_end 1\n";
258 return;
260 typename Container::const_iterator it2 = end(arg1)(c);
261 if (test(it2 != c.end())) {
262 cerr << "Failed " << typeid(Container).name() << " test_end 2\n";
263 return;
266 Container const const_c = c;
267 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
268 // Should not --- does not, Yay! --- compile.
269 typename Container::iterator it3 = end(arg1)(const_c);
270 #endif
272 typename Container::const_iterator it4 = end(arg1)(const_c);
273 if (test(it4 != const_c.end())) {
274 cerr << "Failed " << typeid(Container).name() << " test_end 4\n";
275 return;
279 template <typename Container>
280 void test_erase(Container c)
282 using phx::arg_names::arg1;
283 using phx::arg_names::arg2;
284 using phx::arg_names::arg3;
285 using phx::erase;
287 Container const const_c = c;
289 typename Container::size_type size = c.size();
290 typename Container::iterator c_begin = c.begin();
291 erase(arg1, arg2)(c, c_begin);
292 if (test(c.size() + 1 != size)) {
293 cerr << "Failed " << typeid(Container).name() << " test_erase 1\n";
294 return;
297 c_begin = c.begin();
298 typename Container::iterator c_end = c.end();
299 erase(arg1, arg2, arg3)(c, c_begin, c_end);
300 if (test(!c.empty())) {
301 cerr << "Failed " << typeid(Container).name() << " test_erase 2\n";
302 return;
305 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
306 erase(arg1, const_c.begin())(const_c);
307 erase(arg1, const_c.begin(), const_c.end())(const_c);
308 #endif
311 template <typename Container>
312 void test_map_erase(Container c)
314 test_erase(c);
315 if (boost::report_errors() != 0)
316 return;
318 using phx::arg_names::arg1;
319 using phx::arg_names::arg2;
320 using phx::erase;
322 typename Container::value_type const value = *c.begin();
323 typename Container::key_type const key = value.first;
324 typename Container::size_type const removed =
325 erase(arg1, arg2)(c, key);
326 if (test(removed != 1)) {
327 cerr << "Failed " << typeid(Container).name() << " test_map_erase 1\n";
328 return;
332 template <typename Container>
333 void test_front(Container c)
335 using phx::arg_names::arg1;
336 using phx::front;
338 typename Container::reference r1 = front(arg1)(c);
339 if (test(r1 != c.front())) {
340 cerr << "Failed " << typeid(Container).name() << " test_front 1\n";
341 return;
343 typename Container::const_reference r2 = front(arg1)(c);
344 if (test(r2 != c.front())) {
345 cerr << "Failed " << typeid(Container).name() << " test_front 2\n";
346 return;
349 Container const const_c = c;
350 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
351 // Should not --- does not, Yay! --- compile.
352 typename Container::reference r3 = front(arg1)(const_c);
353 #endif
355 typename Container::const_reference r4 = front(arg1)(const_c);
356 if (test(r4 != c.front())) {
357 cerr << "Failed " << typeid(Container).name() << " test_front 4\n";
358 return;
362 template <typename Container>
363 void test_get_allocator(Container c)
365 using phx::arg_names::arg1;
366 using phx::get_allocator;
368 Container const const_c = c;
370 typename Container::allocator_type a1 = get_allocator(arg1)(c);
371 if (test(a1 != c.get_allocator())) {
372 cerr << "Failed " << typeid(Container).name() << " test_get_allocator 1\n";
373 return;
376 typename Container::allocator_type a2 = get_allocator(arg1)(const_c);
377 if (test(a2 != const_c.get_allocator())) {
378 cerr << "Failed " << typeid(Container).name() << " test_get_allocator 2\n";
379 return;
383 template <typename Container>
384 void test_insert(Container c)
386 using phx::arg_names::arg1;
387 using phx::insert;
389 typename Container::value_type const value = *c.begin();
390 typename Container::iterator it = insert(arg1, c.begin(), value)(c);
391 if (test(it != c.begin() || *it != *(++it))) {
392 cerr << "Failed " << typeid(Container).name() << " test_insert 1\n";
393 return;
396 typename Container::size_type size = c.size();
397 insert(arg1, c.begin(), 3, value)(c);
398 if (test(c.size() != size + 3)) {
399 cerr << "Failed " << typeid(Container).name() << " test_insert 2\n";
400 return;
403 Container const const_c = c;
404 size = c.size();
405 insert(arg1, c.begin(), const_c.begin(), const_c.end())(c);
406 if (test(c.size() != 2 * size)) {
407 cerr << "Failed " << typeid(Container).name() << " test_insert 3\n";
408 return;
412 inline void test_map_insert(std::map<int, int> c)
414 using phx::arg_names::arg1;
415 using phx::arg_names::arg2;
416 using phx::arg_names::arg3;
418 typedef std::map<int, int> Map;
420 Map::value_type const value = *c.begin();
421 Map::iterator c_begin = c.begin();
422 // wrapper for
423 // iterator insert(iterator where, const value_type& val);
424 Map::iterator it =
425 phx::insert(arg1, arg2, arg3)(c, c_begin, value);
427 if (test(it != c.begin() /*|| *it != *(++it)*/)) {
428 cerr << "Failed " << typeid(Map).name() << " test_map_insert 1\n";
429 return;
432 // wrapper for
433 // pair<iterator, bool> insert(const value_type& val);
434 Map::value_type const value2(1400, 2200);
435 std::pair<Map::iterator, bool> result =
436 phx::insert(arg1, arg2)(c, value2);
437 if (test(!result.second)) {
438 cerr << "Failed " << typeid(Map).name() << " test_map_insert 2\n";
439 return;
442 // wrapper for
443 // template<class InIt>
444 // void insert(InIt first, InIt last);
445 Map const const_c = build_map();
446 Map::size_type size = c.size();
447 phx::insert(arg1, const_c.begin(), const_c.end())(c);
448 if (test(c.size() != size + const_c.size())) {
449 cerr << "Failed " << typeid(Map).name() << " test_map_insert 3\n";
450 return;
454 inline void test_multimap_insert(std::multimap<int, int> c)
456 using phx::arg_names::arg1;
457 using phx::arg_names::arg2;
458 using phx::arg_names::arg3;
460 typedef std::multimap<int, int> Multimap;
462 Multimap::value_type const value = *c.begin();
463 Multimap::iterator c_begin = c.begin();
464 // wrapper for
465 // iterator insert(iterator where, const value_type& val);
466 Multimap::iterator it =
467 phx::insert(arg1, arg2, arg3)(c, c_begin, value);
469 if (test(it != c.begin() || *it != *(++it))) {
470 cerr << "Failed " << typeid(Multimap).name()
471 << " test_multimap_insert 1\n";
472 return;
475 // wrapper for
476 // iterator insert(const value_type& val);
477 Multimap::value_type const value2(1400, 2200);
478 it = phx::insert(arg1, arg2)(c, value2);
479 if (test(it == c.end())) {
480 cerr << "Failed " << typeid(Multimap).name()
481 << " test_multimap_insert 2\n";
482 return;
485 // wrapper for
486 // template<class InIt>
487 // void insert(InIt first, InIt last);
488 Multimap const const_c = build_multimap();
489 Multimap::size_type size = c.size();
490 phx::insert(arg1, const_c.begin(), const_c.end())(c);
491 if (test(c.size() != size + const_c.size())) {
492 cerr << "Failed " << typeid(Multimap).name()
493 << " test_multimap_insert 3\n";
494 return;
498 template <typename Container>
499 void test_key_comp(Container c)
501 using phx::arg_names::arg1;
502 using phx::key_comp;
504 typename Container::key_compare comp = key_comp(arg1)(c);
506 Container const const_c = c;
507 comp = key_comp(arg1)(const_c);
510 template <typename Container>
511 void test_max_size(Container c)
513 using phx::arg_names::arg1;
514 using phx::max_size;
516 Container const const_c = c;
518 typename Container::size_type s1 = max_size(arg1)(c);
519 if (test(s1 != c.max_size())) {
520 cerr << "Failed " << typeid(Container).name() << " test_max_size 1\n";
521 return;
524 typename Container::size_type s2 = max_size(arg1)(const_c);
525 if (test(s2 != const_c.max_size())) {
526 cerr << "Failed " << typeid(Container).name() << " test_max_size 2\n";
527 return;
531 template <typename Container>
532 void test_pop_back(Container c)
534 using phx::arg_names::arg1;
535 using phx::pop_back;
537 Container const const_c = c;
539 typename Container::size_type size = c.size();
541 pop_back(arg1)(c);
542 if (test(c.size() + 1 != size)) {
543 cerr << "Failed " << typeid(Container).name() << " test_pop_back 1\n";
544 return;
547 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
548 pop_back(arg1)(const_c);
549 #endif
552 template <typename Container>
553 void test_pop_front(Container c)
555 using phx::arg_names::arg1;
556 using phx::pop_front;
558 Container const const_c = c;
560 typename Container::size_type size = c.size();
562 pop_front(arg1)(c);
563 if (test(c.size() + 1 != size)) {
564 cerr << "Failed " << typeid(Container).name() << " test_pop_front 1\n";
565 return;
567 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
568 pop_front(arg1)(const_c);
569 #endif
572 template <typename Container>
573 void test_push_back(Container c)
575 using phx::arg_names::arg1;
576 using phx::arg_names::arg2;
577 using phx::push_back;
579 Container const const_c = c;
581 typename Container::value_type data = *c.begin();
582 typename Container::size_type size = c.size();
583 push_back(arg1, arg2)(c, data);
584 if (test(c.size() != size + 1)) {
585 cerr << "Failed " << typeid(Container).name() << " test_push_back 1\n";
586 return;
588 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
589 push_back(arg1, arg2)(const_c, data);
590 #endif
593 template <typename Container>
594 void test_push_front(Container c)
596 using phx::arg_names::arg1;
597 using phx::arg_names::arg2;
598 using phx::push_front;
600 Container const const_c = c;
602 typename Container::value_type data = *c.begin();
603 typename Container::size_type size = c.size();
604 push_front(arg1, arg2)(c, data);
605 if (test(c.size() != size + 1)) {
606 cerr << "Failed " << typeid(Container).name() << " test_push_front 1\n";
607 return;
609 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
610 push_front(arg1, arg2)(const_c, data);
611 #endif
614 template <typename Container>
615 void test_rbegin(Container c)
617 using phx::arg_names::arg1;
618 using phx::rbegin;
620 typename Container::reverse_iterator it1 = rbegin(arg1)(c);
621 typename Container::reverse_iterator it1_test = c.rbegin();
622 if (test(it1 != it1_test)) {
623 cerr << "Failed " << typeid(Container).name() << " test_rbegin 1\n";
624 return;
626 typename Container::const_reverse_iterator it2 = rbegin(arg1)(c);
627 typename Container::const_reverse_iterator it2_test = c.rbegin();
628 if (test(it2 != it2_test)) {
629 cerr << "Failed " << typeid(Container).name() << " test_rbegin 2\n";
630 return;
633 Container const const_c = c;
634 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
635 // Should not --- does not, Yay! --- compile.
636 typename Container::reverse_iterator it3 = rbegin(arg1)(const_c);
637 #endif
639 typename Container::const_reverse_iterator it4 = rbegin(arg1)(const_c);
640 it2_test = const_c.rbegin();
641 if (test(it4 != it2_test)) {
642 cerr << "Failed " << typeid(Container).name() << " test_rbegin 4\n";
643 return;
647 template <typename Container>
648 void test_rend(Container c)
650 using phx::arg_names::arg1;
651 using phx::rend;
653 typename Container::reverse_iterator it1 = rend(arg1)(c);
654 typename Container::reverse_iterator it1_test = c.rend();
655 if (test(it1 != it1_test)) {
656 cerr << "Failed " << typeid(Container).name() << " test_rend 1\n";
657 return;
659 typename Container::const_reverse_iterator it2 = rend(arg1)(c);
660 typename Container::const_reverse_iterator it2_test = c.rend();
661 if (test(it2 != it2_test)) {
662 cerr << "Failed " << typeid(Container).name() << " test_rend 2\n";
663 return;
666 Container const const_c = c;
667 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
668 // Should not --- does not, Yay! --- compile.
669 typename Container::reverse_iterator it3 = rend(arg1)(const_c);
670 #endif
672 typename Container::const_reverse_iterator it4 = rend(arg1)(const_c);
673 it2_test = const_c.rend();
674 if (test(it4 != it2_test)) {
675 cerr << "Failed " << typeid(Container).name() << " test_rend 4\n";
676 return;
680 template <typename Container>
681 void test_reserve(Container c)
683 using phx::arg_names::arg1;
684 using phx::reserve;
686 Container const const_c = c;
688 typename Container::size_type count = 2 * c.size();
689 reserve(arg1, count)(c);
690 if (test(c.capacity() < count)) {
691 cerr << "Failed " << typeid(Container).name() << " test_reserve 1\n";
692 return;
694 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
695 reserve(arg1, count)(const_c)(const_c);
696 #endif
699 template <typename Container>
700 void test_resize(Container c)
702 using phx::arg_names::arg1;
703 using phx::resize;
705 Container const const_c = c;
707 typename Container::size_type new_size = 2 * c.size();
708 resize(arg1, new_size)(c);
709 if (test(c.size() != new_size)) {
710 cerr << "Failed " << typeid(Container).name() << " test_resize 1\n";
711 return;
714 new_size = 2 * c.size();
715 typename Container::value_type value = *c.begin();
716 resize(arg1, new_size, value)(c);
717 if (test(c.size() != new_size)) {
718 cerr << "Failed " << typeid(Container).name() << " test_resize 2\n";
719 return;
721 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
722 new_size = 2 * const_c.size();
723 resize(arg1, new_size)(const_c);
725 new_size = 2 * const_c.size();
726 resize(arg1, new_size, value)(const_c);
727 #endif
730 template <typename Container>
731 void test_size(Container c)
733 using phx::arg_names::arg1;
734 using phx::size;
736 Container const const_c = c;
738 typename Container::size_type s1 = size(arg1)(c);
739 if (test(s1 != c.size())) {
740 cerr << "Failed " << typeid(Container).name() << " test_size 1\n";
741 return;
744 typename Container::size_type s2 = size(arg1)(const_c);
745 if (test(s2 != const_c.size())) {
746 cerr << "Failed " << typeid(Container).name() << " test_size 2\n";
747 return;
751 template <typename Container>
752 void test_splice(Container c)
754 using phx::arg_names::arg1;
755 using phx::arg_names::arg2;
756 using phx::arg_names::arg3;
757 using phx::arg_names::arg4;
758 using phx::arg_names::arg5;
759 using phx::splice;
761 typename Container::iterator c_end;
762 typename Container::iterator c2_begin;
763 typename Container::iterator c2_end;
764 typename Container::size_type size = c.size();
766 Container const copy = c;
767 Container const copy2 = build_list();
768 Container c2 = copy2;
770 size = c.size();
771 c_end = c.end();
772 splice(arg1, arg2, arg3)(c, c_end, c2);
773 if (test(c.size() != 2 * size)) {
774 cerr << "Failed " << typeid(Container).name() << " test_splice 1\n";
775 return;
778 c = copy;
779 c_end = c.end();
780 c2 = copy2;
781 c2_begin = c2.begin();
782 size = c.size() + 1;
783 splice(arg1, arg2, arg3, arg4)(c, c_end, c2, c2_begin);
784 if (test(c.size() != size)) {
785 cerr << "Failed " << typeid(Container).name() << " test_splice 2\n";
786 return;
789 c = copy;
790 c_end = c.end();
791 c2 = copy2;
792 c2_begin = c2.begin();
793 c2_end = c2.end();
794 size = c.size() + c2.size();
795 splice(arg1, arg2, arg3, arg4, arg5)(c, c_end, c2, c2_begin, c2_end);
796 if (test(c.size() != size)) {
797 cerr << "Failed " << typeid(Container).name() << " test_splice 3\n";
798 return;
802 template <typename Container>
803 void test_value_comp(Container c)
805 using phx::arg_names::arg1;
806 using phx::value_comp;
808 typename Container::value_compare comp = value_comp(arg1)(c);
810 Container const const_c = c;
811 comp = value_comp(arg1)(const_c);
814 #endif