Merge commit '62e8601919faca57a0fa4be1a910458390450cc9' as 'third_party/variant'

This commit is contained in:
Patrick Niklaus
2016-03-24 21:32:27 +01:00
51 changed files with 15816 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
#include "variant.hpp"
#define NAME_EXT " i-d"
using variant_type = mapbox::util::variant<int, double>;
#include "binary_visitor_impl.hpp"
+7
View File
@@ -0,0 +1,7 @@
#include "variant.hpp"
#define NAME_EXT " b-i-d"
using variant_type = mapbox::util::variant<bool, int, double>;
#include "binary_visitor_impl.hpp"
+7
View File
@@ -0,0 +1,7 @@
#include "variant.hpp"
#define NAME_EXT " i-d-b"
using variant_type = mapbox::util::variant<int, double, bool>;
#include "binary_visitor_impl.hpp"
+7
View File
@@ -0,0 +1,7 @@
#include "variant.hpp"
#define NAME_EXT " b-i-d-c"
using variant_type = mapbox::util::variant<bool, int, double, char>;
#include "binary_visitor_impl.hpp"
+7
View File
@@ -0,0 +1,7 @@
#include "variant.hpp"
#define NAME_EXT " b-i-c-d-i"
using variant_type = mapbox::util::variant<bool, int, char, double, int>;
#include "binary_visitor_impl.hpp"
+7
View File
@@ -0,0 +1,7 @@
#include "variant.hpp"
#define NAME_EXT " b-i-i-d-c-u"
using variant_type = mapbox::util::variant<bool, int, int, double, char, short int>;
#include "binary_visitor_impl.hpp"
+204
View File
@@ -0,0 +1,204 @@
#include <type_traits>
#include "catch.hpp"
#include "variant_io.hpp"
struct add_visitor
{
add_visitor() {}
template <typename A, typename B>
double operator()(A a, B b) const
{
return a + b;
}
};
TEST_CASE("const binary visitor works on const variants" NAME_EXT, "[visitor][binary visitor]")
{
const variant_type a{7};
const variant_type b = 3;
const variant_type c{7.1};
const variant_type d = 2.9;
const add_visitor v;
REQUIRE(mapbox::util::apply_visitor(v, a, b) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(v, c, d) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(v, a, c) == Approx(14.1));
REQUIRE(mapbox::util::apply_visitor(v, a, d) == Approx(9.9));
REQUIRE(mapbox::util::apply_visitor(v, b, a) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(v, d, c) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(v, c, a) == Approx(14.1));
REQUIRE(mapbox::util::apply_visitor(v, d, a) == Approx(9.9));
}
TEST_CASE("non-const binary visitor works on const variants" NAME_EXT, "[visitor][binary visitor]")
{
const variant_type a = 7;
const variant_type b = 3;
const variant_type c = 7.1;
const variant_type d = 2.9;
add_visitor v;
REQUIRE(mapbox::util::apply_visitor(v, a, b) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(v, c, d) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(v, a, c) == Approx(14.1));
REQUIRE(mapbox::util::apply_visitor(v, a, d) == Approx(9.9));
REQUIRE(mapbox::util::apply_visitor(v, b, a) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(v, d, c) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(v, c, a) == Approx(14.1));
REQUIRE(mapbox::util::apply_visitor(v, d, a) == Approx(9.9));
}
TEST_CASE("const binary visitor works on non-const variants" NAME_EXT, "[visitor][binary visitor]")
{
variant_type a = 7;
variant_type b = 3;
variant_type c = 7.1;
variant_type d = 2.9;
const add_visitor v;
REQUIRE(mapbox::util::apply_visitor(v, a, b) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(v, c, d) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(v, a, c) == Approx(14.1));
REQUIRE(mapbox::util::apply_visitor(v, a, d) == Approx(9.9));
REQUIRE(mapbox::util::apply_visitor(v, b, a) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(v, d, c) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(v, c, a) == Approx(14.1));
REQUIRE(mapbox::util::apply_visitor(v, d, a) == Approx(9.9));
}
TEST_CASE("non-const binary visitor works on non-const variants" NAME_EXT, "[visitor][binary visitor]")
{
variant_type a = 7;
variant_type b = 3;
variant_type c = 7.1;
variant_type d = 2.9;
add_visitor v;
REQUIRE(mapbox::util::apply_visitor(v, a, b) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(v, c, d) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(v, a, c) == Approx(14.1));
REQUIRE(mapbox::util::apply_visitor(v, a, d) == Approx(9.9));
REQUIRE(mapbox::util::apply_visitor(v, b, a) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(v, d, c) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(v, c, a) == Approx(14.1));
REQUIRE(mapbox::util::apply_visitor(v, d, a) == Approx(9.9));
}
TEST_CASE("rvalue binary visitor works on const variants" NAME_EXT, "[visitor][binary visitor]")
{
const variant_type a = 7;
const variant_type b = 3;
const variant_type c = 7.1;
const variant_type d = 2.9;
REQUIRE(mapbox::util::apply_visitor(add_visitor{}, a, b) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(add_visitor{}, c, d) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(add_visitor{}, a, c) == Approx(14.1));
REQUIRE(mapbox::util::apply_visitor(add_visitor{}, a, d) == Approx(9.9));
REQUIRE(mapbox::util::apply_visitor(add_visitor{}, b, a) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(add_visitor{}, d, c) == Approx(10));
REQUIRE(mapbox::util::apply_visitor(add_visitor{}, c, a) == Approx(14.1));
REQUIRE(mapbox::util::apply_visitor(add_visitor{}, d, a) == Approx(9.9));
}
struct sum_mul_visitor
{
double sum;
sum_mul_visitor() : sum(0.0) {}
template <typename A, typename B>
double operator()(A a, B b)
{
double m = a * b;
sum += m;
return m;
}
};
TEST_CASE("mutable binary visitor works" NAME_EXT, "[visitor][binary visitor]")
{
const variant_type a = 2;
const variant_type b = 3;
const variant_type c = 0.1;
const variant_type d = 0.2;
sum_mul_visitor v;
REQUIRE(mapbox::util::apply_visitor(v, a, b) == Approx(6));
REQUIRE(mapbox::util::apply_visitor(v, c, d) == Approx(0.02));
REQUIRE(mapbox::util::apply_visitor(v, a, c) == Approx(0.2));
REQUIRE(mapbox::util::apply_visitor(v, a, d) == Approx(0.4));
REQUIRE(v.sum == Approx(6.62));
REQUIRE(mapbox::util::apply_visitor(v, b, a) == Approx(6));
REQUIRE(mapbox::util::apply_visitor(v, d, c) == Approx(0.02));
REQUIRE(mapbox::util::apply_visitor(v, c, a) == Approx(0.2));
REQUIRE(mapbox::util::apply_visitor(v, d, a) == Approx(0.4));
}
struct swap_visitor
{
swap_visitor(){};
template <typename A, typename B>
void operator()(A& a, B& b) const
{
using T = typename std::common_type<A, B>::type;
T tmp = a;
a = b;
b = tmp;
}
};
TEST_CASE("static mutating visitor on mutable variants works" NAME_EXT, "[visitor][binary visitor]")
{
variant_type a = 2;
variant_type b = 3;
variant_type c = 0.1;
variant_type d = 0.2;
const swap_visitor v;
SECTION("swap a and b")
{
mapbox::util::apply_visitor(v, a, b);
REQUIRE(a.get<int>() == 3);
REQUIRE(b.get<int>() == 2);
}
SECTION("swap c and d")
{
mapbox::util::apply_visitor(v, c, d);
REQUIRE(c.get<double>() == Approx(0.2));
REQUIRE(d.get<double>() == Approx(0.1));
}
SECTION("swap a and c")
{
mapbox::util::apply_visitor(v, a, c);
REQUIRE(a.get<int>() == 0);
REQUIRE(c.get<double>() == Approx(2.0));
}
SECTION("swap c and a")
{
mapbox::util::apply_visitor(v, c, a);
REQUIRE(a.get<int>() == 0);
REQUIRE(c.get<double>() == Approx(2.0));
}
}
+48
View File
@@ -0,0 +1,48 @@
#include "catch.hpp"
#include "variant.hpp"
#include "variant_io.hpp"
// https://github.com/mapbox/variant/issues/21
static int count;
struct t1
{
int value;
t1(int v) : value(v)
{
++count;
}
~t1()
{
--count;
}
};
struct t2
{
int value;
t2(int v) : value(v)
{ // constructor fails
throw std::runtime_error("fail");
}
};
TEST_CASE("set() works cleanly even if the constructor throws ", "[variant]")
{
using variant_type = mapbox::util::variant<t1, t2>;
count = 0;
{
variant_type v{42};
REQUIRE(v.is<t1>());
REQUIRE(v.get<t1>().value == 42);
REQUIRE_THROWS({
v.set<t2>(13);
});
}
REQUIRE(count == 0);
}
+36
View File
@@ -0,0 +1,36 @@
#include "catch.hpp"
#include "variant.hpp"
#include "variant_io.hpp"
#include <string>
template <typename T>
struct mutating_visitor
{
mutating_visitor(T& val)
: val_(val) {}
void operator()(T& val) const
{
val = val_;
}
template <typename T1>
void operator()(T1&) const
{
} // no-op
T& val_;
};
TEST_CASE("variant visitation", "[visitor][unary visitor]")
{
mapbox::util::variant<int, double, std::string> var(123);
REQUIRE(var.get<int>() == 123);
int val = 456;
const mutating_visitor<int> visitor(val);
mapbox::util::apply_visitor(visitor, var);
REQUIRE(var.get<int>() == 456);
}
+102
View File
@@ -0,0 +1,102 @@
#include "catch.hpp"
#include "optional.hpp"
struct dummy
{
dummy(int _m_1, int _m_2) : m_1(_m_1), m_2(_m_2) {}
int m_1;
int m_2;
};
TEST_CASE("optional can be instantiated with a POD type", "[optional]")
{
mapbox::util::optional<int> dbl_opt;
REQUIRE(!dbl_opt);
dbl_opt = 3;
REQUIRE(dbl_opt);
REQUIRE(dbl_opt.get() == 3);
REQUIRE(*dbl_opt == 3);
}
TEST_CASE("copy c'tor", "[optional]")
{
mapbox::util::optional<int> dbl_opt;
REQUIRE(!dbl_opt);
dbl_opt = 3;
REQUIRE(dbl_opt);
mapbox::util::optional<int> other = dbl_opt;
REQUIRE(other.get() == 3);
REQUIRE(*other == 3);
}
TEST_CASE("const operator*, const get()", "[optional]")
{
const mapbox::util::optional<int> dbl_opt = 3;
REQUIRE(dbl_opt);
auto pi1 = dbl_opt.get();
auto pi2 = *dbl_opt;
REQUIRE(pi1 == 3);
REQUIRE(pi2 == 3);
}
TEST_CASE("non-const operator*, non-const get()", "[optional]")
{
mapbox::util::optional<int> dbl_opt = 3;
REQUIRE(dbl_opt);
auto pi1 = dbl_opt.get();
auto pi2 = *dbl_opt;
REQUIRE(pi1 == 3);
REQUIRE(pi2 == 3);
}
TEST_CASE("emplace initialization, reset", "[optional]")
{
mapbox::util::optional<dummy> dummy_opt;
REQUIRE(!dummy_opt);
// rvalues, baby!
dummy_opt.emplace(1, 2);
REQUIRE(dummy_opt);
REQUIRE(dummy_opt.get().m_1 == 1);
REQUIRE((*dummy_opt).m_2 == 2);
dummy_opt.reset();
REQUIRE(!dummy_opt);
}
TEST_CASE("assignment", "[optional]")
{
mapbox::util::optional<int> a;
mapbox::util::optional<int> b;
a = 1;
b = 3;
REQUIRE(a.get() == 1);
REQUIRE(b.get() == 3);
b = a;
REQUIRE(a.get() == b.get());
REQUIRE(b.get() == 1);
}
TEST_CASE("self assignment", "[optional]")
{
mapbox::util::optional<int> a;
a = 1;
REQUIRE(a.get() == 1);
a = a;
REQUIRE(a.get() == 1);
}
+158
View File
@@ -0,0 +1,158 @@
#include "catch.hpp"
#include "recursive_wrapper.hpp"
#include <type_traits>
#include <utility>
using rwi = mapbox::util::recursive_wrapper<int>;
using rwp = mapbox::util::recursive_wrapper<std::pair<int, int>>;
static_assert(std::is_same<rwi::type, int>::value, "type check failed");
TEST_CASE("recursive wrapper of int")
{
SECTION("construct with value")
{
rwi a{7};
REQUIRE(a.get() == 7);
REQUIRE(*a.get_pointer() == 7);
a = 8;
REQUIRE(a.get() == 8);
rwi b{a};
REQUIRE(b.get() == 8);
rwi c;
c = b;
REQUIRE(b.get() == 8);
REQUIRE(c.get() == 8);
c = 9;
REQUIRE(c.get() == 9);
int x = 10;
c = x;
REQUIRE(c.get() == 10);
b = std::move(c);
REQUIRE(b.get() == 10);
}
SECTION("construct with const reference")
{
int i = 7;
rwi a{i};
REQUIRE(a.get() == 7);
}
SECTION("implicit conversion to reference of underlying type")
{
SECTION("const")
{
rwi const a{7};
REQUIRE(a.get() == 7);
REQUIRE(*a.get_pointer() == 7);
rwi::type const& underlying = a;
REQUIRE(underlying == 7);
}
SECTION("non const")
{
rwi a{7};
REQUIRE(a.get() == 7);
REQUIRE(*a.get_pointer() == 7);
rwi::type& underlying = a;
REQUIRE(underlying == 7);
a = 8;
REQUIRE(underlying == 8);
}
}
}
TEST_CASE("move of recursive wrapper")
{
rwi a{1};
SECTION("move constructor")
{
rwi b{std::move(a)};
REQUIRE(b.get() == 1);
}
SECTION("operator= on rvalue")
{
rwi b{2};
b = std::move(a);
REQUIRE(b.get() == 1);
}
}
TEST_CASE("swap")
{
rwi a{1};
rwi b{2};
REQUIRE(a.get() == 1);
REQUIRE(b.get() == 2);
using std::swap;
swap(a, b);
REQUIRE(a.get() == 2);
REQUIRE(b.get() == 1);
}
TEST_CASE("recursive wrapper of pair<int, int>")
{
SECTION("default constructed")
{
rwp a;
REQUIRE(a.get().first == 0);
REQUIRE(a.get().second == 0);
}
SECTION("construct with value")
{
rwp a{std::make_pair(1, 2)};
REQUIRE(a.get().first == 1);
REQUIRE(a.get().second == 2);
REQUIRE(a.get_pointer()->first == 1);
REQUIRE(a.get_pointer()->second == 2);
a = {3, 4};
REQUIRE(a.get().first == 3);
REQUIRE(a.get().second == 4);
rwp b{a};
REQUIRE(b.get().first == 3);
REQUIRE(b.get().second == 4);
rwp c;
c = b;
REQUIRE(b.get().first == 3);
REQUIRE(b.get().second == 4);
REQUIRE(c.get().first == 3);
REQUIRE(c.get().second == 4);
c = {5, 6};
REQUIRE(c.get().first == 5);
REQUIRE(c.get().second == 6);
b = std::move(c);
REQUIRE(b.get().first == 5);
REQUIRE(b.get().second == 6);
// REQUIRE(c.get_pointer() == nullptr);
}
}
+52
View File
@@ -0,0 +1,52 @@
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include "catch.hpp"
#include "variant.hpp"
#include "variant_io.hpp"
struct some_struct
{
int a;
bool b;
std::string c;
};
using variant_internal_index_type = size_t;
TEST_CASE("size of variants")
{
constexpr const auto min_overhead = sizeof(variant_internal_index_type);
using namespace std; // workaround for bug in GCC <= 4.8 where max_align_t is not in std
constexpr const auto max_overhead = alignof(max_align_t) + min_overhead;
using v1 = mapbox::util::variant<int>;
using v2 = mapbox::util::variant<int, bool, int64_t>;
using v3 = mapbox::util::variant<int, std::string>;
using v4 = mapbox::util::variant<std::string, std::string>;
using v5 = mapbox::util::variant<some_struct>;
constexpr const auto si = sizeof(int);
constexpr const auto sb = sizeof(bool);
constexpr const auto si64 = sizeof(int64_t);
constexpr const auto sd = sizeof(double);
constexpr const auto sstr = sizeof(std::string);
constexpr const auto spi = sizeof(std::pair<int, int>);
constexpr const auto ss = sizeof(some_struct);
REQUIRE(sizeof(v1) <= max_overhead + si);
REQUIRE(sizeof(v2) <= max_overhead + std::max({si, sb, si64}));
REQUIRE(sizeof(v3) <= max_overhead + std::max({si, sstr}));
REQUIRE(sizeof(v4) <= max_overhead + sstr);
REQUIRE(sizeof(v5) <= max_overhead + ss);
REQUIRE(sizeof(v1) >= min_overhead + si);
REQUIRE(sizeof(v2) >= min_overhead + std::max({si, sb, si64}));
REQUIRE(sizeof(v3) >= min_overhead + std::max({si, sstr}));
REQUIRE(sizeof(v4) >= min_overhead + sstr);
REQUIRE(sizeof(v5) >= min_overhead + ss);
}
+127
View File
@@ -0,0 +1,127 @@
#include "catch.hpp"
#include "variant.hpp"
#include "variant_io.hpp"
#include <string>
struct some_visitor
{
int var_;
some_visitor(int init)
: var_(init) {}
int operator()(int val) const
{
return var_ + val;
}
int operator()(double val) const
{
return var_ + int(val);
}
int operator()(const std::string&) const
{
return 0;
}
};
TEST_CASE("non-const visitor works on const variants", "[visitor][unary visitor]")
{
using variant_type = const mapbox::util::variant<int, double, std::string>;
variant_type var1(123);
variant_type var2(3.2);
variant_type var3("foo");
REQUIRE(var1.get<int>() == 123);
REQUIRE(var2.get<double>() == Approx(3.2));
REQUIRE(var3.get<std::string>() == "foo");
some_visitor visitor{1};
REQUIRE(mapbox::util::apply_visitor(visitor, var1) == 124);
REQUIRE(mapbox::util::apply_visitor(visitor, var2) == 4);
REQUIRE(mapbox::util::apply_visitor(visitor, var3) == 0);
}
TEST_CASE("const visitor works on const variants", "[visitor][unary visitor]")
{
using variant_type = const mapbox::util::variant<int, double, std::string>;
variant_type var1(123);
variant_type var2(3.2);
variant_type var3("foo");
REQUIRE(var1.get<int>() == 123);
REQUIRE(var2.get<double>() == Approx(3.2));
REQUIRE(var3.get<std::string>() == "foo");
const some_visitor visitor{1};
REQUIRE(mapbox::util::apply_visitor(visitor, var1) == 124);
REQUIRE(mapbox::util::apply_visitor(visitor, var2) == 4);
REQUIRE(mapbox::util::apply_visitor(visitor, var3) == 0);
}
TEST_CASE("rvalue visitor works on const variants", "[visitor][unary visitor]")
{
using variant_type = const mapbox::util::variant<int, double, std::string>;
variant_type var1(123);
variant_type var2(3.2);
variant_type var3("foo");
REQUIRE(var1.get<int>() == 123);
REQUIRE(var2.get<double>() == Approx(3.2));
REQUIRE(var3.get<std::string>() == "foo");
REQUIRE(mapbox::util::apply_visitor(some_visitor{1}, var1) == 124);
REQUIRE(mapbox::util::apply_visitor(some_visitor{1}, var2) == 4);
REQUIRE(mapbox::util::apply_visitor(some_visitor{1}, var3) == 0);
}
TEST_CASE("visitor works on rvalue variants", "[visitor][unary visitor]")
{
using variant_type = const mapbox::util::variant<int, double, std::string>;
REQUIRE(mapbox::util::apply_visitor(some_visitor{1}, variant_type{123}) == 124);
REQUIRE(mapbox::util::apply_visitor(some_visitor{1}, variant_type{3.2}) == 4);
REQUIRE(mapbox::util::apply_visitor(some_visitor{1}, variant_type{"foo"}) == 0);
}
struct total_sizeof
{
total_sizeof() : total_(0) {}
template <class Value>
int operator()(const Value&) const
{
total_ += int(sizeof(Value));
return total_;
}
int result() const
{
return total_;
}
mutable int total_;
}; // total_sizeof
TEST_CASE("changes in visitor should be visible", "[visitor][unary visitor]")
{
using variant_type = mapbox::util::variant<int, std::string, double>;
variant_type v;
total_sizeof ts;
v = 5.9;
REQUIRE(mapbox::util::apply_visitor(ts, v) == sizeof(double));
REQUIRE(ts.result() == sizeof(double));
}
TEST_CASE("changes in const visitor (with mutable internals) should be visible", "[visitor][unary visitor]")
{
using variant_type = const mapbox::util::variant<int, std::string, double>;
variant_type v{"foo"};
const total_sizeof ts;
REQUIRE(mapbox::util::apply_visitor(ts, v) == sizeof(std::string));
REQUIRE(ts.result() == sizeof(std::string));
}
+570
View File
@@ -0,0 +1,570 @@
#include "catch.hpp"
#include "variant.hpp"
#include "variant_io.hpp"
#include <algorithm>
#include <cstdint>
#include <functional>
#include <iterator>
#include <limits>
#include <memory>
#include <ostream>
#include <sstream>
#include <string>
// Hack to make nullptr work with Catch
namespace std {
template <class C, class T>
std::basic_ostream<C, T>& operator<<(std::basic_ostream<C, T>& os, std::nullptr_t)
{
return os << (void*)nullptr;
}
}
TEST_CASE("variant can be moved into vector", "[variant]")
{
using variant_type = mapbox::util::variant<bool, std::string>;
variant_type v(std::string("test"));
std::vector<variant_type> vec;
vec.emplace_back(std::move(v));
REQUIRE(v.get<std::string>() != std::string("test"));
REQUIRE(vec.at(0).get<std::string>() == std::string("test"));
}
TEST_CASE("variant should support built-in types", "[variant]")
{
SECTION("bool")
{
mapbox::util::variant<bool> v(true);
REQUIRE(v.valid());
REQUIRE(v.is<bool>());
REQUIRE(v.which() == 0);
REQUIRE(v.get<bool>() == true);
v.set<bool>(false);
REQUIRE(v.get<bool>() == false);
v = true;
REQUIRE(v == mapbox::util::variant<bool>(true));
}
SECTION("nullptr")
{
using value_type = std::nullptr_t;
mapbox::util::variant<value_type> v(nullptr);
REQUIRE(v.valid());
REQUIRE(v.is<value_type>());
REQUIRE(v.which() == 0);
REQUIRE(v.get<value_type>() == nullptr);
REQUIRE(v == mapbox::util::variant<value_type>(nullptr));
}
SECTION("unique_ptr")
{
using value_type = std::unique_ptr<std::string>;
mapbox::util::variant<value_type> v(value_type(new std::string("hello")));
REQUIRE(v.valid());
REQUIRE(v.is<value_type>());
REQUIRE(v.which() == 0);
REQUIRE(*v.get<value_type>().get() == *value_type(new std::string("hello")).get());
REQUIRE(*v.get<value_type>() == "hello");
}
SECTION("string")
{
using value_type = std::string;
mapbox::util::variant<value_type> v(value_type("hello"));
REQUIRE(v.valid());
REQUIRE(v.is<value_type>());
REQUIRE(v.which() == 0);
REQUIRE(v.get<value_type>() == value_type("hello"));
v.set<value_type>(value_type("there"));
REQUIRE(v.get<value_type>() == value_type("there"));
v = value_type("variant");
REQUIRE(v == mapbox::util::variant<value_type>(value_type("variant")));
}
SECTION("size_t")
{
using value_type = std::size_t;
mapbox::util::variant<value_type> v(std::numeric_limits<value_type>::max());
REQUIRE(v.valid());
REQUIRE(v.is<value_type>());
REQUIRE(v.which() == 0);
REQUIRE(v.get<value_type>() == std::numeric_limits<value_type>::max());
v.set<value_type>(value_type(0));
REQUIRE(v.get<value_type>() == value_type(0));
v = value_type(1);
REQUIRE(v == mapbox::util::variant<value_type>(value_type(1)));
}
SECTION("int8_t")
{
using value_type = std::int8_t;
mapbox::util::variant<value_type> v(std::numeric_limits<value_type>::max());
REQUIRE(v.valid());
REQUIRE(v.is<value_type>());
REQUIRE(v.which() == 0);
REQUIRE(v.get<value_type>() == std::numeric_limits<value_type>::max());
v.set<value_type>(0);
REQUIRE(v.get<value_type>() == value_type(0));
v = value_type(1);
REQUIRE(v == mapbox::util::variant<value_type>(value_type(1)));
}
SECTION("int16_t")
{
using value_type = std::int16_t;
mapbox::util::variant<value_type> v(std::numeric_limits<value_type>::max());
REQUIRE(v.valid());
REQUIRE(v.is<value_type>());
REQUIRE(v.which() == 0);
REQUIRE(v.get<value_type>() == std::numeric_limits<value_type>::max());
v.set<value_type>(0);
REQUIRE(v.get<value_type>() == value_type(0));
v = value_type(1);
REQUIRE(v == mapbox::util::variant<value_type>(value_type(1)));
}
SECTION("int32_t")
{
using value_type = std::int32_t;
mapbox::util::variant<value_type> v(std::numeric_limits<value_type>::max());
REQUIRE(v.valid());
REQUIRE(v.is<value_type>());
REQUIRE(v.which() == 0);
REQUIRE(v.get<value_type>() == std::numeric_limits<value_type>::max());
v.set<value_type>(0);
REQUIRE(v.get<value_type>() == value_type(0));
v = value_type(1);
REQUIRE(v == mapbox::util::variant<value_type>(value_type(1)));
}
SECTION("int64_t")
{
using value_type = std::int64_t;
mapbox::util::variant<value_type> v(std::numeric_limits<value_type>::max());
REQUIRE(v.valid());
REQUIRE(v.is<value_type>());
REQUIRE(v.which() == 0);
REQUIRE(v.get<value_type>() == std::numeric_limits<value_type>::max());
v.set<value_type>(0);
REQUIRE(v.get<value_type>() == value_type(0));
v = value_type(1);
REQUIRE(v == mapbox::util::variant<value_type>(value_type(1)));
}
}
struct MissionInteger
{
using value_type = uint64_t;
value_type val_;
public:
MissionInteger(uint64_t val) : val_(val) {}
bool operator==(MissionInteger const& rhs) const
{
return (val_ == rhs.get());
}
uint64_t get() const
{
return val_;
}
};
std::ostream& operator<<(std::ostream& os, MissionInteger const& rhs)
{
os << rhs.get();
return os;
}
TEST_CASE("variant should support custom types", "[variant]")
{
// http://www.missionintegers.com/integer/34838300
mapbox::util::variant<MissionInteger> v(MissionInteger(34838300));
REQUIRE(v.valid());
REQUIRE(v.is<MissionInteger>());
REQUIRE(v.which() == 0);
REQUIRE(v.get<MissionInteger>() == MissionInteger(34838300));
REQUIRE(v.get<MissionInteger>().get() == MissionInteger::value_type(34838300));
// TODO: should both of the set usages below compile?
v.set<MissionInteger>(MissionInteger::value_type(0));
v.set<MissionInteger>(MissionInteger(0));
REQUIRE(v.get<MissionInteger>().get() == MissionInteger::value_type(0));
v = MissionInteger(1);
REQUIRE(v == mapbox::util::variant<MissionInteger>(MissionInteger(1)));
}
TEST_CASE("variant::which() returns zero based index of stored type", "[variant]")
{
using variant_type = mapbox::util::variant<bool, std::string, std::uint64_t, std::int64_t, double, float>;
// which() returns index in forward order
REQUIRE(0 == variant_type(true).which());
REQUIRE(1 == variant_type(std::string("test")).which());
REQUIRE(2 == variant_type(std::uint64_t(0)).which());
REQUIRE(3 == variant_type(std::int64_t(0)).which());
REQUIRE(4 == variant_type(double(0.0)).which());
REQUIRE(5 == variant_type(float(0.0)).which());
}
TEST_CASE("get with wrong type (here: double) should throw", "[variant]")
{
using variant_type = mapbox::util::variant<int, double>;
variant_type var = 5;
REQUIRE(var.is<int>());
REQUIRE_FALSE(var.is<double>());
REQUIRE(var.get<int>() == 5);
REQUIRE_THROWS_AS({
var.get<double>();
},
mapbox::util::bad_variant_access&);
}
TEST_CASE("get with wrong type (here: int) should throw", "[variant]")
{
using variant_type = mapbox::util::variant<int, double>;
variant_type var = 5.0;
REQUIRE(var.is<double>());
REQUIRE_FALSE(var.is<int>());
REQUIRE(var.get<double>() == 5.0);
REQUIRE(mapbox::util::get<double>(var) == 5.0);
REQUIRE_THROWS_AS({
var.get<int>();
},
mapbox::util::bad_variant_access&);
REQUIRE_THROWS_AS({
mapbox::util::get<int>(var);
},
mapbox::util::bad_variant_access&);
}
TEST_CASE("get on const varint with wrong type (here: int) should throw", "[variant]")
{
using variant_type = mapbox::util::variant<int, double>;
const variant_type var = 5.0;
REQUIRE(var.is<double>());
REQUIRE_FALSE(var.is<int>());
REQUIRE(var.get<double>() == 5.0);
REQUIRE(mapbox::util::get<double>(var) == 5.0);
REQUIRE_THROWS_AS({
var.get<int>();
},
mapbox::util::bad_variant_access&);
REQUIRE_THROWS_AS({
mapbox::util::get<int>(var);
},
mapbox::util::bad_variant_access&);
}
TEST_CASE("get with any type should throw if not initialized", "[variant]")
{
mapbox::util::variant<int, double> var{mapbox::util::no_init()};
REQUIRE_THROWS_AS({
var.get<int>();
},
mapbox::util::bad_variant_access&);
REQUIRE_THROWS_AS({
var.get<double>();
},
mapbox::util::bad_variant_access&);
}
TEST_CASE("no_init variant can be copied and moved from", "[variant]")
{
using variant_type = mapbox::util::variant<int, double>;
variant_type v1{mapbox::util::no_init()};
variant_type v2{42};
variant_type v3{23};
REQUIRE(v2.get<int>() == 42);
v2 = v1;
REQUIRE_THROWS_AS({
v2.get<int>();
},
mapbox::util::bad_variant_access&);
REQUIRE(v3.get<int>() == 23);
v3 = std::move(v1);
REQUIRE_THROWS_AS({
v3.get<int>();
},
mapbox::util::bad_variant_access&);
}
TEST_CASE("no_init variant can be copied and moved to", "[variant]")
{
using variant_type = mapbox::util::variant<int, double>;
variant_type v1{42};
variant_type v2{mapbox::util::no_init()};
variant_type v3{mapbox::util::no_init()};
REQUIRE_THROWS_AS({
v2.get<int>();
},
mapbox::util::bad_variant_access&);
REQUIRE(v1.get<int>() == 42);
v2 = v1;
REQUIRE(v2.get<int>() == 42);
REQUIRE(v1.get<int>() == 42);
REQUIRE_THROWS_AS({
v3.get<int>();
},
mapbox::util::bad_variant_access&);
v3 = std::move(v1);
REQUIRE(v3.get<int>() == 42);
}
TEST_CASE("implicit conversion", "[variant][implicit conversion]")
{
using variant_type = mapbox::util::variant<int>;
variant_type var(5.0); // converted to int
REQUIRE(var.get<int>() == 5);
var = 6.0; // works for operator=, too
REQUIRE(var.get<int>() == 6);
}
TEST_CASE("implicit conversion to first type in variant type list", "[variant][implicit conversion]")
{
using variant_type = mapbox::util::variant<long, char>;
variant_type var = 5.0; // converted to long
REQUIRE(var.get<long>() == 5);
REQUIRE_THROWS_AS({
var.get<char>();
},
mapbox::util::bad_variant_access&);
}
TEST_CASE("implicit conversion to unsigned char", "[variant][implicit conversion]")
{
using variant_type = mapbox::util::variant<unsigned char>;
variant_type var = 100.0;
CHECK(var.get<unsigned char>() == static_cast<unsigned char>(100.0));
CHECK(var.get<unsigned char>() == static_cast<unsigned char>(static_cast<unsigned int>(100.0)));
}
struct dummy
{
};
TEST_CASE("implicit conversion to a suitable type", "[variant][implicit conversion]")
{
using mapbox::util::variant;
CHECK_NOTHROW((variant<dummy, float, std::string>(123)).get<float>());
CHECK_NOTHROW((variant<dummy, float, std::string>("foo")).get<std::string>());
}
TEST_CASE("value_traits for non-convertible type", "[variant::detail]")
{
namespace detail = mapbox::util::detail;
using target_type = detail::value_traits<dummy, int>::target_type;
CHECK((std::is_same<target_type, void>::value) == true);
}
TEST_CASE("Type indexing should work with variants with duplicated types", "[variant::detail]")
{
// Index is in reverse order
REQUIRE((mapbox::util::detail::value_traits<bool, bool, int, double, std::string>::index == 3));
REQUIRE((mapbox::util::detail::value_traits<bool, bool, int, double, int>::index == 3));
REQUIRE((mapbox::util::detail::value_traits<int, bool, int, double, std::string>::index == 2));
REQUIRE((mapbox::util::detail::value_traits<int, bool, int, double, int>::index == 2));
REQUIRE((mapbox::util::detail::value_traits<double, bool, int, double, std::string>::index == 1));
REQUIRE((mapbox::util::detail::value_traits<bool, bool, int, bool, std::string>::index == 3));
REQUIRE((mapbox::util::detail::value_traits<std::string, bool, int, double, std::string>::index == 0));
REQUIRE((mapbox::util::detail::value_traits<dummy, bool, int, double, std::string>::index == mapbox::util::detail::invalid_value));
REQUIRE((mapbox::util::detail::value_traits<std::vector<int>, bool, int, double, std::string>::index == mapbox::util::detail::invalid_value));
}
TEST_CASE("variant default constructor", "[variant][default constructor]")
{
// By default variant is initialised with (default constructed) first type in template parameters pack
// As a result first type in Types... must be default constructable
// NOTE: index in reverse order -> index = N - 1
using variant_type = mapbox::util::variant<int, double, std::string>;
REQUIRE(variant_type{}.which() == 0);
REQUIRE(variant_type{}.valid());
REQUIRE_FALSE(variant_type{mapbox::util::no_init()}.valid());
}
TEST_CASE("variant printer", "[visitor][unary visitor][printer]")
{
using variant_type = mapbox::util::variant<int, double, std::string>;
std::vector<variant_type> var = {2.1, 123, "foo", 456};
std::stringstream out;
std::copy(var.begin(), var.end(), std::ostream_iterator<variant_type>(out, ","));
out << var[2];
REQUIRE(out.str() == "2.1,123,foo,456,foo");
}
TEST_CASE("swapping variants should do the right thing", "[variant]")
{
using variant_type = mapbox::util::variant<int, double, std::string>;
variant_type a = 7;
variant_type b = 3;
variant_type c = 3.141;
variant_type d = "foo";
variant_type e = "a long string that is longer than small string optimization";
using std::swap;
swap(a, b);
REQUIRE(a.get<int>() == 3);
REQUIRE(a.which() == 0);
REQUIRE(b.get<int>() == 7);
REQUIRE(b.which() == 0);
swap(b, c);
REQUIRE(b.get<double>() == Approx(3.141));
REQUIRE(b.which() == 1);
REQUIRE(c.get<int>() == 7);
REQUIRE(c.which() == 0);
swap(b, d);
REQUIRE(b.get<std::string>() == "foo");
REQUIRE(b.which() == 2);
REQUIRE(d.get<double>() == Approx(3.141));
REQUIRE(d.which() == 1);
swap(b, e);
REQUIRE(b.get<std::string>() == "a long string that is longer than small string optimization");
REQUIRE(b.which() == 2);
REQUIRE(e.get<std::string>() == "foo");
REQUIRE(e.which() == 2);
}
TEST_CASE("variant should work with equality operators")
{
using variant_type = mapbox::util::variant<int, std::string>;
variant_type a{1};
variant_type b{1};
variant_type c{2};
variant_type s{"foo"};
REQUIRE(a == a);
REQUIRE(a == b);
REQUIRE_FALSE(a == c);
REQUIRE_FALSE(a == s);
REQUIRE_FALSE(c == s);
REQUIRE_FALSE(a != a);
REQUIRE_FALSE(a != b);
REQUIRE(a != c);
REQUIRE(a != s);
REQUIRE(c != s);
}
TEST_CASE("variant should work with comparison operators")
{
using variant_type = mapbox::util::variant<int, std::string>;
variant_type a{1};
variant_type b{1};
variant_type c{2};
variant_type s{"foo"};
variant_type t{"bar"};
REQUIRE_FALSE(a < a);
REQUIRE_FALSE(a < b);
REQUIRE(a < c);
REQUIRE(a < s);
REQUIRE(c < s);
REQUIRE(t < s);
REQUIRE_FALSE(a > a);
REQUIRE_FALSE(a > b);
REQUIRE_FALSE(a > c);
REQUIRE_FALSE(a > s);
REQUIRE_FALSE(c > s);
REQUIRE_FALSE(t > s);
REQUIRE(a <= a);
REQUIRE(a <= b);
REQUIRE(a <= c);
REQUIRE(a <= s);
REQUIRE(c <= s);
REQUIRE(t <= s);
REQUIRE(a >= a);
REQUIRE(a >= b);
REQUIRE_FALSE(a >= c);
REQUIRE_FALSE(a >= s);
REQUIRE_FALSE(c >= s);
REQUIRE_FALSE(t >= s);
}
TEST_CASE("storing reference wrappers works")
{
using variant_type = mapbox::util::variant<std::reference_wrapper<int>, std::reference_wrapper<double>>;
int a = 1;
variant_type v{std::ref(a)};
REQUIRE(v.get<int>() == 1);
REQUIRE(mapbox::util::get<int>(v) == 1);
REQUIRE_THROWS_AS({
v.get<double>();
},
mapbox::util::bad_variant_access&);
REQUIRE_THROWS_AS({
mapbox::util::get<double>(v);
},
mapbox::util::bad_variant_access&);
a = 2;
REQUIRE(v.get<int>() == 2);
v.get<int>() = 3;
REQUIRE(a == 3);
double b = 3.141;
v = std::ref(b);
REQUIRE(v.get<double>() == Approx(3.141));
REQUIRE(mapbox::util::get<double>(v) == Approx(3.141));
REQUIRE_THROWS_AS({
v.get<int>();
},
mapbox::util::bad_variant_access&);
REQUIRE_THROWS_AS({
mapbox::util::get<int>(v);
},
mapbox::util::bad_variant_access&);
b = 2.718;
REQUIRE(v.get<double>() == Approx(2.718));
a = 3;
REQUIRE(v.get<double>() == Approx(2.718));
v.get<double>() = 4.1;
REQUIRE(b == Approx(4.1));
REQUIRE_THROWS_AS({
v.get<int>() = 4;
},
mapbox::util::bad_variant_access&);
}
TEST_CASE("storing reference wrappers to consts works")
{
using variant_type = mapbox::util::variant<std::reference_wrapper<int const>, std::reference_wrapper<double const>>;
int a = 1;
variant_type v{std::cref(a)};
REQUIRE(v.get<int const>() == 1);
REQUIRE(v.get<int>() == 1); // this works (see #82)
REQUIRE(mapbox::util::get<int const>(v) == 1);
// REQUIRE(mapbox::util::get<int>(v) == 1); // this doesn't work (see #82)
REQUIRE_THROWS_AS({
v.get<double const>();
},
mapbox::util::bad_variant_access&);
REQUIRE_THROWS_AS({
mapbox::util::get<double const>(v);
},
mapbox::util::bad_variant_access&);
double b = 3.141;
v = std::cref(b);
REQUIRE(v.get<double const>() == Approx(3.141));
REQUIRE(mapbox::util::get<double const>(v) == Approx(3.141));
REQUIRE_THROWS_AS({
v.get<int const>();
},
mapbox::util::bad_variant_access&);
REQUIRE_THROWS_AS({
mapbox::util::get<int const>(v);
},
mapbox::util::bad_variant_access&);
}