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
|
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
#define CAF_SUITE variant
#include "caf/variant.hpp"
#include "core-test.hpp"
#include <string>
#include "caf/actor_system.hpp"
#include "caf/actor_system_config.hpp"
#include "caf/byte_buffer.hpp"
#include "caf/deep_to_string.hpp"
#include "caf/none.hpp"
using namespace std::string_literals;
using namespace caf;
// 20 integer wrappers for building a variant with 20 distinct types
#define i_n(n) \
class i##n { \
public: \
i##n(int y = 0) : x(y) { \
} \
i##n(i##n&& other) : x(other.x) { \
other.x = 0; \
} \
i##n& operator=(i##n&& other) { \
x = other.x; \
other.x = 0; \
return *this; \
} \
i##n(const i##n&) = default; \
i##n& operator=(const i##n&) = default; \
int x; \
}; \
bool operator==(int x, i##n y) { \
return x == y.x; \
} \
bool operator==(i##n x, int y) { \
return y == x; \
} \
bool operator==(i##n x, i##n y) { \
return x.x == y.x; \
} \
template <class Inspector> \
bool inspect(Inspector& f, i##n& x) { \
return f.object(x).fields(f.field("x", x.x)); \
}
#define macro_repeat20(macro) \
macro(01) macro(02) macro(03) macro(04) macro(05) macro(06) macro(07) \
macro(08) macro(09) macro(10) macro(11) macro(12) macro(13) macro(14) \
macro(15) macro(16) macro(17) macro(18) macro(19) macro(20)
macro_repeat20(i_n)
// a variant with 20 element types
using v20 = variant<i01, i02, i03, i04, i05, i06, i07, i08, i09, i10, i11,
i12, i13, i14, i15, i16, i17, i18, i19, i20>;
#define VARIANT_EQ(x, y) \
do { \
using type = std::decay_t<decltype(y)>; \
auto&& tmp = x; \
if (CHECK(holds_alternative<type>(tmp))) \
CHECK_EQ(get<type>(tmp), y); \
} while (false)
#define v20_test(n) \
x3 = i##n{0x##n}; \
VARIANT_EQ(v20{x3}, i##n{0x##n}); \
x4 = x3; \
VARIANT_EQ(x4, i##n{0x##n}); \
VARIANT_EQ(v20{std::move(x3)}, i##n{0x##n}); \
VARIANT_EQ(x3, i##n{0}); \
x3 = std::move(x4); \
VARIANT_EQ(x4, i##n{0}); \
VARIANT_EQ(x3, i##n{0x##n});
// copy construction, copy assign, move construction, move assign
// and finally serialization round-trip
CAF_TEST(copying_moving_roundtrips) {
actor_system_config cfg;
actor_system sys{cfg};
variant<int, none_t> x2;
VARIANT_EQ(x2, 0);
v20 x3;
VARIANT_EQ(x3, i01{0});
v20 x4;
macro_repeat20(v20_test);
}
namespace {
struct test_visitor {
template <class... Ts>
std::string operator()(const Ts&... xs) {
return deep_to_string_as_tuple(xs...);
}
};
} // namespace
CAF_TEST(constructors) {
variant<int, std::string> a{42};
variant<float, int, std::string> b{"bar"s};
variant<int, std::string, double> c{123};
variant<bool, uint8_t> d{uint8_t{252}};
VARIANT_EQ(a, 42);
VARIANT_EQ(b, "bar"s);
VARIANT_EQ(c, 123);
VARIANT_EQ(d, uint8_t{252});
}
CAF_TEST(n_ary_visit) {
variant<int, std::string> a{42};
variant<float, int, std::string> b{"bar"s};
variant<int, std::string, double> c{123};
test_visitor f;
CHECK_EQ(visit(f, a), R"__([42])__");
CHECK_EQ(visit(f, a, b), R"__([42, "bar"])__");
CHECK_EQ(visit(f, a, b, c), R"__([42, "bar", 123])__");
}
CAF_TEST(get_if) {
variant<int, std::string> b = "foo"s;
MESSAGE("test get_if directly");
CHECK_EQ(get_if<int>(&b), nullptr);
CHECK_NE(get_if<std::string>(&b), nullptr);
MESSAGE("test get_if via unit test framework");
VARIANT_EQ(b, "foo"s);
}
CAF_TEST(less_than) {
using variant_type = variant<char, int>;
auto a = variant_type{'x'};
auto b = variant_type{'y'};
CHECK(a < b);
CHECK(!(a > b));
CHECK(a <= b);
CHECK(!(a >= b));
b = 42;
CHECK(a < b);
CHECK(!(a > b));
CHECK(a <= b);
CHECK(!(a >= b));
a = 42;
CHECK(!(a < b));
CHECK(!(a > b));
CHECK(a <= b);
CHECK(a >= b);
b = 'x';
}
CAF_TEST(equality) {
variant<uint16_t, int> x = 42;
variant<uint16_t, int> y = uint16_t{42};
CHECK_NE(x, y);
}
|