mirror of
https://github.com/ETLCPP/etl.git
synced 2026-07-31 00:36:20 +08:00
commit c5dc331004b4d9611ab996935bedada203676c71
Author: John Wellbelove <john.wellbelove@etlcpp.com>
Date: Sat Jul 11 12:32:20 2026 +0100
Updated release notes
commit 4bf6ed6ce8d79857658d763e368ee56617542b2c
Author: vcoselev <145198867+vcoselev@users.noreply.github.com>
Date: Sat Jul 11 12:05:39 2026 +0100
Fix for 1405: etl::exchange doesn't work with unique_ptr (#1477)
* Separate bit order and endianness in bit_stream.h (#1495)
* Factor out ranges tests from test_algorithm.cpp to test_algorithm_ranges.cpp (#1497)
* Fix exchange to use forwarding reference (U&&) and move
- etl::exchange was using the copy constructor. Changed for etl::move.
- The assignment to the new object is now done with forward.
- Added a test with unique_ptr.
---------
Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>
Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
commit 2ed9019df04d470530c41c1f27d35adc30b9a32d
Author: Moritz Pflanzer <moritz@pflanzer.eu>
Date: Fri Jul 10 11:42:32 2026 +0200
Update operator[] docs for map class (#1499)
* Separate bit order and endianness in bit_stream.h (#1495)
* Update operator[] docs for map class
No const variant is implemented for the operator[].
---------
Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>
commit 423e18d96dc645f1c733cb0d8ae6b675b9eb7e83
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date: Fri Jul 10 11:14:08 2026 +0200
Factor out ranges tests from test_algorithm.cpp to test_algorithm_ranges.cpp (#1497)
commit 338bfdbb1f4df3c5c08a68a9038f13b8dae1c28b
Author: John Wellbelove <jwellbelove@users.noreply.github.com>
Date: Fri Jul 10 10:12:04 2026 +0100
Add coverage report to docs (#1498)
* Updated release notes
* Added link to coverage report
---------
Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.com>
commit 269025424f4c6f61cebac090f4df82985fb3b239
Merge: c004cc9d faa4c15b
Author: John Wellbelove <john.wellbelove@etlcpp.com>
Date: Thu Jul 9 14:18:32 2026 +0100
Merge branch 'development' of https://github.com/ETLCPP/etl into development
commit c004cc9d508efcf398630758a47aae95b37a7eb8
Author: John Wellbelove <john.wellbelove@etlcpp.com>
Date: Thu Jul 9 10:06:17 2026 +0100
Updated release notes
commit faa4c15b49c8ad15475c83ae23e00f5a46f803ca
Author: John Wellbelove <john.wellbelove@etlcpp.com>
Date: Thu Jul 9 10:06:17 2026 +0100
Updated release notes
commit 1523e63e35a15cfd1fa72d230a3c79efa7b9c3e0
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date: Wed Jul 8 20:48:33 2026 +0200
Support non-assignable alternatives in variant (#1496)
commit 878a0ba5a28f6b0dcae9f472a6a6a1f107de0a74
Author: John Wellbelove <john.wellbelove@etlcpp.com>
Date: Wed Jul 8 11:07:01 2026 +0100
Updated version and release notes
commit 5287fe4b3b9f58330881d2a62abf6ce8e1159b8d
Author: John Wellbelove <jwellbelove@users.noreply.github.com>
Date: Wed Jul 8 09:06:50 2026 +0100
Allow etl::vector to store pointers to functions and member functions (#1492)
* Refactored vector.h
Deleted ivectorpointer.h
* Minor changes
* Fixed code incompatible with C++03
* Clang-format changes
* Removed redundant double-qualified name
* Fixed is_is_object_pointer_v to is_object_pointer_v
* Added new test files to meson.build
* Added overflow checks to vector_ext copy and move constructors and assignments
Added assert throw tests.
* Added diagnostic pushes to eliminate GCC warning false positives
* Changed the GCC C++23 Github action to use GCC14
* Applied clang-format
* Added guard to pop_heap
* Added guard to adjust_heap
* Attempt to fix GCC diagnostic
* Attempt to fix GCC diagnostic
Re-enable ranges algorithm tests
* Attempt to fix GCC diagnostic
Re-enable ranges algorithm tests
* clang-format
* Attempt to fix GCC diagnostic
Re-enable ranges algorithm tests
* Attempt to fix GCC diagnostic
Re-enable ranges algorithm tests
* Attempt to fix GCC diagnostic
Re-enable ranges algorithm tests
* Missing newline at end of file
* Attempt to fix GCC diagnostic
Re-enable ranges algorithm tests
* Attempt to fix GCC diagnostic
Re-enable ranges algorithm tests
* Attempt to fix GCC diagnostic
Re-enable ranges algorithm tests
* Attempt to fix GCC diagnostic
Re-enable ranges algorithm tests
* Attempt to fix GCC diagnostic
* clang-format
* Changed gcc --version to gcc-14 --version
---------
Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.com>
Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>
commit 40dddd7f9601985ae7350e60a404af9028bc9fe9
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date: Tue Jul 7 23:41:17 2026 +0200
Add optional etl_profile label_flag for Bazel profile injection (#1491)
Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
commit 84fca4cd62f72c382c062376ca934803116a20d5
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date: Tue Jul 7 21:45:47 2026 +0200
Add missing memory functions, according the standard (#1490)
Adding:
- etl::align
- etl::assume_aligned
- etl::is_sufficiently_aligned
- etl::launder
- etl::pointer_traits
- etl::start_lifetime_as
Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
commit dca6653b3759b2cdc79307fec7425589feea478d
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date: Tue Jul 7 21:00:09 2026 +0200
Add missing interfaces to etl::chrono (#1489)
Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
commit c52b2ac7b1f675e6b1d34052239423b7f3634033
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date: Tue Jul 7 18:06:05 2026 +0200
Fix etl::mem_cast cross-size copy constructor and assignment (#1488)
The templated cross-size copy constructor and assignment operator were
broken in two ways:
- They accessed other.buffer / rhs.buffer, which is private in a
different mem_cast instantiation, so the cross-size overloads failed
to compile whenever they were actually instantiated.
- They copied Size_ (the destination size) bytes from a source buffer
that is only Other_Size bytes large, reading past the end of the
source when the destination was larger.
Use the public data() accessor and copy Other_Size bytes. The existing
static_assert(Size >= Other_Size) guarantees the destination is big
enough.
Add test_mem_cast_copy_and_assign_from_smaller regression test.
Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
commit 165674cb23dda1fd4f7e5a1182f49554a796bfe8
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date: Tue Jul 7 16:41:50 2026 +0200
Fix etl::pearson::reset() not resetting the first-byte flag (#1487)
reset() cleared the hash array but left the `first` flag false, so a
calculator reused after reset() took the XOR update path on its next
add() instead of the initialisation path. This produced an incorrect
hash for any pearson object that was reset and reused.
Reset `first` to true in reset() so a reset calculator behaves like a
freshly constructed one.
Add test_pearson_reset regression test.
Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
commit 032ec4cc80f1c0f1baa0383bdbc5f9239b68f268
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date: Tue Jul 7 13:40:04 2026 +0200
Fix etl::bitset single-element from_string shift on empty string (#1486)
The single-element from_string overloads (char, wchar_t, char16_t,
char32_t) computed element_type(1) << (string_length - 1U) before the
copy loop. For an empty string, or when active_bits is 0, string_length
is 0, so the shift count underflows to SIZE_MAX. Shifting by more than
the element width is undefined behaviour, which also breaks constexpr
evaluation.
Guard the mask with a zero-length check so the shift is only performed
when string_length > 0. The multi-element from_string overloads were
already safe.
Add test_construct_from_empty_string regression test.
Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
commit 98cb365d4c28e7b72f8547b734a03a4068acd3f5
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date: Tue Jul 7 12:32:56 2026 +0200
Fix etl::histogram copy/move not preserving start_index (#1485)
The run-time-offset specialization of etl::histogram stores a start_index
member used to map keys to bins (accumulator[key - start_index]). Its copy
constructor, move constructor, copy assignment, and move assignment copied
only the accumulator and left start_index uninitialized, so a copied or
moved histogram indexed the wrong bin in operator[]/add(), causing
out-of-bounds access (undefined behavior).
All four special member functions now also copy start_index. The
compile-time-offset specialization is unaffected, as its start index is a
template constant rather than a data member.
Add regression tests covering copy/move construction and assignment.
Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
commit 5cd2f28cd54e4019f7be126bcf7394a6a8318eb9
Author: Drew Rife <darife@jlg.com>
Date: Tue Jul 7 03:14:48 2026 -0400
Refactor variant::emplace to perfect forward args instead of copy/move construct (#1494)
* refactor: emplace logic to use do_emplace for in-place construction with forwarded arguments #1493
* Add test for variant_variadic
---------
Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>
commit a47c0aae81b886259918961d5fcee20faa06fdd0
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date: Mon Jul 6 17:32:26 2026 +0200
Fix etl::message_timer firing following timers early on unregister (#1484)
active_list.remove(id, has_expired) adjusts the next timer's delta only
when has_expired is false. When unregistering an active, non-expired timer,
unregister_timer() incorrectly passed true, skipping that adjustment, so
every timer after the removed one in the active list fired early by the
removed timer's delta.
Pass false instead, matching the etl::callback_timer family. tick() still
passes true, since the timer has genuinely expired there. The bug affected
all four variants: message_timer, message_timer_interrupt,
message_timer_atomic and message_timer_locked. Existing tests missed it
because they only ever unregistered the tail timer, where the delta
adjustment is a no-op.
Add a regression test that unregisters a non-tail active timer and checks
the following timer still fires at its original absolute time.
2851 lines
87 KiB
C++
2851 lines
87 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;
|
|
};
|
|
|
|
//*********************************************
|
|
// 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;
|
|
};
|
|
} // 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
|
|
{
|
|
};
|
|
#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
|
|
|
|
//*************************************************************************
|
|
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());
|
|
}
|
|
|
|
//*************************************************************************
|
|
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
|