]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/type_erasure/test/test_assign.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / type_erasure / test / test_assign.cpp
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/type_erasure/relaxed.hpp>
17 #include <boost/mpl/vector.hpp>
18
19 #define BOOST_TEST_MAIN
20 #include <boost/test/unit_test.hpp>
21
22 using namespace boost::type_erasure;
23
24 template<class T = _self>
25 struct common : ::boost::mpl::vector<
26 copy_constructible<T>,
27 typeid_<T>
28 > {};
29
30 BOOST_AUTO_TEST_CASE(test_basic)
31 {
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);
36 x = y;
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);
41 }
42
43 BOOST_AUTO_TEST_CASE(test_basic_relaxed)
44 {
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);
49 x = y;
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);
54 }
55
56 BOOST_AUTO_TEST_CASE(test_relaxed_no_copy)
57 {
58 typedef ::boost::mpl::vector<
59 destructible<>,
60 typeid_<>,
61 assignable<>,
62 relaxed
63 > test_concept;
64 any<test_concept> x(1);
65 int* ip = any_cast<int*>(&x);
66 any<test_concept> y(2);
67 x = y;
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);
72 }
73
74 BOOST_AUTO_TEST_CASE(test_relaxed_no_assign)
75 {
76 typedef ::boost::mpl::vector<
77 common<>,
78 relaxed
79 > test_concept;
80 any<test_concept> x(1);
81 any<test_concept> y(2);
82 x = y;
83 BOOST_CHECK_EQUAL(any_cast<int>(x), 2);
84 }
85
86 BOOST_AUTO_TEST_CASE(test_dynamic_fallback)
87 {
88 typedef ::boost::mpl::vector<common<>, assignable<>, relaxed> test_concept;
89 any<test_concept> x(1);
90 any<test_concept> y(2.0);
91 x = y;
92 BOOST_CHECK_EQUAL(any_cast<double>(x), 2.0);
93 }
94
95 BOOST_AUTO_TEST_CASE(test_dynamic_fail)
96 {
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);
101 }
102
103 BOOST_AUTO_TEST_CASE(test_basic_int)
104 {
105 typedef ::boost::mpl::vector<common<>, assignable<_self, int> > test_concept;
106 any<test_concept> x(1);
107 int* ip = any_cast<int*>(&x);
108 x = 2;
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);
113 }
114
115 BOOST_AUTO_TEST_CASE(test_basic_relaxed_int)
116 {
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);
120 x = 2;
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);
125 }
126
127 BOOST_AUTO_TEST_CASE(test_relaxed_no_copy_int)
128 {
129 typedef ::boost::mpl::vector<
130 destructible<>,
131 typeid_<>,
132 assignable<_self, int>,
133 relaxed
134 > test_concept;
135 any<test_concept> x(1);
136 int* ip = any_cast<int*>(&x);
137 x = 2;
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);
142 }
143
144 BOOST_AUTO_TEST_CASE(test_relaxed_no_assign_int)
145 {
146 typedef ::boost::mpl::vector<
147 common<>,
148 relaxed
149 > test_concept;
150 any<test_concept> x(1);
151 x = 2;
152 BOOST_CHECK_EQUAL(any_cast<int>(x), 2);
153 }
154
155 BOOST_AUTO_TEST_CASE(test_basic_ref)
156 {
157 typedef ::boost::mpl::vector<common<>, assignable<> > test_concept;
158 int i = 1;
159 any<test_concept, _self&> x(i);
160 any<test_concept> y(2);
161 x = y;
162 BOOST_CHECK_EQUAL(i, 2);
163 }
164
165 BOOST_AUTO_TEST_CASE(test_basic_relaxed_ref)
166 {
167 typedef ::boost::mpl::vector<common<>, assignable<>, relaxed > test_concept;
168 int i = 1;
169 any<test_concept, _self&> x(i);
170 any<test_concept> y(2);
171 x = y;
172 BOOST_CHECK_EQUAL(i, 2);
173 }
174
175 BOOST_AUTO_TEST_CASE(test_relaxed_no_assign_ref)
176 {
177 typedef ::boost::mpl::vector<
178 common<>,
179 relaxed
180 > test_concept;
181 int i = 1;
182 any<test_concept, _self&> x(i);
183 any<test_concept> y(2);
184 x = y;
185 BOOST_CHECK_EQUAL(i, 1);
186 BOOST_CHECK_EQUAL(any_cast<int*>(&x), any_cast<int*>(&y));
187 }
188
189 BOOST_AUTO_TEST_CASE(test_dynamic_fallback_ref)
190 {
191 typedef ::boost::mpl::vector<common<>, assignable<>, relaxed> test_concept;
192 int i = 1;
193 any<test_concept, _self&> x(i);
194 any<test_concept> y(2.0);
195 x = y;
196 BOOST_CHECK_EQUAL(any_cast<double>(x), 2.0);
197 }