Files
scylladb/test/boost/expr_test.cc
Jan Ciolek 353ab8f438 cql3: expr: Add a test to show that std::forward is needed in expr::visit
Adds a test with a vistior that can only be used as a rvalue.
Without std::forward in expr::visit this test doesn't compile.

Signed-off-by: Jan Ciolek <jan.ciolek@scylladb.com>
2022-02-18 14:19:49 +01:00

87 lines
2.5 KiB
C++

#define BOOST_TEST_MODULE core
#include <boost/test/unit_test.hpp>
#include <utility>
#include "cql3/expr/expression.hh"
#include "utils/overloaded_functor.hh"
#include <cassert>
using namespace cql3::expr;
bind_variable new_bind_variable(int bind_index) {
return bind_variable {
.shape = bind_variable::shape_type::scalar,
.bind_index = bind_index,
.receiver = nullptr
};
}
BOOST_AUTO_TEST_CASE(expr_visit_get_int) {
expression e = new_bind_variable(1245);
int read_value = visit(overloaded_functor {
[](const bind_variable& bv) -> int { return bv.bind_index; },
[](const auto&) -> int { throw std::runtime_error("Unreachable"); }
}, e);
BOOST_REQUIRE_EQUAL(read_value, 1245);
}
BOOST_AUTO_TEST_CASE(expr_visit_void_return) {
expression e = new_bind_variable(1245);
visit(overloaded_functor {
[](const bind_variable& bv) { BOOST_REQUIRE_EQUAL(bv.bind_index, 1245); },
[](const auto&) { throw std::runtime_error("Unreachable"); }
}, e);
}
BOOST_AUTO_TEST_CASE(expr_visit_const_ref) {
const expression e = new_bind_variable(123);
const bind_variable& ref = visit(overloaded_functor {
[](const bind_variable& bv) -> const bind_variable& { return bv; },
[](const auto&) -> const bind_variable& { throw std::runtime_error("Unreachable"); }
}, e);
BOOST_REQUIRE_EQUAL(ref.bind_index, 123);
}
BOOST_AUTO_TEST_CASE(expr_visit_ref) {
expression e = new_bind_variable(456);
bind_variable& ref = visit(overloaded_functor {
[](bind_variable& bv) -> bind_variable& { return bv; },
[](auto&) -> bind_variable& { throw std::runtime_error("Unreachable"); }
}, e);
BOOST_REQUIRE_EQUAL(ref.bind_index, 456);
ref.bind_index = 135;
bind_variable& ref2 = visit(overloaded_functor {
[](bind_variable& bv) -> bind_variable& { return bv; },
[](auto&) -> bind_variable& { throw std::runtime_error("Unreachable"); }
}, e);
BOOST_REQUIRE_EQUAL(ref2.bind_index, 135);
}
struct rvalue_visitor {
rvalue_visitor(){};
rvalue_visitor(const rvalue_visitor&) = delete;
bind_variable& operator()(bind_variable& bv) && { return bv; }
bind_variable& operator()(auto&) && { throw std::runtime_error("Unreachable"); }
} v;
BOOST_AUTO_TEST_CASE(expr_visit_visitor_rvalue) {
expression e = new_bind_variable(456);
rvalue_visitor visitor;
bind_variable& ref2 = visit(std::move(visitor), e);
BOOST_REQUIRE_EQUAL(ref2.bind_index, 456);
}