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>
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
;
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!
59 std::advance(second
, 1);
60 if (test(*first
!= *second
)) {
61 cerr
<< "Failed " << typeid(Container
).name() << " test_assign 1\n";
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
);
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
;
80 typename
Container::const_iterator first
= c2
.begin();
81 typename
Container::const_iterator last
= c2
.end();
82 typename
Container::size_type size
= c2
.size();
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';
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
);
100 template <typename Container
>
101 void test_at(Container c
)
103 using phx::arg_names::arg1
;
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";
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";
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
);
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";
131 template <typename Container
>
132 void test_back(Container c
)
134 using phx::arg_names::arg1
;
137 typename
Container::reference r1
= back(arg1
)(c
);
138 if (test(r1
!= c
.back())) {
139 cerr
<< "Failed " << typeid(Container
).name() << " test_back 1\n";
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";
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
);
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";
161 template <typename Container
>
162 void test_begin(Container c
)
164 using phx::arg_names::arg1
;
167 typename
Container::iterator it1
= begin(arg1
)(c
);
168 if (test(it1
!= c
.begin())) {
169 cerr
<< "Failed " << typeid(Container
).name() << " test_begin 1\n";
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";
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
);
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";
191 template <typename Container
>
192 void test_capacity(Container c
)
194 using phx::arg_names::arg1
;
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";
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";
211 template <typename Container
>
212 void test_clear(Container c
)
214 using phx::arg_names::arg1
;
218 if (test(!c
.empty())) {
219 cerr
<< "Failed " << typeid(Container
).name() << " test_clear 1\n";
223 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
224 Container
const const_c
= c
;
225 clear(arg1
)(const_c
);
229 template <typename Container
>
230 void test_empty(Container c
)
232 using phx::arg_names::arg1
;
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";
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";
249 template <typename Container
>
250 void test_end(Container c
)
252 using phx::arg_names::arg1
;
255 typename
Container::iterator it1
= end(arg1
)(c
);
256 if (test(it1
!= c
.end())) {
257 cerr
<< "Failed " << typeid(Container
).name() << " test_end 1\n";
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";
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
);
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";
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
;
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";
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";
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
);
311 template <typename Container
>
312 void test_map_erase(Container c
)
315 if (boost::report_errors() != 0)
318 using phx::arg_names::arg1
;
319 using phx::arg_names::arg2
;
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";
332 template <typename Container
>
333 void test_front(Container c
)
335 using phx::arg_names::arg1
;
338 typename
Container::reference r1
= front(arg1
)(c
);
339 if (test(r1
!= c
.front())) {
340 cerr
<< "Failed " << typeid(Container
).name() << " test_front 1\n";
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";
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
);
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";
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";
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";
383 template <typename Container
>
384 void test_insert(Container c
)
386 using phx::arg_names::arg1
;
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";
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";
403 Container
const const_c
= c
;
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";
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();
423 // iterator insert(iterator where, const value_type& val);
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";
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";
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";
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();
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";
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";
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";
498 template <typename Container
>
499 void test_key_comp(Container c
)
501 using phx::arg_names::arg1
;
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
;
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";
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";
531 template <typename Container
>
532 void test_pop_back(Container c
)
534 using phx::arg_names::arg1
;
537 Container
const const_c
= c
;
539 typename
Container::size_type size
= c
.size();
542 if (test(c
.size() + 1 != size
)) {
543 cerr
<< "Failed " << typeid(Container
).name() << " test_pop_back 1\n";
547 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
548 pop_back(arg1
)(const_c
);
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();
563 if (test(c
.size() + 1 != size
)) {
564 cerr
<< "Failed " << typeid(Container
).name() << " test_pop_front 1\n";
567 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
568 pop_front(arg1
)(const_c
);
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";
588 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
589 push_back(arg1
, arg2
)(const_c
, data
);
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";
609 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
610 push_front(arg1
, arg2
)(const_c
, data
);
614 template <typename Container
>
615 void test_rbegin(Container c
)
617 using phx::arg_names::arg1
;
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";
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";
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
);
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";
647 template <typename Container
>
648 void test_rend(Container c
)
650 using phx::arg_names::arg1
;
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";
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";
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
);
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";
680 template <typename Container
>
681 void test_reserve(Container c
)
683 using phx::arg_names::arg1
;
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";
694 #if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)
695 reserve(arg1
, count
)(const_c
)(const_c
);
699 template <typename Container
>
700 void test_resize(Container c
)
702 using phx::arg_names::arg1
;
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";
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";
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
);
730 template <typename Container
>
731 void test_size(Container c
)
733 using phx::arg_names::arg1
;
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";
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";
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
;
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
;
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";
781 c2_begin
= c2
.begin();
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";
792 c2_begin
= c2
.begin();
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";
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
);