]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/variant2/test/variant_get_by_type.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
<int>(v
)), int&>));
30 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<int>(std::move(v
))), int&&>));
31 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<int>(&v
)), int*>));
37 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<int>(v
)), int const&>));
38 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<int>(std::move(v
))), int const&&>));
39 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<int>(&v
)), int const*>));
45 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<int const>(v
)), int const&>));
46 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<int const>(std::move(v
))), int const&&>));
47 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<int const>(&v
)), int const*>));
51 variant
<int volatile> const v
;
53 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<int volatile>(v
)), int const volatile&>));
54 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<int volatile>(std::move(v
))), int const volatile&&>));
55 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<int volatile>(&v
)), int const volatile*>));
61 BOOST_TEST_EQ( get
<int>(v
), 0 );
62 BOOST_TEST_EQ( get_if
<int>(&v
), &get
<int>(v
) );
64 BOOST_TEST_EQ( get
<int>(std::move(v
)), 0 );
70 BOOST_TEST_EQ( get
<int>(v
), 0 );
71 BOOST_TEST_EQ( get_if
<int>(&v
), &get
<int>(v
) );
73 BOOST_TEST_EQ( get
<int>(std::move(v
)), 0 );
79 BOOST_TEST_EQ( get
<int>(v
), 1 );
80 BOOST_TEST_EQ( get_if
<int>(&v
), &get
<int>(v
) );
82 BOOST_TEST_EQ( get
<int>(std::move(v
)), 1 );
86 variant
<int> const v( 1 );
88 BOOST_TEST_EQ( get
<int>(v
), 1 );
89 BOOST_TEST_EQ( get_if
<int>(&v
), &get
<int>(v
) );
91 BOOST_TEST_EQ( get
<int>(std::move(v
)), 1 );
95 variant
<int, float> v
;
97 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<int>(v
)), int&>));
98 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<int>(std::move(v
))), int&&>));
99 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<int>(&v
)), int*>));
101 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<float>(v
)), float&>));
102 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<float>(std::move(v
))), float&&>));
103 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<float>(&v
)), float*>));
107 variant
<int, float> const v
;
109 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<int>(v
)), int const&>));
110 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<int>(std::move(v
))), int const&&>));
111 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<int>(&v
)), int const*>));
113 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<float>(v
)), float const&>));
114 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<float>(std::move(v
))), float const&&>));
115 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<float>(&v
)), float const*>));
119 variant
<int const, float volatile> v
;
121 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<int const>(v
)), int const&>));
122 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<int const>(std::move(v
))), int const&&>));
123 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<int const>(&v
)), int const*>));
125 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<float volatile>(v
)), float volatile&>));
126 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<float volatile>(std::move(v
))), float volatile&&>));
127 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<float volatile>(&v
)), float volatile*>));
131 variant
<int const, float volatile> const v
;
133 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<int const>(v
)), int const&>));
134 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<int const>(std::move(v
))), int const&&>));
135 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<int const>(&v
)), int const*>));
137 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<float volatile>(v
)), float const volatile&>));
138 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get
<float volatile>(std::move(v
))), float const volatile&&>));
139 BOOST_TEST_TRAIT_TRUE((std::is_same
<decltype(get_if
<float volatile>(&v
)), float const volatile*>));
143 variant
<int, float> v
;
145 BOOST_TEST_EQ( get
<int>(v
), 0 );
146 BOOST_TEST_EQ( get_if
<int>(&v
), &get
<int>(v
) );
148 BOOST_TEST_THROWS( get
<float>(v
), bad_variant_access
);
149 BOOST_TEST_EQ( get_if
<float>(&v
), nullptr );
151 BOOST_TEST_EQ( get
<int>(std::move(v
)), 0 );
155 variant
<int, float> const v
;
157 BOOST_TEST_EQ( get
<int>(v
), 0 );
158 BOOST_TEST_EQ( get_if
<int>(&v
), &get
<int>(v
) );
160 BOOST_TEST_THROWS( get
<float>(v
), bad_variant_access
);
161 BOOST_TEST_EQ( get_if
<float>(&v
), nullptr );
163 BOOST_TEST_EQ( get
<int>(std::move(v
)), 0 );
167 variant
<int, float> v( 1 );
169 BOOST_TEST_EQ( get
<int>(v
), 1 );
170 BOOST_TEST_EQ( get_if
<int>(&v
), &get
<int>(v
) );
172 BOOST_TEST_THROWS( get
<float>(v
), bad_variant_access
);
173 BOOST_TEST_EQ( get_if
<float>(&v
), nullptr );
175 BOOST_TEST_EQ( get
<int>(std::move(v
)), 1 );
179 variant
<int, float> const v( 1 );
181 BOOST_TEST_EQ( get
<int>(v
), 1 );
182 BOOST_TEST_EQ( get_if
<int>(&v
), &get
<int>(v
) );
184 BOOST_TEST_THROWS( get
<float>(v
), bad_variant_access
);
185 BOOST_TEST_EQ( get_if
<float>(&v
), nullptr );
187 BOOST_TEST_EQ( get
<int>(std::move(v
)), 1 );
191 variant
<int, float> v( 3.14f
);
193 BOOST_TEST_THROWS( get
<int>(v
), bad_variant_access
);
194 BOOST_TEST_EQ( get_if
<int>(&v
), nullptr );
196 BOOST_TEST_EQ( get
<float>(v
), 3.14f
);
197 BOOST_TEST_EQ( get_if
<float>(&v
), &get
<float>(v
) );
199 BOOST_TEST_EQ( get
<float>(std::move(v
)), 3.14f
);
203 variant
<int, float> const v( 3.14f
);
205 BOOST_TEST_THROWS( get
<int>(v
), bad_variant_access
);
206 BOOST_TEST_EQ( get_if
<int>(&v
), nullptr );
208 BOOST_TEST_EQ( get
<float>(v
), 3.14f
);
209 BOOST_TEST_EQ( get_if
<float>(&v
), &get
<float>(v
) );
211 BOOST_TEST_EQ( get
<float>(std::move(v
)), 3.14f
);
215 variant
<int, float, float> v
;
217 BOOST_TEST_EQ( get
<int>(v
), 0 );
218 BOOST_TEST_EQ( get_if
<int>(&v
), &get
<int>(v
) );
220 BOOST_TEST_EQ( get
<int>(std::move(v
)), 0 );
224 variant
<int, float, float> const v
;
226 BOOST_TEST_EQ( get
<int>(v
), 0 );
227 BOOST_TEST_EQ( get_if
<int>(&v
), &get
<int>(v
) );
229 BOOST_TEST_EQ( get
<int>(std::move(v
)), 0 );
233 variant
<int, float, float> v( 1 );
235 BOOST_TEST_EQ( get
<int>(v
), 1 );
236 BOOST_TEST_EQ( get_if
<int>(&v
), &get
<int>(v
) );
238 BOOST_TEST_EQ( get
<int>(std::move(v
)), 1 );
242 variant
<int, float, float> const v( 1 );
244 BOOST_TEST_EQ( get
<int>(v
), 1 );
245 BOOST_TEST_EQ( get_if
<int>(&v
), &get
<int>(v
) );
247 BOOST_TEST_EQ( get
<int>(std::move(v
)), 1 );
251 variant
<int, int, float> v( 3.14f
);
253 BOOST_TEST_EQ( get
<float>(v
), 3.14f
);
254 BOOST_TEST_EQ( get_if
<float>(&v
), &get
<float>(v
) );
256 BOOST_TEST_EQ( get
<float>(std::move(v
)), 3.14f
);
260 variant
<int, int, float> const v( 3.14f
);
262 BOOST_TEST_EQ( get
<float>(v
), 3.14f
);
263 BOOST_TEST_EQ( get_if
<float>(&v
), &get
<float>(v
) );
265 BOOST_TEST_EQ( get
<float>(std::move(v
)), 3.14f
);
269 variant
<int> * p
= 0;
270 BOOST_TEST_EQ( get_if
<int>(p
), nullptr );
274 variant
<int const> * p
= 0;
275 BOOST_TEST_EQ( get_if
<int const>(p
), nullptr );
279 variant
<int> const * p
= 0;
280 BOOST_TEST_EQ( get_if
<int>(p
), nullptr );
284 variant
<int const> const * p
= 0;
285 BOOST_TEST_EQ( get_if
<int const>(p
), nullptr );
289 variant
<int, float> * p
= 0;
291 BOOST_TEST_EQ( get_if
<int>(p
), nullptr );
292 BOOST_TEST_EQ( get_if
<float>(p
), nullptr );
296 variant
<int, float> const * p
= 0;
298 BOOST_TEST_EQ( get_if
<int>(p
), nullptr );
299 BOOST_TEST_EQ( get_if
<float>(p
), nullptr );
303 variant
<int, int, float> * p
= 0;
304 BOOST_TEST_EQ( get_if
<float>(p
), nullptr );
308 variant
<int, int, float> const * p
= 0;
309 BOOST_TEST_EQ( get_if
<float>(p
), nullptr );
312 return boost::report_errors();