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
18 struct X
19 {
operator intX20 operator int() const { return 2; }
21 };
22
main()23 int main()
24 {
25 {
26 variant<int> v( 1 );
27
28 BOOST_TEST_EQ( v.index(), 0 );
29 BOOST_TEST_EQ( get<0>(v), 1 );
30 }
31
32 {
33 variant<int> v( 'a' );
34
35 BOOST_TEST_EQ( v.index(), 0 );
36 BOOST_TEST_EQ( get<0>(v), 'a' );
37 }
38
39 {
40 variant<int> v( X{} );
41
42 BOOST_TEST_EQ( v.index(), 0 );
43 BOOST_TEST_EQ( get<0>(v), 2 );
44 }
45
46 {
47 variant<int const> v( 1 );
48
49 BOOST_TEST_EQ( v.index(), 0 );
50 BOOST_TEST_EQ( get<0>(v), 1 );
51 }
52
53 {
54 variant<int const> v( 'a' );
55
56 BOOST_TEST_EQ( v.index(), 0 );
57 BOOST_TEST_EQ( get<0>(v), 'a' );
58 }
59
60 {
61 variant<int const> v( X{} );
62
63 BOOST_TEST_EQ( v.index(), 0 );
64 BOOST_TEST_EQ( get<0>(v), 2 );
65 }
66
67 {
68 variant<int, float, std::string> v( 1 );
69
70 BOOST_TEST_EQ( v.index(), 0 );
71 BOOST_TEST( holds_alternative<int>(v) );
72 BOOST_TEST_EQ( get<0>(v), 1 );
73 }
74
75 {
76 variant<int, float, std::string> v( 'a' );
77
78 BOOST_TEST_EQ( v.index(), 0 );
79 BOOST_TEST( holds_alternative<int>(v) );
80 BOOST_TEST_EQ( get<0>(v), 'a' );
81 }
82
83 {
84 variant<int, float, std::string> v( X{} );
85
86 BOOST_TEST_EQ( v.index(), 0 );
87 BOOST_TEST( holds_alternative<int>(v) );
88 BOOST_TEST_EQ( get<0>(v), 2 );
89 }
90
91 {
92 variant<int, float, std::string> v( 3.14f );
93
94 BOOST_TEST_EQ( v.index(), 1 );
95 BOOST_TEST( holds_alternative<float>(v) );
96 BOOST_TEST_EQ( get<1>(v), 3.14f );
97 }
98
99 {
100 variant<int, float, std::string> v( "text" );
101
102 BOOST_TEST_EQ( v.index(), 2 );
103 BOOST_TEST( holds_alternative<std::string>(v) );
104 BOOST_TEST_EQ( get<2>(v), std::string("text") );
105 }
106
107 {
108 variant<int, int, float, std::string> v( 3.14f );
109
110 BOOST_TEST_EQ( v.index(), 2 );
111 BOOST_TEST( holds_alternative<float>(v) );
112 BOOST_TEST_EQ( get<2>(v), 3.14f );
113 }
114
115 {
116 variant<int, int, float, std::string> v( "text" );
117
118 BOOST_TEST_EQ( v.index(), 3 );
119 BOOST_TEST( holds_alternative<std::string>(v) );
120 BOOST_TEST_EQ( get<3>(v), std::string("text") );
121 }
122
123 return boost::report_errors();
124 }
125