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
|
// Copyright 2015, Tobias Hermann and the FunctionalPlus contributors.
// https://github.com/Dobiasd/FunctionalPlus
// 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)
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest/doctest.h"
#include <fplus/fplus.hpp>
namespace {
std::string print_output;
bool print_int(int x)
{
print_output = "int " + std::to_string(x);
return true;
}
bool print_double(double x)
{
print_output = "double " + std::to_string(fplus::round(x));
return true;
}
bool print_string(const std::string& str)
{
print_output = "string " + str;
return true;
}
std::string show_int(int x)
{
return fplus::show(x);
}
std::string show_string(const std::string& str)
{
return fplus::show(str);
}
}
TEST_CASE("variant_test, visit_one")
{
using namespace fplus;
fplus::variant<int, double> int_or_double(3);
int_or_double.visit_one(print_int);
REQUIRE_EQ(print_output, "int 3");
print_output.clear();
int_or_double.visit_one(print_double);
REQUIRE_EQ(print_output, "");
print_output.clear();
int_or_double = 23.0;
int_or_double.visit_one(print_int);
REQUIRE_EQ(print_output, "");
print_output.clear();
int_or_double.visit_one(print_double);
REQUIRE_EQ(print_output, "double 23");
print_output.clear();
using float_or_double = fplus::variant<float, double>;
using nested = fplus::variant<int, float_or_double>;
nested int_or_float_double_variant(fplus::variant<float, double>(3.0));
const auto print_nested_double = [&](const float_or_double& f_o_d) -> int
{
f_o_d.visit_one(print_double);
return 0;
};
int_or_float_double_variant.visit_one(print_nested_double);
REQUIRE_EQ(print_output, "double 3");
print_output.clear();
}
TEST_CASE("variant_test, equality_test")
{
using namespace fplus;
fplus::variant<int, std::string> int_or_string_i(3);
fplus::variant<int, std::string> int_or_string_s(std::string("hi"));
REQUIRE(int_or_string_i == int_or_string_i);
REQUIRE_FALSE(int_or_string_i == int_or_string_s);
}
TEST_CASE("variant_test, inequality_test")
{
using namespace fplus;
fplus::variant<int, std::string> int_or_string_i(3);
fplus::variant<int, std::string> int_or_string_s(std::string("hi"));
REQUIRE_FALSE(int_or_string_i != int_or_string_i);
REQUIRE(int_or_string_i != int_or_string_s);
}
TEST_CASE("variant_test, visit")
{
using namespace fplus;
// should not compile
//int_or_double.visit_one<std::string>(print_string);
fplus::variant<int, std::string> int_or_string(3);
REQUIRE(int_or_string.is<int>());
REQUIRE_FALSE(int_or_string.is<std::string>());
int_or_string.visit(print_int, print_string);
REQUIRE_EQ(print_output, "int 3");
print_output.clear();
const auto transform_result =
int_or_string.transform(show_int, show_string);
transform_result.visit_one(print_string);
REQUIRE_EQ(print_output, "string 3");
print_output.clear();
// should not compile
//std::cout << int_or_string.visit(show_int, show_float) << std::endl;
// should not compile
//std::cout << int_or_string.visit(show_int, show_int) << std::endl;
// should not compile
//std::cout << int_or_string.visit(show_int, show_string, show_double) << std::endl;
// should not compile
//std::cout << int_or_string.visit(show_int) << std::endl;
}
|