1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
// Copyright (c) 2017 Levon Tarakchyan
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "boost/config.hpp"
#include "boost/core/lightweight_test.hpp"
#include "boost/variant.hpp"
#include "boost/variant/apply_visitor.hpp"
#include "boost/variant/multivisitors.hpp"
#include "boost/lexical_cast.hpp"
#define lcs(val) boost::lexical_cast<std::string>(val)
struct construction_logger
{
int val_;
construction_logger(int val) : val_(val)
{
std::cout << val_ << " constructed\n";
}
construction_logger(const construction_logger& cl) :
val_(cl.val_)
{
std::cout << val_ << " copy constructed\n";
}
construction_logger(construction_logger&& cl) :
val_(cl.val_)
{
std::cout << val_ << " move constructed\n";
}
friend std::ostream& operator << (std::ostream& os, const construction_logger& cl)
{
return os << cl.val_;
}
friend std::istream& operator << (std::istream& is, construction_logger& cl)
{
return is >> cl.val_;
}
};
struct lex_streamer_explicit : boost::static_visitor<std::string>
{
template <class T>
std::string operator()(const T& val) const
{
return lcs(val);
}
template <class T, class V>
std::string operator()(const T& val, const V& val2) const
{
return lcs(val) + '+' + lcs(val2);
}
template <class T, class V, class P, class S>
std::string operator()(const T& val, const V& val2, const P& val3, const S& val4) const
{
return lcs(val) + '+' + lcs(val2) + '+' + lcs(val3) + '+' + lcs(val4);
}
};
struct lvalue_rvalue_detector : boost::static_visitor<std::string>
{
template <class T>
std::string operator()(T&&) const
{
return std::is_lvalue_reference<T>::value ? "lvalue reference"
: "rvalue reference";
}
template <class T, class V>
std::string operator()(T&& t, V&& v) const
{
return operator()(std::forward<T>(t)) + ", " + operator()(std::forward<V>(v));
}
template <class T, class V, class P>
std::string operator()(T&& t, V&& v, P&& p) const
{
return operator()(std::forward<T>(t), std::forward<V>(v)) + ", " + operator()(std::forward<P>(p));
}
template <class T, class V, class P, class S>
std::string operator()(T&& t, V&& v, P&& p, S&& s) const
{
return operator()(std::forward<T>(t), std::forward<V>(v), std::forward<P>(p)) + ", " + operator()(std::forward<S>(s));
}
};
typedef boost::variant<construction_logger, std::string> variant_type;
void test_const_ref_parameter(const variant_type& test_var)
{
std::cout << "Testing const lvalue reference visitable\n";
BOOST_TEST(boost::apply_visitor(lvalue_rvalue_detector(), test_var) == "lvalue reference");
}
void test_const_ref_parameter2(const variant_type& test_var, const variant_type& test_var2)
{
std::cout << "Testing const lvalue reference visitable\n";
BOOST_TEST(boost::apply_visitor(lvalue_rvalue_detector(), test_var, test_var2) == "lvalue reference, lvalue reference");
}
void test_const_ref_parameter4(const variant_type& test_var, const variant_type& test_var2, const variant_type& test_var3, const variant_type& test_var4)
{
std::cout << "Testing const lvalue reference visitable with multivisitor\n";
BOOST_TEST(boost::apply_visitor(lvalue_rvalue_detector(), test_var, test_var2, test_var3, test_var4)
== "lvalue reference, lvalue reference, lvalue reference, lvalue reference");
}
void test_rvalue_parameter(variant_type&& test_var)
{
std::cout << "Testing rvalue visitable\n";
const auto expected_val = lcs(test_var);
BOOST_TEST(boost::apply_visitor(lvalue_rvalue_detector(), std::move(test_var)) == "rvalue reference");
}
void test_rvalue_parameter2(variant_type&& test_var, variant_type&& test_var2)
{
std::cout << "Testing rvalue visitable\n";
BOOST_TEST(boost::apply_visitor(lvalue_rvalue_detector(), std::move(test_var), std::move(test_var2)) == "rvalue reference, rvalue reference");
}
void test_rvalue_parameter4(variant_type&& test_var, variant_type&& test_var2, variant_type&& test_var3, variant_type&& test_var4)
{
std::cout << "Testing rvalue visitable with multivisitor\n";
auto result = boost::apply_visitor(lvalue_rvalue_detector(), std::move(test_var), std::move(test_var2), std::move(test_var3), std::move(test_var4));
std::cout << "result: " << result << std::endl;
BOOST_TEST(result == "rvalue reference, rvalue reference, rvalue reference, rvalue reference");
}
#ifndef BOOST_NO_CXX14_DECLTYPE_AUTO
#define FORWARD(x) std::forward<decltype(x)>(x)
void test_cpp14_visitor(const variant_type& test_var)
{
std::cout << "Testing const lvalue visitable for c++14\n";
BOOST_TEST(boost::apply_visitor([](auto&& v) { return lvalue_rvalue_detector()(FORWARD(v)); }, test_var) == "lvalue reference");
}
void test_cpp14_mutable_visitor(const variant_type& test_var)
{
std::cout << "Testing const lvalue visitable for c++14 with inline mutable lambda\n";
BOOST_TEST(boost::apply_visitor([](auto&& v) mutable -> auto { return lvalue_rvalue_detector()(FORWARD(v)); }, test_var) == "lvalue reference");
}
void test_cpp14_visitor(const variant_type& test_var, const variant_type& test_var2)
{
std::cout << "Testing const lvalue visitable for c++14\n";
BOOST_TEST(boost::apply_visitor([](auto&& v, auto&& vv) { return lvalue_rvalue_detector()(FORWARD(v), FORWARD(vv)); }, test_var, test_var2)
== "lvalue reference, lvalue reference");
}
void test_cpp14_visitor(const variant_type& test_var, const variant_type& test_var2, const variant_type& test_var3)
{
std::cout << "Testing const lvalue visitable for c++14\n";
auto result = boost::apply_visitor([](auto&& v, auto&& t, auto&& p) { return lvalue_rvalue_detector()(FORWARD(v), FORWARD(t), FORWARD(p)); },
test_var, test_var2, test_var3);
std::cout << "result: " << result << std::endl;
BOOST_TEST(result == "lvalue reference, lvalue reference, lvalue reference");
}
void test_cpp14_visitor(variant_type& test_var)
{
std::cout << "Testing lvalue visitable for c++14\n";
BOOST_TEST(boost::apply_visitor([](auto& v) { return lvalue_rvalue_detector()(v); }, test_var) == "lvalue reference");
}
void test_cpp14_mutable_visitor(variant_type& test_var)
{
std::cout << "Testing lvalue visitable for c++14 with inline mutable lambda\n";
BOOST_TEST(boost::apply_visitor([](auto& v) mutable -> auto { return lvalue_rvalue_detector()(v); }, test_var) == "lvalue reference");
}
void test_cpp14_visitor(variant_type& test_var, variant_type& test_var2)
{
std::cout << "Testing lvalue visitable for c++14\n";
BOOST_TEST(boost::apply_visitor([](auto& v, auto& vv) { return lvalue_rvalue_detector()(v, vv); }, test_var, test_var2)
== "lvalue reference, lvalue reference");
}
void test_cpp14_visitor(variant_type& test_var, variant_type& test_var2, variant_type& test_var3)
{
std::cout << "Testing lvalue visitable for c++14\n";
auto result = boost::apply_visitor([](auto& v, auto& t, auto& p) { return lvalue_rvalue_detector()(v, t, p); },
test_var, test_var2, test_var3);
std::cout << "result: " << result << std::endl;
BOOST_TEST(result == "lvalue reference, lvalue reference, lvalue reference");
}
void test_cpp14_visitor(variant_type&& test_var)
{
std::cout << "Testing rvalue visitable for c++14\n";
BOOST_TEST(boost::apply_visitor([](auto&& v) { return lvalue_rvalue_detector()(FORWARD(v)); }, std::move(test_var)) == "rvalue reference");
}
void test_cpp14_visitor(variant_type&& test_var, variant_type&& test_var2)
{
std::cout << "Testing rvalue visitable for c++14\n";
BOOST_TEST(boost::apply_visitor([](auto&& v, auto&& vv) { return lvalue_rvalue_detector()(FORWARD(v), FORWARD(vv)); }, std::move(test_var), std::move(test_var2))
== "rvalue reference, rvalue reference");
}
void test_cpp14_visitor(variant_type&& test_var, variant_type&& test_var2, variant_type&& test_var3)
{
std::cout << "Testing rvalue visitable for c++14\n";
auto result = boost::apply_visitor([](auto&& v, auto&& t, auto&& p) { return lvalue_rvalue_detector()(FORWARD(v), FORWARD(t), FORWARD(p)); },
std::move(test_var), std::move(test_var2), std::move(test_var3));
std::cout << "result: " << result << std::endl;
BOOST_TEST(result == "rvalue reference, rvalue reference, rvalue reference");
}
#endif // #ifndef BOOST_NO_CXX14_DECLTYPE_AUTO
void run_const_lvalue_ref_tests()
{
const variant_type v1(1), v2(2), v3(3), v4(4);
test_const_ref_parameter(v1);
test_const_ref_parameter2(v1, v2);
test_const_ref_parameter4(v1, v2, v3, v4);
}
void run_rvalue_ref_tests()
{
variant_type v1(10), v2(20), v3(30);
test_rvalue_parameter(std::move(v1));
test_rvalue_parameter2(std::move(v2), std::move(v3));
variant_type vv1(100), vv2(200), vv3(300), vv4(400);
test_rvalue_parameter4(std::move(vv1), std::move(vv2), std::move(vv3), std::move(vv4));
}
void run_mixed_tests()
{
variant_type v1(1), v2(2);
std::cout << "Testing lvalue + rvalue visitable\n";
BOOST_TEST(boost::apply_visitor(lvalue_rvalue_detector(), v1, variant_type(10)) == "lvalue reference, rvalue reference");
std::cout << "Testing rvalue + lvalue visitable\n";
BOOST_TEST(boost::apply_visitor(lvalue_rvalue_detector(), variant_type(10), v1) == "rvalue reference, lvalue reference");
std::cout << "Testing rvalue + lvalue + rvalue visitable\n";
BOOST_TEST(boost::apply_visitor(lvalue_rvalue_detector(), variant_type(10), v1, variant_type(20)) == "rvalue reference, lvalue reference, rvalue reference");
std::cout << "Testing lvalue + rvalue + lvalue + rvalue visitable\n";
BOOST_TEST(boost::apply_visitor(lvalue_rvalue_detector(), v1, variant_type(10), v2, variant_type(20)) == "lvalue reference, rvalue reference, lvalue reference, rvalue reference");
}
void run_cpp14_mixed_tests()
{
#ifndef BOOST_NO_CXX14_DECLTYPE_AUTO
variant_type v1(1), v2(2);
std::cout << "Testing lvalue + rvalue visitable\n";
BOOST_TEST(boost::apply_visitor([](auto&& v, auto&& t) { return lvalue_rvalue_detector()(FORWARD(v), FORWARD(t)); },
v1, variant_type(10)) == "lvalue reference, rvalue reference");
std::cout << "Testing rvalue + lvalue visitable\n";
BOOST_TEST(boost::apply_visitor([](auto&& v, auto&& t) { return lvalue_rvalue_detector()(FORWARD(v), FORWARD(t)); },
variant_type(10), v1) == "rvalue reference, lvalue reference");
std::cout << "Testing rvalue + lvalue + lvalue visitable\n";
BOOST_TEST(boost::apply_visitor([](auto&& v, auto&& t, auto&& p) { return lvalue_rvalue_detector()(FORWARD(v), FORWARD(t), FORWARD(p)); },
variant_type(10), v1, v2) == "rvalue reference, lvalue reference, lvalue reference");
std::cout << "Testing lvalue + rvalue + lvalue visitable\n";
BOOST_TEST(boost::apply_visitor([](auto&& v, auto&& t, auto&& p) { return lvalue_rvalue_detector()(FORWARD(v), FORWARD(t), FORWARD(p)); },
v1, variant_type(10), v2) == "lvalue reference, rvalue reference, lvalue reference");
#endif // #ifndef BOOST_NO_CXX14_DECLTYPE_AUTO
}
void run_cpp14_tests()
{
#ifndef BOOST_NO_CXX14_DECLTYPE_AUTO
variant_type const c1(10), c2(20), c3(30);
variant_type v1(10), v2(20), v3(30);
test_cpp14_visitor(c1);
test_cpp14_mutable_visitor(c1);
test_cpp14_visitor(c2, c3);
test_cpp14_visitor(c1, c2, c3);
test_cpp14_visitor(v1);
test_cpp14_mutable_visitor(v1);
test_cpp14_visitor(v2, v3);
test_cpp14_visitor(v1, v2, v3);
test_cpp14_visitor(std::move(v1));
test_cpp14_visitor(std::move(v2), std::move(v3));
variant_type vv1(100), vv2(200), vv3(300);
test_cpp14_visitor(std::move(vv1), std::move(vv2), std::move(vv3));
#endif
}
int main()
{
run_const_lvalue_ref_tests();
run_rvalue_ref_tests();
run_mixed_tests();
run_cpp14_mixed_tests();
run_cpp14_tests();
return boost::report_errors();
}
|