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/tuple.hpp> |
13 | #include <boost/type_erasure/builtin.hpp> |
14 | #include <boost/type_erasure/operators.hpp> |
15 | #include <boost/type_erasure/any_cast.hpp> |
16 | #include <boost/mpl/vector.hpp> |
17 | |
18 | #define BOOST_TEST_MAIN |
19 | #include <boost/test/unit_test.hpp> |
20 | |
21 | using namespace boost::type_erasure; |
22 | |
23 | template<class T = _self> |
24 | struct common : ::boost::mpl::vector< |
25 | copy_constructible<T>, |
26 | typeid_<T> |
27 | > {}; |
28 | |
29 | BOOST_AUTO_TEST_CASE(test_value_to_value) |
30 | { |
31 | typedef ::boost::mpl::vector<common<> > test_concept; |
32 | any<test_concept> x(2); |
33 | BOOST_CHECK_EQUAL(any_cast<int>(x), 2); |
34 | BOOST_CHECK_THROW(any_cast<double>(x), bad_any_cast); |
35 | const any<test_concept> y(x); |
36 | BOOST_CHECK_EQUAL(any_cast<int>(y), 2); |
37 | BOOST_CHECK_THROW(any_cast<double>(y), bad_any_cast); |
38 | } |
39 | |
40 | BOOST_AUTO_TEST_CASE(test_value_to_ref) |
41 | { |
42 | typedef ::boost::mpl::vector<common<> > test_concept; |
43 | any<test_concept> x(2); |
44 | BOOST_CHECK_EQUAL(any_cast<int&>(x), 2); |
45 | BOOST_CHECK_EQUAL(any_cast<const int&>(x), 2); |
46 | BOOST_CHECK_THROW(any_cast<double&>(x), bad_any_cast); |
47 | BOOST_CHECK_THROW(any_cast<const double&>(x), bad_any_cast); |
48 | const any<test_concept> y(x); |
49 | // BOOST_CHECK_EQUAL(any_cast<int&>(y), 2); |
50 | BOOST_CHECK_EQUAL(any_cast<const int&>(y), 2); |
51 | // BOOST_CHECK_THROW(any_cast<double&>(y), bad_any_cast); |
52 | BOOST_CHECK_THROW(any_cast<const double&>(y), bad_any_cast); |
53 | } |
54 | |
55 | BOOST_AUTO_TEST_CASE(test_value_to_pointer) |
56 | { |
57 | typedef ::boost::mpl::vector<common<> > test_concept; |
58 | any<test_concept> x(2); |
59 | BOOST_CHECK_EQUAL(*any_cast<int*>(&x), 2); |
60 | BOOST_CHECK_EQUAL(*any_cast<const int*>(&x), 2); |
61 | BOOST_CHECK_EQUAL(any_cast<void*>(&x), any_cast<int*>(&x)); |
62 | BOOST_CHECK_EQUAL(any_cast<const void*>(&x), any_cast<const int*>(&x)); |
63 | BOOST_CHECK_EQUAL(any_cast<double*>(&x), (double*)0); |
64 | BOOST_CHECK_EQUAL(any_cast<const double*>(&x), (double*)0); |
65 | const any<test_concept> y(x); |
66 | // BOOST_CHECK_EQUAL(*any_cast<int*>(&y), 2); |
67 | BOOST_CHECK_EQUAL(*any_cast<const int*>(&y), 2); |
68 | // BOOST_CHECK_EQUAL(any_cast<void*>(&y), any_cast<int*>(&y)); |
69 | BOOST_CHECK_EQUAL(any_cast<const void*>(&y), any_cast<const int*>(&y)); |
70 | // BOOST_CHECK_EQUAL(any_cast<double*>(&y), (double*)0); |
71 | BOOST_CHECK_EQUAL(any_cast<const double*>(&y), (double*)0); |
72 | } |
73 | |
74 | BOOST_AUTO_TEST_CASE(test_ref_to_value) |
75 | { |
76 | typedef ::boost::mpl::vector<common<> > test_concept; |
77 | int i = 2; |
78 | any<test_concept, _self&> x(i); |
79 | BOOST_CHECK_EQUAL(any_cast<int>(x), 2); |
80 | BOOST_CHECK_THROW(any_cast<double>(x), bad_any_cast); |
81 | const any<test_concept, _self&> y(x); |
82 | BOOST_CHECK_EQUAL(any_cast<int>(y), 2); |
83 | BOOST_CHECK_THROW(any_cast<double>(y), bad_any_cast); |
84 | } |
85 | |
86 | BOOST_AUTO_TEST_CASE(test_ref_to_ref) |
87 | { |
88 | typedef ::boost::mpl::vector<common<> > test_concept; |
89 | int i = 2; |
90 | any<test_concept, _self&> x(i); |
91 | BOOST_CHECK_EQUAL(any_cast<int&>(x), 2); |
92 | BOOST_CHECK_EQUAL(any_cast<const int&>(x), 2); |
93 | BOOST_CHECK_THROW(any_cast<double&>(x), bad_any_cast); |
94 | BOOST_CHECK_THROW(any_cast<const double&>(x), bad_any_cast); |
95 | const any<test_concept, _self&> y(x); |
96 | BOOST_CHECK_EQUAL(any_cast<int&>(y), 2); |
97 | BOOST_CHECK_EQUAL(any_cast<const int&>(y), 2); |
98 | BOOST_CHECK_THROW(any_cast<double&>(y), bad_any_cast); |
99 | BOOST_CHECK_THROW(any_cast<const double&>(y), bad_any_cast); |
100 | } |
101 | |
102 | BOOST_AUTO_TEST_CASE(test_ref_to_pointer) |
103 | { |
104 | typedef ::boost::mpl::vector<common<> > test_concept; |
105 | int i = 2; |
106 | any<test_concept, _self&> x(i); |
107 | BOOST_CHECK_EQUAL(*any_cast<int*>(&x), 2); |
108 | BOOST_CHECK_EQUAL(*any_cast<const int*>(&x), 2); |
109 | BOOST_CHECK_EQUAL(any_cast<void*>(&x), any_cast<int*>(&x)); |
110 | BOOST_CHECK_EQUAL(any_cast<const void*>(&x), any_cast<const int*>(&x)); |
111 | BOOST_CHECK_EQUAL(any_cast<double*>(&x), (double*)0); |
112 | BOOST_CHECK_EQUAL(any_cast<const double*>(&x), (double*)0); |
113 | const any<test_concept, _self&> y(x); |
114 | BOOST_CHECK_EQUAL(*any_cast<int*>(&y), 2); |
115 | BOOST_CHECK_EQUAL(*any_cast<const int*>(&y), 2); |
116 | BOOST_CHECK_EQUAL(any_cast<void*>(&y), any_cast<int*>(&y)); |
117 | BOOST_CHECK_EQUAL(any_cast<const void*>(&y), any_cast<const int*>(&y)); |
118 | BOOST_CHECK_EQUAL(any_cast<double*>(&y), (double*)0); |
119 | BOOST_CHECK_EQUAL(any_cast<const double*>(&y), (double*)0); |
120 | } |
121 | |
122 | BOOST_AUTO_TEST_CASE(test_cref_to_value) |
123 | { |
124 | typedef ::boost::mpl::vector<common<> > test_concept; |
125 | int i = 2; |
126 | any<test_concept, const _self&> x(i); |
127 | BOOST_CHECK_EQUAL(any_cast<int>(x), 2); |
128 | BOOST_CHECK_THROW(any_cast<double>(x), bad_any_cast); |
129 | const any<test_concept, const _self&> y(x); |
130 | BOOST_CHECK_EQUAL(any_cast<int>(y), 2); |
131 | BOOST_CHECK_THROW(any_cast<double>(y), bad_any_cast); |
132 | } |
133 | |
134 | BOOST_AUTO_TEST_CASE(test_cref_to_ref) |
135 | { |
136 | typedef ::boost::mpl::vector<common<> > test_concept; |
137 | int i = 2; |
138 | any<test_concept, const _self&> x(i); |
139 | // BOOST_CHECK_EQUAL(any_cast<int&>(x), 2); |
140 | BOOST_CHECK_EQUAL(any_cast<const int&>(x), 2); |
141 | // BOOST_CHECK_THROW(any_cast<double&>(x), bad_any_cast); |
142 | BOOST_CHECK_THROW(any_cast<const double&>(x), bad_any_cast); |
143 | const any<test_concept, const _self&> y(x); |
144 | // BOOST_CHECK_EQUAL(any_cast<int&>(y), 2); |
145 | BOOST_CHECK_EQUAL(any_cast<const int&>(y), 2); |
146 | // BOOST_CHECK_THROW(any_cast<double&>(y), bad_any_cast); |
147 | BOOST_CHECK_THROW(any_cast<const double&>(y), bad_any_cast); |
148 | } |
149 | |
150 | BOOST_AUTO_TEST_CASE(test_cref_to_pointer) |
151 | { |
152 | typedef ::boost::mpl::vector<common<> > test_concept; |
153 | int i = 2; |
154 | any<test_concept, const _self&> x(i); |
155 | // BOOST_CHECK_EQUAL(*any_cast<int*>(&x), 2); |
156 | BOOST_CHECK_EQUAL(*any_cast<const int*>(&x), 2); |
157 | // BOOST_CHECK_EQUAL(any_cast<void*>(&x), any_cast<int*>(&x)); |
158 | BOOST_CHECK_EQUAL(any_cast<const void*>(&x), any_cast<const int*>(&x)); |
159 | // BOOST_CHECK_EQUAL(any_cast<double*>(&x), (double*)0); |
160 | BOOST_CHECK_EQUAL(any_cast<const double*>(&x), (double*)0); |
161 | const any<test_concept, const _self&> y(x); |
162 | // BOOST_CHECK_EQUAL(*any_cast<int*>(&y), 2); |
163 | BOOST_CHECK_EQUAL(*any_cast<const int*>(&y), 2); |
164 | // BOOST_CHECK_EQUAL(any_cast<void*>(&y), any_cast<int*>(&y)); |
165 | BOOST_CHECK_EQUAL(any_cast<const void*>(&y), any_cast<const int*>(&y)); |
166 | // BOOST_CHECK_EQUAL(any_cast<double*>(&y), (double*)0); |
167 | BOOST_CHECK_EQUAL(any_cast<const double*>(&y), (double*)0); |
168 | } |
169 | |