]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/variant/test/variant_multivisit_test.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / variant / test / variant_multivisit_test.cpp
1 //-----------------------------------------------------------------------------
2 // boost-libs variant/test/variant_multivisit_test.cpp source file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
5 //
6 // Copyright (c) 2013-2015 Antony Polukhin
7 //
8 // Distributed under the Boost Software License, Version 1.0. (See
9 // accompanying file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt)
11
12 #include "boost/config.hpp"
13 #include "boost/noncopyable.hpp"
14 #define BOOST_VARAINT_MAX_MULTIVIZITOR_PARAMS 5
15 #include "boost/variant/multivisitors.hpp"
16 #include "boost/variant.hpp"
17
18 #include "boost/test/minimal.hpp"
19
20 struct my_noncopyable : boost::noncopyable {
21 my_noncopyable(){}
22 ~my_noncopyable(){}
23 };
24
25 typedef boost::variant<my_noncopyable, int> variant_noncopy_t;
26
27
28 typedef boost::variant<char, unsigned char, signed char, unsigned short, int, unsigned int> variant6_t;
29
30 struct test_visitor: boost::static_visitor<> {
31 // operators that shall not be called
32 template <class T1, class T2, class T3>
33 void operator()(T1&, T2&, T3&) const
34 {
35 BOOST_CHECK(false);
36 }
37
38 template <class T1, class T2, class T3, class T4>
39 void operator()(T1&, T2&, T3&, T4&) const
40 {
41 BOOST_CHECK(false);
42 }
43
44 template <class T1, class T2, class T3, class T4, class T5>
45 void operator()(T1&, T2&, T3&, T4&, T5&) const
46 {
47 BOOST_CHECK(false);
48 }
49
50 // operators that are OK to call
51 void operator()(char v0, unsigned char v1, signed char v2) const
52 {
53 BOOST_CHECK(v0 == 0);
54 BOOST_CHECK(v1 == 1);
55 BOOST_CHECK(v2 == 2);
56 }
57
58 void operator()(char v0, unsigned char v1, signed char v2, unsigned short v3) const
59 {
60 BOOST_CHECK(v0 == 0);
61 BOOST_CHECK(v1 == 1);
62 BOOST_CHECK(v2 == 2);
63 BOOST_CHECK(v3 == 3);
64 }
65
66 void operator()(char v0, unsigned char v1, signed char v2, unsigned short v3, int v4) const
67 {
68 BOOST_CHECK(v0 == 0);
69 BOOST_CHECK(v1 == 1);
70 BOOST_CHECK(v2 == 2);
71 BOOST_CHECK(v3 == 3);
72 BOOST_CHECK(v4 == 4);
73 }
74
75
76 // Noncopyables
77 void operator()(my_noncopyable&, my_noncopyable&, my_noncopyable&) const {
78 BOOST_CHECK(true);
79 }
80 void operator()(my_noncopyable&, my_noncopyable&, my_noncopyable&, my_noncopyable&) const {
81 BOOST_CHECK(true);
82 }
83 void operator()(my_noncopyable&, my_noncopyable&, my_noncopyable&, my_noncopyable&, my_noncopyable&) const {
84 BOOST_CHECK(true);
85 }
86 void operator()(my_noncopyable&, my_noncopyable&, my_noncopyable&, my_noncopyable&, my_noncopyable&, my_noncopyable&) const {
87 BOOST_CHECK(true);
88 }
89 };
90
91 typedef boost::variant<int, double, bool> bool_like_t;
92 typedef boost::variant<int, double> arithmetics_t;
93
94 struct if_visitor: public boost::static_visitor<arithmetics_t> {
95 template <class T0, class T1, class T2>
96 arithmetics_t operator()(T0 b, T1 v1, T2 v2) const {
97 if (!!b) {
98 return v1;
99 } else {
100 return v2;
101 }
102 }
103 };
104
105
106 int test_main(int , char* [])
107 {
108 test_visitor v;
109
110 variant6_t v_array6[6];
111 v_array6[0] = char(0);
112 v_array6[1] = static_cast<unsigned char>(1);
113 v_array6[2] = static_cast<signed char>(2);
114 v_array6[3] = static_cast<unsigned short>(3);
115 v_array6[4] = static_cast<int>(4);
116 v_array6[5] = static_cast<unsigned int>(5);
117
118 boost::apply_visitor(v, v_array6[0], v_array6[1], v_array6[2]);
119 boost::apply_visitor(test_visitor(), v_array6[0], v_array6[1], v_array6[2]);
120
121 // Following test also pass, but requires many Gigabytes of RAM for compilation and compile for about 15 minutes
122 //#define BOOST_VARIANT_MULTIVISITORS_TEST_VERY_EXTREME
123 #ifdef BOOST_VARIANT_MULTIVISITORS_TEST_VERY_EXTREME
124 boost::apply_visitor(v, v_array6[0], v_array6[1], v_array6[2], v_array6[3]);
125 boost::apply_visitor(test_visitor(), v_array6[0], v_array6[1], v_array6[2], v_array6[3]);
126
127 boost::apply_visitor(v, v_array6[0], v_array6[1], v_array6[2], v_array6[3], v_array6[4]);
128 boost::apply_visitor(test_visitor(), v_array6[0], v_array6[1], v_array6[2], v_array6[3], v_array6[4]);
129 #endif
130
131 bool_like_t v0(1), v1(true), v2(1.0);
132
133 BOOST_CHECK(
134 boost::apply_visitor(if_visitor(), v0, v1, v2)
135 ==
136 arithmetics_t(true)
137 );
138
139 #if !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES)
140 if_visitor if_vis;
141 BOOST_CHECK(
142 boost::apply_visitor(if_vis)(v0, v1, v2)
143 ==
144 arithmetics_t(true)
145 );
146 #endif
147
148
149 variant_noncopy_t vnonc[6];
150 boost::apply_visitor(v, vnonc[0], vnonc[1], vnonc[2]);
151 boost::apply_visitor(test_visitor(), vnonc[0], vnonc[1], vnonc[2], vnonc[3]);
152
153 #ifdef BOOST_VARIANT_MULTIVISITORS_TEST_VERY_EXTREME
154 boost::apply_visitor(v, vnonc[0], vnonc[1], vnonc[2], vnonc[3], vnonc[4]);
155 boost::apply_visitor(test_visitor(), vnonc[0], vnonc[1], vnonc[2], vnonc[3], vnonc[4], vnonc[5]);
156 #endif
157
158 return boost::exit_success;
159 }