]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/variant2/test/variant_get_by_type.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / variant2 / test / variant_get_by_type.cpp
CommitLineData
92f5a8d4
TL
1
2// Copyright 2017 Peter Dimov.
3//
4// Distributed under the Boost Software License, Version 1.0.
5//
6// See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt
8
20effc67
TL
9#if defined( __clang__ ) && defined( __has_warning )
10# if __has_warning( "-Wdeprecated-volatile" )
11# pragma clang diagnostic ignored "-Wdeprecated-volatile"
12# endif
13#endif
14
92f5a8d4
TL
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>
19#include <utility>
20#include <string>
21
22using namespace boost::variant2;
23
24int main()
25{
26 {
27 variant<int> v;
28
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*>));
32 }
33
34 {
35 variant<int> const v;
36
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*>));
40 }
41
42 {
43 variant<int const> v;
44
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*>));
48 }
49
50 {
51 variant<int volatile> const v;
52
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*>));
56 }
57
58 {
59 variant<int> v;
60
61 BOOST_TEST_EQ( get<int>(v), 0 );
62 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
63
64 BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
65 }
66
67 {
68 variant<int> const v;
69
70 BOOST_TEST_EQ( get<int>(v), 0 );
71 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
72
73 BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
74 }
75
76 {
77 variant<int> v( 1 );
78
79 BOOST_TEST_EQ( get<int>(v), 1 );
80 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
81
82 BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
83 }
84
85 {
86 variant<int> const v( 1 );
87
88 BOOST_TEST_EQ( get<int>(v), 1 );
89 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
90
91 BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
92 }
93
94 {
95 variant<int, float> v;
96
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*>));
100
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*>));
104 }
105
106 {
107 variant<int, float> const v;
108
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*>));
112
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*>));
116 }
117
118 {
119 variant<int const, float volatile> v;
120
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*>));
124
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*>));
128 }
129
130 {
131 variant<int const, float volatile> const v;
132
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*>));
136
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*>));
140 }
141
142 {
143 variant<int, float> v;
144
145 BOOST_TEST_EQ( get<int>(v), 0 );
146 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
147
148 BOOST_TEST_THROWS( get<float>(v), bad_variant_access );
149 BOOST_TEST_EQ( get_if<float>(&v), nullptr );
150
151 BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
152 }
153
154 {
155 variant<int, float> const v;
156
157 BOOST_TEST_EQ( get<int>(v), 0 );
158 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
159
160 BOOST_TEST_THROWS( get<float>(v), bad_variant_access );
161 BOOST_TEST_EQ( get_if<float>(&v), nullptr );
162
163 BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
164 }
165
166 {
167 variant<int, float> v( 1 );
168
169 BOOST_TEST_EQ( get<int>(v), 1 );
170 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
171
172 BOOST_TEST_THROWS( get<float>(v), bad_variant_access );
173 BOOST_TEST_EQ( get_if<float>(&v), nullptr );
174
175 BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
176 }
177
178 {
179 variant<int, float> const v( 1 );
180
181 BOOST_TEST_EQ( get<int>(v), 1 );
182 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
183
184 BOOST_TEST_THROWS( get<float>(v), bad_variant_access );
185 BOOST_TEST_EQ( get_if<float>(&v), nullptr );
186
187 BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
188 }
189
190 {
191 variant<int, float> v( 3.14f );
192
193 BOOST_TEST_THROWS( get<int>(v), bad_variant_access );
194 BOOST_TEST_EQ( get_if<int>(&v), nullptr );
195
196 BOOST_TEST_EQ( get<float>(v), 3.14f );
197 BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
198
199 BOOST_TEST_EQ( get<float>(std::move(v)), 3.14f );
200 }
201
202 {
203 variant<int, float> const v( 3.14f );
204
205 BOOST_TEST_THROWS( get<int>(v), bad_variant_access );
206 BOOST_TEST_EQ( get_if<int>(&v), nullptr );
207
208 BOOST_TEST_EQ( get<float>(v), 3.14f );
209 BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
210
211 BOOST_TEST_EQ( get<float>(std::move(v)), 3.14f );
212 }
213
214 {
215 variant<int, float, float> v;
216
217 BOOST_TEST_EQ( get<int>(v), 0 );
218 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
219
220 BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
221 }
222
223 {
224 variant<int, float, float> const v;
225
226 BOOST_TEST_EQ( get<int>(v), 0 );
227 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
228
229 BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
230 }
231
232 {
233 variant<int, float, float> v( 1 );
234
235 BOOST_TEST_EQ( get<int>(v), 1 );
236 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
237
238 BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
239 }
240
241 {
242 variant<int, float, float> const v( 1 );
243
244 BOOST_TEST_EQ( get<int>(v), 1 );
245 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
246
247 BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
248 }
249
250 {
251 variant<int, int, float> v( 3.14f );
252
253 BOOST_TEST_EQ( get<float>(v), 3.14f );
254 BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
255
256 BOOST_TEST_EQ( get<float>(std::move(v)), 3.14f );
257 }
258
259 {
260 variant<int, int, float> const v( 3.14f );
261
262 BOOST_TEST_EQ( get<float>(v), 3.14f );
263 BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
264
265 BOOST_TEST_EQ( get<float>(std::move(v)), 3.14f );
266 }
267
268 {
269 variant<int> * p = 0;
270 BOOST_TEST_EQ( get_if<int>(p), nullptr );
271 }
272
273 {
274 variant<int const> * p = 0;
275 BOOST_TEST_EQ( get_if<int const>(p), nullptr );
276 }
277
278 {
279 variant<int> const * p = 0;
280 BOOST_TEST_EQ( get_if<int>(p), nullptr );
281 }
282
283 {
284 variant<int const> const * p = 0;
285 BOOST_TEST_EQ( get_if<int const>(p), nullptr );
286 }
287
288 {
289 variant<int, float> * p = 0;
290
291 BOOST_TEST_EQ( get_if<int>(p), nullptr );
292 BOOST_TEST_EQ( get_if<float>(p), nullptr );
293 }
294
295 {
296 variant<int, float> const * p = 0;
297
298 BOOST_TEST_EQ( get_if<int>(p), nullptr );
299 BOOST_TEST_EQ( get_if<float>(p), nullptr );
300 }
301
302 {
303 variant<int, int, float> * p = 0;
304 BOOST_TEST_EQ( get_if<float>(p), nullptr );
305 }
306
307 {
308 variant<int, int, float> const * p = 0;
309 BOOST_TEST_EQ( get_if<float>(p), nullptr );
310 }
311
312 return boost::report_errors();
313}