1 | // Boost.TypeErasure library |
2 | // |
3 | // Copyright 2011 Steven Watanabe |
4 | // |
5 | // Distributed under the Boost Software License Version 1.0. (See |
6 | // accompanying file LICENSE_1_0.txt or copy at |
7 | // http://www.boost.org/LICENSE_1_0.txt) |
8 | // |
9 | // $Id$ |
10 | |
11 | #include <boost/type_erasure/any.hpp> |
12 | #include <boost/type_erasure/builtin.hpp> |
13 | #include <boost/type_erasure/operators.hpp> |
14 | #include <boost/type_erasure/any_cast.hpp> |
15 | #include <boost/type_erasure/iterator.hpp> |
16 | #include <boost/type_erasure/same_type.hpp> |
17 | #include <boost/type_erasure/binding_of.hpp> |
18 | #include <boost/mpl/vector.hpp> |
19 | #include <boost/concept_check.hpp> |
20 | |
21 | #define BOOST_TEST_MAIN |
22 | #include <boost/test/unit_test.hpp> |
23 | |
24 | using namespace boost::type_erasure; |
25 | |
26 | BOOST_AUTO_TEST_CASE(test_basic) |
27 | { |
28 | typedef boost::mpl::vector< |
29 | forward_iterator<>, |
30 | same_type<forward_iterator<>::value_type, int> |
31 | > test_concept; |
32 | std::vector<int> vec(10); |
33 | any<test_concept> x(vec.begin()); |
34 | any<test_concept> y(vec.end()); |
35 | |
36 | for(int i = 0; x != y; ++x, ++i) { |
37 | *x = i; |
38 | } |
39 | int expected[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
40 | BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), &expected[0], &expected[0] + 10); |
41 | |
42 | BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::value_type, int>)); |
43 | BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::reference, int&>)); |
44 | BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::pointer, int*>)); |
45 | BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::difference_type, std::ptrdiff_t>)); |
46 | BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::iterator_category, std::forward_iterator_tag>)); |
47 | } |
48 | |
49 | BOOST_AUTO_TEST_CASE(test_any_value_type) |
50 | { |
51 | typedef boost::mpl::vector< |
52 | forward_iterator<>, |
53 | same_type<forward_iterator<>::value_type, _a>, |
54 | copy_constructible<_a>, |
55 | assignable<_a>, |
56 | incrementable<_a> |
57 | > test_concept; |
58 | std::vector<int> vec(10); |
59 | any<test_concept> x(vec.begin()); |
60 | any<test_concept> y(vec.end()); |
61 | |
62 | for(any<test_concept, _a> i = *x; x != y; ++x, ++i) { |
63 | *x = i; |
64 | } |
65 | int expected[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
66 | BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), &expected[0], &expected[0] + 10); |
67 | |
68 | BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::value_type, any<test_concept, _a> >)); |
69 | BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::reference, any<test_concept, _a&> >)); |
70 | BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::pointer, any<test_concept, _a>*>)); |
71 | BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::difference_type, std::ptrdiff_t>)); |
72 | BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::iterator_category, std::forward_iterator_tag>)); |
73 | } |
74 | |
75 | BOOST_AUTO_TEST_CASE(test_relaxed) |
76 | { |
77 | typedef boost::mpl::vector< |
78 | forward_iterator<>, |
79 | same_type<forward_iterator<>::value_type, int>, |
80 | relaxed |
81 | > test_concept; |
82 | std::vector<int> vec(10); |
83 | any<test_concept> x(vec.begin()); |
84 | any<test_concept> y(vec.end()); |
85 | |
86 | for(int i = 0; x != y; ++x, ++i) { |
87 | *x = i; |
88 | } |
89 | int expected[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
90 | BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), &expected[0], &expected[0] + 10); |
91 | |
92 | BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::value_type, int>)); |
93 | BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::reference, int&>)); |
94 | BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::pointer, int*>)); |
95 | BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::difference_type, std::ptrdiff_t>)); |
96 | BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::iterator_category, std::forward_iterator_tag>)); |
97 | |
98 | BOOST_CONCEPT_ASSERT((boost::ForwardIterator<any<test_concept> >)); |
99 | } |
100 | |