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
9 #include <boost/variant2/variant.hpp>
10 #include <boost/core/lightweight_test.hpp>
11 #include <boost/core/lightweight_test_trait.hpp>
12 #include <type_traits>
13 #include <utility>
14 #include <string>
15
16 using namespace boost::variant2;
17
main()18 int main()
19 {
20 {
21 variant<int> v;
22
23 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(v)), int&>));
24 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(std::move(v))), int&&>));
25 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int>(&v)), int*>));
26 }
27
28 {
29 variant<int> const v;
30
31 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(v)), int const&>));
32 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(std::move(v))), int const&&>));
33 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int>(&v)), int const*>));
34 }
35
36 {
37 variant<int const> v;
38
39 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(v)), int const&>));
40 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(std::move(v))), int const&&>));
41 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int const>(&v)), int const*>));
42 }
43
44 {
45 variant<int volatile> const v;
46
47 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int volatile>(v)), int const volatile&>));
48 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int volatile>(std::move(v))), int const volatile&&>));
49 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int volatile>(&v)), int const volatile*>));
50 }
51
52 {
53 variant<int> v;
54
55 BOOST_TEST_EQ( get<int>(v), 0 );
56 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
57
58 BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
59 }
60
61 {
62 variant<int> const v;
63
64 BOOST_TEST_EQ( get<int>(v), 0 );
65 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
66
67 BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
68 }
69
70 {
71 variant<int> v( 1 );
72
73 BOOST_TEST_EQ( get<int>(v), 1 );
74 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
75
76 BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
77 }
78
79 {
80 variant<int> const v( 1 );
81
82 BOOST_TEST_EQ( get<int>(v), 1 );
83 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
84
85 BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
86 }
87
88 {
89 variant<int, float> v;
90
91 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(v)), int&>));
92 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(std::move(v))), int&&>));
93 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int>(&v)), int*>));
94
95 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float>(v)), float&>));
96 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float>(std::move(v))), float&&>));
97 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<float>(&v)), float*>));
98 }
99
100 {
101 variant<int, float> const v;
102
103 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(v)), int const&>));
104 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(std::move(v))), int const&&>));
105 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int>(&v)), int const*>));
106
107 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float>(v)), float const&>));
108 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float>(std::move(v))), float const&&>));
109 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<float>(&v)), float const*>));
110 }
111
112 {
113 variant<int const, float volatile> v;
114
115 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(v)), int const&>));
116 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(std::move(v))), int const&&>));
117 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int const>(&v)), int const*>));
118
119 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float volatile>(v)), float volatile&>));
120 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float volatile>(std::move(v))), float volatile&&>));
121 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<float volatile>(&v)), float volatile*>));
122 }
123
124 {
125 variant<int const, float volatile> const v;
126
127 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(v)), int const&>));
128 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(std::move(v))), int const&&>));
129 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int const>(&v)), int const*>));
130
131 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float volatile>(v)), float const volatile&>));
132 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float volatile>(std::move(v))), float const volatile&&>));
133 BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<float volatile>(&v)), float const volatile*>));
134 }
135
136 {
137 variant<int, float> v;
138
139 BOOST_TEST_EQ( get<int>(v), 0 );
140 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
141
142 BOOST_TEST_THROWS( get<float>(v), bad_variant_access );
143 BOOST_TEST_EQ( get_if<float>(&v), nullptr );
144
145 BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
146 }
147
148 {
149 variant<int, float> const v;
150
151 BOOST_TEST_EQ( get<int>(v), 0 );
152 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
153
154 BOOST_TEST_THROWS( get<float>(v), bad_variant_access );
155 BOOST_TEST_EQ( get_if<float>(&v), nullptr );
156
157 BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
158 }
159
160 {
161 variant<int, float> v( 1 );
162
163 BOOST_TEST_EQ( get<int>(v), 1 );
164 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
165
166 BOOST_TEST_THROWS( get<float>(v), bad_variant_access );
167 BOOST_TEST_EQ( get_if<float>(&v), nullptr );
168
169 BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
170 }
171
172 {
173 variant<int, float> const v( 1 );
174
175 BOOST_TEST_EQ( get<int>(v), 1 );
176 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
177
178 BOOST_TEST_THROWS( get<float>(v), bad_variant_access );
179 BOOST_TEST_EQ( get_if<float>(&v), nullptr );
180
181 BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
182 }
183
184 {
185 variant<int, float> v( 3.14f );
186
187 BOOST_TEST_THROWS( get<int>(v), bad_variant_access );
188 BOOST_TEST_EQ( get_if<int>(&v), nullptr );
189
190 BOOST_TEST_EQ( get<float>(v), 3.14f );
191 BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
192
193 BOOST_TEST_EQ( get<float>(std::move(v)), 3.14f );
194 }
195
196 {
197 variant<int, float> const v( 3.14f );
198
199 BOOST_TEST_THROWS( get<int>(v), bad_variant_access );
200 BOOST_TEST_EQ( get_if<int>(&v), nullptr );
201
202 BOOST_TEST_EQ( get<float>(v), 3.14f );
203 BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
204
205 BOOST_TEST_EQ( get<float>(std::move(v)), 3.14f );
206 }
207
208 {
209 variant<int, float, float> v;
210
211 BOOST_TEST_EQ( get<int>(v), 0 );
212 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
213
214 BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
215 }
216
217 {
218 variant<int, float, float> const v;
219
220 BOOST_TEST_EQ( get<int>(v), 0 );
221 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
222
223 BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
224 }
225
226 {
227 variant<int, float, float> v( 1 );
228
229 BOOST_TEST_EQ( get<int>(v), 1 );
230 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
231
232 BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
233 }
234
235 {
236 variant<int, float, float> const v( 1 );
237
238 BOOST_TEST_EQ( get<int>(v), 1 );
239 BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
240
241 BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
242 }
243
244 {
245 variant<int, int, float> v( 3.14f );
246
247 BOOST_TEST_EQ( get<float>(v), 3.14f );
248 BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
249
250 BOOST_TEST_EQ( get<float>(std::move(v)), 3.14f );
251 }
252
253 {
254 variant<int, int, float> const v( 3.14f );
255
256 BOOST_TEST_EQ( get<float>(v), 3.14f );
257 BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
258
259 BOOST_TEST_EQ( get<float>(std::move(v)), 3.14f );
260 }
261
262 {
263 variant<int> * p = 0;
264 BOOST_TEST_EQ( get_if<int>(p), nullptr );
265 }
266
267 {
268 variant<int const> * p = 0;
269 BOOST_TEST_EQ( get_if<int const>(p), nullptr );
270 }
271
272 {
273 variant<int> const * p = 0;
274 BOOST_TEST_EQ( get_if<int>(p), nullptr );
275 }
276
277 {
278 variant<int const> const * p = 0;
279 BOOST_TEST_EQ( get_if<int const>(p), nullptr );
280 }
281
282 {
283 variant<int, float> * p = 0;
284
285 BOOST_TEST_EQ( get_if<int>(p), nullptr );
286 BOOST_TEST_EQ( get_if<float>(p), nullptr );
287 }
288
289 {
290 variant<int, float> const * p = 0;
291
292 BOOST_TEST_EQ( get_if<int>(p), nullptr );
293 BOOST_TEST_EQ( get_if<float>(p), nullptr );
294 }
295
296 {
297 variant<int, int, float> * p = 0;
298 BOOST_TEST_EQ( get_if<float>(p), nullptr );
299 }
300
301 {
302 variant<int, int, float> const * p = 0;
303 BOOST_TEST_EQ( get_if<float>(p), nullptr );
304 }
305
306 return boost::report_errors();
307 }
308