etl/test/test_variant_variadic.cpp
Roland Reichwein a74c33bab9
Fix variant emplace to be valueless after a throwing construction (#1510)
variant::do_destroy() now always resets type_id to variant_npos after
destroying the active alternative. emplace()/operator= set the new
discriminator only after successful construction, so if the constructor
throws the variant is left valueless (index() == variant_npos) instead of
reporting the stale old index.

Add regression test test_emplace_throwing_is_valueless_by_exception
covering both the trivially-destructible (union) and non-trivially-
destructible (buffer) storage suites.

Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
2026-07-20 12:26:02 +02:00

3199 lines
99 KiB
C++

/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2021 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#include "unit_test_framework.h"
#include "etl/overload.h"
#include "etl/visitor.h"
#include "etl/private/variant_variadic.h"
#if ETL_USING_CPP11
#include <algorithm>
#include <array>
#include <string>
#include <type_traits>
#include <vector>
#if ETL_USING_CPP17
#include <variant>
#endif
#if ETL_USING_CPP20
#include <compare>
std::ostream& operator<<(std::ostream& os, const std::strong_ordering& ordering)
{
if (ordering == std::strong_ordering::equal)
{
os << "std::strong_ordering::equal";
}
else if (ordering == std::strong_ordering::equivalent)
{
os << "std::strong_ordering::equivalent";
}
else if (ordering == std::strong_ordering::greater)
{
os << "std::strong_ordering::greater";
}
else if (ordering == std::strong_ordering::less)
{
os << "std::strong_ordering::less";
}
return os;
}
#endif
#include "etl/private/diagnostic_useless_cast_push.h"
namespace
{
// Test variant_etl types.
using test_variant_etl_3 = etl::variant<char, int, std::string>;
struct D1
{
D1(const std::string& a_) noexcept
: a(a_)
{
copied = false;
moved = false;
}
D1(const D1& other) noexcept
: a(other.a)
{
copied = true;
moved = false;
}
D1(D1&& other) noexcept
: a(std::move(other.a))
{
copied = false;
moved = true;
}
std::string a;
bool copied;
bool moved;
};
struct D2
{
D2(const std::string& a_, const std::string& b_)
: a(a_)
, b(b_)
{
}
std::string a;
std::string b;
};
struct D3
{
D3(const std::string& a_, const std::string& b_, const std::string& c_)
: a(a_)
, b(b_)
, c(c_)
{
}
std::string a;
std::string b;
std::string c;
};
struct D4
{
D4(const std::string& a_, const std::string& b_, const std::string& c_, const std::string& d_)
: a(a_)
, b(b_)
, c(c_)
, d(d_)
{
}
std::string a;
std::string b;
std::string c;
std::string d;
};
bool operator==(const D1& lhs, const D1& rhs)
{
return (lhs.a == rhs.a);
}
bool operator==(const D2& lhs, const D2& rhs)
{
return (lhs.a == rhs.a) && (lhs.b == rhs.b);
}
bool operator==(const D3& lhs, const D3& rhs)
{
return (lhs.a == rhs.a) && (lhs.b == rhs.b) && (lhs.c == rhs.c);
}
bool operator==(const D4& lhs, const D4& rhs)
{
return (lhs.a == rhs.a) && (lhs.b == rhs.b) && (lhs.c == rhs.c) && (lhs.d == rhs.d);
}
std::ostream& operator<<(std::ostream& os, const D1& d1)
{
os << d1.a;
return os;
}
std::ostream& operator<<(std::ostream& os, const D2& d2)
{
os << d2.a << " " << d2.b;
return os;
}
std::ostream& operator<<(std::ostream& os, const D3& d3)
{
os << d3.a << " " << d3.b << " " << d3.c;
return os;
}
std::ostream& operator<<(std::ostream& os, const D4& d4)
{
os << d4.a << " " << d4.b << " " << d4.c << " " << d4.d;
return os;
}
typedef etl::variant<etl::monostate, D1, D2, D3, D4> test_variant_emplace;
//*********************************************
struct Copyable
{
Copyable()
: moved_from(false)
, moved_to(false)
, copied_to(false)
{
}
Copyable(const Copyable&) noexcept
{
moved_from = false;
moved_to = false;
copied_to = true;
}
Copyable& operator=(const Copyable&) noexcept
{
moved_from = false;
moved_to = false;
copied_to = true;
return *this;
}
bool moved_from;
bool moved_to;
bool copied_to;
};
//*********************************************
struct Moveable
{
Moveable()
: moved_from(false)
, moved_to(false)
, copied_to(false)
{
}
Moveable(Moveable&& other) noexcept
{
moved_from = false;
moved_to = true;
copied_to = false;
other.moved_from = true;
other.moved_to = false;
other.copied_to = false;
}
Moveable& operator=(Moveable&& rhs) noexcept
{
moved_from = false;
moved_to = true;
copied_to = false;
rhs.moved_from = true;
rhs.moved_to = false;
rhs.copied_to = false;
return *this;
}
Moveable(const Moveable& other) = delete;
Moveable& operator=(const Moveable& rhs) = delete;
bool moved_from;
bool moved_to;
bool copied_to;
};
//*********************************************
struct MoveableCopyable
{
MoveableCopyable()
: moved_from(false)
, moved_to(false)
, copied_to(false)
{
}
MoveableCopyable(MoveableCopyable&& other) noexcept
{
moved_from = false;
moved_to = true;
copied_to = false;
other.moved_from = true;
other.moved_to = false;
other.copied_to = false;
}
MoveableCopyable& operator=(MoveableCopyable&& rhs) noexcept
{
moved_from = false;
moved_to = true;
copied_to = false;
rhs.moved_from = true;
rhs.moved_to = false;
rhs.copied_to = false;
return *this;
}
MoveableCopyable(const MoveableCopyable&)
{
moved_from = false;
moved_to = false;
copied_to = true;
}
MoveableCopyable& operator=(const MoveableCopyable&)
{
moved_to = false;
moved_from = false;
copied_to = true;
return *this;
}
bool moved_from;
bool moved_to;
bool copied_to;
};
//*********************************************
// Copy constructible, but copy assignment is deleted.
struct CopyConstructibleNonCopyAssignable
{
CopyConstructibleNonCopyAssignable() = default;
CopyConstructibleNonCopyAssignable(const CopyConstructibleNonCopyAssignable&) = default;
CopyConstructibleNonCopyAssignable& operator=(const CopyConstructibleNonCopyAssignable&) = delete;
};
//*********************************************
// Move constructible, but move assignment is deleted.
struct MoveConstructibleNonMoveAssignable
{
MoveConstructibleNonMoveAssignable() = default;
MoveConstructibleNonMoveAssignable(MoveConstructibleNonMoveAssignable&&) = default;
MoveConstructibleNonMoveAssignable& operator=(MoveConstructibleNonMoveAssignable&&) = delete;
MoveConstructibleNonMoveAssignable(const MoveConstructibleNonMoveAssignable&) = delete;
MoveConstructibleNonMoveAssignable& operator=(const MoveConstructibleNonMoveAssignable&) = delete;
};
//*********************************************
// Trivially destructible, but non-copyable and non-movable.
// Exercises the trivially-destructible (variadic_union) emplace path.
struct TrivialNonMovable
{
TrivialNonMovable(int a_, int b_)
: a(a_)
, b(b_)
{
}
TrivialNonMovable(const TrivialNonMovable&) = delete;
TrivialNonMovable(TrivialNonMovable&&) = delete;
TrivialNonMovable& operator=(const TrivialNonMovable&) = delete;
TrivialNonMovable& operator=(TrivialNonMovable&&) = delete;
int a;
int b;
};
//*********************************************
// Non-trivially destructible, non-copyable and non-movable.
// Exercises the non-trivially-destructible (uninitialized_buffer) emplace path,
// including the initializer_list overload.
struct NonTrivialNonMovable
{
NonTrivialNonMovable(int a_, int b_)
: a(a_)
, b(b_)
, sum_of_list(0)
{
}
NonTrivialNonMovable(std::initializer_list<int> il, int b_)
: a(0)
, b(b_)
, sum_of_list(0)
{
for (int value : il)
{
sum_of_list += value;
}
}
~NonTrivialNonMovable() // Makes it non-trivially destructible.
{
}
NonTrivialNonMovable(const NonTrivialNonMovable&) = delete;
NonTrivialNonMovable(NonTrivialNonMovable&&) = delete;
NonTrivialNonMovable& operator=(const NonTrivialNonMovable&) = delete;
NonTrivialNonMovable& operator=(NonTrivialNonMovable&&) = delete;
int a;
int b;
int sum_of_list;
};
// Trivially destructible but not assignable (deleted copy and move
// assignment). Constructing an alternative of this type exercises the
// trivially-destructible-suite path, which must begin the alternative's
// lifetime with placement new rather than assigning into the union member.
struct NonAssignable
{
explicit NonAssignable(int value_)
: value(value_)
{
}
NonAssignable(const NonAssignable&) = default;
NonAssignable(NonAssignable&&) = default;
NonAssignable& operator=(const NonAssignable&) = delete;
NonAssignable& operator=(NonAssignable&&) = delete;
int value;
};
//*********************************************
// Trivially destructible type that overloads operator& (here by deleting it).
// Constructing an alternative of this type must obtain the placement-new
// target with etl::addressof, not the built-in unary operator&: taking the
// address with & would be ill-formed (deleted) or, for an operator& that
// returns something other than the object's address, would construct into the
// wrong storage.
struct NonAddressable
{
explicit NonAddressable(int value_)
: value(value_)
{
}
NonAddressable(const NonAddressable&) = default;
NonAddressable(NonAddressable&&) = default;
NonAddressable* operator&() = delete;
const NonAddressable* operator&() const = delete;
int value;
};
//*********************************************
// Non-trivially destructible type whose copy/move ASSIGNMENT has a
// distinguishable effect from copy/move CONSTRUCTION. Used to verify that
// same-type variant assignment dispatches through the alternative's own
// assignment operator (no destroy + reconstruct). Exercises the
// uninitialized_buffer (non-trivial) path.
struct AssignTracker
{
AssignTracker(int value_)
: value(value_)
, constructed(true)
, copy_assigned(false)
, move_assigned(false)
{
}
AssignTracker(const AssignTracker& other)
: value(other.value)
, constructed(true)
, copy_assigned(false)
, move_assigned(false)
{
}
AssignTracker(AssignTracker&& other)
: value(other.value)
, constructed(true)
, copy_assigned(false)
, move_assigned(false)
{
}
~AssignTracker() {}
AssignTracker& operator=(const AssignTracker& rhs)
{
value = rhs.value;
copy_assigned = true;
move_assigned = false;
return *this;
}
AssignTracker& operator=(AssignTracker&& rhs)
{
value = rhs.value;
copy_assigned = false;
move_assigned = true;
return *this;
}
int value;
bool constructed;
bool copy_assigned;
bool move_assigned;
};
//*********************************************
// Trivially destructible variant of AssignTracker (no user destructor) that
// exercises the trivially-destructible (variadic_union) path.
struct TrivialAssignTracker
{
TrivialAssignTracker(int value_)
: value(value_)
, copy_assigned(false)
, move_assigned(false)
{
}
TrivialAssignTracker(const TrivialAssignTracker& other)
: value(other.value)
, copy_assigned(false)
, move_assigned(false)
{
}
TrivialAssignTracker(TrivialAssignTracker&& other)
: value(other.value)
, copy_assigned(false)
, move_assigned(false)
{
}
TrivialAssignTracker& operator=(const TrivialAssignTracker& rhs)
{
value = rhs.value;
copy_assigned = true;
move_assigned = false;
return *this;
}
TrivialAssignTracker& operator=(TrivialAssignTracker&& rhs)
{
value = rhs.value;
copy_assigned = false;
move_assigned = true;
return *this;
}
int value;
bool copy_assigned;
bool move_assigned;
};
} // namespace
// Moved from the top of the file otherwise clang has issues with
// operator<< for std::strong_ordering.
// #include "unit_test_framework.h"
// Definitions for when the STL and compiler built-ins are not available.
#if ETL_NOT_USING_STL && !defined(ETL_USE_TYPE_TRAITS_BUILTINS)
using etl::is_copy_constructible;
using etl::is_move_constructible;
//*************************
template <>
struct etl::is_copy_constructible<D1> : public etl::true_type
{
};
template <>
struct etl::is_move_constructible<D1> : public etl::true_type
{
};
//*************************
template <>
struct etl::is_copy_constructible<D2> : public etl::true_type
{
};
template <>
struct etl::is_move_constructible<D2> : public etl::true_type
{
};
//*************************
template <>
struct etl::is_copy_constructible<D3> : public etl::true_type
{
};
template <>
struct etl::is_move_constructible<D3> : public etl::true_type
{
};
//*************************
template <>
struct etl::is_copy_constructible<D4> : public etl::true_type
{
};
template <>
struct etl::is_move_constructible<D4> : public etl::true_type
{
};
//*************************
template <>
struct etl::is_copy_constructible<std::string> : public etl::true_type
{
};
template <>
struct etl::is_move_constructible<std::string> : public etl::true_type
{
};
//*************************
template <>
struct etl::is_copy_constructible<Copyable> : public etl::true_type
{
};
template <>
struct etl::is_move_constructible<Copyable> : public etl::false_type
{
};
//*************************
template <>
struct etl::is_copy_constructible<Moveable> : public etl::false_type
{
};
template <>
struct etl::is_move_constructible<Moveable> : public etl::true_type
{
};
//*************************
template <>
struct etl::is_copy_constructible<MoveableCopyable> : public etl::true_type
{
};
template <>
struct etl::is_move_constructible<MoveableCopyable> : public etl::true_type
{
};
//*************************
template <>
struct etl::is_copy_constructible<TrivialNonMovable> : public etl::false_type
{
};
template <>
struct etl::is_copy_constructible<NonAssignable> : public etl::true_type
{
};
template <>
struct etl::is_move_constructible<TrivialNonMovable> : public etl::false_type
{
};
template <>
struct etl::is_move_constructible<NonAssignable> : public etl::true_type
{
};
//*************************
template <>
struct etl::is_copy_constructible<NonTrivialNonMovable> : public etl::false_type
{
};
template <>
struct etl::is_copy_constructible<NonAddressable> : public etl::true_type
{
};
template <>
struct etl::is_move_constructible<NonTrivialNonMovable> : public etl::false_type
{
};
template <>
struct etl::is_move_constructible<NonAddressable> : public etl::true_type
{
};
template <>
struct etl::is_copy_constructible<AssignTracker> : public etl::true_type
{
};
template <>
struct etl::is_move_constructible<AssignTracker> : public etl::true_type
{
};
template <>
struct etl::is_copy_constructible<TrivialAssignTracker> : public etl::true_type
{
};
template <>
struct etl::is_move_constructible<TrivialAssignTracker> : public etl::true_type
{
};
#endif
namespace
{
SUITE(test_variant)
{
TEST(test_alignment)
{
using test_variant_a = etl::variant<char, unsigned char>;
using test_variant_b = etl::variant<char, short>;
using test_variant_c = etl::variant<char, int>;
using test_variant_d = etl::variant<char, double>;
static test_variant_a a(char('1'));
static test_variant_b b(short(2));
static test_variant_c c(3);
static test_variant_d d(4.5);
CHECK((uintptr_t(&etl::get<char>(a)) % uintptr_t(etl::alignment_of<char>::value)) == 0);
CHECK((uintptr_t(&etl::get<short>(b)) % uintptr_t(etl::alignment_of<short>::value)) == 0);
CHECK((uintptr_t(&etl::get<int>(c)) % uintptr_t(etl::alignment_of<int>::value)) == 0);
CHECK((uintptr_t(&etl::get<double>(d)) % uintptr_t(etl::alignment_of<double>::value)) == 0);
}
//*************************************************************************
TEST(test_variant_from_type_list)
{
struct DefaultConstructible
{
DefaultConstructible()
: value(1)
{
}
int value = 0;
};
using type_list = etl::type_list<DefaultConstructible, int, std::string>;
using test_variant_t = etl::variant_from_type_list_t<type_list>;
using normal_variant_t = etl::variant<DefaultConstructible, int, std::string>;
CHECK_NO_THROW(test_variant_t variant_etl);
test_variant_t variant_etl;
// Are the type lists the same?
CHECK_TRUE((std::is_same<test_variant_t::type_list, type_list>::value));
// Are the variants the same?
CHECK_TRUE((std::is_same<test_variant_t, normal_variant_t>::value));
CHECK_TRUE(etl::holds_alternative<DefaultConstructible>(variant_etl));
CHECK_FALSE(etl::holds_alternative<int>(variant_etl));
CHECK_FALSE(etl::holds_alternative<std::string>(variant_etl));
CHECK_EQUAL(1, etl::get<0U>(variant_etl).value);
CHECK_TRUE(etl::holds_alternative<0U>(variant_etl));
CHECK_FALSE(etl::holds_alternative<1U>(variant_etl));
CHECK_FALSE(etl::holds_alternative<2U>(variant_etl));
CHECK_TRUE(etl::holds_alternative(0U, variant_etl));
CHECK_FALSE(etl::holds_alternative(1U, variant_etl));
CHECK_FALSE(etl::holds_alternative(2U, variant_etl));
CHECK_FALSE(etl::holds_alternative(99U, variant_etl));
}
//*************************************************************************
TEST(test_constructor_default)
{
struct DefaultConstructible
{
DefaultConstructible()
: value(1)
{
}
int value = 0;
};
using test_variant_t = etl::variant<DefaultConstructible, int, std::string>;
CHECK_NO_THROW(test_variant_t variant_etl);
test_variant_t variant_etl;
CHECK_TRUE((std::is_same<test_variant_t::type_list, etl::type_list<DefaultConstructible, int, std::string>>::value));
CHECK_TRUE(etl::holds_alternative<DefaultConstructible>(variant_etl));
CHECK_FALSE(etl::holds_alternative<int>(variant_etl));
CHECK_FALSE(etl::holds_alternative<std::string>(variant_etl));
CHECK_EQUAL(1, etl::get<0U>(variant_etl).value);
CHECK_TRUE(etl::holds_alternative<0U>(variant_etl));
CHECK_FALSE(etl::holds_alternative<1U>(variant_etl));
CHECK_FALSE(etl::holds_alternative<2U>(variant_etl));
CHECK_TRUE(etl::holds_alternative(0U, variant_etl));
CHECK_FALSE(etl::holds_alternative(1U, variant_etl));
CHECK_FALSE(etl::holds_alternative(2U, variant_etl));
CHECK_FALSE(etl::holds_alternative(99U, variant_etl));
}
//*************************************************************************
TEST(test_constructor_value)
{
// Char.
char c = 'a';
test_variant_etl_3 variant_char_etl(c);
CHECK(c == 'a');
CHECK(etl::holds_alternative<char>(variant_char_etl));
CHECK_EQUAL(c, etl::get<char>(variant_char_etl));
// Int.
int i = 1;
test_variant_etl_3 variant_int_etl(i);
CHECK(i == 1);
CHECK(etl::holds_alternative<int>(variant_int_etl));
CHECK_EQUAL(i, etl::get<int>(variant_int_etl));
// String.
std::string text("Some Text");
test_variant_etl_3 variant_text_etl(text);
CHECK(text == "Some Text");
CHECK(etl::holds_alternative<std::string>(variant_text_etl));
CHECK_EQUAL(text, etl::get<std::string>(variant_text_etl));
}
//*************************************************************************
TEST(test_constructor_move_value)
{
// Char.
char c = 'a';
test_variant_etl_3 variant_char_etl(etl::move(c));
CHECK(etl::holds_alternative<char>(variant_char_etl));
CHECK_EQUAL(c, etl::get<char>(variant_char_etl));
// Int.
int i = 1;
test_variant_etl_3 variant_int_etl(etl::move(i));
CHECK(etl::holds_alternative<int>(variant_int_etl));
CHECK_EQUAL(i, etl::get<int>(variant_int_etl));
// String.
std::string text("Some Text");
test_variant_etl_3 variant_text_etl(etl::move(text));
CHECK(etl::holds_alternative<std::string>(variant_text_etl));
CHECK_EQUAL(std::string("Some Text"), etl::get<std::string>(variant_text_etl));
}
//*************************************************************************
TEST(test_construct_multiple_parameters_by_type)
{
#if ETL_USING_CPP17
test_variant_emplace variant_etl1(etl::in_place_type<D1>, "1");
CHECK(etl::holds_alternative<D1>(variant_etl1));
CHECK_EQUAL(D1("1"), etl::get<D1>(variant_etl1));
test_variant_emplace variant_etl2(etl::in_place_type<D2>, "1", "2");
CHECK(etl::holds_alternative<D2>(variant_etl2));
CHECK_EQUAL(D2("1", "2"), etl::get<D2>(variant_etl2));
test_variant_emplace variant_etl3(etl::in_place_type<D3>, "1", "2", "3");
CHECK(etl::holds_alternative<D3>(variant_etl3));
CHECK_EQUAL(D3("1", "2", "3"), etl::get<D3>(variant_etl3));
test_variant_emplace variant_etl4(etl::in_place_type<D4>, "1", "2", "3", "4");
CHECK(etl::holds_alternative<D4>(variant_etl4));
CHECK_EQUAL(D4("1", "2", "3", "4"), etl::get<D4>(variant_etl4));
#else
test_variant_emplace variant_etl1(etl::in_place_type_t<D1>{}, "1");
CHECK(etl::holds_alternative<D1>(variant_etl1));
CHECK_EQUAL(D1("1"), etl::get<D1>(variant_etl1));
test_variant_emplace variant_etl2(etl::in_place_type_t<D2>{}, "1", "2");
CHECK(etl::holds_alternative<D2>(variant_etl2));
CHECK_EQUAL(D2("1", "2"), etl::get<D2>(variant_etl2));
test_variant_emplace variant_etl3(etl::in_place_type_t<D3>{}, "1", "2", "3");
CHECK(etl::holds_alternative<D3>(variant_etl3));
CHECK_EQUAL(D3("1", "2", "3"), etl::get<D3>(variant_etl3));
test_variant_emplace variant_etl4(etl::in_place_type_t<D4>{}, "1", "2", "3", "4");
CHECK(etl::holds_alternative<D4>(variant_etl4));
CHECK_EQUAL(D4("1", "2", "3", "4"), etl::get<D4>(variant_etl4));
#endif
}
//*************************************************************************
TEST(test_construct_multiple_parameters_by_index)
{
#if ETL_USING_CPP17
test_variant_emplace variant_etl1(etl::in_place_index<1>, "1");
CHECK(etl::holds_alternative<D1>(variant_etl1));
CHECK_EQUAL(D1("1"), etl::get<D1>(variant_etl1));
test_variant_emplace variant_etl2(etl::in_place_index<2>, "1", "2");
CHECK(etl::holds_alternative<D2>(variant_etl2));
CHECK_EQUAL(D2("1", "2"), etl::get<D2>(variant_etl2));
test_variant_emplace variant_etl3(etl::in_place_index<3>, "1", "2", "3");
CHECK(etl::holds_alternative<D3>(variant_etl3));
CHECK_EQUAL(D3("1", "2", "3"), etl::get<D3>(variant_etl3));
test_variant_emplace variant_etl4(etl::in_place_index<4>, "1", "2", "3", "4");
CHECK(etl::holds_alternative<D4>(variant_etl4));
CHECK_EQUAL(D4("1", "2", "3", "4"), etl::get<D4>(variant_etl4));
#else
test_variant_emplace variant_etl1(etl::in_place_index_t<1>{}, "1");
CHECK(etl::holds_alternative<D1>(variant_etl1));
CHECK_EQUAL(D1("1"), etl::get<D1>(variant_etl1));
test_variant_emplace variant_etl2(etl::in_place_index_t<2>{}, "1", "2");
CHECK(etl::holds_alternative<D2>(variant_etl2));
CHECK_EQUAL(D2("1", "2"), etl::get<D2>(variant_etl2));
test_variant_emplace variant_etl3(etl::in_place_index_t<3>{}, "1", "2", "3");
CHECK(etl::holds_alternative<D3>(variant_etl3));
CHECK_EQUAL(D3("1", "2", "3"), etl::get<D3>(variant_etl3));
test_variant_emplace variant_etl4(etl::in_place_index_t<4>{}, "1", "2", "3", "4");
CHECK(etl::holds_alternative<D4>(variant_etl4));
CHECK_EQUAL(D4("1", "2", "3", "4"), etl::get<D4>(variant_etl4));
#endif
}
#if ETL_HAS_INITIALIZER_LIST
//*************************************************************************
TEST(test_construct_with_initializer_list_by_type)
{
etl::variant<std::vector<int>, std::string> v(etl::in_place_type_t<std::vector<int>>{}, {0, 1, 2, 3});
std::vector<int> expected = {0, 1, 2, 3};
std::vector<int> result = etl::get<std::vector<int>>(v);
CHECK_EQUAL(expected.size(), result.size());
CHECK_ARRAY_EQUAL(expected.data(), result.data(), expected.size());
}
//*************************************************************************
TEST(test_construct_with_initializer_list_by_index)
{
etl::variant<std::vector<int>, std::string> v(etl::in_place_index_t<0U>{}, {0, 1, 2, 3});
std::vector<int> expected = {0, 1, 2, 3};
std::vector<int> result = etl::get<std::vector<int>>(v);
CHECK_EQUAL(expected.size(), result.size());
CHECK_ARRAY_EQUAL(expected.data(), result.data(), expected.size());
}
#endif
//*************************************************************************
TEST(test_emplace_value_by_type)
{
// Char.
char c = 'a';
test_variant_etl_3 variant_char_etl;
variant_char_etl.emplace<char>(c);
CHECK(c == 'a');
CHECK(etl::holds_alternative<char>(variant_char_etl));
CHECK_EQUAL(c, etl::get<char>(variant_char_etl));
// Int.
int i = 1;
test_variant_etl_3 variant_int_etl;
variant_int_etl.emplace<int>(i);
CHECK(i == 1);
CHECK(etl::holds_alternative<int>(variant_int_etl));
CHECK_EQUAL(i, etl::get<int>(variant_int_etl));
// String.
std::string text("Some Text");
test_variant_etl_3 variant_text_etl;
variant_text_etl.emplace<std::string>(text);
CHECK(text == "Some Text");
CHECK(etl::holds_alternative<std::string>(variant_text_etl));
CHECK_EQUAL(text, etl::get<std::string>(variant_text_etl));
}
//*************************************************************************
TEST(test_emplace_value_by_index)
{
// Char.
char c = 'a';
test_variant_etl_3 variant_char_etl;
c = variant_char_etl.emplace<0>(c);
CHECK(c == 'a');
CHECK(etl::holds_alternative<char>(variant_char_etl));
CHECK_EQUAL(c, etl::get<char>(variant_char_etl));
// Int.
int i = 1;
test_variant_etl_3 variant_int_etl;
i = variant_int_etl.emplace<1>(i);
CHECK(i == 1);
CHECK(etl::holds_alternative<int>(variant_int_etl));
CHECK_EQUAL(i, etl::get<int>(variant_int_etl));
// String.
std::string text("Some Text");
test_variant_etl_3 variant_text_etl;
text = variant_text_etl.emplace<2>(text);
CHECK(text == "Some Text");
CHECK(etl::holds_alternative<std::string>(variant_text_etl));
CHECK_EQUAL(text, etl::get<std::string>(variant_text_etl));
}
//*************************************************************************
// emplace must construct the alternative in place from the forwarded
// arguments, without requiring the alternative to be copyable or movable
// (see issue #1493).
TEST(test_emplace_non_movable_type)
{
// Trivially destructible suite (variadic_union storage).
{
etl::variant<int, TrivialNonMovable> v;
TrivialNonMovable& r1 = v.emplace<TrivialNonMovable>(3, 4);
CHECK(etl::holds_alternative<TrivialNonMovable>(v));
CHECK_EQUAL(3, r1.a);
CHECK_EQUAL(4, r1.b);
CHECK_EQUAL(&r1, &etl::get<TrivialNonMovable>(v));
// Emplace by index over the existing alternative.
TrivialNonMovable& r2 = v.emplace<1>(5, 6);
CHECK_EQUAL(5, r2.a);
CHECK_EQUAL(6, r2.b);
// Switch to the trivial alternative and back again.
v.emplace<int>(42);
CHECK(etl::holds_alternative<int>(v));
CHECK_EQUAL(42, etl::get<int>(v));
TrivialNonMovable& r3 = v.emplace<TrivialNonMovable>(7, 8);
CHECK(etl::holds_alternative<TrivialNonMovable>(v));
CHECK_EQUAL(7, r3.a);
CHECK_EQUAL(8, r3.b);
}
// Non-trivially destructible suite (uninitialized_buffer storage).
{
etl::variant<std::string, NonTrivialNonMovable> v;
NonTrivialNonMovable& r1 = v.emplace<NonTrivialNonMovable>(1, 2);
CHECK(etl::holds_alternative<NonTrivialNonMovable>(v));
CHECK_EQUAL(1, r1.a);
CHECK_EQUAL(2, r1.b);
CHECK_EQUAL(&r1, &etl::get<NonTrivialNonMovable>(v));
// Emplace by index over the existing alternative.
NonTrivialNonMovable& r2 = v.emplace<1>(9, 10);
CHECK_EQUAL(9, r2.a);
CHECK_EQUAL(10, r2.b);
// Switch to the std::string alternative and back again.
v.emplace<std::string>("Some Text");
CHECK(etl::holds_alternative<std::string>(v));
CHECK_EQUAL(std::string("Some Text"), etl::get<std::string>(v));
NonTrivialNonMovable& r3 = v.emplace<NonTrivialNonMovable>(11, 12);
CHECK(etl::holds_alternative<NonTrivialNonMovable>(v));
CHECK_EQUAL(11, r3.a);
CHECK_EQUAL(12, r3.b);
}
}
#if ETL_HAS_INITIALIZER_LIST
//*************************************************************************
// The initializer_list emplace overloads must also construct in place,
// without requiring the alternative to be copyable or movable (#1493).
TEST(test_emplace_non_movable_type_with_initializer_list)
{
etl::variant<std::string, NonTrivialNonMovable> v;
// By type.
NonTrivialNonMovable& r1 = v.emplace<NonTrivialNonMovable>({10, 20, 30}, 99);
CHECK(etl::holds_alternative<NonTrivialNonMovable>(v));
CHECK_EQUAL(60, r1.sum_of_list);
CHECK_EQUAL(99, r1.b);
// By index.
NonTrivialNonMovable& r2 = v.emplace<1>({1, 2, 3, 4}, 7);
CHECK_EQUAL(10, r2.sum_of_list);
CHECK_EQUAL(7, r2.b);
}
#endif
#if ETL_USING_EXCEPTIONS
//*************************************************************************
// A throwing emplace() must leave the variant valueless-after-exception
// (index() == variant_npos) rather than reporting the stale old index.
struct ThrowOnConstruct
{
struct exception
{
};
ThrowOnConstruct() = default;
explicit ThrowOnConstruct(int)
{
throw exception();
}
};
TEST(test_emplace_throwing_is_valueless_by_exception)
{
// Trivially destructible suite (variadic_union storage).
{
etl::variant<int, ThrowOnConstruct> v;
v.emplace<int>(42);
CHECK(!v.valueless_by_exception());
CHECK_EQUAL(0U, v.index());
bool threw = false;
try
{
v.emplace<ThrowOnConstruct>(1);
}
catch (const ThrowOnConstruct::exception&)
{
threw = true;
}
CHECK(threw);
CHECK(v.valueless_by_exception());
CHECK_EQUAL(etl::variant_npos, v.index());
}
// Non-trivially destructible suite (uninitialized_buffer storage).
{
etl::variant<std::string, ThrowOnConstruct> v;
v.emplace<std::string>("Some Text");
CHECK(!v.valueless_by_exception());
CHECK_EQUAL(0U, v.index());
bool threw = false;
try
{
v.emplace<1>(1);
}
catch (const ThrowOnConstruct::exception&)
{
threw = true;
}
CHECK(threw);
CHECK(v.valueless_by_exception());
CHECK_EQUAL(etl::variant_npos, v.index());
}
}
#endif
//*************************************************************************
TEST(test_copy_constructor)
{
std::string text("Some Text");
test_variant_etl_3 variant_1_etl(text);
test_variant_etl_3 variant_2_etl(variant_1_etl);
CHECK_EQUAL(variant_1_etl.index(), variant_2_etl.index());
CHECK_EQUAL(etl::get<std::string>(variant_1_etl), etl::get<std::string>(variant_2_etl));
}
//*************************************************************************
TEST(test_copy_constructor_from_empty)
{
test_variant_etl_3 variant_1_etl;
test_variant_etl_3 variant_2_etl(variant_1_etl);
CHECK_EQUAL(variant_1_etl.index(), variant_2_etl.index());
}
//*************************************************************************
TEST(test_move_constructor)
{
std::string text("Some Text");
test_variant_etl_3 variant_1_etl(text);
test_variant_etl_3 variant_2_etl(etl::move(variant_1_etl));
CHECK_EQUAL(variant_1_etl.index(), variant_2_etl.index());
CHECK(etl::get<std::string>(variant_1_etl) != etl::get<std::string>(variant_2_etl));
}
//*************************************************************************
TEST(test_move_constructor_from_empty)
{
std::string text("Some Text");
test_variant_etl_3 variant_1_etl;
test_variant_etl_3 variant_2_etl(etl::move(variant_1_etl));
CHECK_EQUAL(variant_1_etl.index(), variant_2_etl.index());
}
//*************************************************************************
// Issue #1512: a variant must only be copy constructible / copy assignable
// when every alternative is copy constructible. Moveable is move-only
// (its copy operations are deleted), so a variant containing it must not
// be copyable, while remaining move constructible / move assignable.
TEST(test_copy_disabled_with_non_copyable_alternative)
{
typedef etl::variant<int, Moveable> non_copyable_variant;
CHECK(!etl::is_copy_constructible<non_copyable_variant>::value);
CHECK(!etl::is_copy_assignable<non_copyable_variant>::value);
CHECK(etl::is_move_constructible<non_copyable_variant>::value);
CHECK(etl::is_move_assignable<non_copyable_variant>::value);
// A variant of purely copyable alternatives must remain copyable.
typedef etl::variant<int, Copyable> copyable_variant;
CHECK(etl::is_copy_constructible<copyable_variant>::value);
CHECK(etl::is_copy_assignable<copyable_variant>::value);
}
//*************************************************************************
// A non-copy-assignable alternative must disable the variant's copy
// assignment operator, and a non-move-assignable alternative must disable
// the variant's move assignment operator, while leaving the corresponding
// constructors available.
TEST(test_assignment_disabled_with_non_assignable_alternative)
{
typedef etl::variant<int, CopyConstructibleNonCopyAssignable> copy_non_assignable_variant;
CHECK(etl::is_copy_constructible<copy_non_assignable_variant>::value);
CHECK(!etl::is_copy_assignable<copy_non_assignable_variant>::value);
typedef etl::variant<int, MoveConstructibleNonMoveAssignable> move_non_assignable_variant;
CHECK(etl::is_move_constructible<move_non_assignable_variant>::value);
CHECK(!etl::is_move_assignable<move_non_assignable_variant>::value);
}
//*************************************************************************
// When source and destination hold the SAME alternative, assignment must
// dispatch through that alternative's own assignment operator rather than
// destroying and reconstructing it. Non-trivially-destructible path.
TEST(test_copy_assign_same_type_uses_alternative_assignment)
{
typedef etl::variant<int, AssignTracker> tracker_variant;
tracker_variant variant_1(etl::in_place_type_t<AssignTracker>{}, 1);
tracker_variant variant_2(etl::in_place_type_t<AssignTracker>{}, 2);
variant_1 = variant_2;
CHECK_EQUAL(1U, variant_1.index());
CHECK_EQUAL(2, etl::get<AssignTracker>(variant_1).value);
CHECK(etl::get<AssignTracker>(variant_1).copy_assigned);
CHECK(!etl::get<AssignTracker>(variant_1).move_assigned);
}
//*************************************************************************
TEST(test_move_assign_same_type_uses_alternative_assignment)
{
typedef etl::variant<int, AssignTracker> tracker_variant;
tracker_variant variant_1(etl::in_place_type_t<AssignTracker>{}, 1);
tracker_variant variant_2(etl::in_place_type_t<AssignTracker>{}, 2);
variant_1 = etl::move(variant_2);
CHECK_EQUAL(1U, variant_1.index());
CHECK_EQUAL(2, etl::get<AssignTracker>(variant_1).value);
CHECK(!etl::get<AssignTracker>(variant_1).copy_assigned);
CHECK(etl::get<AssignTracker>(variant_1).move_assigned);
}
//*************************************************************************
// When source and destination hold DIFFERENT alternatives, assignment must
// still destroy and reconstruct.
TEST(test_copy_assign_different_type_reconstructs)
{
typedef etl::variant<int, AssignTracker> tracker_variant;
tracker_variant variant_1(etl::in_place_type_t<int>{}, 5);
tracker_variant variant_2(etl::in_place_type_t<AssignTracker>{}, 2);
variant_1 = variant_2;
CHECK_EQUAL(1U, variant_1.index());
CHECK_EQUAL(2, etl::get<AssignTracker>(variant_1).value);
CHECK(etl::get<AssignTracker>(variant_1).constructed);
CHECK(!etl::get<AssignTracker>(variant_1).copy_assigned);
CHECK(!etl::get<AssignTracker>(variant_1).move_assigned);
}
//*************************************************************************
// Same as above but for the trivially-destructible (variadic_union) path.
TEST(test_assign_same_type_uses_alternative_assignment_trivial)
{
typedef etl::variant<int, TrivialAssignTracker> tracker_variant;
tracker_variant variant_1(etl::in_place_type_t<TrivialAssignTracker>{}, 1);
tracker_variant variant_2(etl::in_place_type_t<TrivialAssignTracker>{}, 2);
variant_1 = variant_2;
CHECK_EQUAL(1U, variant_1.index());
CHECK_EQUAL(2, etl::get<TrivialAssignTracker>(variant_1).value);
CHECK(etl::get<TrivialAssignTracker>(variant_1).copy_assigned);
CHECK(!etl::get<TrivialAssignTracker>(variant_1).move_assigned);
tracker_variant variant_3(etl::in_place_type_t<TrivialAssignTracker>{}, 3);
variant_1 = etl::move(variant_3);
CHECK_EQUAL(1U, variant_1.index());
CHECK_EQUAL(3, etl::get<TrivialAssignTracker>(variant_1).value);
CHECK(etl::get<TrivialAssignTracker>(variant_1).move_assigned);
}
//*************************************************************************
// Issue #1512: even though the copy operations are disabled at compile
// time, a move-only variant must still move construct / move assign
// correctly, preserving the active alternative.
TEST(test_move_only_variant_moves_correctly)
{
typedef etl::variant<int, Moveable> non_copyable_variant;
non_copyable_variant variant_1(etl::in_place_type_t<Moveable>{});
CHECK_EQUAL(1U, variant_1.index());
// Move construction preserves the active alternative.
non_copyable_variant variant_2(etl::move(variant_1));
CHECK_EQUAL(1U, variant_2.index());
CHECK(etl::get<Moveable>(variant_2).moved_to);
// Move assignment preserves the active alternative.
non_copyable_variant variant_3;
variant_3 = etl::move(variant_2);
CHECK_EQUAL(1U, variant_3.index());
CHECK(etl::get<Moveable>(variant_3).moved_to);
}
//*************************************************************************
TEST(test_assign_from_value)
{
std::string text("Some Text");
test_variant_etl_3 variant_etl;
variant_etl = text;
CHECK_EQUAL(text, etl::get<std::string>(variant_etl));
}
//*************************************************************************
TEST(test_assign_from_variant)
{
std::string text("Some Text");
test_variant_etl_3 variant_1_etl;
test_variant_etl_3 variant_2_etl;
variant_1_etl = text;
variant_2_etl = variant_1_etl;
CHECK_EQUAL(text, etl::get<std::string>(variant_2_etl));
}
//*************************************************************************
TEST(test_assign_from_variant2)
{
std::string text("Some Text");
int integer(99);
test_variant_etl_3 variant_1_etl;
test_variant_etl_3 variant_2_etl;
variant_1_etl = text;
variant_2_etl = integer;
variant_2_etl = variant_1_etl;
CHECK_EQUAL(text, etl::get<std::string>(variant_2_etl));
}
//*************************************************************************
TEST(test_assignment_incorrect_type_exception)
{
std::string text("Some Text");
test_variant_etl_3 variant_etl(text);
int i;
CHECK_THROW(etl::get<int>(variant_etl), etl::variant_incorrect_type_exception);
(void)i;
}
//*************************************************************************
TEST(test_self_assignment)
{
test_variant_etl_3 variant_etl;
variant_etl = 1;
#include "etl/private/diagnostic_self_assign_overloaded_push.h"
variant_etl = variant_etl;
#include "etl/private/diagnostic_pop.h"
CHECK_EQUAL(1, etl::get<int>(variant_etl));
}
//*************************************************************************
TEST(test_member_swap_variants)
{
std::string text("Some Text");
int integer(99);
test_variant_etl_3 variant_1_etl(text);
test_variant_etl_3 variant_2_etl(integer);
variant_1_etl.swap(variant_2_etl);
CHECK(etl::holds_alternative<int>(variant_1_etl));
CHECK_EQUAL(integer, etl::get<int>(variant_1_etl));
CHECK(etl::holds_alternative<std::string>(variant_2_etl));
CHECK_EQUAL(text, etl::get<std::string>(variant_2_etl));
}
//*************************************************************************
TEST(test_global_swap_variants)
{
std::string text("Some Text");
int integer(99);
test_variant_etl_3 variant_1_etl(text);
test_variant_etl_3 variant_2_etl(integer);
etl::swap(variant_1_etl, variant_2_etl);
CHECK(etl::holds_alternative<int>(variant_1_etl));
CHECK_EQUAL(integer, etl::get<int>(variant_1_etl));
CHECK(etl::holds_alternative<std::string>(variant_2_etl));
CHECK_EQUAL(text, etl::get<std::string>(variant_2_etl));
}
//*************************************************************************
TEST(test_emplace_multiple_parameters)
{
test_variant_emplace variant_etl;
variant_etl.emplace<D1>("1");
CHECK(etl::holds_alternative<D1>(variant_etl));
CHECK_EQUAL(D1("1"), etl::get<D1>(variant_etl));
variant_etl.emplace<D2>("1", "2");
CHECK(etl::holds_alternative<D2>(variant_etl));
CHECK_EQUAL(D2("1", "2"), etl::get<D2>(variant_etl));
variant_etl.emplace<D3>("1", "2", "3");
CHECK(etl::holds_alternative<D3>(variant_etl));
CHECK_EQUAL(D3("1", "2", "3"), etl::get<D3>(variant_etl));
variant_etl.emplace<D4>("1", "2", "3", "4");
CHECK(etl::holds_alternative<D4>(variant_etl));
CHECK_EQUAL(D4("1", "2", "3", "4"), etl::get<D4>(variant_etl));
}
//*************************************************************************
// Constructing an alternative must begin the object's lifetime with
// placement new, not assign into the (inactive) union member. A trivially
// destructible but non-assignable type would fail to compile (deleted
// assignment) or invoke undefined behaviour if assignment were used.
// (The trivially-destructible-suite union path is taken when the STL or the
// compiler type-trait built-ins are available; otherwise construction goes
// through the buffer path, which is exercised here just the same.)
TEST(test_construct_trivially_destructible_non_assignable_type)
{
using variant_type = etl::variant<int, NonAssignable>;
// Emplace by type.
variant_type variant_by_type;
variant_by_type.emplace<NonAssignable>(42);
CHECK(etl::holds_alternative<NonAssignable>(variant_by_type));
CHECK_EQUAL(42, etl::get<NonAssignable>(variant_by_type).value);
// Emplace by index.
variant_type variant_by_index;
variant_by_index.emplace<1>(43);
CHECK(etl::holds_alternative<NonAssignable>(variant_by_index));
CHECK_EQUAL(43, etl::get<NonAssignable>(variant_by_index).value);
// Assignment from a value (operator=(T&&)).
variant_type variant_by_assignment;
variant_by_assignment = NonAssignable(44);
CHECK(etl::holds_alternative<NonAssignable>(variant_by_assignment));
CHECK_EQUAL(44, etl::get<NonAssignable>(variant_by_assignment).value);
// Re-emplace over an already-active alternative (do_destroy then
// placement new into the same union member).
variant_by_assignment.emplace<NonAssignable>(45);
CHECK(etl::holds_alternative<NonAssignable>(variant_by_assignment));
CHECK_EQUAL(45, etl::get<NonAssignable>(variant_by_assignment).value);
}
//*************************************************************************
// Constructing an alternative must take the raw storage address for
// placement new via etl::addressof, not the built-in unary operator&.
// NonAddressable deletes operator&, so the old `&variadic_union_get(...)`
// form would be ill-formed. Read-back uses by-index etl::get (which returns
// a reference) and the reference returned by emplace, so it never invokes
// operator&. (The trivially-destructible-suite union path is taken when the
// STL or the compiler built-ins are available; otherwise construction goes
// through the buffer path, which is exercised here just the same.)
TEST(test_construct_type_with_overloaded_address_of_operator)
{
using variant_type = etl::variant<int, NonAddressable>;
// Emplace by type (returns a reference, so no operator& on read-back).
variant_type variant_by_type;
NonAddressable& by_type = variant_by_type.emplace<NonAddressable>(42);
CHECK(etl::holds_alternative<NonAddressable>(variant_by_type));
CHECK_EQUAL(42, by_type.value);
CHECK_EQUAL(42, etl::get<1>(variant_by_type).value);
// Emplace by index.
variant_type variant_by_index;
variant_by_index.emplace<1>(43);
CHECK(etl::holds_alternative<NonAddressable>(variant_by_index));
CHECK_EQUAL(43, etl::get<1>(variant_by_index).value);
// Assignment from a value (operator=(T&&)).
variant_type variant_by_assignment;
variant_by_assignment = NonAddressable(44);
CHECK(etl::holds_alternative<NonAddressable>(variant_by_assignment));
CHECK_EQUAL(44, etl::get<1>(variant_by_assignment).value);
// Re-emplace over an already-active alternative.
variant_by_assignment.emplace<1>(45);
CHECK(etl::holds_alternative<NonAddressable>(variant_by_assignment));
CHECK_EQUAL(45, etl::get<1>(variant_by_assignment).value);
}
//*************************************************************************
TEST(test_variant_accept_visitor)
{
struct Visitor : public etl::visitor<char&, int&, std::string&>
{
Visitor()
: result_c(0)
, result_i(0)
, result_s("")
{
}
void visit(char& c)
{
result_c = c;
++c;
}
void visit(int& i)
{
result_i = i;
++i;
}
void visit(std::string& s)
{
result_s = s;
s = "4";
}
char result_c;
int result_i;
std::string result_s;
};
Visitor visitor;
test_variant_etl_3 variant_etl;
variant_etl = char(1);
variant_etl.accept(visitor);
CHECK_EQUAL(1, visitor.result_c);
CHECK_EQUAL(2, etl::get<char>(variant_etl));
variant_etl = int(2);
variant_etl.accept(visitor);
CHECK_EQUAL(2, visitor.result_i);
CHECK_EQUAL(3, etl::get<int>(variant_etl));
variant_etl = std::string("3");
variant_etl.accept(visitor);
CHECK_EQUAL("3", visitor.result_s);
CHECK_EQUAL("4", etl::get<std::string>(variant_etl));
}
//*************************************************************************
TEST(test_variant_accept_visitor_deprecated)
{
struct Visitor : public etl::visitor<char&, int&, std::string&>
{
Visitor()
: result_c(0)
, result_i(0)
, result_s("")
{
}
void visit(char& c)
{
result_c = c;
++c;
}
void visit(int& i)
{
result_i = i;
++i;
}
void visit(std::string& s)
{
result_s = s;
s = "4";
}
char result_c;
int result_i;
std::string result_s;
};
Visitor visitor;
test_variant_etl_3 variant_etl;
variant_etl = char(1);
variant_etl.accept_visitor(visitor);
CHECK_EQUAL(1, visitor.result_c);
CHECK_EQUAL(2, etl::get<char>(variant_etl));
variant_etl = int(2);
variant_etl.accept_visitor(visitor);
CHECK_EQUAL(2, visitor.result_i);
CHECK_EQUAL(3, etl::get<int>(variant_etl));
variant_etl = std::string("3");
variant_etl.accept_visitor(visitor);
CHECK_EQUAL("3", visitor.result_s);
CHECK_EQUAL("4", etl::get<std::string>(variant_etl));
}
//*************************************************************************
#include "etl/private/diagnostic_uninitialized_push.h"
TEST(test_const_variant_accept_visitor)
{
struct Visitor : public etl::visitor<const char&, const int&, const std::string&>
{
Visitor()
: result_c(0)
, result_i(0)
, result_s("")
{
}
void visit(const char& c)
{
result_c = c;
}
void visit(const int& i)
{
result_i = i;
}
void visit(const std::string& s)
{
result_s = s;
}
char result_c;
int result_i;
std::string result_s;
};
Visitor visitor;
test_variant_etl_3 variant_etl;
variant_etl = char(1);
const test_variant_etl_3 const_variant_etl1(variant_etl);
const_variant_etl1.accept(visitor);
CHECK_EQUAL(1, visitor.result_c);
variant_etl = int(2);
const test_variant_etl_3 const_variant_etl2(variant_etl);
const_variant_etl2.accept(visitor);
CHECK_EQUAL(2, visitor.result_i);
variant_etl = std::string("3");
const test_variant_etl_3 const_variant_etl3(variant_etl);
const_variant_etl3.accept(visitor);
CHECK_EQUAL("3", visitor.result_s);
}
#include "etl/private/diagnostic_pop.h"
//*************************************************************************
#include "etl/private/diagnostic_uninitialized_push.h"
TEST(test_const_variant_accept_visitor_deprecated)
{
struct Visitor : public etl::visitor<char, int, const std::string&>
{
Visitor()
: result_c(0)
, result_i(0)
, result_s("")
{
}
void visit(char c)
{
result_c = c;
}
void visit(int i)
{
result_i = i;
}
void visit(const std::string& s)
{
result_s = s;
}
char result_c;
int result_i;
std::string result_s;
};
Visitor visitor;
test_variant_etl_3 variant_etl;
variant_etl = char(1);
const test_variant_etl_3 const_variant_etl1(variant_etl);
const_variant_etl1.accept(visitor);
CHECK_EQUAL(1, visitor.result_c);
variant_etl = int(2);
const test_variant_etl_3 const_variant_etl2(variant_etl);
// const_variant_etl2.accept_visitor(visitor);
const_variant_etl2.accept(visitor);
CHECK_EQUAL(2, visitor.result_i);
variant_etl = std::string("3");
const test_variant_etl_3 const_variant_etl3(variant_etl);
// const_variant_etl3.accept_visitor(visitor);
const_variant_etl3.accept(visitor);
CHECK_EQUAL("3", visitor.result_s);
}
#include "etl/private/diagnostic_pop.h"
//*************************************************************************
TEST(test_variant_accept_functor_with_functor_class)
{
struct Visitor
{
Visitor()
: result_c(0)
, result_i(0)
, result_s("")
{
}
void operator()(char& c)
{
result_c = c;
++c;
}
void operator()(int& i)
{
result_i = i;
++i;
}
void operator()(std::string& s)
{
result_s = s;
s = "4";
}
char result_c;
int result_i;
std::string result_s;
};
Visitor visitor;
test_variant_etl_3 variant_etl;
variant_etl = char(1);
variant_etl.accept(visitor);
CHECK_EQUAL(1, visitor.result_c);
CHECK_EQUAL(2, etl::get<char>(variant_etl));
variant_etl = int(2);
variant_etl.accept(visitor);
CHECK_EQUAL(2, visitor.result_i);
CHECK_EQUAL(3, etl::get<int>(variant_etl));
variant_etl = std::string("3");
variant_etl.accept(visitor);
CHECK_EQUAL("3", visitor.result_s);
CHECK_EQUAL("4", etl::get<std::string>(variant_etl));
}
//*************************************************************************
TEST(test_variant_accept_functor_with_functor_class_deprecated)
{
struct Visitor
{
Visitor()
: result_c(0)
, result_i(0)
, result_s("")
{
}
void operator()(char c)
{
result_c = c;
}
void operator()(int i)
{
result_i = i;
}
void operator()(const std::string& s)
{
result_s = s;
}
char result_c;
int result_i;
std::string result_s;
};
Visitor visitor;
test_variant_etl_3 variant_etl;
variant_etl = char(1);
variant_etl.accept_functor(visitor);
CHECK_EQUAL(1, visitor.result_c);
variant_etl = int(2);
variant_etl.accept_functor(visitor);
CHECK_EQUAL(2, visitor.result_i);
variant_etl = std::string("3");
// variant_etl.accept_functor(visitor);
variant_etl.accept(visitor);
CHECK_EQUAL("3", visitor.result_s);
}
//*************************************************************************
#include "etl/private/diagnostic_uninitialized_push.h"
TEST(test_const_variant_accept_functor_with_functor_class)
{
struct Visitor
{
Visitor()
: result_c(0)
, result_i(0)
, result_s("")
{
}
void operator()(char c)
{
result_c = c;
}
void operator()(int i)
{
result_i = i;
}
void operator()(const std::string& s)
{
result_s = s;
}
char result_c;
int result_i;
std::string result_s;
};
Visitor visitor;
test_variant_etl_3 variant_etl;
variant_etl = char(1);
const test_variant_etl_3 const_variant_etl1(variant_etl);
const_variant_etl1.accept(visitor);
CHECK_EQUAL(1, int(visitor.result_c));
variant_etl = int(2);
const test_variant_etl_3 const_variant_etl2(variant_etl);
const_variant_etl2.accept(visitor);
CHECK_EQUAL(2, visitor.result_i);
variant_etl = std::string("3");
const test_variant_etl_3 const_variant_etl3(variant_etl);
const_variant_etl3.accept(visitor);
CHECK_EQUAL("3", visitor.result_s);
}
#include "etl/private/diagnostic_pop.h"
//*************************************************************************
#include "etl/private/diagnostic_uninitialized_push.h"
TEST(test_const_variant_accept_functor_with_functor_class_deprecated)
{
struct Visitor
{
Visitor()
: result_c(0)
, result_i(0)
, result_s("")
{
}
void operator()(char c)
{
result_c = c;
}
void operator()(int i)
{
result_i = i;
}
void operator()(const std::string& s)
{
result_s = s;
}
char result_c;
int result_i;
std::string result_s;
};
Visitor visitor;
test_variant_etl_3 variant_etl;
variant_etl = char(1);
const test_variant_etl_3 const_variant_etl1(variant_etl);
const_variant_etl1.accept_functor(visitor);
CHECK_EQUAL(1, visitor.result_c);
variant_etl = int(2);
const test_variant_etl_3 const_variant_etl2(variant_etl);
const_variant_etl2.accept(visitor);
CHECK_EQUAL(2, visitor.result_i);
variant_etl = std::string("3");
const test_variant_etl_3 const_variant_etl3(variant_etl);
const_variant_etl3.accept(visitor);
CHECK_EQUAL("3", visitor.result_s);
}
#include "etl/private/diagnostic_pop.h"
//*************************************************************************
#if ETL_USING_CPP17
TEST(test_variant_accept_functor_with_overload)
{
char result_c;
int result_i;
std::string result_s;
auto visitor = etl::make_overload(
[&result_c](char& c)
{
result_c = 1;
++c;
},
[&result_i](int& i)
{
result_i = 2;
++i;
},
[&result_s](std::string& s)
{
result_s = "3";
s = "4";
});
test_variant_etl_3 variant_etl;
variant_etl = char(1);
variant_etl.accept(visitor);
CHECK_EQUAL(1, result_c);
CHECK_EQUAL(2, etl::get<char>(variant_etl));
variant_etl = int(2);
variant_etl.accept(visitor);
CHECK_EQUAL(2, result_i);
CHECK_EQUAL(3, etl::get<int>(variant_etl));
variant_etl = std::string("3");
variant_etl.accept(visitor);
CHECK_EQUAL("3", result_s);
CHECK_EQUAL("4", etl::get<std::string>(variant_etl));
}
//*************************************************************************
TEST(test_variant_accept_functor_with_overload_deprecated)
{
char result_c;
int result_i;
std::string result_s;
auto visitor = etl::make_overload([&result_c](char) { result_c = 1; }, [&result_i](int) { result_i = 2; },
[&result_s](const std::string&) { result_s = "3"; });
test_variant_etl_3 variant_etl;
variant_etl = char(1);
variant_etl.accept_functor(visitor);
CHECK_EQUAL(1, result_c);
variant_etl = int(2);
variant_etl.accept(visitor);
CHECK_EQUAL(2, result_i);
variant_etl = std::string("3");
variant_etl.accept(visitor);
CHECK_EQUAL("3", result_s);
}
//*************************************************************************
#include "etl/private/diagnostic_uninitialized_push.h"
TEST(test_const_variant_accept_functor_with_overload)
{
char result_c;
int result_i;
std::string result_s;
auto visitor = etl::make_overload([&result_c](char) { result_c = 1; }, [&result_i](int) { result_i = 2; },
[&result_s](const std::string&) { result_s = "3"; });
test_variant_etl_3 variant_etl;
variant_etl = char(1);
const test_variant_etl_3 const_variant_etl1(variant_etl);
const_variant_etl1.accept(visitor);
CHECK_EQUAL(1, result_c);
variant_etl = int(2);
const test_variant_etl_3 const_variant_etl2(variant_etl);
const_variant_etl2.accept(visitor);
CHECK_EQUAL(2, result_i);
variant_etl = std::string("3");
const test_variant_etl_3 const_variant_etl3(variant_etl);
const_variant_etl3.accept(visitor);
CHECK_EQUAL("3", result_s);
}
#include "etl/private/diagnostic_pop.h"
//*************************************************************************
#include "etl/private/diagnostic_uninitialized_push.h"
TEST(test_const_variant_accept_functor_with_overload_deprecated)
{
char result_c;
int result_i;
std::string result_s;
auto visitor = etl::make_overload([&result_c](char) { result_c = 1; }, [&result_i](int) { result_i = 2; },
[&result_s](const std::string&) { result_s = "3"; });
test_variant_etl_3 variant_etl;
variant_etl = char(1);
const test_variant_etl_3 const_variant_etl1(variant_etl);
const_variant_etl1.accept_functor(visitor);
CHECK_EQUAL(1, result_c);
variant_etl = int(2);
const test_variant_etl_3 const_variant_etl2(variant_etl);
const_variant_etl2.accept(visitor);
CHECK_EQUAL(2, result_i);
variant_etl = std::string("3");
const test_variant_etl_3 const_variant_etl3(variant_etl);
const_variant_etl3.accept(visitor);
CHECK_EQUAL("3", result_s);
}
#include "etl/private/diagnostic_pop.h"
#endif
//*************************************************************************
TEST(test_get_if_index)
{
test_variant_etl_3 variant_etl;
variant_etl = char(1);
CHECK(etl::get_if<0>(&variant_etl) != nullptr);
CHECK(etl::get_if<1>(&variant_etl) == nullptr);
CHECK(etl::get_if<2>(&variant_etl) == nullptr);
variant_etl = int(2);
CHECK(etl::get_if<0>(&variant_etl) == nullptr);
CHECK(etl::get_if<1>(&variant_etl) != nullptr);
CHECK(etl::get_if<2>(&variant_etl) == nullptr);
variant_etl = std::string("3");
CHECK(etl::get_if<0>(&variant_etl) == nullptr);
CHECK(etl::get_if<1>(&variant_etl) == nullptr);
CHECK(etl::get_if<2>(&variant_etl) != nullptr);
}
//*************************************************************************
TEST(test_get_if_type)
{
test_variant_etl_3 variant_etl;
variant_etl = char(1);
CHECK(etl::get_if<char>(&variant_etl) != nullptr);
CHECK(etl::get_if<int>(&variant_etl) == nullptr);
CHECK(etl::get_if<std::string>(&variant_etl) == nullptr);
#include "etl/private/diagnostic_useless_cast_push.h"
variant_etl = int(2);
#include "etl/private/diagnostic_pop.h"
CHECK(etl::get_if<char>(&variant_etl) == nullptr);
CHECK(etl::get_if<int>(&variant_etl) != nullptr);
CHECK(etl::get_if<std::string>(&variant_etl) == nullptr);
variant_etl = std::string("3");
CHECK(etl::get_if<char>(&variant_etl) == nullptr);
CHECK(etl::get_if<int>(&variant_etl) == nullptr);
CHECK(etl::get_if<std::string>(&variant_etl) != nullptr);
}
//*************************************************************************
TEST(test_variant_size)
{
test_variant_etl_3 variant_etl;
#if ETL_USING_CPP17
CHECK_EQUAL(3U, etl::variant_size_v<test_variant_etl_3>);
#else
CHECK_EQUAL(3U, etl::variant_size<test_variant_etl_3>::value);
#endif
}
#if ETL_USING_CPP17
//*************************************************************************
TEST(test_compare_etl_and_stl_variant_with_moveable_type)
{
Moveable from_etl;
Moveable to_etl;
Moveable from_std;
Moveable to_std;
etl::variant<Moveable> variant_etl(etl::move(from_etl));
std::variant<Moveable> variant_std(etl::move(from_std));
variant_etl = etl::move(from_etl);
variant_std = etl::move(from_std);
CHECK_EQUAL(from_std.moved_from, from_etl.moved_from);
CHECK_EQUAL(from_std.moved_to, from_etl.moved_to);
CHECK_EQUAL(from_std.copied_to, from_etl.copied_to);
to_etl = etl::move(etl::get<0>(variant_etl));
to_std = etl::move(std::get<0>(variant_std));
CHECK_EQUAL(to_std.moved_from, to_etl.moved_from);
CHECK_EQUAL(to_std.moved_to, to_etl.moved_to);
CHECK_EQUAL(to_std.copied_to, to_etl.copied_to);
}
//*************************************************************************
TEST(test_compare_etl_and_stl_variant_with_copyable_type)
{
Copyable from_etl;
Copyable to_etl;
Copyable from_std;
Copyable to_std;
etl::variant<Copyable> variant_etl;
std::variant<Copyable> variant_std;
variant_etl = from_etl;
variant_std = from_std;
CHECK_EQUAL(from_std.moved_from, from_etl.moved_from);
CHECK_EQUAL(from_std.moved_to, from_etl.moved_to);
CHECK_EQUAL(from_std.copied_to, from_etl.copied_to);
to_etl = etl::get<0>(variant_etl);
to_std = std::get<0>(variant_std);
CHECK_EQUAL(to_std.moved_from, to_etl.moved_from);
CHECK_EQUAL(to_std.moved_to, to_etl.moved_to);
CHECK_EQUAL(to_std.copied_to, to_etl.copied_to);
}
//*************************************************************************
TEST(test_compare_etl_and_stl_variant_with_moveable_copyable_type)
{
MoveableCopyable from_etl;
MoveableCopyable to_etl;
MoveableCopyable from_std;
MoveableCopyable to_std;
etl::variant<MoveableCopyable> variant_etl;
std::variant<MoveableCopyable> variant_std;
variant_etl = from_etl;
variant_std = from_std;
CHECK_EQUAL(from_std.moved_from, from_etl.moved_from);
CHECK_EQUAL(from_std.moved_to, from_etl.moved_to);
CHECK_EQUAL(from_std.copied_to, from_etl.copied_to);
variant_etl = etl::move(from_etl);
variant_std = etl::move(from_std);
CHECK_EQUAL(from_std.moved_from, from_etl.moved_from);
CHECK_EQUAL(from_std.moved_to, from_etl.moved_to);
CHECK_EQUAL(from_std.copied_to, from_etl.copied_to);
to_etl = etl::get<0>(variant_etl);
to_std = std::get<0>(variant_std);
CHECK_EQUAL(to_std.moved_from, to_etl.moved_from);
CHECK_EQUAL(to_std.moved_to, to_etl.moved_to);
CHECK_EQUAL(to_std.copied_to, to_etl.copied_to);
to_etl = etl::move(etl::get<0>(variant_etl));
to_std = etl::move(std::get<0>(variant_std));
CHECK_EQUAL(to_std.moved_from, to_etl.moved_from);
CHECK_EQUAL(to_std.moved_to, to_etl.moved_to);
CHECK_EQUAL(to_std.copied_to, to_etl.copied_to);
}
//*************************************************************************
TEST(test_get_by_type)
{
MoveableCopyable value1;
MoveableCopyable value2;
MoveableCopyable value3;
etl::variant<MoveableCopyable> v_etl(value1);
const etl::variant<MoveableCopyable> cv_etl(value2);
etl::variant<MoveableCopyable>& rv_etl(v_etl);
const etl::variant<MoveableCopyable>& crv_etl(value3);
std::variant<MoveableCopyable> v_std(value1);
const std::variant<MoveableCopyable> cv_std(value2);
std::variant<MoveableCopyable>& rv_std(v_std);
const std::variant<MoveableCopyable>& crv_std(value3);
// From variant reference
MoveableCopyable value_vr_etl = etl::get<MoveableCopyable>(rv_etl);
MoveableCopyable value_vr_std = std::get<MoveableCopyable>(rv_std);
CHECK_EQUAL(value_vr_std.moved_from, value_vr_etl.moved_from);
CHECK_EQUAL(value_vr_std.moved_to, value_vr_etl.moved_to);
CHECK_EQUAL(value_vr_std.copied_to, value_vr_etl.copied_to);
// From variant const reference
const MoveableCopyable& value_vcr_etl = etl::get<MoveableCopyable>(crv_etl);
const MoveableCopyable& value_vcr_std = std::get<MoveableCopyable>(crv_std);
CHECK_EQUAL(value_vcr_std.moved_from, value_vcr_etl.moved_from);
CHECK_EQUAL(value_vcr_std.moved_to, value_vcr_etl.moved_to);
CHECK_EQUAL(value_vcr_std.copied_to, value_vcr_etl.copied_to);
// From variant rvalue reference
MoveableCopyable&& value_vrr_etl = etl::get<MoveableCopyable>(etl::move(v_etl));
MoveableCopyable&& value_vrr_std = std::get<MoveableCopyable>(etl::move(v_std));
CHECK_EQUAL(value_vrr_std.moved_from, value_vrr_etl.moved_from);
CHECK_EQUAL(value_vrr_std.moved_to, value_vrr_etl.moved_to);
CHECK_EQUAL(value_vrr_std.copied_to, value_vrr_etl.copied_to);
// From variant const rvalue reference
const MoveableCopyable&& value_vcrr_etl = etl::get<MoveableCopyable>(etl::move(cv_etl));
const MoveableCopyable&& value_vcrr_std = std::get<MoveableCopyable>(etl::move(cv_std));
CHECK_EQUAL(value_vcrr_std.moved_from, value_vcrr_etl.moved_from);
CHECK_EQUAL(value_vcrr_std.moved_to, value_vcrr_etl.moved_to);
CHECK_EQUAL(value_vcrr_std.copied_to, value_vcrr_etl.copied_to);
}
//*************************************************************************
TEST(test_get_by_index)
{
MoveableCopyable value1;
MoveableCopyable value2;
MoveableCopyable value3;
etl::variant<MoveableCopyable> v_etl(value1);
const etl::variant<MoveableCopyable> cv_etl(value2);
etl::variant<MoveableCopyable>& rv_etl(v_etl);
const etl::variant<MoveableCopyable>& crv_etl(value3);
std::variant<MoveableCopyable> v_std(value1);
const std::variant<MoveableCopyable> cv_std(value2);
std::variant<MoveableCopyable>& rv_std(v_std);
const std::variant<MoveableCopyable>& crv_std(value3);
// From variant reference
MoveableCopyable value_vr_etl = etl::get<0U>(rv_etl);
MoveableCopyable value_vr_std = std::get<0U>(rv_std);
CHECK_EQUAL(value_vr_std.moved_from, value_vr_etl.moved_from);
CHECK_EQUAL(value_vr_std.moved_to, value_vr_etl.moved_to);
CHECK_EQUAL(value_vr_std.copied_to, value_vr_etl.copied_to);
// From variant const reference
const MoveableCopyable& value_vcr_etl = etl::get<0U>(crv_etl);
const MoveableCopyable& value_vcr_std = std::get<0U>(crv_std);
CHECK_EQUAL(value_vcr_std.moved_from, value_vcr_etl.moved_from);
CHECK_EQUAL(value_vcr_std.moved_to, value_vcr_etl.moved_to);
CHECK_EQUAL(value_vcr_std.copied_to, value_vcr_etl.copied_to);
// From variant rvalue reference
MoveableCopyable&& value_vrr_etl = etl::get<0U>(etl::move(v_etl));
MoveableCopyable&& value_vrr_std = std::get<0U>(etl::move(v_std));
CHECK_EQUAL(value_vrr_std.moved_from, value_vrr_etl.moved_from);
CHECK_EQUAL(value_vrr_std.moved_to, value_vrr_etl.moved_to);
CHECK_EQUAL(value_vrr_std.copied_to, value_vrr_etl.copied_to);
// From variant const rvalue reference
const MoveableCopyable&& value_vcrr_etl = etl::get<0U>(etl::move(cv_etl));
const MoveableCopyable&& value_vcrr_std = std::get<0U>(etl::move(cv_std));
CHECK_EQUAL(value_vcrr_std.moved_from, value_vcrr_etl.moved_from);
CHECK_EQUAL(value_vcrr_std.moved_to, value_vcrr_etl.moved_to);
CHECK_EQUAL(value_vcrr_std.copied_to, value_vcrr_etl.copied_to);
}
#endif
//*************************************************************************
TEST(test_get_if_by_type)
{
int value = 0;
etl::variant<int, double> v(value);
const etl::variant<int, double> cv(value);
etl::variant<int, double>& rv(v);
const etl::variant<int, double>& crv(v);
int* pi;
const int* pci;
double* pd;
const double* pcd;
etl::variant<int, double>* pv = nullptr;
// From nullptr
pi = etl::get_if<int>(pv);
CHECK(pi == nullptr);
// From variant reference
pi = etl::get_if<int>(&rv);
CHECK(pi != nullptr);
pd = etl::get_if<double>(&rv);
CHECK(pd == nullptr);
// From variant const reference
pci = etl::get_if<int>(&crv);
CHECK(pci != nullptr);
pcd = etl::get_if<double>(&crv);
CHECK(pcd == nullptr);
}
//*************************************************************************
TEST(test_get_if_by_index)
{
int value = 0;
etl::variant<int, double> v(value);
const etl::variant<int, double> cv(value);
etl::variant<int, double>& rv(v);
const etl::variant<int, double>& crv(v);
int* pi;
const int* pci;
double* pd;
const double* pcd;
etl::variant<int, double>* pv = nullptr;
// From nullptr
pi = etl::get_if<0U>(pv);
CHECK(pi == nullptr);
// From variant reference
pi = etl::get_if<0U>(&rv);
CHECK(pi != nullptr);
pd = etl::get_if<1U>(&rv);
CHECK(pd == nullptr);
// From variant const reference
pci = etl::get_if<0U>(&crv);
CHECK(pci != nullptr);
pcd = etl::get_if<1U>(&crv);
CHECK(pcd == nullptr);
}
//*************************************************************************
struct variant_test_visit_dispatcher
{
// const overloads
int8_t operator()(int8_t&) const
{
return 1;
}
int8_t operator()(int8_t const&) const
{
return 10;
}
int8_t operator()(uint8_t&) const
{
return 2;
}
int8_t operator()(uint8_t const&) const
{
return 20;
}
int8_t operator()(int16_t&) const
{
return 3;
}
int8_t operator()(int16_t const&) const
{
return 30;
}
// non-const overloads
int8_t operator()(int8_t&)
{
return 5;
}
int8_t operator()(int8_t const&)
{
return 50;
}
int8_t operator()(uint8_t&)
{
return 6;
}
int8_t operator()(uint8_t const&)
{
return 60;
}
int8_t operator()(int16_t&)
{
return 7;
}
int8_t operator()(int16_t const&)
{
return 70;
}
template <typename T>
int8_t operator()(T const&) const
{
return -1;
}
};
TEST(test_variant_visit)
{
etl::variant<int8_t, uint8_t, int16_t> variant;
variant = int8_t{};
variant_test_visit_dispatcher visitor;
auto const& visitor_const = visitor;
int16_t type = etl::visit(visitor_const, variant);
CHECK_EQUAL(1, type);
auto const& variant_const = variant;
type = etl::visit(visitor_const, variant_const);
CHECK_EQUAL(10, type);
type = etl::visit(visitor, variant_const);
CHECK_EQUAL(50, type);
variant = int16_t{};
type = etl::visit(visitor_const, variant);
CHECK_EQUAL(3, type);
type = etl::visit(visitor_const, variant_const);
CHECK_EQUAL(30, type);
type = etl::visit(visitor, variant_const);
CHECK_EQUAL(70, type);
}
//*************************************************************************
struct test_variant_multiple_visit_helper
{
template <typename T1, typename T2>
int16_t operator()(T1 v1, T2 v2) const
{
int16_t res{};
if (std::is_same<T1, int8_t>::value)
res = 1;
else if (std::is_same<T1, uint8_t>::value)
res = 2;
if (std::is_same<T2, int8_t>::value)
res += 10;
else if (std::is_same<T2, uint16_t>::value)
res += 20;
else if (std::is_same<T2, uint8_t>::value)
res += 30;
return res - static_cast<int16_t>(v1) * static_cast<int16_t>(v2);
}
};
TEST(test_variant_multiple_visit)
{
etl::variant<int8_t, uint8_t> variant1;
etl::variant<int8_t, uint16_t, uint8_t> variant2;
variant1 = int8_t{3};
variant2 = int8_t{1};
auto res = etl::visit<int16_t>(test_variant_multiple_visit_helper{}, variant1, variant2);
CHECK_EQUAL(11 - 3, res);
variant2 = uint16_t{2};
res = etl::visit<int16_t>(test_variant_multiple_visit_helper{}, variant1, variant2);
CHECK_EQUAL(21 - 3 * 2, res);
variant1 = uint8_t{};
variant2 = uint8_t{};
res = etl::visit<int16_t>(test_variant_multiple_visit_helper{}, variant1, variant2);
CHECK_EQUAL(32, res);
}
#if ETL_USING_CPP14
//*************************************************************************
TEST(test_variant_multiple_visit_auto_return)
{
etl::variant<int8_t, uint8_t> variant1;
etl::variant<int8_t, uint16_t, uint8_t> variant2;
variant1 = int8_t{3};
variant2 = int8_t{1};
auto const f = [](auto v1, auto v2)
{
return v1 * v2;
};
auto res = etl::visit(f, variant1, variant2);
CHECK_EQUAL(3, res);
variant2 = uint16_t{2};
res = etl::visit(f, variant1, variant2);
CHECK_EQUAL(3 * 2, res);
}
//*************************************************************************
TEST(test_variant_visit_void)
{
etl::variant<int8_t, uint8_t> variant1 = int8_t{};
bool variant_was_signed{};
auto const f = [&variant_was_signed](auto v)
{
variant_was_signed = etl::is_signed<etl::remove_reference_t<decltype(v)>>::value;
};
etl::visit(f, variant1);
CHECK_EQUAL(true, variant_was_signed);
variant1 = uint8_t{};
etl::visit<void>(f, variant1);
CHECK_EQUAL(false, variant_was_signed);
}
#endif // ETL_USING_CPP14
#if ETL_USING_CPP17
//*************************************************************************
TEST(test_variant_visit_with_overload)
{
struct TypeA
{
};
struct TypeB
{
};
struct TypeC
{
};
struct TypeD
{
};
std::string result = "?";
etl::variant<TypeA, TypeB, TypeC, TypeD> package = TypeA{};
etl::visit(etl::overload{[&result](TypeA&) { result = "TypeA"; }, [&result](TypeB&) { result = "TypeB"; },
[&result](TypeC&) { result = "TypeC"; },
[&result](TypeD&)
{
result = "TypeD";
}},
package);
CHECK_EQUAL(std::string("TypeA"), result);
package = TypeA{};
etl::visit(etl::overload{[&result](TypeA&) { result = "TypeA"; }, [&result](TypeB&) { result = "TypeB"; },
[&result](TypeC&) { result = "TypeC"; },
[&result](TypeD&)
{
result = "TypeD";
}},
package);
CHECK_EQUAL(std::string("TypeA"), result);
package = TypeB{};
etl::visit(etl::overload{[&result](TypeA&) { result = "TypeA"; }, [&result](TypeB&) { result = "TypeB"; },
[&result](TypeC&) { result = "TypeC"; },
[&result](TypeD&)
{
result = "TypeD";
}},
package);
CHECK_EQUAL(std::string("TypeB"), result);
package = TypeC{};
etl::visit(etl::overload{[&result](TypeA&) { result = "TypeA"; }, [&result](TypeB&) { result = "TypeB"; },
[&result](TypeC&) { result = "TypeC"; },
[&result](TypeD&)
{
result = "TypeD";
}},
package);
CHECK_EQUAL(std::string("TypeC"), result);
package = TypeD{};
etl::visit(etl::overload{[&result](TypeA&) { result = "TypeA"; }, [&result](TypeB&) { result = "TypeB"; },
[&result](TypeC&) { result = "TypeC"; },
[&result](TypeD&)
{
result = "TypeD";
}},
package);
CHECK_EQUAL(std::string("TypeD"), result);
}
#endif
#if ETL_USING_CPP14
//*************************************************************************
TEST(test_variant_comparisons)
{
using Variant = etl::variant<char, int, std::string>;
Variant v_empty1;
Variant v_empty2;
Variant v_char_a('A');
Variant v_char_b('B');
Variant v_int_1(1);
Variant v_int_2(2);
Variant v_hello(std::string("hello"));
Variant v_world(std::string("world"));
CHECK_TRUE(v_empty1 == v_empty2);
CHECK_TRUE(v_empty1 < v_char_a);
CHECK_FALSE(v_char_a < v_empty1);
CHECK_TRUE(v_char_a == v_char_a);
CHECK_TRUE(v_char_a < v_char_b);
CHECK_FALSE(v_char_b < v_char_a);
CHECK_FALSE(v_char_a > v_char_b);
CHECK_TRUE(v_char_b > v_char_a);
CHECK_TRUE(v_char_a <= v_char_b);
CHECK_FALSE(v_char_b <= v_char_a);
CHECK_FALSE(v_char_a >= v_char_b);
CHECK_TRUE(v_char_b >= v_char_a);
CHECK_TRUE(v_char_a <= v_char_a);
CHECK_TRUE(v_char_a >= v_char_a);
CHECK_TRUE(v_int_1 == v_int_1);
CHECK_TRUE(v_int_1 < v_int_2);
CHECK_FALSE(v_int_2 < v_int_1);
CHECK_FALSE(v_int_1 > v_int_2);
CHECK_TRUE(v_int_2 > v_int_1);
CHECK_TRUE(v_int_1 <= v_int_2);
CHECK_FALSE(v_int_2 <= v_int_1);
CHECK_FALSE(v_int_1 >= v_int_2);
CHECK_TRUE(v_int_2 >= v_int_1);
CHECK_TRUE(v_int_1 <= v_int_1);
CHECK_TRUE(v_int_1 >= v_int_1);
CHECK_TRUE(v_hello == v_hello);
CHECK_TRUE(v_hello < v_world);
CHECK_FALSE(v_world < v_hello);
CHECK_FALSE(v_hello > v_world);
CHECK_TRUE(v_world > v_hello);
CHECK_TRUE(v_hello <= v_world);
CHECK_FALSE(v_world <= v_hello);
CHECK_FALSE(v_hello >= v_world);
CHECK_TRUE(v_world >= v_hello);
CHECK_TRUE(v_hello <= v_hello);
CHECK_TRUE(v_hello >= v_hello);
}
#endif
#if ETL_USING_CPP20 && ETL_USING_STL && !(defined(ETL_DEVELOPMENT_OS_APPLE) && defined(ETL_COMPILER_CLANG))
//*************************************************************************
TEST(test_variant_spaceship_operator)
{
using Variant = etl::variant<char, int>;
Variant v_empty1;
Variant v_empty2;
Variant v_char_a('A');
Variant v_char_b('B');
Variant v_int_1(1);
Variant v_int_2(2);
CHECK(std::strong_ordering::equal == v_empty1 <=> v_empty2);
CHECK(std::strong_ordering::less == v_empty1 <=> v_char_a);
CHECK(std::strong_ordering::greater == v_char_a <=> v_empty1);
CHECK(std::strong_ordering::equal == v_char_a <=> v_char_a);
CHECK(std::strong_ordering::less == v_char_a <=> v_char_b);
CHECK(std::strong_ordering::greater == v_char_b <=> v_char_a);
CHECK(std::strong_ordering::equal == v_int_1 <=> v_int_1);
CHECK(std::strong_ordering::less == v_int_1 <=> v_int_2);
CHECK(std::strong_ordering::greater == v_int_2 <=> v_int_1);
CHECK(std::strong_ordering::less == v_char_a <=> v_int_1);
CHECK(std::strong_ordering::greater == v_int_2 <=> v_char_a);
}
#endif
//*************************************************************************
TEST(test_variant_three_way_compare_using_etl_compare_cmp)
{
using Variant = etl::variant<char, int>;
Variant v_empty1;
Variant v_empty2;
Variant v_char_a('A');
Variant v_char_b('B');
Variant v_int_1(1);
Variant v_int_2(2);
using Compare = etl::compare<Variant>;
CHECK_EQUAL(Compare::Equal, (Compare::cmp(v_empty1, v_empty2)));
CHECK_EQUAL(Compare::Equal, (Compare::cmp(v_char_a, v_char_a)));
CHECK_EQUAL(Compare::Less, (Compare::cmp(v_char_a, v_char_b)));
CHECK_EQUAL(Compare::Greater, (Compare::cmp(v_char_b, v_char_a)));
CHECK_EQUAL(Compare::Equal, (Compare::cmp(v_int_1, v_int_1)));
CHECK_EQUAL(Compare::Less, (Compare::cmp(v_int_1, v_int_2)));
CHECK_EQUAL(Compare::Greater, (Compare::cmp(v_int_2, v_int_1)));
CHECK_EQUAL(Compare::Less, (Compare::cmp(v_char_a, v_int_1)));
CHECK_EQUAL(Compare::Greater, (Compare::cmp(v_int_1, v_char_a)));
}
//*************************************************************************
TEST(test_is_type_legacy_api)
{
// Char.
char c = 'a';
test_variant_etl_3 variant_char_etl(c);
CHECK(c == 'a');
CHECK_TRUE(variant_char_etl.is_type<char>());
CHECK_FALSE(variant_char_etl.is_type<int>());
CHECK_FALSE(variant_char_etl.is_type<std::string>());
CHECK_FALSE(variant_char_etl.is_type<double>());
// Int.
int i = 1;
test_variant_etl_3 variant_int_etl(i);
CHECK(i == 1);
CHECK_FALSE(variant_int_etl.is_type<char>());
CHECK_TRUE(variant_int_etl.is_type<int>());
CHECK_FALSE(variant_int_etl.is_type<std::string>());
CHECK_FALSE(variant_int_etl.is_type<double>());
// String.
std::string text("Some Text");
test_variant_etl_3 variant_text_etl(text);
CHECK(text == "Some Text");
CHECK_FALSE(variant_text_etl.is_type<char>());
CHECK_FALSE(variant_text_etl.is_type<int>());
CHECK_TRUE(variant_text_etl.is_type<std::string>());
CHECK_FALSE(variant_text_etl.is_type<double>());
}
//*************************************************************************
TEST(test_is_same_type_legacy_api)
{
char c = 'a';
int i = 1;
test_variant_etl_3 variant1a(c);
test_variant_etl_3 variant1b(c);
test_variant_etl_3 variant2a(i);
CHECK_TRUE(variant1a.is_same_type(variant1b));
CHECK_FALSE(variant1a.is_same_type(variant2a));
}
//*************************************************************************
TEST(test_is_supported_type_legacy_api)
{
test_variant_etl_3 variant1;
CHECK_TRUE(variant1.is_supported_type<char>());
CHECK_TRUE(variant1.is_supported_type<int>());
CHECK_TRUE(variant1.is_supported_type<std::string>());
CHECK_FALSE(variant1.is_supported_type<double>());
}
//*************************************************************************
TEST(test_variant_same_types_get)
{
etl::variant<int, int> v1;
etl::variant<int, int> v2;
v1.emplace<0>(1);
v2.emplace<0>(1);
CHECK(v1 == v2);
v1.emplace<0>(1);
v2.emplace<1>(1);
CHECK(v1 != v2);
v1.emplace<0>(0);
v2.emplace<0>(1);
CHECK(v1 != v2);
v1.emplace<1>(0);
v2.emplace<1>(1);
CHECK(v1 != v2);
v1.emplace<1>(1);
v2.emplace<1>(1);
CHECK(v1 == v2);
v1.emplace<0>(42);
CHECK_EQUAL(42, etl::get<0>(v1));
CHECK_EQUAL(0U, v1.index());
v1.emplace<1>(99);
CHECK_EQUAL(99, etl::get<1>(v1));
CHECK_EQUAL(1U, v1.index());
CHECK(etl::holds_alternative<int>(v1));
CHECK(etl::get_if<int>(&v1) != nullptr);
}
//*************************************************************************
// Tests for noexcept properties of etl::variant
// The noexcept specs only take effect when ETL_USING_EXCEPTIONS is enabled,
// because ETL_NOEXCEPT_IF expands to nothing otherwise.
// The etl::is_nothrow_* traits only work with STL or builtins.
//*************************************************************************
#if ETL_USING_EXCEPTIONS && (defined(ETL_USE_TYPE_TRAITS_BUILTINS) || (ETL_USING_STL && !defined(ETL_USER_DEFINED_TYPE_TRAITS)))
struct VariantNothrowType
{
VariantNothrowType() noexcept {}
VariantNothrowType(const VariantNothrowType&) noexcept {}
VariantNothrowType(VariantNothrowType&&) noexcept {}
VariantNothrowType& operator=(const VariantNothrowType&) noexcept
{
return *this;
}
VariantNothrowType& operator=(VariantNothrowType&&) noexcept
{
return *this;
}
};
struct VariantThrowingCopy
{
VariantThrowingCopy() noexcept {}
VariantThrowingCopy(const VariantThrowingCopy&) {} // may throw
VariantThrowingCopy(VariantThrowingCopy&&) noexcept {}
VariantThrowingCopy& operator=(const VariantThrowingCopy&)
{
return *this;
}
VariantThrowingCopy& operator=(VariantThrowingCopy&&) noexcept
{
return *this;
}
};
struct VariantThrowingMove
{
VariantThrowingMove() noexcept {}
VariantThrowingMove(const VariantThrowingMove&) noexcept {}
VariantThrowingMove(VariantThrowingMove&&) {} // may throw
VariantThrowingMove& operator=(const VariantThrowingMove&) noexcept
{
return *this;
}
VariantThrowingMove& operator=(VariantThrowingMove&&)
{
return *this;
}
};
TEST(test_variant_nothrow_copy_constructible)
{
// variant<Ts...> is nothrow copy constructible only if all Ts are
using AllNothrow = etl::variant<int, double, VariantNothrowType>;
static_assert(etl::is_nothrow_copy_constructible<AllNothrow>::value, "variant<int, double, NothrowType> should be nothrow copy constructible");
using HasThrowingCopy = etl::variant<int, VariantThrowingCopy>;
static_assert(!etl::is_nothrow_copy_constructible<HasThrowingCopy>::value,
"variant<int, ThrowingCopy> should NOT be nothrow copy constructible");
using HasThrowingMove = etl::variant<int, VariantThrowingMove>;
static_assert(etl::is_nothrow_copy_constructible<HasThrowingMove>::value,
"variant<int, ThrowingMove> should be nothrow copy constructible (copy is nothrow)");
CHECK(true);
}
TEST(test_variant_nothrow_move_constructible)
{
// variant<Ts...> is nothrow move constructible only if all Ts are
using AllNothrow = etl::variant<int, double, VariantNothrowType>;
static_assert(etl::is_nothrow_move_constructible<AllNothrow>::value, "variant<int, double, NothrowType> should be nothrow move constructible");
using HasThrowingMove = etl::variant<int, VariantThrowingMove>;
static_assert(!etl::is_nothrow_move_constructible<HasThrowingMove>::value,
"variant<int, ThrowingMove> should NOT be nothrow move constructible");
using HasThrowingCopy = etl::variant<int, VariantThrowingCopy>;
static_assert(etl::is_nothrow_move_constructible<HasThrowingCopy>::value,
"variant<int, ThrowingCopy> should be nothrow move constructible (move is nothrow)");
CHECK(true);
}
TEST(test_variant_nothrow_default_constructible)
{
// variant default-constructs the first alternative
using NothrowFirst = etl::variant<int, VariantThrowingCopy>;
static_assert(etl::is_nothrow_default_constructible<NothrowFirst>::value,
"variant<int, ...> should be nothrow default constructible (int is nothrow)");
using ThrowingFirst = etl::variant<VariantThrowingCopy, int>;
// VariantThrowingCopy has noexcept default ctor, so this should be nothrow
static_assert(etl::is_nothrow_default_constructible<ThrowingFirst>::value,
"variant<ThrowingCopy, int> should be nothrow default constructible (ThrowingCopy has noexcept default ctor)");
CHECK(true);
}
TEST(test_variant_nothrow_copy_assignable)
{
using AllNothrow = etl::variant<int, double, VariantNothrowType>;
static_assert(etl::is_nothrow_copy_assignable<AllNothrow>::value, "variant<int, double, NothrowType> should be nothrow copy assignable");
using HasThrowingCopy = etl::variant<int, VariantThrowingCopy>;
static_assert(!etl::is_nothrow_copy_assignable<HasThrowingCopy>::value, "variant<int, ThrowingCopy> should NOT be nothrow copy assignable");
CHECK(true);
}
TEST(test_variant_nothrow_move_assignable)
{
using AllNothrow = etl::variant<int, double, VariantNothrowType>;
static_assert(etl::is_nothrow_move_assignable<AllNothrow>::value, "variant<int, double, NothrowType> should be nothrow move assignable");
using HasThrowingMove = etl::variant<int, VariantThrowingMove>;
static_assert(!etl::is_nothrow_move_assignable<HasThrowingMove>::value, "variant<int, ThrowingMove> should NOT be nothrow move assignable");
CHECK(true);
}
#endif
}
} // namespace
//*************************************************************************
// Tests for constexpr / ROM-placeable variant (trivially destructible types)
//*************************************************************************
#if ETL_USING_CPP17
namespace
{
// Verify that variant of trivially destructible types is itself trivially destructible.
static_assert(std::is_trivially_destructible<etl::variant<int, float, char>>::value, "variant<int, float, char> should be trivially destructible");
// Verify that variant of non-trivially destructible types is NOT trivially destructible.
static_assert(!std::is_trivially_destructible<etl::variant<int, std::string>>::value,
"variant<int, std::string> should NOT be trivially destructible");
// constexpr default construction
constexpr etl::variant<int, float, char> cv_default{};
static_assert(cv_default.index() == 0, "Default constructed variant should have index 0");
// constexpr construction from value
constexpr etl::variant<int, float, char> cv_int{42};
static_assert(cv_int.index() == 0, "variant holding int should have index 0");
static_assert(etl::get<int>(cv_int) == 42, "get<int> should return 42");
static_assert(etl::get<0>(cv_int) == 42, "get<0> should return 42");
constexpr etl::variant<int, float, char> cv_float{3.0f};
static_assert(cv_float.index() == 1, "variant holding float should have index 1");
constexpr etl::variant<int, float, char> cv_char{'A'};
static_assert(cv_char.index() == 2, "variant holding char should have index 2");
static_assert(etl::get<char>(cv_char) == 'A', "get<char> should return 'A'");
static_assert(etl::get<2>(cv_char) == 'A', "get<2> should return 'A'");
// constexpr construction with in_place_type
constexpr etl::variant<int, float, char> cv_ipt{etl::in_place_type_t<float>{}, 2.0f};
static_assert(cv_ipt.index() == 1, "in_place_type_t<float> should set index 1");
// constexpr construction with in_place_index
constexpr etl::variant<int, float, char> cv_ipi{etl::in_place_index_t<2>{}, 'Z'};
static_assert(cv_ipi.index() == 2, "in_place_index_t<2> should set index 2");
static_assert(etl::get<2>(cv_ipi) == 'Z', "get<2> should return 'Z'");
// constexpr holds_alternative
static_assert(etl::holds_alternative<int>(cv_int), "cv_int should hold int");
static_assert(!etl::holds_alternative<float>(cv_int), "cv_int should not hold float");
static_assert(etl::holds_alternative<float>(cv_float), "cv_float should hold float");
// constexpr get_if
static_assert(etl::get_if<int>(&cv_int) != nullptr, "get_if<int> should not be nullptr");
static_assert(*etl::get_if<int>(&cv_int) == 42, "get_if<int> should point to 42");
static_assert(etl::get_if<float>(&cv_int) == nullptr, "get_if<float> on int variant should be nullptr");
// Verify the constexpr variant can be used in ROM-like context
// (static constexpr / const at namespace scope should be placed in .rodata)
static constexpr etl::variant<int, float, char> rom_variant{100};
static_assert(etl::get<int>(rom_variant) == 100, "ROM variant should hold 100");
} // namespace
SUITE(test_variant_constexpr)
{
TEST(test_constexpr_default_construction)
{
constexpr etl::variant<int, float, char> v{};
CHECK_EQUAL(0U, v.index());
}
TEST(test_constexpr_value_construction)
{
constexpr etl::variant<int, float, char> v{42};
CHECK_EQUAL(0U, v.index());
CHECK_EQUAL(42, etl::get<int>(v));
}
TEST(test_constexpr_in_place_type)
{
constexpr etl::variant<int, float, char> v{etl::in_place_type_t<float>{}, 1.5f};
CHECK_EQUAL(1U, v.index());
CHECK_CLOSE(1.5f, etl::get<float>(v), 0.001f);
}
TEST(test_constexpr_in_place_index)
{
constexpr etl::variant<int, float, char> v{etl::in_place_index_t<2>{}, 'X'};
CHECK_EQUAL(2U, v.index());
CHECK_EQUAL('X', etl::get<char>(v));
}
TEST(test_constexpr_get_by_type)
{
constexpr etl::variant<int, float, char> v{99};
constexpr int value = etl::get<int>(v);
CHECK_EQUAL(99, value);
}
TEST(test_constexpr_get_by_index)
{
constexpr etl::variant<int, float, char> v{3.14f};
constexpr float value = etl::get<1>(v);
CHECK_CLOSE(3.14f, value, 0.001f);
}
TEST(test_constexpr_holds_alternative)
{
constexpr etl::variant<int, float, char> v{'B'};
CHECK(etl::holds_alternative<char>(v));
CHECK(!etl::holds_alternative<int>(v));
CHECK(!etl::holds_alternative<float>(v));
}
TEST(test_constexpr_get_if)
{
constexpr etl::variant<int, float, char> v{42};
CHECK(etl::get_if<int>(&v) != nullptr);
CHECK_EQUAL(42, *etl::get_if<int>(&v));
CHECK(etl::get_if<float>(&v) == nullptr);
}
TEST(test_trivially_destructible_trait)
{
using trivial_variant = etl::variant<int, float, char>;
using trivial_variant_1 = etl::variant<int>;
using trivial_variant_3 = etl::variant<int, double, long>;
using nontrivial_variant = etl::variant<int, std::string>;
using nontrivial_variant_1 = etl::variant<std::string>;
CHECK(std::is_trivially_destructible<trivial_variant>::value);
CHECK(std::is_trivially_destructible<trivial_variant_1>::value);
CHECK(std::is_trivially_destructible<trivial_variant_3>::value);
CHECK(!std::is_trivially_destructible<nontrivial_variant>::value);
CHECK(!std::is_trivially_destructible<nontrivial_variant_1>::value);
}
TEST(test_constexpr_rom_placement)
{
// This test verifies that a constexpr variant can be stored in ROM
// (static constexpr at function scope)
static constexpr etl::variant<int, float, char> v1{42};
static constexpr etl::variant<int, float, char> v2{3.14f};
static constexpr etl::variant<int, float, char> v3{'Z'};
CHECK_EQUAL(42, etl::get<int>(v1));
CHECK_CLOSE(3.14f, etl::get<float>(v2), 0.001f);
CHECK_EQUAL('Z', etl::get<char>(v3));
}
TEST(test_runtime_trivially_destructible_variant)
{
// Ensure trivially destructible variants still work at runtime too
etl::variant<int, float, char> v{10};
CHECK_EQUAL(10, etl::get<int>(v));
v = 2.5f;
CHECK_CLOSE(2.5f, etl::get<float>(v), 0.001f);
v = 'Q';
CHECK_EQUAL('Q', etl::get<char>(v));
v.emplace<0>(77);
CHECK_EQUAL(77, etl::get<0>(v));
}
}
#endif // ETL_USING_CPP17
#include "etl/private/diagnostic_pop.h"
#endif