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/phoenix/core.hpp>
12 #include <boost/phoenix/stl/container/container.hpp>
23 #pragma warning(disable : 4800)
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();
42 template <typename Container>
43 void test_assign(Container c)
45 using phx::arg_names::arg1;
47 typename Container::size_type count = 2;
48 typename Container::const_iterator first = c.begin();
49 typename Container::const_iterator second = first;
50 typename Container::value_type value = *first;
52 phx::assign(arg1, count, value)(c);
54 // iterators may be invalidated!
58 std::advance(second, 1);
59 if (test(*first != *second)) {
60 cerr << "Failed " << typeid(Container).name() << " test_assign 1\n";
63 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
64 // Should not --- does not, Yay! --- compile.
65 Container const const_c = c;
66 phx::assign(const_c, count, value);
70 template <typename Container>
71 void test_assign2(Container c)
73 using phx::arg_names::arg1;
74 using phx::arg_names::arg2;
75 using phx::arg_names::arg3;
78 typename Container::const_iterator first = c2.begin();
79 typename Container::const_iterator last = c2.end();
80 typename Container::size_type size = c2.size();
83 phx::assign(arg1, arg2, arg3)(c, first, last);
84 if (test(c.size() != size)) {
85 cerr << "Failed " << typeid(Container).name()
86 << " test_assign2 1\n"
87 << "size == " << c.size() << '\n';
91 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
92 // Should not --- does not, Yay! --- compile.
93 Container const const_c = c;
94 phx::assign(const_c, first, second);
98 template <typename Container>
99 void test_at(Container c)
101 using phx::arg_names::arg1;
104 typename Container::reference r1 = at(arg1, 0)(c);
105 if (test(r1 != c.at(0))) {
106 cerr << "Failed " << typeid(Container).name() << " test_at 1\n";
110 typename Container::const_reference r2 = at(arg1, 0)(c);
111 if (test(r2 != c.at(0))) {
112 cerr << "Failed " << typeid(Container).name() << " test_at 2\n";
116 Container const const_c = c;
117 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
118 // Should not --- does not, Yay! --- compile.
119 typename Container::reference r3 = at(arg1, 0)(const_c);
122 typename Container::const_reference r4 = at(arg1, 0)(const_c);
123 if (test(r4 != c.at(0))) {
124 cerr << "Failed " << typeid(Container).name() << " test_at 4\n";
129 template <typename Container>
130 void test_back(Container c)
132 using phx::arg_names::arg1;
135 typename Container::reference r1 = back(arg1)(c);
136 if (test(r1 != c.back())) {
137 cerr << "Failed " << typeid(Container).name() << " test_back 1\n";
140 typename Container::const_reference r2 = back(arg1)(c);
141 if (test(r2 != c.back())) {
142 cerr << "Failed " << typeid(Container).name() << " test_back 2\n";
146 Container const const_c = c;
147 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
148 // Should not --- does not, Yay! --- compile.
149 typename Container::reference r3 = back(arg1)(const_c);
152 typename Container::const_reference r4 = back(arg1)(const_c);
153 if (test(r4 != c.back())) {
154 cerr << "Failed " << typeid(Container).name() << " test_back 4\n";
159 template <typename Container>
160 void test_begin(Container c)
162 using phx::arg_names::arg1;
165 typename Container::iterator it1 = begin(arg1)(c);
166 if (test(it1 != c.begin())) {
167 cerr << "Failed " << typeid(Container).name() << " test_begin 1\n";
170 typename Container::const_iterator it2 = begin(arg1)(c);
171 if (test(it2 != c.begin())) {
172 cerr << "Failed " << typeid(Container).name() << " test_begin 2\n";
176 Container const const_c = c;
177 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
178 // Should not --- does not, Yay! --- compile.
179 typename Container::iterator it3 = begin(arg1)(const_c);
182 typename Container::const_iterator it4 = begin(arg1)(const_c);
183 if (test(it4 != const_c.begin())) {
184 cerr << "Failed " << typeid(Container).name() << " test_begin 4\n";
189 template <typename Container>
190 void test_capacity(Container c)
192 using phx::arg_names::arg1;
195 typename Container::size_type s1 = capacity(arg1)(c);
196 if (test(s1 != c.capacity())) {
197 cerr << "Failed " << typeid(Container).name() << " test_capacity 1\n";
201 Container const const_c = c;
202 typename Container::size_type s2 = capacity(arg1)(const_c);
203 if (test(s2 != const_c.capacity())) {
204 cerr << "Failed " << typeid(Container).name() << " test_capacity 2\n";
209 template <typename Container>
210 void test_clear(Container c)
212 using phx::arg_names::arg1;
216 if (test(!c.empty())) {
217 cerr << "Failed " << typeid(Container).name() << " test_clear 1\n";
221 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
222 Container const const_c = c;
223 clear(arg1)(const_c);
227 template <typename Container>
228 void test_empty(Container c)
230 using phx::arg_names::arg1;
233 typename Container::size_type s1 = empty(arg1)(c);
234 if (test(bool(s1) != c.empty())) {
235 cerr << "Failed " << typeid(Container).name() << " test_empty 1\n";
239 Container const const_c = c;
240 typename Container::size_type s2 = empty(arg1)(const_c);
241 if (test(bool(s2) != const_c.empty())) {
242 cerr << "Failed " << typeid(Container).name() << " test_empty 2\n";
247 template <typename Container>
248 void test_end(Container c)
250 using phx::arg_names::arg1;
253 typename Container::iterator it1 = end(arg1)(c);
254 if (test(it1 != c.end())) {
255 cerr << "Failed " << typeid(Container).name() << " test_end 1\n";
258 typename Container::const_iterator it2 = end(arg1)(c);
259 if (test(it2 != c.end())) {
260 cerr << "Failed " << typeid(Container).name() << " test_end 2\n";
264 Container const const_c = c;
265 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
266 // Should not --- does not, Yay! --- compile.
267 typename Container::iterator it3 = end(arg1)(const_c);
270 typename Container::const_iterator it4 = end(arg1)(const_c);
271 if (test(it4 != const_c.end())) {
272 cerr << "Failed " << typeid(Container).name() << " test_end 4\n";
277 template <typename Container>
278 void test_erase(Container c)
280 using phx::arg_names::arg1;
281 using phx::arg_names::arg2;
282 using phx::arg_names::arg3;
285 Container const const_c = c;
287 typename Container::size_type size = c.size();
288 typename Container::iterator c_begin = c.begin();
289 erase(arg1, arg2)(c, c_begin);
290 if (test(c.size() + 1 != size)) {
291 cerr << "Failed " << typeid(Container).name() << " test_erase 1\n";
296 typename Container::iterator c_end = c.end();
297 erase(arg1, arg2, arg3)(c, c_begin, c_end);
298 if (test(!c.empty())) {
299 cerr << "Failed " << typeid(Container).name() << " test_erase 2\n";
303 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
304 erase(arg1, const_c.begin())(const_c);
305 erase(arg1, const_c.begin(), const_c.end())(const_c);
309 template <typename Container>
310 void test_map_erase(Container c)
313 if (boost::report_errors() != 0)
316 using phx::arg_names::arg1;
317 using phx::arg_names::arg2;
320 typename Container::value_type const value = *c.begin();
321 typename Container::key_type const key = value.first;
322 typename Container::size_type const removed =
323 erase(arg1, arg2)(c, key);
324 if (test(removed != 1)) {
325 cerr << "Failed " << typeid(Container).name() << " test_map_erase 1\n";
330 template <typename Container>
331 void test_front(Container c)
333 using phx::arg_names::arg1;
336 typename Container::reference r1 = front(arg1)(c);
337 if (test(r1 != c.front())) {
338 cerr << "Failed " << typeid(Container).name() << " test_front 1\n";
341 typename Container::const_reference r2 = front(arg1)(c);
342 if (test(r2 != c.front())) {
343 cerr << "Failed " << typeid(Container).name() << " test_front 2\n";
347 Container const const_c = c;
348 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
349 // Should not --- does not, Yay! --- compile.
350 typename Container::reference r3 = front(arg1)(const_c);
353 typename Container::const_reference r4 = front(arg1)(const_c);
354 if (test(r4 != c.front())) {
355 cerr << "Failed " << typeid(Container).name() << " test_front 4\n";
360 template <typename Container>
361 void test_get_allocator(Container c)
363 using phx::arg_names::arg1;
364 using phx::get_allocator;
366 Container const const_c = c;
368 typename Container::allocator_type a1 = get_allocator(arg1)(c);
369 if (test(a1 != c.get_allocator())) {
370 cerr << "Failed " << typeid(Container).name() << " test_get_allocator 1\n";
374 typename Container::allocator_type a2 = get_allocator(arg1)(const_c);
375 if (test(a2 != const_c.get_allocator())) {
376 cerr << "Failed " << typeid(Container).name() << " test_get_allocator 2\n";
381 template <typename Container>
382 void test_insert(Container c)
384 using phx::arg_names::arg1;
387 typename Container::value_type const value = *c.begin();
388 typename Container::iterator it = insert(arg1, c.begin(), value)(c);
389 if (test(it != c.begin() || *it != *(++it))) {
390 cerr << "Failed " << typeid(Container).name() << " test_insert 1\n";
394 typename Container::size_type size = c.size();
395 insert(arg1, c.begin(), 3, value)(c);
396 if (test(c.size() != size + 3)) {
397 cerr << "Failed " << typeid(Container).name() << " test_insert 2\n";
401 Container const const_c = c;
403 insert(arg1, c.begin(), const_c.begin(), const_c.end())(c);
404 if (test(c.size() != 2 * size)) {
405 cerr << "Failed " << typeid(Container).name() << " test_insert 3\n";
410 inline void test_map_insert(std::map<int, int> c)
412 using phx::arg_names::arg1;
413 using phx::arg_names::arg2;
414 using phx::arg_names::arg3;
416 typedef std::map<int, int> Map;
418 Map::value_type const value = *c.begin();
419 Map::iterator c_begin = c.begin();
421 // iterator insert(iterator where, const value_type& val);
423 phx::insert(arg1, arg2, arg3)(c, c_begin, value);
425 if (test(it != c.begin() /*|| *it != *(++it)*/)) {
426 cerr << "Failed " << typeid(Map).name() << " test_map_insert 1\n";
431 // pair<iterator, bool> insert(const value_type& val);
432 Map::value_type const value2(1400, 2200);
433 std::pair<Map::iterator, bool> result =
434 phx::insert(arg1, arg2)(c, value2);
435 if (test(!result.second)) {
436 cerr << "Failed " << typeid(Map).name() << " test_map_insert 2\n";
441 // template<class InIt>
442 // void insert(InIt first, InIt last);
443 Map const const_c = build_map();
444 Map::size_type size = c.size();
445 phx::insert(arg1, const_c.begin(), const_c.end())(c);
446 if (test(c.size() != size + const_c.size())) {
447 cerr << "Failed " << typeid(Map).name() << " test_map_insert 3\n";
452 inline void test_multimap_insert(std::multimap<int, int> c)
454 using phx::arg_names::arg1;
455 using phx::arg_names::arg2;
456 using phx::arg_names::arg3;
458 typedef std::multimap<int, int> Multimap;
460 Multimap::value_type const value = *c.begin();
461 Multimap::iterator c_begin = c.begin();
462 std::size_t old_size = c.size();
464 // iterator insert(iterator where, const value_type& val);
465 Multimap::iterator it =
466 phx::insert(arg1, arg2, arg3)(c, c_begin, value);
468 if (test(*it != value || c.size() != old_size + 1)) {
469 cerr << "Failed " << typeid(Multimap).name()
470 << " test_multimap_insert 1\n";
475 // iterator insert(const value_type& val);
476 Multimap::value_type const value2(1400, 2200);
477 it = phx::insert(arg1, arg2)(c, value2);
478 if (test(it == c.end())) {
479 cerr << "Failed " << typeid(Multimap).name()
480 << " test_multimap_insert 2\n";
485 // template<class InIt>
486 // void insert(InIt first, InIt last);
487 Multimap const const_c = build_multimap();
488 Multimap::size_type size = c.size();
489 phx::insert(arg1, const_c.begin(), const_c.end())(c);
490 if (test(c.size() != size + const_c.size())) {
491 cerr << "Failed " << typeid(Multimap).name()
492 << " test_multimap_insert 3\n";
497 template <typename Container>
498 void test_key_comp(Container c)
500 using phx::arg_names::arg1;
503 typename Container::key_compare comp = key_comp(arg1)(c);
505 Container const const_c = c;
506 comp = key_comp(arg1)(const_c);
509 template <typename Container>
510 void test_max_size(Container c)
512 using phx::arg_names::arg1;
515 Container const const_c = c;
517 typename Container::size_type s1 = max_size(arg1)(c);
518 if (test(s1 != c.max_size())) {
519 cerr << "Failed " << typeid(Container).name() << " test_max_size 1\n";
523 typename Container::size_type s2 = max_size(arg1)(const_c);
524 if (test(s2 != const_c.max_size())) {
525 cerr << "Failed " << typeid(Container).name() << " test_max_size 2\n";
530 template <typename Container>
531 void test_pop_back(Container c)
533 using phx::arg_names::arg1;
536 Container const const_c = c;
538 typename Container::size_type size = c.size();
541 if (test(c.size() + 1 != size)) {
542 cerr << "Failed " << typeid(Container).name() << " test_pop_back 1\n";
546 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
547 pop_back(arg1)(const_c);
551 template <typename Container>
552 void test_pop_front(Container c)
554 using phx::arg_names::arg1;
555 using phx::pop_front;
557 Container const const_c = c;
559 typename Container::size_type size = c.size();
562 if (test(c.size() + 1 != size)) {
563 cerr << "Failed " << typeid(Container).name() << " test_pop_front 1\n";
566 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
567 pop_front(arg1)(const_c);
571 template <typename Container>
572 void test_push_back(Container c)
574 using phx::arg_names::arg1;
575 using phx::arg_names::arg2;
576 using phx::push_back;
578 Container const const_c = c;
580 typename Container::value_type data = *c.begin();
581 typename Container::size_type size = c.size();
582 push_back(arg1, arg2)(c, data);
583 if (test(c.size() != size + 1)) {
584 cerr << "Failed " << typeid(Container).name() << " test_push_back 1\n";
587 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
588 push_back(arg1, arg2)(const_c, data);
592 template <typename Container>
593 void test_push_front(Container c)
595 using phx::arg_names::arg1;
596 using phx::arg_names::arg2;
597 using phx::push_front;
599 Container const const_c = c;
601 typename Container::value_type data = *c.begin();
602 typename Container::size_type size = c.size();
603 push_front(arg1, arg2)(c, data);
604 if (test(c.size() != size + 1)) {
605 cerr << "Failed " << typeid(Container).name() << " test_push_front 1\n";
608 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
609 push_front(arg1, arg2)(const_c, data);
613 template <typename Container>
614 void test_rbegin(Container c)
616 using phx::arg_names::arg1;
619 typename Container::reverse_iterator it1 = rbegin(arg1)(c);
620 typename Container::reverse_iterator it1_test = c.rbegin();
621 if (test(it1 != it1_test)) {
622 cerr << "Failed " << typeid(Container).name() << " test_rbegin 1\n";
625 typename Container::const_reverse_iterator it2 = rbegin(arg1)(c);
626 typename Container::const_reverse_iterator it2_test = c.rbegin();
627 if (test(it2 != it2_test)) {
628 cerr << "Failed " << typeid(Container).name() << " test_rbegin 2\n";
632 Container const const_c = c;
633 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
634 // Should not --- does not, Yay! --- compile.
635 typename Container::reverse_iterator it3 = rbegin(arg1)(const_c);
638 typename Container::const_reverse_iterator it4 = rbegin(arg1)(const_c);
639 it2_test = const_c.rbegin();
640 if (test(it4 != it2_test)) {
641 cerr << "Failed " << typeid(Container).name() << " test_rbegin 4\n";
646 template <typename Container>
647 void test_rend(Container c)
649 using phx::arg_names::arg1;
652 typename Container::reverse_iterator it1 = rend(arg1)(c);
653 typename Container::reverse_iterator it1_test = c.rend();
654 if (test(it1 != it1_test)) {
655 cerr << "Failed " << typeid(Container).name() << " test_rend 1\n";
658 typename Container::const_reverse_iterator it2 = rend(arg1)(c);
659 typename Container::const_reverse_iterator it2_test = c.rend();
660 if (test(it2 != it2_test)) {
661 cerr << "Failed " << typeid(Container).name() << " test_rend 2\n";
665 Container const const_c = c;
666 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
667 // Should not --- does not, Yay! --- compile.
668 typename Container::reverse_iterator it3 = rend(arg1)(const_c);
671 typename Container::const_reverse_iterator it4 = rend(arg1)(const_c);
672 it2_test = const_c.rend();
673 if (test(it4 != it2_test)) {
674 cerr << "Failed " << typeid(Container).name() << " test_rend 4\n";
679 template <typename Container>
680 void test_reserve(Container c)
682 using phx::arg_names::arg1;
685 Container const const_c = c;
687 typename Container::size_type count = 2 * c.size();
688 reserve(arg1, count)(c);
689 if (test(c.capacity() < count)) {
690 cerr << "Failed " << typeid(Container).name() << " test_reserve 1\n";
693 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
694 reserve(arg1, count)(const_c)(const_c);
698 template <typename Container>
699 void test_resize(Container c)
701 using phx::arg_names::arg1;
704 Container const const_c = c;
706 typename Container::size_type new_size = 2 * c.size();
707 resize(arg1, new_size)(c);
708 if (test(c.size() != new_size)) {
709 cerr << "Failed " << typeid(Container).name() << " test_resize 1\n";
713 new_size = 2 * c.size();
714 typename Container::value_type value = *c.begin();
715 resize(arg1, new_size, value)(c);
716 if (test(c.size() != new_size)) {
717 cerr << "Failed " << typeid(Container).name() << " test_resize 2\n";
720 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
721 new_size = 2 * const_c.size();
722 resize(arg1, new_size)(const_c);
724 new_size = 2 * const_c.size();
725 resize(arg1, new_size, value)(const_c);
729 template <typename Container>
730 void test_size(Container c)
732 using phx::arg_names::arg1;
735 Container const const_c = c;
737 typename Container::size_type s1 = size(arg1)(c);
738 if (test(s1 != c.size())) {
739 cerr << "Failed " << typeid(Container).name() << " test_size 1\n";
743 typename Container::size_type s2 = size(arg1)(const_c);
744 if (test(s2 != const_c.size())) {
745 cerr << "Failed " << typeid(Container).name() << " test_size 2\n";
750 template <typename Container>
751 void test_splice(Container c)
753 using phx::arg_names::arg1;
754 using phx::arg_names::arg2;
755 using phx::arg_names::arg3;
756 using phx::arg_names::arg4;
757 using phx::arg_names::arg5;
760 typename Container::iterator c_end;
761 typename Container::iterator c2_begin;
762 typename Container::iterator c2_end;
763 typename Container::size_type size = c.size();
765 Container const copy = c;
766 Container const copy2 = build_list();
767 Container c2 = copy2;
771 splice(arg1, arg2, arg3)(c, c_end, c2);
772 if (test(c.size() != 2 * size)) {
773 cerr << "Failed " << typeid(Container).name() << " test_splice 1\n";
780 c2_begin = c2.begin();
782 splice(arg1, arg2, arg3, arg4)(c, c_end, c2, c2_begin);
783 if (test(c.size() != size)) {
784 cerr << "Failed " << typeid(Container).name() << " test_splice 2\n";
791 c2_begin = c2.begin();
793 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";
803 template <typename Container>
804 void test_value_comp(Container c)
806 using phx::arg_names::arg1;
807 using phx::value_comp;
809 typename Container::value_compare comp = value_comp(arg1)(c);
811 Container const const_c = c;
812 comp = value_comp(arg1)(const_c);