etl/test/test_utility.cpp
John Wellbelove afa17c5042 Squashed commit of the following:
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.
2026-07-11 13:02:42 +01:00

1149 lines
33 KiB
C++

/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2014 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/memory.h"
#include "etl/utility.h"
#include <algorithm>
#include <map>
#include <type_traits>
#include <vector>
#include "data.h"
namespace
{
bool nonConstCalled;
bool constCalled;
void TestText(std::string&)
{
nonConstCalled = true;
}
void TestText(const std::string&)
{
constCalled = true;
}
int TestGlobal(int i)
{
return 2 * i;
}
using ItemM1 = TestDataM<int>;
using ItemM2 = TestDataM<double>;
//*****************************************************************************
// The test class to call.
class TestClass
{
public:
int MemberFunction(int i)
{
return 2 * i;
}
int operator()(int i)
{
return 2 * i;
}
};
static TestClass test;
} // namespace
namespace
{
SUITE(test_utility)
{
//*************************************************************************
TEST(pair_default_construct)
{
etl::pair<int, double> p1;
CHECK_EQUAL(int(), p1.first);
CHECK_EQUAL(double(), p1.second);
}
//*************************************************************************
TEST(test_pair_construct)
{
etl::pair<int, double> p1(1, 2.3);
CHECK_EQUAL(1, p1.first);
CHECK_EQUAL(2.3, p1.second);
}
#if ETL_USING_CPP17
//*************************************************************************
TEST(test_cpp17_deduced_pair_construct)
{
etl::pair p1{1, 2.3};
CHECK((std::is_same_v<decltype(p1.first), int>));
CHECK((std::is_same_v<decltype(p1.second), double>));
CHECK_EQUAL(1, p1.first);
CHECK_EQUAL(2.3, p1.second);
}
#endif
//*************************************************************************
TEST(test_pair_move_parameter_construct)
{
etl::pair<ItemM1, ItemM2> p1(1, 2.3);
CHECK_EQUAL(1, p1.first.value);
CHECK_EQUAL(2.3, p1.second.value);
}
//*************************************************************************
TEST(test_pair_copy_construct)
{
etl::pair<int, double> p1(1, 2.3);
etl::pair<int, double> p2(p1);
CHECK_EQUAL(p1.first, p2.first);
CHECK_EQUAL(p1.second, p2.second);
}
//*************************************************************************
TEST(test_pair_move_construct)
{
etl::pair<ItemM1, ItemM2> p1(1, 2.3);
etl::pair<ItemM1, ItemM2> p2(std::move(p1));
CHECK(!bool(p1.first));
CHECK(!bool(p1.second));
CHECK_EQUAL(1, p2.first.value);
CHECK_EQUAL(2.3, p2.second.value);
}
//*************************************************************************
TEST(test_pair_copy_construct_alternate)
{
etl::pair<char, float> p1(1, 2.3f);
etl::pair<int, double> p2(p1);
CHECK_EQUAL(p1.first, p2.first);
CHECK_EQUAL(p1.second, p2.second);
}
//*************************************************************************
TEST(test_make_pair)
{
etl::pair<int, double> p1(1, 2.3);
etl::pair<int, double> p2;
p2 = etl::make_pair(1, 2.3);
CHECK_EQUAL(p1.first, p2.first);
CHECK_EQUAL(p1.second, p2.second);
}
//*************************************************************************
TEST(test_make_pair_move)
{
etl::pair<ItemM1, ItemM2> p1(1, 2.3);
etl::pair<ItemM1, ItemM2> p2(0, 0);
#include "etl/private/diagnostic_pessimizing_move_push.h"
p2 = etl::make_pair(std::move(ItemM1(1)), std::move(ItemM2(2.3)));
#include "etl/private/diagnostic_pop.h"
CHECK_EQUAL(p1.first, p2.first);
CHECK_EQUAL(p1.second, p2.second);
}
//*************************************************************************
TEST(test_pair_swap_member)
{
etl::pair<int, double> p1(1, 2.3);
etl::pair<int, double> p2(2, 3.4);
p1.swap(p2);
CHECK_EQUAL(2, p1.first);
CHECK_EQUAL(3.4, p1.second);
CHECK_EQUAL(1, p2.first);
CHECK_EQUAL(2.3, p2.second);
}
//*************************************************************************
TEST(test_pair_swap_global)
{
etl::pair<int, double> p1(1, 2.3);
etl::pair<int, double> p2(2, 3.4);
swap(p1, p2);
CHECK_EQUAL(2, p1.first);
CHECK_EQUAL(3.4, p1.second);
CHECK_EQUAL(1, p2.first);
CHECK_EQUAL(2.3, p2.second);
}
//*************************************************************************
TEST(test_pair_conditional)
{
etl::pair<int, double> p1(1, 2.3);
etl::pair<int, double> p2(1, 2.3);
etl::pair<int, double> p3(2, 3.4);
CHECK(p1 == p2);
CHECK(!(p1 == p3));
CHECK(p1 != p3);
CHECK(!(p1 != p2));
CHECK(p1 <= p2);
CHECK(p1 <= p3);
CHECK(!(p1 < p2));
CHECK(p1 < p3);
CHECK(!(p3 < p1));
CHECK(p1 >= p2);
CHECK(!(p1 >= p3));
CHECK(!(p1 > p2));
CHECK(!(p1 > p3));
CHECK(p3 > p1);
}
//*************************************************************************
TEST(test_pair_conversion)
{
#if ETL_USING_CPP11
etl::pair<int, std::string> ep1(1, "Hello");
std::pair<int, std::string> sp1(2, "World");
etl::pair<int, std::string> ep2(sp1);
std::pair<int, std::string> sp2(ep1);
etl::pair<const int, std::string> ep3(1, "Hello");
std::pair<const int, std::string> sp3(2, "World");
etl::pair<int, std::string> ep4(sp3);
std::pair<int, std::string> sp4(ep3);
etl::pair<const int, std::string> ep5(sp1);
std::pair<const int, std::string> sp5(ep1);
CHECK_EQUAL(2, ep2.first);
CHECK_EQUAL(std::string("World"), ep2.second);
CHECK_EQUAL(1, sp2.first);
CHECK_EQUAL(std::string("Hello"), sp2.second);
CHECK_EQUAL(2, ep4.first);
CHECK_EQUAL(std::string("World"), ep4.second);
CHECK_EQUAL(1, sp4.first);
CHECK_EQUAL(std::string("Hello"), sp4.second);
CHECK_EQUAL(2, ep5.first);
CHECK_EQUAL(std::string("World"), ep5.second);
CHECK_EQUAL(1, sp5.first);
CHECK_EQUAL(std::string("Hello"), sp5.second);
#endif
}
//*************************************************************************
TEST(test_pair_move_conversion)
{
etl::pair<int, std::string> ep1(1, "Hello");
std::pair<int, std::string> sp1(2, "World");
etl::pair<int, std::string> ep2(std::move(sp1));
std::pair<int, std::string> sp2(std::move(ep1));
CHECK_EQUAL(2, ep2.first);
CHECK_EQUAL(std::string("World"), ep2.second);
CHECK_EQUAL(1, sp2.first);
CHECK_EQUAL(std::string("Hello"), sp2.second);
}
//*************************************************************************
TEST(test_exchange)
{
int a = 1;
int b = 2;
int c = etl::exchange(a, b); // c = a, a = b
CHECK_EQUAL(2, a);
CHECK_EQUAL(2, b);
CHECK_EQUAL(1, c);
}
//*************************************************************************
TEST(test_exchange_const)
{
int a = 1;
const int b = 2;
int c = etl::exchange(a, b); // c = a, a = b
CHECK_EQUAL(2, a);
CHECK_EQUAL(2, b);
CHECK_EQUAL(1, c);
}
//*************************************************************************
TEST(test_exchange_unique_ptr)
{
etl::unique_ptr<int> p1(new int(1));
etl::unique_ptr<int> p2 = etl::exchange(p1, nullptr);
CHECK_FALSE(p1);
CHECK_TRUE(p2);
CHECK_EQUAL(*p2, 1);
}
//*************************************************************************
TEST(test_exchange_unique_ptr_move_in)
{
etl::unique_ptr<int> a(new int(10));
etl::unique_ptr<int> b(new int(20));
etl::unique_ptr<int> old = etl::exchange(a, etl::move(b));
CHECK_TRUE(old);
CHECK_EQUAL(*old, 10);
CHECK_TRUE(a);
CHECK_EQUAL(*a, 20);
CHECK_FALSE(b);
}
//*************************************************************************
TEST(test_as_const)
{
std::string text = "Hello World!";
nonConstCalled = false;
constCalled = false;
TestText(text);
CHECK(nonConstCalled);
CHECK(!constCalled);
nonConstCalled = false;
constCalled = false;
TestText(etl::as_const(text));
CHECK(!nonConstCalled);
CHECK(constCalled);
}
//*************************************************************************
TEST(test_select1st)
{
typedef etl::pair<int, std::string> EtlPair;
typedef std::pair<int, std::string> StdPair;
EtlPair ep1(1, "Hello");
StdPair sp2(2, "World");
auto selector = etl::select1st<EtlPair>();
CHECK_EQUAL(1, selector(ep1));
CHECK_EQUAL(2, selector(sp2));
}
//*************************************************************************
TEST(test_select1st_example)
{
//! [test_select1st_example]
using Map = std::map<int, double>;
using Vector = std::vector<int>;
const Map map = {{1, 0.3}, {47, 0.8}, {33, 0.1}};
Vector result{};
// Extract the map keys into a vector
std::transform(map.begin(), map.end(), std::back_inserter(result), etl::select1st<Map::value_type>());
//! [test_select1st_example]
CHECK_EQUAL(3, result.size());
const Vector expected{1, 33, 47};
CHECK_ARRAY_EQUAL(expected, result, 3);
}
//*************************************************************************
TEST(test_select2nd)
{
typedef etl::pair<int, std::string> EtlPair;
typedef std::pair<int, std::string> StdPair;
EtlPair ep1(1, "Hello");
StdPair sp2(2, "World");
auto selector = etl::select2nd<EtlPair>();
CHECK_EQUAL(std::string("Hello"), selector(ep1));
CHECK_EQUAL(std::string("World"), selector(sp2));
}
//*************************************************************************
TEST(test_select2nd_example)
{
//! [test_select2nd_example]
using Map = std::map<int, double>;
using Vector = std::vector<double>;
const Map map = {{1, 0.3}, {47, 0.8}, {33, 0.1}};
Vector result{};
// Extract the map values into a vector
std::transform(map.begin(), map.end(), std::back_inserter(result), etl::select2nd<Map::value_type>());
//! [test_select2nd_example]
CHECK_EQUAL(3, result.size());
const Vector expected{0.1, 0.3, 0.8};
sort(result.begin(), result.end()); // sort for comparison
CHECK_ARRAY_CLOSE(expected, result, 3, 0.0001);
}
//*************************************************************************
TEST(test_functor_deprecated)
{
constexpr etl::functor<int, int> fw1(TestGlobal);
CHECK_EQUAL(2, fw1(1));
}
//*************************************************************************
TEST(test_member_function_wrapper_deprecated)
{
constexpr int (*pf)(int) = &etl::member_function_wrapper<int(int)>::function<TestClass, test, &TestClass::MemberFunction>;
CHECK_EQUAL(2, pf(1));
}
//*************************************************************************
TEST(test_functor_wrapper_deprecated)
{
constexpr int (*pf)(int) = &etl::functor_wrapper<int(int)>::function<TestClass, test>;
CHECK_EQUAL(2, pf(1));
}
//*************************************************************************
#if ETL_USING_CPP17
TEST(test_function_as_functor)
{
constexpr etl::function_as_functor<TestGlobal> faf;
CHECK_EQUAL(2, faf(1));
}
#endif
//*************************************************************************
TEST(test_function_ptr_as_functor)
{
using function_type = decltype(TestGlobal);
constexpr function_type* fptr = TestGlobal;
constexpr etl::function_ptr_as_functor<function_type> fpaf(fptr);
CHECK_EQUAL(2, fpaf(1));
}
//*************************************************************************
#if ETL_USING_CPP17
TEST(test_functor_as_static)
{
using fas_t = etl::functor_as_static<test>;
CHECK_EQUAL(2, fas_t::call(1));
}
#endif
//*************************************************************************
#if ETL_USING_CPP17
TEST(test_member_function_as_static)
{
using mfas_t = etl::member_function_as_static<&TestClass::MemberFunction, test>;
CHECK_EQUAL(2, mfas_t::call(1));
}
#endif
#if ETL_USING_CPP17
//*************************************************************************
TEST(test_member_function_as_functor)
{
constexpr etl::member_function_as_functor<&TestClass::MemberFunction, test> mfaf;
CHECK_EQUAL(2, mfaf(1));
}
#endif
//*************************************************************************
struct SF
{
};
//*********************************
enum class forward_call_type
{
LValue,
ConstLValue,
RValue,
ConstRValue
};
//*********************************
std::ostream& operator<<(std::ostream& os, forward_call_type type)
{
switch (type)
{
case forward_call_type::LValue:
{
os << "LValue";
break;
}
case forward_call_type::ConstLValue:
{
os << "ConstLValue";
break;
}
case forward_call_type::RValue:
{
os << "RValue";
break;
}
case forward_call_type::ConstRValue:
{
os << "ConstRValue";
break;
}
default:
{
os << "Unknown type";
break;
}
}
return os;
}
//*********************************
forward_call_type function_f(SF&)
{
return forward_call_type::LValue;
}
//*********************************
forward_call_type function_f(const SF&)
{
return forward_call_type::ConstLValue;
}
//*********************************
forward_call_type function_f(SF&&)
{
return forward_call_type::RValue;
}
//*********************************
forward_call_type function_f(const SF&&)
{
return forward_call_type::ConstRValue;
}
//*********************************
template <typename T>
forward_call_type template_function_f(T && t)
{
return function_f(etl::forward<T>(t));
}
//*********************************
TEST(test_forward)
{
SF s1;
const SF s2;
CHECK_EQUAL(forward_call_type::LValue, template_function_f(s1));
CHECK_EQUAL(forward_call_type::RValue, template_function_f(etl::move(s1)));
CHECK_EQUAL(forward_call_type::ConstLValue, template_function_f(s2));
CHECK_EQUAL(forward_call_type::ConstRValue, template_function_f(etl::move(s2)));
}
//*************************************************************************
struct TFL
{
};
struct UFL
{
};
enum class forward_like_call_type
{
LValue,
ConstLValue,
RValue,
ConstRValue
};
//*********************************
std::ostream& operator<<(std::ostream& os, forward_like_call_type type)
{
switch (type)
{
case forward_like_call_type::LValue:
{
os << "LValue";
break;
}
case forward_like_call_type::ConstLValue:
{
os << "ConstLValue";
break;
}
case forward_like_call_type::RValue:
{
os << "RValue";
break;
}
case forward_like_call_type::ConstRValue:
{
os << "ConstRValue";
break;
}
default:
{
os << "Unknown type";
break;
}
}
return os;
}
//*********************************
forward_like_call_type function_fl(UFL&)
{
return forward_like_call_type::LValue;
}
//*********************************
forward_like_call_type function_fl(const UFL&)
{
return forward_like_call_type::ConstLValue;
}
//*********************************
forward_like_call_type function_fl(UFL&&)
{
return forward_like_call_type::RValue;
}
//*********************************
forward_like_call_type function_fl(const UFL&&)
{
return forward_like_call_type::ConstRValue;
}
//*********************************
template <typename T, typename U>
forward_like_call_type template_function_fl(U && u)
{
return function_fl(etl::forward_like<T>(u));
}
//*********************************
TEST(test_forward_like)
{
UFL u1;
const UFL u2;
UFL& u3 = u1;
const UFL& u4 = u2;
CHECK_EQUAL(forward_like_call_type::LValue, template_function_fl<TFL&>(u1));
CHECK_EQUAL(forward_like_call_type::ConstLValue, template_function_fl<const TFL&>(u1));
CHECK_EQUAL(forward_like_call_type::RValue, template_function_fl<TFL&&>(u1));
CHECK_EQUAL(forward_like_call_type::ConstRValue, template_function_fl<const TFL&&>(u1));
CHECK_EQUAL(forward_like_call_type::LValue, template_function_fl<TFL&>(etl::move(u1)));
CHECK_EQUAL(forward_like_call_type::ConstLValue, template_function_fl<const TFL&>(etl::move(u1)));
CHECK_EQUAL(forward_like_call_type::RValue, template_function_fl<TFL&&>(etl::move(u1)));
CHECK_EQUAL(forward_like_call_type::ConstRValue, template_function_fl<const TFL&&>(etl::move(u1)));
CHECK_EQUAL(forward_like_call_type::ConstLValue, template_function_fl<TFL&>(u2));
CHECK_EQUAL(forward_like_call_type::ConstLValue, template_function_fl<const TFL&>(u2));
CHECK_EQUAL(forward_like_call_type::ConstRValue, template_function_fl<TFL&&>(u2));
CHECK_EQUAL(forward_like_call_type::ConstRValue, template_function_fl<const TFL&&>(u2));
CHECK_EQUAL(forward_like_call_type::ConstLValue, template_function_fl<TFL&>(etl::move(u2)));
CHECK_EQUAL(forward_like_call_type::ConstLValue, template_function_fl<const TFL&>(etl::move(u2)));
CHECK_EQUAL(forward_like_call_type::ConstRValue, template_function_fl<TFL&&>(etl::move(u2)));
CHECK_EQUAL(forward_like_call_type::ConstRValue, template_function_fl<const TFL&&>(etl::move(u2)));
CHECK_EQUAL(forward_like_call_type::LValue, template_function_fl<TFL&>(u3));
CHECK_EQUAL(forward_like_call_type::ConstLValue, template_function_fl<const TFL&>(u3));
CHECK_EQUAL(forward_like_call_type::RValue, template_function_fl<TFL&&>(u3));
CHECK_EQUAL(forward_like_call_type::ConstRValue, template_function_fl<const TFL&&>(u3));
CHECK_EQUAL(forward_like_call_type::LValue, template_function_fl<TFL&>(etl::move(u3)));
CHECK_EQUAL(forward_like_call_type::ConstLValue, template_function_fl<const TFL&>(etl::move(u3)));
CHECK_EQUAL(forward_like_call_type::RValue, template_function_fl<TFL&&>(etl::move(u3)));
CHECK_EQUAL(forward_like_call_type::ConstRValue, template_function_fl<const TFL&&>(etl::move(u3)));
CHECK_EQUAL(forward_like_call_type::ConstLValue, template_function_fl<TFL&>(u4));
CHECK_EQUAL(forward_like_call_type::ConstLValue, template_function_fl<const TFL&>(u4));
CHECK_EQUAL(forward_like_call_type::ConstRValue, template_function_fl<TFL&&>(u4));
CHECK_EQUAL(forward_like_call_type::ConstRValue, template_function_fl<const TFL&&>(u4));
CHECK_EQUAL(forward_like_call_type::ConstLValue, template_function_fl<TFL&>(etl::move(u4)));
CHECK_EQUAL(forward_like_call_type::ConstLValue, template_function_fl<const TFL&>(etl::move(u4)));
CHECK_EQUAL(forward_like_call_type::ConstRValue, template_function_fl<TFL&&>(etl::move(u4)));
CHECK_EQUAL(forward_like_call_type::ConstRValue, template_function_fl<const TFL&&>(etl::move(u4)));
}
#if ETL_USING_BUILTIN_UNDERLYING_TYPE
//*********************************
TEST(test_to_underlying)
{
enum enum0_t : int8_t
{
a0 = 'e',
a1 = 't',
a2 = 'l',
a3 = '3'
};
enum class enum1_t : unsigned char
{
b0 = 2,
b1 = 3,
b2 = 5,
};
enum enum2_t : int32_t
{
c0 = -2,
c1 = 100,
};
enum0_t e0 = enum0_t::a1;
enum1_t e1 = enum1_t::b2;
enum2_t e2 = enum2_t::c0;
CHECK_TRUE((etl::is_same<int8_t, etl::underlying_type_t<enum0_t>>::value));
CHECK_TRUE((etl::is_same<unsigned char, etl::underlying_type_t<enum1_t>>::value));
CHECK_TRUE((etl::is_same<int32_t, etl::underlying_type_t<enum2_t>>::value));
CHECK_EQUAL(etl::to_underlying(e0), 't');
CHECK_EQUAL(etl::to_underlying(e1), 5);
CHECK_EQUAL(etl::to_underlying(e2), -2);
CHECK_EQUAL(etl::to_underlying(enum0_t::a0), 'e');
CHECK_EQUAL(etl::to_underlying(enum0_t::a2), 'l');
CHECK_EQUAL(etl::to_underlying(enum0_t::a3), '3');
CHECK_EQUAL(etl::to_underlying(enum1_t::b1), 3);
CHECK_EQUAL(etl::to_underlying(enum2_t::c1), 100);
}
#endif
#if ETL_HAS_PACKED
//*********************************
TEST(test_packed)
{
struct Unpacked
{
uint32_t a = 0x12345678;
uint8_t b = 0x9A;
uint32_t c = 0x87654321;
};
ETL_PACKED_STRUCT(Packed)
{
uint32_t a = 0x12345678;
uint8_t b = 0x9A;
uint32_t c = 0x87654321;
};
ETL_END_PACKED
Unpacked unpacked;
Packed packed;
CHECK_TRUE(sizeof(unpacked) > sizeof(packed));
CHECK_EQUAL(9U, sizeof(packed));
}
#endif
//*********************************
TEST(test_swap)
{
uint32_t a = 0x12345678;
uint32_t b = 0x87654321;
ETL_OR_STD::swap(a, b);
CHECK_EQUAL(a, 0x87654321);
CHECK_EQUAL(b, 0x12345678);
uint32_t* p_a = &a;
uint32_t* p_b = &b;
ETL_OR_STD::swap(p_a, p_b);
CHECK_EQUAL(a, 0x87654321);
CHECK_EQUAL(b, 0x12345678);
CHECK_EQUAL(p_a, &b);
CHECK_EQUAL(p_b, &a);
uint32_t a_a[4]{0x12345678, 0x23456789, 0x34567890, 0x09876543};
uint32_t a_b[4]{0x12abcd78, 0x23abcd89, 0x34abcd90, 0x09abcd43};
ETL_OR_STD::swap(a_a, a_b);
CHECK_EQUAL(a_a[0], 0x12abcd78);
CHECK_EQUAL(a_a[1], 0x23abcd89);
CHECK_EQUAL(a_a[2], 0x34abcd90);
CHECK_EQUAL(a_a[3], 0x09abcd43);
CHECK_EQUAL(a_b[0], 0x12345678);
CHECK_EQUAL(a_b[1], 0x23456789);
CHECK_EQUAL(a_b[2], 0x34567890);
CHECK_EQUAL(a_b[3], 0x09876543);
}
#if ETL_USING_CPP17 && !defined(ETL_FORCE_CPP11_NONTYPE)
//*********************************
TEST(test_nontype_t_cpp17)
{
// Test with int
CHECK_TRUE(42 == etl::nontype_t<42>::value);
// Test with bool
CHECK_TRUE(true == etl::nontype_t<true>::value);
// Test with char
CHECK_TRUE('A' == etl::nontype_t<'A'>::value);
// Test with enum
enum class E : uint8_t
{
A = 1,
B = 2
};
CHECK_TRUE(E::A == etl::nontype_t<E::A>::value);
CHECK_TRUE(E::B == etl::nontype_t<E::B>::value);
}
#elif ETL_USING_CPP11
enum class E : uint8_t
{
A = 1,
B = 2
};
TEST(test_nontype_t_cpp11)
{
// Test with int
CHECK_TRUE(42 == (etl::nontype_t<int, 42>::value));
// Test with bool
CHECK_TRUE(true == (etl::nontype_t<bool, true>::value));
// Test with char
CHECK_TRUE('A' == (etl::nontype_t<char, 'A'>::value));
// Test with enum
CHECK_TRUE(E::A == (etl::nontype_t<E, E::A>::value));
CHECK_TRUE(E::B == (etl::nontype_t<E, E::B>::value));
}
#endif
//*********************************
TEST(test_make_index_sequence_matches_expected)
{
using seq0 = etl::make_index_sequence<0U>;
using seq1 = etl::make_index_sequence<1U>;
using seq4 = etl::make_index_sequence<4U>;
using expect0 = etl::index_sequence<>;
using expect1 = etl::index_sequence<0U>;
using expect4 = etl::index_sequence<0U, 1U, 2U, 3U>;
CHECK_TRUE((std::is_same<seq0, expect0>::value));
CHECK_TRUE((std::is_same<seq1, expect1>::value));
CHECK_TRUE((std::is_same<seq4, expect4>::value));
}
//*********************************
TEST(test_make_index_sequence_for_pack_matches_expected)
{
struct T1
{
};
struct T2
{
};
struct T3
{
};
using seq12 = etl::make_index_sequence_for<T1, T2>;
using seq123 = etl::make_index_sequence_for<T1, T2, T3>;
using expect12 = etl::index_sequence<0U, 1U>;
using expect123 = etl::index_sequence<0U, 1U, 2U>;
CHECK_TRUE((std::is_same<seq12, expect12>::value));
CHECK_TRUE((std::is_same<seq123, expect123>::value));
}
//*********************************
TEST(test_make_index_sequence_for_type_list_matches_expected)
{
struct T1
{
};
struct T2
{
};
struct T3
{
};
using list12 = etl::type_list<T1, T2>;
using list123 = etl::type_list<T1, T2, T3>;
using seq12 = etl::make_index_sequence_for<list12>;
using seq123 = etl::make_index_sequence_for<list123>;
using expect12 = etl::index_sequence<0U, 1U>;
using expect123 = etl::index_sequence<0U, 1U, 2U>;
CHECK_TRUE((std::is_same<seq12, expect12>::value));
CHECK_TRUE((std::is_same<seq123, expect123>::value));
}
//*********************************
TEST(test_make_index_sequence_with_offset_matches_expected)
{
using seq0 = etl::make_index_sequence_with_offset<5U, 0U>;
using seq2 = etl::make_index_sequence_with_offset<3U, 2U>;
using seq4 = etl::make_index_sequence_with_offset<7U, 4U>;
using expect0 = etl::index_sequence<>;
using expect2 = etl::index_sequence<3U, 4U>;
using expect4 = etl::index_sequence<7U, 8U, 9U, 10U>;
CHECK_TRUE((std::is_same<seq0, expect0>::value));
CHECK_TRUE((std::is_same<seq2, expect2>::value));
CHECK_TRUE((std::is_same<seq4, expect4>::value));
}
//*********************************
TEST(test_index_sequence_push_front_matches_expected)
{
using seq0 = etl::index_sequence<>;
using seq1 = etl::index_sequence<1U, 2U>;
using result0 = etl::index_sequence_push_front_t<seq0, 5U>;
using result1 = etl::index_sequence_push_front_t<seq1, 0U>;
using expect0 = etl::index_sequence<5U>;
using expect1 = etl::index_sequence<0U, 1U, 2U>;
CHECK_TRUE((std::is_same<result0, expect0>::value));
CHECK_TRUE((std::is_same<result1, expect1>::value));
}
//*********************************
TEST(test_index_sequence_pop_front_matches_expected)
{
using seq0 = etl::index_sequence<>;
using seq1 = etl::index_sequence<1U, 2U>;
using result0 = etl::index_sequence_pop_front_t<seq0>;
using result1 = etl::index_sequence_pop_front_t<seq1>;
using expect0 = etl::index_sequence<>;
using expect1 = etl::index_sequence<2U>;
CHECK_TRUE((std::is_same<result0, expect0>::value));
CHECK_TRUE((std::is_same<result1, expect1>::value));
}
//*********************************
TEST(test_index_sequence_push_back_matches_expected)
{
using seq0 = etl::index_sequence<>;
using seq1 = etl::index_sequence<1U, 2U>;
using result0 = etl::index_sequence_push_back_t<seq0, 5U>;
using result1 = etl::index_sequence_push_back_t<seq1, 3U>;
using expect0 = etl::index_sequence<5U>;
using expect1 = etl::index_sequence<1U, 2U, 3U>;
CHECK_TRUE((std::is_same<result0, expect0>::value));
CHECK_TRUE((std::is_same<result1, expect1>::value));
}
//*********************************
TEST(test_index_sequence_pop_back_matches_expected)
{
using seq0 = etl::index_sequence<>;
using seq1 = etl::index_sequence<1U, 2U>;
using result0 = etl::index_sequence_pop_back_t<seq0>;
using result1 = etl::index_sequence_pop_back_t<seq1>;
using expect0 = etl::index_sequence<>;
using expect1 = etl::index_sequence<1U>;
CHECK_TRUE((std::is_same<result0, expect0>::value));
CHECK_TRUE((std::is_same<result1, expect1>::value));
}
//*********************************
TEST(test_index_sequence_cat_matches_expected)
{
using seq0 = etl::index_sequence<>;
using seq1 = etl::index_sequence<0U, 1U>;
using seq2 = etl::index_sequence<2U, 3U>;
using result0 = etl::index_sequence_cat_t<seq0, seq1>;
using result1 = etl::index_sequence_cat_t<seq1, seq2>;
using expect0 = etl::index_sequence<0U, 1U>;
using expect1 = etl::index_sequence<0U, 1U, 2U, 3U>;
CHECK_TRUE((std::is_same<result0, expect0>::value));
CHECK_TRUE((std::is_same<result1, expect1>::value));
}
//*********************************
TEST(test_index_sequence_at_matches_expected)
{
// using seq0 = etl::index_sequence<>; // This should fail to
// compile as seq0 is empty
using seq1 = etl::index_sequence<1U, 2U, 3U>;
// using result0 = etl::index_sequence_at<seq0, 0>; // This should fail to
// compile as seq0 is empty auto ignore0 = result0; // Uses result0
size_t result1a = etl::index_sequence_at<seq1, 0>::value;
size_t result1b = etl::index_sequence_at<seq1, 1>::value;
size_t result1c = etl::index_sequence_at<seq1, 2>::value;
// size_t result1d = etl::index_sequence_at<seq1, 3>::value; // This
// should fail to compile as seq1 only has 3 elements auto ignore1d =
// result1d; // Uses result1d
size_t expect1a = 1U;
size_t expect1b = 2U;
size_t expect1c = 3U;
CHECK_EQUAL(expect1a, result1a);
CHECK_EQUAL(expect1b, result1b);
CHECK_EQUAL(expect1c, result1c);
#if ETL_USING_CPP17
size_t result1e = etl::index_sequence_at_v<seq1, 0>;
size_t result1f = etl::index_sequence_at_v<seq1, 1>;
size_t result1g = etl::index_sequence_at_v<seq1, 2>;
CHECK_EQUAL(expect1a, result1e);
CHECK_EQUAL(expect1b, result1f);
CHECK_EQUAL(expect1c, result1g);
#endif
}
//*************************************************************************
TEST(test_pair_equality_uses_equality_operator)
{
// Basic equality
etl::pair<int, int> p1(1, 2);
etl::pair<int, int> p2(1, 2);
etl::pair<int, int> p3(1, 3);
etl::pair<int, int> p4(2, 2);
CHECK_TRUE(p1 == p2);
CHECK_FALSE(p1 == p3); // different second
CHECK_FALSE(p1 == p4); // different first
// Custom type where operator== and operator< can disagree
// The old code used !(a<b) && !(a>b), which is NOT equivalent to a==b
// for types that don't define a total order consistent with equality.
struct WeirdType
{
int value;
bool equal_flag;
bool operator==(const WeirdType& other) const
{
return equal_flag && other.equal_flag;
}
bool operator<(const WeirdType& other) const
{
return value < other.value;
}
bool operator>(const WeirdType& other) const
{
return value > other.value;
}
};
WeirdType w1{1, false};
WeirdType w2{1, false}; // same value, but equal_flag is false
// With proper ==: w1 == w2 should be false (both equal_flags are false)
// With old !(w1<w2)&&!(w1>w2): would be true (same value)
etl::pair<int, WeirdType> pw1(0, w1);
etl::pair<int, WeirdType> pw2(0, w2);
CHECK_FALSE(pw1 == pw2); // This would FAIL with the old < > based comparison
}
#if ETL_USING_CPP14
//*************************************************************************
TEST(test_pair_constexpr_copy_ctor)
{
constexpr etl::pair<int, int> p1(1, 2);
constexpr etl::pair<int, int> p2(p1);
static_assert(p2.first == 1, "constexpr pair copy ctor first");
static_assert(p2.second == 2, "constexpr pair copy ctor second");
CHECK(true);
}
//*************************************************************************
TEST(test_coordinate_2d_constexpr_ctors)
{
constexpr etl::coordinate_2d<int> c1;
constexpr etl::coordinate_2d<int> c2(3, 4);
static_assert(c1.x == 0, "constexpr default ctor x");
static_assert(c1.y == 0, "constexpr default ctor y");
static_assert(c2.x == 3, "constexpr value ctor x");
static_assert(c2.y == 4, "constexpr value ctor y");
CHECK(true);
}
#endif
}
} // namespace