1#include <sysio/vm/variant.hpp>
8TEST_CASE(
"Testing variant with stateless class",
"[variant_stateless_tests]") {
10 int operator()(
signed char v) {
return v + 1; }
11 int operator()(
short v) {
return v + 2; }
12 int operator()(
int v) {
return v + 3; }
13 int operator()(
long v) {
return v + 4; }
14 int operator()(
float v) {
return v + 5.0f; }
15 int operator()(
double v) {
return v + 6.0; }
21 variant_type v((
signed char)42);
25 variant_type v((
short)142);
29 variant_type v((
int)242);
33 variant_type v((
long)342);
37 variant_type v((
float)442);
41 variant_type v((
double)542);
47TEST_CASE(
"Testing argument forwarding for visit",
"[variant_forward_tests]") {
57 void operator()(
double v) & {
r = v + 1; }
58 void operator()(...) {}
60 variant_type v((
double)42);
69 void operator()(
double v) && {
r = v + 1; }
70 void operator()(...) {}
72 variant_type v((
double)42);
81 void operator()(
double v)
const & {
r = v + 1; }
82 void operator()(...) {}
84 variant_type v((
double)42);
95 void operator()(
double& v) { v = v + 1; }
96 void operator()(...) {}
98 variant_type v((
double)42);
100 CHECK(v.get<
double>() == 43);
106 void operator()(
double&& v) {
109 void operator()(...) {}
111 variant_type v((
double)42);
112 visit(vis{}, std::move(v));
113 CHECK(v.get<
double>() == 43);
119 void operator()(
const double& v) {
120 const_cast<double&
>(v) = v + 1;
122 void operator()(...) {}
125 variant_type v((
double)42);
126 visit(vis{},
static_cast<const variant_type&
>(v));
127 CHECK(v.get<
double>() == 43);
131template<
typename V,
typename T>
136 v =
static_cast<T&&
>(t);
137 result = result && (v.template get<T>() == expected);
139 result = result && (v.template get<T>() == expected);
140 v =
static_cast<const T&
>(t);
141 result = result && (v.template get<T>() == expected);
144 v =
static_cast<V&
>(v);
145 result = result && (v.template get<T>() == expected);
146 v =
static_cast<V&&
>(v);
147 result = result && (v.template get<T>() == expected);
148 v =
static_cast<const V&
>(v);
149 result = result && (v.template get<T>() == expected);
153template<
typename V,
typename T>
158 static_assert(std::is_same_v<
decltype(v.template get<T>()),
T&>,
"check get result type");
159 static_assert(std::is_same_v<decltype(static_cast<V&&>(v).template get<T>()),
T&&>,
"check get result type");
160 static_assert(std::is_same_v<decltype(static_cast<const V&>(v).template get<T>()),
const T&>,
"check get result type");
161 static_assert(std::is_same_v<decltype(static_cast<const V&&>(v).template get<T>()),
const T&&>,
"check get result type");
163 result = result && (v.template get<T>() == expected);
164 result = result && (
static_cast<V&&
>(v).
template get<T>() == expected);
165 result = result && (
static_cast<const V&
>(v).
template get<T>() == expected);
166 result = result && (
static_cast<const V&&
>(v).
template get<T>() == expected);
170TEST_CASE(
"Testing constexpr variant",
"[variant_constexpr_tests]") {
174 constexpr variant_type v{(
signed char)2};
175 constexpr signed char result_lv{v.get<
signed char>()};
176 CHECK(result_lv == 2);
177 constexpr signed char result_rv{variant_type{(
signed char)4}.get<
signed char>()};
178 CHECK(result_rv == 4);
181 constexpr variant_type v{(double)2};
182 constexpr double result_lv{v.get<
double>()};
183 CHECK(result_lv == 2);
184 constexpr double result_rv{variant_type{(double)4}.get<
double>()};
185 CHECK(result_rv == 4);
197#ifdef CONSTEXPR_STD_INVOKE
200 constexpr int operator()(
signed char v) {
return v + 1; }
201 constexpr int operator()(
short v) {
return v + 2; }
202 constexpr int operator()(
int v) {
return v + 3; }
203 constexpr int operator()(
long v) {
return v + 4; }
204 constexpr int operator()(
float v) {
return v + 5.0f; }
205 constexpr int operator()(
double v) {
return v + 6.0; }
211 constexpr int test =
visit(vis{}, variant_type{(
signed char)2});
233 variant_type v((
double)42);
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
constexpr auto visit(Visitor &&vis, Variant &&var)
#define T(meth, val, expected)
minimal_vis(const minimal_vis &)=delete
int operator()(T v) const
static minimal_vis & make()
minimal_vis & operator=(const minimal_vis &)=delete
void check_requirements()
constexpr bool variant_constexpr_assign(T &&t)
constexpr bool variant_constexpr_get(T &&t)