]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/variant2/test/variant_get_by_index.cpp
2 // Copyright 2017 Peter Dimov.
4 // Distributed under the Boost Software License, Version 1.0.
6 // See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt
9 #if defined( __clang__ ) && defined( __has_warning )
10 # if __has_warning( "-Wdeprecated-volatile" )
11 # pragma clang diagnostic ignored "-Wdeprecated-volatile"
15 #include <boost/variant2/variant.hpp>
16 #include <boost/core/lightweight_test.hpp>
17 #include <boost/core/lightweight_test_trait.hpp>
18 #include <type_traits>
22 using namespace boost::variant2
;
29 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<0>(v
)), int&>));
30 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<0>(std::move(v
))), int&&>));
31 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<0>(&v
)), int*>));
37 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<0>(v
)), int const&>));
38 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<0>(std::move(v
))), int const&&>));
39 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<0>(&v
)), int const*>));
45 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<0>(v
)), int const&>));
46 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<0>(std::move(v
))), int const&&>));
47 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<0>(&v
)), int const*>));
51 variant
<int volatile> const v
;
53 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<0>(v
)), int const volatile&>));
54 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<0>(std::move(v
))), int const volatile&&>));
55 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<0>(&v
)), int const volatile*>));
61 BOOST_TEST_EQ( get
<0>(v
), 0 );
62 BOOST_TEST_EQ( get_if
<0>(&v
), &get
<0>(v
) );
64 BOOST_TEST_EQ( get
<0>(std::move(v
)), 0 );
70 BOOST_TEST_EQ( get
<0>(v
), 0 );
71 BOOST_TEST_EQ( get_if
<0>(&v
), &get
<0>(v
) );
73 BOOST_TEST_EQ( get
<0>(std::move(v
)), 0 );
79 BOOST_TEST_EQ( get
<0>(v
), 1 );
80 BOOST_TEST_EQ( get_if
<0>(&v
), &get
<0>(v
) );
82 BOOST_TEST_EQ( get
<0>(std::move(v
)), 1 );
86 variant
<int> const v( 1 );
88 BOOST_TEST_EQ( get
<0>(v
), 1 );
89 BOOST_TEST_EQ( get_if
<0>(&v
), &get
<0>(v
) );
91 BOOST_TEST_EQ( get
<0>(std::move(v
)), 1 );
95 variant
<int, float> v
;
97 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<0>(v
)), int&>));
98 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<0>(std::move(v
))), int&&>));
99 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<0>(&v
)), int*>));
101 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<1>(v
)), float&>));
102 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<1>(std::move(v
))), float&&>));
103 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<1>(&v
)), float*>));
107 variant
<int, float> const v
;
109 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<0>(v
)), int const&>));
110 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<0>(std::move(v
))), int const&&>));
111 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<0>(&v
)), int const*>));
113 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<1>(v
)), float const&>));
114 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<1>(std::move(v
))), float const&&>));
115 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<1>(&v
)), float const*>));
119 variant
<int const, float volatile> v
;
121 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<0>(v
)), int const&>));
122 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<0>(std::move(v
))), int const&&>));
123 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<0>(&v
)), int const*>));
125 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<1>(v
)), float volatile&>));
126 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<1>(std::move(v
))), float volatile&&>));
127 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<1>(&v
)), float volatile*>));
131 variant
<int const, float volatile> const v
;
133 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<0>(v
)), int const&>));
134 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<0>(std::move(v
))), int const&&>));
135 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<0>(&v
)), int const*>));
137 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<1>(v
)), float const volatile&>));
138 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<1>(std::move(v
))), float const volatile&&>));
139 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<1>(&v
)), float const volatile*>));
143 variant
<int, float> v
;
145 BOOST_TEST_EQ( get
<0>(v
), 0 );
146 BOOST_TEST_EQ( get_if
<0>(&v
), &get
<0>(v
) );
148 BOOST_TEST_THROWS( get
<1>(v
), bad_variant_access
);
149 BOOST_TEST_EQ( get_if
<1>(&v
), nullptr );
151 BOOST_TEST_EQ( get
<0>(std::move(v
)), 0 );
155 variant
<int, float> const v
;
157 BOOST_TEST_EQ( get
<0>(v
), 0 );
158 BOOST_TEST_EQ( get_if
<0>(&v
), &get
<0>(v
) );
160 BOOST_TEST_THROWS( get
<1>(v
), bad_variant_access
);
161 BOOST_TEST_EQ( get_if
<1>(&v
), nullptr );
163 BOOST_TEST_EQ( get
<0>(std::move(v
)), 0 );
167 variant
<int, float> v( 1 );
169 BOOST_TEST_EQ( get
<0>(v
), 1 );
170 BOOST_TEST_EQ( get_if
<0>(&v
), &get
<0>(v
) );
172 BOOST_TEST_THROWS( get
<1>(v
), bad_variant_access
);
173 BOOST_TEST_EQ( get_if
<1>(&v
), nullptr );
175 BOOST_TEST_EQ( get
<0>(std::move(v
)), 1 );
179 variant
<int, float> const v( 1 );
181 BOOST_TEST_EQ( get
<0>(v
), 1 );
182 BOOST_TEST_EQ( get_if
<0>(&v
), &get
<0>(v
) );
184 BOOST_TEST_THROWS( get
<1>(v
), bad_variant_access
);
185 BOOST_TEST_EQ( get_if
<1>(&v
), nullptr );
187 BOOST_TEST_EQ( get
<0>(std::move(v
)), 1 );
191 variant
<int, float> v( 3.14f
);
193 BOOST_TEST_THROWS( get
<0>(v
), bad_variant_access
);
194 BOOST_TEST_EQ( get_if
<0>(&v
), nullptr );
196 BOOST_TEST_EQ( get
<1>(v
), 3.14f
);
197 BOOST_TEST_EQ( get_if
<1>(&v
), &get
<1>(v
) );
199 BOOST_TEST_EQ( get
<1>(std::move(v
)), 3.14f
);
203 variant
<int, float> const v( 3.14f
);
205 BOOST_TEST_THROWS( get
<0>(v
), bad_variant_access
);
206 BOOST_TEST_EQ( get_if
<0>(&v
), nullptr );
208 BOOST_TEST_EQ( get
<1>(v
), 3.14f
);
209 BOOST_TEST_EQ( get_if
<1>(&v
), &get
<1>(v
) );
211 BOOST_TEST_EQ( get
<1>(std::move(v
)), 3.14f
);
215 variant
<int, float, float> v
;
217 BOOST_TEST_EQ( get
<0>(v
), 0 );
218 BOOST_TEST_EQ( get_if
<0>(&v
), &get
<0>(v
) );
220 BOOST_TEST_THROWS( get
<1>(v
), bad_variant_access
);
221 BOOST_TEST_EQ( get_if
<1>(&v
), nullptr );
223 BOOST_TEST_THROWS( get
<2>(v
), bad_variant_access
);
224 BOOST_TEST_EQ( get_if
<2>(&v
), nullptr );
226 BOOST_TEST_EQ( get
<0>(std::move(v
)), 0 );
230 variant
<int, float, float> const v
;
232 BOOST_TEST_EQ( get
<0>(v
), 0 );
233 BOOST_TEST_EQ( get_if
<0>(&v
), &get
<0>(v
) );
235 BOOST_TEST_THROWS( get
<1>(v
), bad_variant_access
);
236 BOOST_TEST_EQ( get_if
<1>(&v
), nullptr );
238 BOOST_TEST_THROWS( get
<2>(v
), bad_variant_access
);
239 BOOST_TEST_EQ( get_if
<2>(&v
), nullptr );
241 BOOST_TEST_EQ( get
<0>(std::move(v
)), 0 );
245 variant
<int, float, float> v( 1 );
247 BOOST_TEST_EQ( get
<0>(v
), 1 );
248 BOOST_TEST_EQ( get_if
<0>(&v
), &get
<0>(v
) );
250 BOOST_TEST_THROWS( get
<1>(v
), bad_variant_access
);
251 BOOST_TEST_EQ( get_if
<1>(&v
), nullptr );
253 BOOST_TEST_THROWS( get
<2>(v
), bad_variant_access
);
254 BOOST_TEST_EQ( get_if
<2>(&v
), nullptr );
256 BOOST_TEST_EQ( get
<0>(std::move(v
)), 1 );
260 variant
<int, float, float> const v( 1 );
262 BOOST_TEST_EQ( get
<0>(v
), 1 );
263 BOOST_TEST_EQ( get_if
<0>(&v
), &get
<0>(v
) );
265 BOOST_TEST_THROWS( get
<1>(v
), bad_variant_access
);
266 BOOST_TEST_EQ( get_if
<1>(&v
), nullptr );
268 BOOST_TEST_THROWS( get
<2>(v
), bad_variant_access
);
269 BOOST_TEST_EQ( get_if
<2>(&v
), nullptr );
271 BOOST_TEST_EQ( get
<0>(std::move(v
)), 1 );
275 variant
<int, int, float> v( 3.14f
);
277 BOOST_TEST_THROWS( get
<0>(v
), bad_variant_access
);
278 BOOST_TEST_EQ( get_if
<0>(&v
), nullptr );
280 BOOST_TEST_THROWS( get
<1>(v
), bad_variant_access
);
281 BOOST_TEST_EQ( get_if
<1>(&v
), nullptr );
283 BOOST_TEST_EQ( get
<2>(v
), 3.14f
);
284 BOOST_TEST_EQ( get_if
<2>(&v
), &get
<2>(v
) );
286 BOOST_TEST_EQ( get
<2>(std::move(v
)), 3.14f
);
290 variant
<int, int, float> const v( 3.14f
);
292 BOOST_TEST_THROWS( get
<0>(v
), bad_variant_access
);
293 BOOST_TEST_EQ( get_if
<0>(&v
), nullptr );
295 BOOST_TEST_THROWS( get
<1>(v
), bad_variant_access
);
296 BOOST_TEST_EQ( get_if
<1>(&v
), nullptr );
298 BOOST_TEST_EQ( get
<2>(v
), 3.14f
);
299 BOOST_TEST_EQ( get_if
<2>(&v
), &get
<2>(v
) );
301 BOOST_TEST_EQ( get
<2>(std::move(v
)), 3.14f
);
305 variant
<int> * p
= 0;
306 BOOST_TEST_EQ( get_if
<0>(p
), nullptr );
310 variant
<int const> * p
= 0;
311 BOOST_TEST_EQ( get_if
<0>(p
), nullptr );
315 variant
<int> const * p
= 0;
316 BOOST_TEST_EQ( get_if
<0>(p
), nullptr );
320 variant
<int const> const * p
= 0;
321 BOOST_TEST_EQ( get_if
<0>(p
), nullptr );
325 variant
<int, float> * p
= 0;
326 BOOST_TEST_EQ( get_if
<0>(p
), nullptr );
330 variant
<int, float> const * p
= 0;
331 BOOST_TEST_EQ( get_if
<0>(p
), nullptr );
335 variant
<int const, float volatile> * p
= 0;
336 BOOST_TEST_EQ( get_if
<0>(p
), nullptr );
340 variant
<int const, float volatile> const * p
= 0;
341 BOOST_TEST_EQ( get_if
<0>(p
), nullptr );
345 variant
<int, int, float, float> * p
= 0;
346 BOOST_TEST_EQ( get_if
<0>(p
), nullptr );
350 variant
<int, int, float, float> const * p
= 0;
351 BOOST_TEST_EQ( get_if
<0>(p
), nullptr );
354 return boost::report_errors();