Exemple #1
0
#include <eggs/variant.hpp>
#include <string>
#include <type_traits>

#include <eggs/variant/detail/config/prefix.hpp>

#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "constexpr.hpp"

EGGS_CXX11_STATIC_CONSTEXPR std::size_t npos = eggs::variant<>::npos;

TEST_CASE("variant<Ts...>::variant(variant<Ts...> const&)", "[variant.cnstr]")
{
    eggs::variant<int, std::string> const v1(42);

    REQUIRE(bool(v1) == true);
    REQUIRE(v1.which() == 0u);
    REQUIRE(*v1.target<int>() == 42);

    eggs::variant<int, std::string> v2(v1);

    CHECK(bool(v2) == true);
    CHECK(v2.which() == v1.which());
    REQUIRE(v1.target<int>() != nullptr);
    CHECK(*v1.target<int>() == 42);
    REQUIRE(v2.target<int>() != nullptr);
    CHECK(*v2.target<int>() == 42);

#if EGGS_CXX11_STD_HAS_IS_TRIVIALLY_COPYABLE
Exemple #2
0
    constexpr std::size_t operator()(T const&) const
    {
        return sizeof(T);
    }

    template <typename T, typename U>
    constexpr std::size_t operator()(T const&, U const&) const
    {
        return sizeof(T) + sizeof(U);
    }
};
#endif

TEST_CASE("apply<R>(F&&, variant<Ts...>&)", "[variant.apply]")
{
    eggs::variant<int, std::string> v(42);

    REQUIRE(v.which() == 0u);
    REQUIRE(*v.target<int>() == 42);

    fun f;
    std::string ret = eggs::variants::apply<std::string>(f, v);

    CHECK(f.nonconst_lvalue == 1u);
    CHECK(ret == "42");

#if EGGS_CXX98_HAS_EXCEPTIONS
    SECTION("throws")
    {
        eggs::variant<int, std::string> empty;
#include <eggs/variant/detail/config/prefix.hpp>

#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "constexpr.hpp"
#include "dtor.hpp"
#include "throw.hpp"

EGGS_CXX11_STATIC_CONSTEXPR std::size_t npos = eggs::variant<>::npos;

TEST_CASE("variant<Ts...>::operator=(T&&)", "[variant.assign]")
{
    // empty target
    {
        eggs::variant<int, std::string> v;

        REQUIRE(bool(v) == false);
        REQUIRE(v.which() == npos);

        v = 42;

        CHECK(bool(v) == true);
        CHECK(v.which() == 0u);
        REQUIRE(v.target<int>() != nullptr);
        CHECK(*v.target<int>() == 42);

#if EGGS_CXX98_HAS_RTTI
        CHECK(v.target_type() == typeid(int));
#endif
Exemple #4
0
// 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)

#include <eggs/variant.hpp>
#include <string>

#include <eggs/variant/detail/config/prefix.hpp>

#define CATCH_CONFIG_MAIN
#include "catch.hpp"

using eggs::variants::in_place;

TEST_CASE("variant<Ts...>::variant(in_place<I>, Args&&...)", "[variant.cnstr]")
{
    eggs::variant<int, std::string> v(in_place<0>, 42);

    CHECK(bool(v) == true);
    CHECK(v.which() == 0u);
    CHECK(v.target_type() == typeid(int));
    REQUIRE(v.target<int>() != nullptr);
    CHECK(*v.target<int>() == 42);
}

TEST_CASE("variant<Ts...>::variant(in_place<I>, std::initializer_list<U>, Args&&...)", "[variant.cnstr]")
{
    eggs::variant<int, std::string> v(in_place<1>, {'4', '2'});

    CHECK(bool(v) == true);
    CHECK(v.which() == 1u);
    CHECK(v.target_type() == typeid(std::string));
Exemple #5
0
};

#  if !EGGS_CXX11_STD_HAS_IS_TRIVIALLY_COPYABLE || !EGGS_CXX11_STD_HAS_IS_TRIVIALLY_DESTRUCTIBLE
namespace eggs { namespace variants { namespace detail
{
    template <> struct is_trivially_copyable<NonCopyAssignableTrivial> : std::true_type {};
    template <> struct is_trivially_destructible<NonCopyAssignableTrivial> : std::true_type {};
}}}
#  endif
#endif

TEST_CASE("variant<Ts...>::operator=(variant<Ts...> const&)", "[variant.assign]")
{
    // empty source
    {
        eggs::variant<int, std::string> const v1;

        REQUIRE(bool(v1) == false);
        REQUIRE(v1.which() == eggs::variant_npos);

        eggs::variant<int, std::string> v2(42);

        REQUIRE(bool(v2) == true);
        REQUIRE(v2.which() == 0u);
        REQUIRE(*v2.target<int>() == 42);

        v2 = v1;

        CHECK(bool(v2) == false);
        CHECK(v2.which() == v1.which());
Exemple #6
0
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <eggs/variant.hpp>
#include <string>

#include <eggs/variant/detail/config/prefix.hpp>

#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "constexpr.hpp"

using eggs::variants::in_place;

TEST_CASE("get<I>(variant<Ts...>&)", "[variant.elem]")
{
    eggs::variant<int, std::string> v(42);

    REQUIRE(v.which() == 0u);
    REQUIRE(*v.target<int>() == 42);

    int& ref = eggs::variants::get<0>(v);

    CHECK(ref == 42);

#if EGGS_CXX98_HAS_EXCEPTIONS
    SECTION("throws")
    {
        CHECK_THROWS_AS(
            eggs::variants::get<1>(v)
          , eggs::variants::bad_variant_access);
    }
Exemple #7
0
{};

template <typename T, typename U>
struct has_greater_equal<
    T, U, typename _void<
        decltype(std::declval<T>() >= std::declval<U>())
    >::type
> : std::true_type
{};
#endif

TEST_CASE("operator<(variant<Ts...> const&, variant<Ts...> const&)", "[variant.rel]")
{
    // same members
    {
        eggs::variant<int, std::string> const v1(42);

        REQUIRE(v1.which() == 0u);
        REQUIRE(*v1.target<int>() == 42);

        eggs::variant<int, std::string> const v2(43);

        REQUIRE(v2.which() == v1.which());
        REQUIRE(*v2.target<int>() == 43);

        CHECK((v1 < v2) == true);
        CHECK((v1 > v2) == false);
        CHECK((v1 <= v2) == true);
        CHECK((v1 >= v2) == false);

        // partial order
Exemple #8
0
#include <eggs/variant.hpp>
#include <string>
#include <typeinfo>

#include <eggs/variant/detail/config/prefix.hpp>

#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "constexpr.hpp"

using eggs::variants::in_place;

TEST_CASE("variant<Ts...>::variant(in_place<I>, Args&&...)", "[variant.cnstr]")
{
    eggs::variant<int, std::string> v(in_place<0>, 42);

    CHECK(bool(v) == true);
    CHECK(v.which() == 0u);
    REQUIRE(v.target<int>() != nullptr);
    CHECK(*v.target<int>() == 42);

#if EGGS_CXX98_HAS_RTTI
    CHECK(v.target_type() == typeid(int));
#endif

#if EGGS_CXX11_HAS_CONSTEXPR
    SECTION("constexpr")
    {
        constexpr eggs::variant<int, Constexpr> v(in_place<1>, 42);
        constexpr bool vb = bool(v);