1 // Boost.TypeErasure library
3 // Copyright 2011 Steven Watanabe
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)
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/type_erasure/relaxed.hpp>
17 #include <boost/mpl/vector.hpp>
19 #define BOOST_TEST_MAIN
20 #include <boost/test/unit_test.hpp>
22 using namespace boost::type_erasure
;
24 template<class T
= _self
>
25 struct common
: ::boost::mpl::vector
<
26 copy_constructible
<T
>,
30 BOOST_AUTO_TEST_CASE(test_basic
)
32 typedef ::boost::mpl::vector
<common
<>, assignable
<> > test_concept
;
33 any
<test_concept
> x(1);
34 int* ip
= any_cast
<int*>(&x
);
35 any
<test_concept
> y(2);
37 BOOST_CHECK_EQUAL(any_cast
<int>(x
), 2);
38 // make sure that we're actually using assignment
39 // of the underlying object, not copy and swap.
40 BOOST_CHECK_EQUAL(any_cast
<int*>(&x
), ip
);
43 BOOST_AUTO_TEST_CASE(test_basic_relaxed
)
45 typedef ::boost::mpl::vector
<common
<>, assignable
<>, relaxed
> test_concept
;
46 any
<test_concept
> x(1);
47 int* ip
= any_cast
<int*>(&x
);
48 any
<test_concept
> y(2);
50 BOOST_CHECK_EQUAL(any_cast
<int>(x
), 2);
51 // make sure that we're actually using assignment
52 // of the underlying object, not copy and swap.
53 BOOST_CHECK_EQUAL(any_cast
<int*>(&x
), ip
);
56 BOOST_AUTO_TEST_CASE(test_relaxed_no_copy
)
58 typedef ::boost::mpl::vector
<
64 any
<test_concept
> x(1);
65 int* ip
= any_cast
<int*>(&x
);
66 any
<test_concept
> y(2);
68 BOOST_CHECK_EQUAL(any_cast
<int>(x
), 2);
69 // make sure that we're actually using assignment
70 // of the underlying object, not copy and swap.
71 BOOST_CHECK_EQUAL(any_cast
<int*>(&x
), ip
);
74 BOOST_AUTO_TEST_CASE(test_relaxed_no_assign
)
76 typedef ::boost::mpl::vector
<
80 any
<test_concept
> x(1);
81 any
<test_concept
> y(2);
83 BOOST_CHECK_EQUAL(any_cast
<int>(x
), 2);
86 BOOST_AUTO_TEST_CASE(test_dynamic_fallback
)
88 typedef ::boost::mpl::vector
<common
<>, assignable
<>, relaxed
> test_concept
;
89 any
<test_concept
> x(1);
90 any
<test_concept
> y(2.0);
92 BOOST_CHECK_EQUAL(any_cast
<double>(x
), 2.0);
95 BOOST_AUTO_TEST_CASE(test_dynamic_fail
)
97 typedef ::boost::mpl::vector
<destructible
<>, typeid_
<>, assignable
<>, relaxed
> test_concept
;
98 any
<test_concept
> x(1);
99 any
<test_concept
> y(2.0);
100 BOOST_CHECK_THROW(x
= y
, bad_function_call
);
103 BOOST_AUTO_TEST_CASE(test_basic_int
)
105 typedef ::boost::mpl::vector
<common
<>, assignable
<_self
, int> > test_concept
;
106 any
<test_concept
> x(1);
107 int* ip
= any_cast
<int*>(&x
);
109 BOOST_CHECK_EQUAL(any_cast
<int>(x
), 2);
110 // make sure that we're actually using assignment
111 // of the underlying object, not copy and swap.
112 BOOST_CHECK_EQUAL(any_cast
<int*>(&x
), ip
);
115 BOOST_AUTO_TEST_CASE(test_basic_relaxed_int
)
117 typedef ::boost::mpl::vector
<common
<>, assignable
<_self
, int>, relaxed
> test_concept
;
118 any
<test_concept
> x(1);
119 int* ip
= any_cast
<int*>(&x
);
121 BOOST_CHECK_EQUAL(any_cast
<int>(x
), 2);
122 // make sure that we're actually using assignment
123 // of the underlying object, not copy and swap.
124 BOOST_CHECK_EQUAL(any_cast
<int*>(&x
), ip
);
127 BOOST_AUTO_TEST_CASE(test_relaxed_no_copy_int
)
129 typedef ::boost::mpl::vector
<
132 assignable
<_self
, int>,
135 any
<test_concept
> x(1);
136 int* ip
= any_cast
<int*>(&x
);
138 BOOST_CHECK_EQUAL(any_cast
<int>(x
), 2);
139 // make sure that we're actually using assignment
140 // of the underlying object, not copy and swap.
141 BOOST_CHECK_EQUAL(any_cast
<int*>(&x
), ip
);
144 BOOST_AUTO_TEST_CASE(test_relaxed_no_assign_int
)
146 typedef ::boost::mpl::vector
<
150 any
<test_concept
> x(1);
152 BOOST_CHECK_EQUAL(any_cast
<int>(x
), 2);
155 BOOST_AUTO_TEST_CASE(test_basic_ref
)
157 typedef ::boost::mpl::vector
<common
<>, assignable
<> > test_concept
;
159 any
<test_concept
, _self
&> x(i
);
160 any
<test_concept
> y(2);
162 BOOST_CHECK_EQUAL(i
, 2);
165 BOOST_AUTO_TEST_CASE(test_basic_relaxed_ref
)
167 typedef ::boost::mpl::vector
<common
<>, assignable
<>, relaxed
> test_concept
;
169 any
<test_concept
, _self
&> x(i
);
170 any
<test_concept
> y(2);
172 BOOST_CHECK_EQUAL(i
, 2);
175 BOOST_AUTO_TEST_CASE(test_relaxed_no_assign_ref
)
177 typedef ::boost::mpl::vector
<
182 any
<test_concept
, _self
&> x(i
);
183 any
<test_concept
> y(2);
185 BOOST_CHECK_EQUAL(i
, 1);
186 BOOST_CHECK_EQUAL(any_cast
<int*>(&x
), any_cast
<int*>(&y
));
189 BOOST_AUTO_TEST_CASE(test_dynamic_fallback_ref
)
191 typedef ::boost::mpl::vector
<common
<>, assignable
<>, relaxed
> test_concept
;
193 any
<test_concept
, _self
&> x(i
);
194 any
<test_concept
> y(2.0);
196 BOOST_CHECK_EQUAL(any_cast
<double>(x
), 2.0);