etl/test/test_array.cpp
rleigh-electradx 70d64496e6
Fix make_array and make_deque rejecting const-qualified lvalues of the element type (#1526)
* Fixed make_array rejecting const-qualified lvalues of the element type

make_array forwarded each argument as T (the element type) rather than
as its own deduced type. etl::forward<T> accepts only a non-const T& or
a T&&, and a T&& cannot bind to a reference-related const lvalue, so
any call passing a const-qualified lvalue of type T failed to compile:

  static const char c = 42;
  etl::make_array<char>(c, c);   // error: would lose const qualifier

Arguments of *other* types slipped through via temporary
materialisation, which is why the existing conversion and move tests
never caught it.

Forward each argument as its own deduced type and convert explicitly
with static_cast<T>. The explicit conversion is required rather than
plain forwarding: function parameters are not constant expressions, so
narrowing initialisers such as make_array<char>(0, 1) - which the old
code accepted and the tests cover - would otherwise be rejected by the
braced initialiser list.

Adds regression tests for const/non-const lvalues of the element type
and for constexpr evaluation. Verified with clang 22 (C++14/17/20) and
GCC 14 (C++11/20); existing conversion and move-only patterns still
compile.

* Fixed make_deque rejecting const-qualified lvalues of the element type

make_deque had the same defect fixed for make_array in the previous
commit: it forwarded each argument as T (the element type) rather than
as its own deduced type. etl::forward<T> accepts only a non-const T& or
a T&&, and a T&& cannot bind to a reference-related const lvalue, so
any call passing a const-qualified lvalue of type T failed to compile:

  static const char c = 42;
  etl::make_deque<char>(c, c);   // error: would lose const qualifier

An audit of all other make_xxx container factories found no further
instances - they forward each argument as its own deduced pack type,
which is the correct idiom.

Forward each argument as its own deduced type and convert explicitly
with static_cast<T>. The explicit conversion keeps narrowing
initialisers such as make_deque<char>(0, 1) - which the old code
accepted and test_make_deque covers - working, which plain forwarding
into the braced initialiser list would reject.

Adds a regression test for const/non-const lvalues of the element type.
No constexpr test, unlike make_array: etl::deque is not a literal type,
so make_deque cannot be evaluated at compile time. Verified with
clang 22 (C++14/17/20); the new test fails to compile before the fix
and passes after, and all 101 deque tests pass.

* Added narrowing-conversion regression tests for make_array and make_deque

Review feedback on the make_deque fix: the const-lvalue regression test
only passes arguments that already have the element type, so nothing
exercises the static_cast<T> conversion that keeps narrowing
initialisers working. If the implementation regressed to plain
forwarding into the braced initialiser list, narrowing calls such as
make_deque<char>(0, 1) would fail to compile.

make_array already covered narrowing from int literals via
test_make_array, but neither container covered conversion from
const-qualified lvalues of a different type (const int narrowed to
char), which combines both dimensions of the fix.

Adds a matching test to each container covering int literals plus
const/non-const int lvalues, all narrowed to char. Verified with
clang 22 (C++17): 102 deque and 53 array tests pass.

* Added const-lvalue regression tests for the remaining make_xxx factories

Review feedback on the make_array/make_deque fixes asked whether the
same defect - rejecting const-qualified lvalue arguments by forwarding
each argument as the element type rather than as its own deduced type -
exists in the other make_xxx container factories. The audit found that
it does not: the other factories forward each argument as its own
deduced pack type. These tests pin that down explicitly for every
remaining factory, mirroring the make_array and make_deque regression
tests:

- make_vector, make_forward_list, make_indirect_vector (element type
  deduced via common_type): const/non-const char lvalues, element type
  checked.
- make_set, make_multiset, make_flat_set, make_flat_multiset,
  make_unordered_set, make_unordered_multiset: const/non-const lvalues
  of the key type.
- make_map, make_multimap, make_flat_map, make_flat_multimap,
  make_unordered_map, make_unordered_multimap: const/non-const lvalues
  of the pair type.

make_reference_flat_* are excluded: they cannot currently be
instantiated at all (no initializer_list constructor exists in the
reference containers), which is reported separately.

Verified with clang 22 (C++17): all 15 test suites pass (996 tests).

* Added make_forward_list parity tests matching the make_list fixes

Checked whether the make_list defect - taking its argument pack by
value, costing an extra move per lvalue argument - also applies to
make_forward_list. It does not: make_forward_list already takes its
pack by forwarding reference, as make_vector does. No header change is
needed.

Adds the two tests that accompanied the make_list fix so that
forward_list has the same coverage and the same regression detector:

- test_make_forward_list_common_type_from_mixed_types, confirming the
  element type deduction through common_type.
- test_make_forward_list_argument_copy_and_move_counts, an
  instrumented type counting copy/move constructions. This is the test
  that fails if the pack ever regresses to by-value (one extra move
  per lvalue argument). The exact move counts are C++17-only, as
  before C++17 the non-guaranteed return elision may add a move per
  element.

Verified with clang 22 (C++17): all 67 forward_list tests pass.

---------

Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>
2026-08-17 10:13:34 +02:00

1162 lines
36 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 "data.h"
#include "etl/array.h"
#include <algorithm>
#include <array>
#include <iterator>
#if ETL_USING_CPP11
#include <tuple>
#endif
#include <type_traits>
#include "etl/integral_limits.h"
namespace
{
using Moveable = TestDataM<int>;
SUITE(test_array)
{
static const size_t SIZE = 10UL;
using Data = etl::array<int, SIZE>;
using Compare_Data = std::array<int, SIZE>;
using ZeroData = etl::array<int, 0>;
Compare_Data compare_data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Compare_Data swap_data = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
//*************************************************************************
TEST(test_constructor)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
CHECK_EQUAL(data.size(), size_t(SIZE));
CHECK_EQUAL(data.max_size(), SIZE);
}
//*************************************************************************
TEST(test_constructor_zero_sized_array)
{
ZeroData data;
CHECK_TRUE(data.empty());
CHECK_EQUAL(data.size(), size_t(0));
CHECK_EQUAL(data.max_size(), 0);
}
#if ETL_USING_CPP17 && ETL_HAS_INITIALIZER_LIST && !defined(ETL_TEMPLATE_DEDUCTION_GUIDE_TESTS_DISABLED)
//*************************************************************************
TEST(test_cpp17_deduced_constructor)
{
etl::array data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Data compare = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
bool isEqual = std::equal(data.begin(), data.end(), compare.begin());
CHECK(isEqual);
}
#endif
//*************************************************************************
TEST(test_assignment)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Data other_data;
other_data = data;
bool isEqual = std::equal(data.begin(), data.end(), other_data.begin());
CHECK(isEqual);
}
//*************************************************************************
TEST(test_at)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for (size_t i = 0UL; i < data.size(); ++i)
{
CHECK_EQUAL(data.at(i), compare_data.at(i));
}
CHECK_THROW(
{
int d = data.at(data.size());
(void)d;
},
etl::array_out_of_range);
}
//*************************************************************************
TEST(test_at_const)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for (size_t i = 0UL; i < data.size(); ++i)
{
CHECK_EQUAL(data.at(i), compare_data.at(i));
}
CHECK_THROW(
{
int d = data.at(data.size());
(void)d;
},
etl::array_out_of_range);
}
//*************************************************************************
TEST(test_index_operator)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for (size_t i = 0UL; i < data.size(); ++i)
{
CHECK_EQUAL(data[i], compare_data[i]);
}
CHECK_THROW(
{
int d = data[data.size()];
(void)d;
},
etl::array_out_of_range);
}
//*************************************************************************
TEST(test_index_operator_const)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for (size_t i = 0UL; i < data.size(); ++i)
{
CHECK_EQUAL(data[i], compare_data[i]);
}
CHECK_THROW(
{
int d = data[data.size()];
(void)d;
},
etl::array_out_of_range);
}
//*************************************************************************
TEST(test_front)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int& ref = data.front();
CHECK(ref == compare_data.front());
++ref;
CHECK(ref == compare_data.front() + 1);
}
//*************************************************************************
TEST(test_front_const)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
const int& ref = data.front();
CHECK(ref == compare_data.front());
}
//*************************************************************************
TEST(test_back)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int& ref = data.back();
CHECK(ref == compare_data.back());
++ref;
CHECK(ref == compare_data.back() + 1);
}
//*************************************************************************
TEST(test_back_const)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
const int& ref = data.back();
CHECK(ref == compare_data.back());
}
//*************************************************************************
TEST(test_data)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
bool isEqual = std::equal(data.begin(), data.end(), data.data());
CHECK(isEqual);
}
//*************************************************************************
TEST(test_data_const)
{
const Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
bool isEqual = std::equal(data.begin(), data.end(), data.data());
CHECK(isEqual);
}
//*************************************************************************
TEST(test_begin)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
CHECK_EQUAL(data.begin(), &data[0]);
}
//*************************************************************************
TEST(test_end)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
CHECK_EQUAL(data.end(), data.data() + SIZE);
}
//*************************************************************************
TEST(test_cbegin)
{
const Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
CHECK_EQUAL(data.cbegin(), data.data());
}
//*************************************************************************
TEST(test_cend)
{
const Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
CHECK_EQUAL(data.cend(), data.data() + SIZE);
}
//*************************************************************************
TEST(test_rbegin)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
CHECK(data.rbegin() == Data::reverse_iterator(data.data() + SIZE));
}
//*************************************************************************
TEST(test_rend)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
CHECK(data.rend() == Data::reverse_iterator(&data[0]));
}
//*************************************************************************
TEST(test_crbegin)
{
const Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
CHECK(data.crbegin() == Data::const_reverse_iterator(data.data() + SIZE));
}
//*************************************************************************
TEST(test_crend)
{
const Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
CHECK(data.crend() == Data::const_reverse_iterator(&data[0]));
}
//*************************************************************************
TEST(test_iterator)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
bool isEqual = std::equal(data.begin(), data.end(), compare_data.begin());
CHECK(isEqual);
}
//*************************************************************************
TEST(test_const_iterator)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
bool isEqual = std::equal(data.cbegin(), data.cend(), compare_data.cbegin());
CHECK(isEqual);
}
//*************************************************************************
TEST(test_reverse_iterator)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
bool isEqual = std::equal(data.rbegin(), data.rend(), compare_data.rbegin());
CHECK(isEqual);
}
//*************************************************************************
TEST(test_const_reverse_iterator)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
bool isEqual = std::equal(data.crbegin(), data.crend(), compare_data.crbegin());
CHECK(isEqual);
}
//*************************************************************************
TEST(test_empty)
{
Data data = {0};
CHECK(!data.empty());
}
//*************************************************************************
TEST(test_size)
{
Data data = {0};
CHECK_EQUAL(SIZE, data.size());
}
//*************************************************************************
TEST(test_max_size)
{
Data data = {0};
CHECK_EQUAL(SIZE, data.max_size());
}
//*************************************************************************
TEST(test_fill)
{
Data data = {0};
data.fill(1);
Compare_Data compare;
compare.fill(1);
bool isEqual = std::equal(data.begin(), data.end(), compare.begin());
CHECK(isEqual);
}
//*************************************************************************
TEST(test_swap)
{
Data data1 = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
Data data2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
swap(data1, data2);
CHECK(std::equal(compare_data.begin(), compare_data.end(), data1.begin()));
CHECK(std::equal(swap_data.begin(), swap_data.end(), data2.begin()));
}
//*************************************************************************
TEST(test_get)
{
Data data1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
const Data data2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
CHECK_EQUAL(data1[3], etl::get<3>(data1));
CHECK_EQUAL(data2[3], etl::get<3>(data2));
// The following line should fail with a compile error.
// int i = etl::get<11>(data2);
}
#if ETL_USING_CPP11
//*************************************************************************
TEST(test_get_rvalue)
{
Data data1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
const Data data2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int&& r0 = etl::get<3>(std::move(data1));
const int&& r1 = etl::get<3>(std::move(data2));
CHECK_EQUAL(3, r0);
CHECK_EQUAL(3, r1);
// The rvalue overloads must be selected and return rvalue references.
CHECK((std::is_same<int&&, decltype(etl::get<3>(std::move(data1)))>::value));
CHECK((std::is_same<const int&&, decltype(etl::get<3>(std::move(data2)))>::value));
// Moving out of an rvalue array element actually moves.
etl::array<Moveable, 2U> data3 = {Moveable(1), Moveable(2)};
Moveable moved(etl::get<0>(std::move(data3)));
CHECK_EQUAL(1, moved.value);
CHECK(!data3[0].valid);
}
#endif
//*************************************************************************
TEST(test_get_constexpr)
{
ETL_CONSTEXPR14 Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
ETL_CONSTEXPR14 int result = etl::get<3>(data);
CHECK_EQUAL(3, result);
}
#if ETL_USING_CPP11
//*************************************************************************
TEST(test_tuple_size)
{
CHECK_EQUAL(SIZE, (etl::tuple_size<Data>::value));
CHECK_EQUAL(SIZE, (std::tuple_size<Data>::value));
#if ETL_USING_CPP17
CHECK_EQUAL(SIZE, (etl::tuple_size_v<Data>));
CHECK_EQUAL(SIZE, (std::tuple_size_v<Data>));
#endif
}
//*************************************************************************
TEST(test_tuple_element)
{
CHECK_TRUE((std::is_same<int, etl::tuple_element_t<0, Data>>::value));
CHECK_TRUE((std::is_same<int, etl::tuple_element_t<SIZE - 1, Data>>::value));
CHECK_TRUE((std::is_same<int, std::tuple_element<0, Data>::type>::value));
CHECK_TRUE((std::is_same<int, std::tuple_element<SIZE - 1, Data>::type>::value));
}
#endif
#if ETL_USING_CPP17
//*************************************************************************
TEST(test_structured_bindings)
{
etl::array<int, 3> data = {1, 2, 3};
// Bind by reference and modify.
auto& [a, b, c] = data;
CHECK_EQUAL(1, a);
CHECK_EQUAL(2, b);
CHECK_EQUAL(3, c);
a = 10;
b = 20;
c = 30;
CHECK_EQUAL(10, data[0]);
CHECK_EQUAL(20, data[1]);
CHECK_EQUAL(30, data[2]);
// Bind by const reference.
const auto& [x, y, z] = data;
CHECK_EQUAL(10, x);
CHECK_EQUAL(20, y);
CHECK_EQUAL(30, z);
}
#endif
//*************************************************************************
TEST(test_assign)
{
int initial[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
int source[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int check1[] = {0, 1, 2, 3, 4, -1, -1, -1, -1, -1};
int check2[] = {0, 1, 2, 3, 4, 99, 99, 99, 99, 99};
Data data = {0};
Data::iterator result;
// Initial data.
result = data.assign(std::begin(initial), std::end(initial));
CHECK(result == data.end());
bool isEqual = std::equal(data.begin(), data.end(), std::begin(initial));
CHECK(isEqual);
// Assign smaller.
result = data.assign(std::begin(initial), std::end(initial));
CHECK(result == data.end());
result = data.assign(&source[0], &source[5]);
CHECK(result == &data[5]);
isEqual = std::equal(data.begin(), data.end(), std::begin(check1));
CHECK(isEqual);
// Assign smaller + default.
result = data.assign(std::begin(initial), std::end(initial));
CHECK(result == data.end());
result = data.assign(&source[0], &source[5], 99);
CHECK(result == &data[5]);
isEqual = std::equal(data.begin(), data.end(), std::begin(check2));
CHECK(isEqual);
}
//*************************************************************************
TEST(test_insert_value)
{
int initial[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int check1[] = {99, 0, 1, 2, 3, 4, 5, 6, 7, 8};
int check2[] = {0, 1, 2, 3, 4, 99, 5, 6, 7, 8};
int check3[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 99};
Data data = {0};
Data::iterator result;
// Insert beginning.
data.assign(std::begin(initial), std::end(initial));
result = data.insert_at(0, 99);
CHECK_EQUAL(data[0], *result);
bool isEqual = std::equal(data.begin(), data.end(), std::begin(check1));
CHECK(isEqual);
// Insert middle.
data.assign(std::begin(initial), std::end(initial));
result = data.insert_at(5, 99);
CHECK_EQUAL(data[5], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check2));
CHECK(isEqual);
// Insert end.
data.assign(std::begin(initial), std::end(initial));
result = data.insert_at(9, 99);
CHECK_EQUAL(data[9], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check3));
CHECK(isEqual);
// Insert out of range
CHECK_THROW({ result = data.insert_at(data.size(), 99); }, etl::array_out_of_range);
}
//*************************************************************************
TEST(test_insert_range)
{
int source1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int source2[] = {12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
int check1[] = {12, 11, 10, 0, 1, 2, 3, 4, 5, 6};
int check2[] = {0, 1, 2, 3, 12, 11, 10, 4, 5, 6};
int check3[] = {0, 1, 2, 3, 4, 5, 6, 12, 11, 10};
int check4[] = {12, 11, 10, 9, 8, 7, 6, 5, 4, 3};
int check5[] = {0, 1, 2, 3, 12, 11, 10, 9, 8, 7, 6};
Data data = {0};
Data::iterator result;
// Insert smaller, beginning.
data.assign(std::begin(source1), std::end(source1));
result = data.insert_at(0, &source2[0], &source2[3]);
CHECK_EQUAL(data[0], *result);
bool isEqual = std::equal(data.begin(), data.end(), std::begin(check1));
CHECK(isEqual);
// Insert smaller, middle.
data.assign(std::begin(source1), std::end(source1));
result = data.insert_at(4, &source2[0], &source2[3]);
CHECK_EQUAL(data[4], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check2));
CHECK(isEqual);
// Insert smaller, end.
data.assign(std::begin(source1), std::end(source1));
result = data.insert_at(7, &source2[0], &source2[3]);
CHECK_EQUAL(data[7], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check3));
CHECK(isEqual);
// Insert larger, beginning.
data.assign(std::begin(source1), std::end(source1));
result = data.insert_at(0, &source2[0], &source2[13]);
CHECK_EQUAL(data[0], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check4));
CHECK(isEqual);
// Insert larger, middle.
data.assign(std::begin(source1), std::end(source1));
result = data.insert_at(4, &source2[0], &source2[13]);
CHECK_EQUAL(data[4], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check5));
CHECK(isEqual);
// Insert out of range
CHECK_THROW({ result = data.insert_at(data.size(), &source2[0], &source2[13]); }, etl::array_out_of_range);
}
//*************************************************************************
TEST(test_erase_single)
{
int initial[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int check1a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 9};
int check1b[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 99};
int check2a[] = {0, 1, 2, 3, 4, 6, 7, 8, 9, 9};
int check2b[] = {0, 1, 2, 3, 4, 6, 7, 8, 9, 99};
int check3a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int check3b[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 99};
Data data = {0};
Data::iterator result;
// Erase beginning.
data.assign(std::begin(initial), std::end(initial));
result = data.erase_at(0);
CHECK_EQUAL(data[0], *result);
bool isEqual = std::equal(data.begin(), data.end(), std::begin(check1a));
CHECK(isEqual);
data.assign(std::begin(initial), std::end(initial));
result = data.erase_at(0, 99);
CHECK_EQUAL(data[0], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check1b));
CHECK(isEqual);
// Erase middle.
data.assign(std::begin(initial), std::end(initial));
result = data.erase_at(5);
CHECK_EQUAL(data[5], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check2a));
CHECK(isEqual);
data.assign(std::begin(initial), std::end(initial));
result = data.erase_at(5, 99);
CHECK_EQUAL(data[5], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check2b));
CHECK(isEqual);
// Erase last.
data.assign(std::begin(initial), std::end(initial));
result = data.erase_at(9);
CHECK_EQUAL(data[9], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check3a));
CHECK(isEqual);
data.assign(std::begin(initial), std::end(initial));
result = data.erase_at(9, 99);
CHECK_EQUAL(data[9], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check3b));
CHECK(isEqual);
// Erase out of range
CHECK_THROW({ result = data.erase_at(data.size()); }, etl::array_out_of_range);
}
//*************************************************************************
TEST(test_erase_range)
{
int initial[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int check1a[] = {5, 6, 7, 8, 9, 5, 6, 7, 8, 9};
int check1b[] = {5, 6, 7, 8, 9, 99, 99, 99, 99, 99};
int check2a[] = {0, 1, 7, 8, 9, 5, 6, 7, 8, 9};
int check2b[] = {0, 1, 7, 8, 9, 99, 99, 99, 99, 99};
int check3a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int check3b[] = {0, 1, 2, 3, 4, 99, 99, 99, 99, 99};
Data data = {0};
Data::iterator result;
// Erase beginning.
data.assign(std::begin(initial), std::end(initial));
result = data.erase_range(0, 5);
CHECK_EQUAL(data[0], *result);
bool isEqual = std::equal(data.begin(), data.end(), std::begin(check1a));
CHECK(isEqual);
data.assign(std::begin(initial), std::end(initial));
result = data.erase_range(0, 5, 99);
CHECK_EQUAL(data[0], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check1b));
CHECK(isEqual);
// Erase middle.
data.assign(std::begin(initial), std::end(initial));
result = data.erase_range(2, 7);
CHECK_EQUAL(data[2], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check2a));
CHECK(isEqual);
data.assign(std::begin(initial), std::end(initial));
result = data.erase_range(2, 7, 99);
CHECK_EQUAL(data[2], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check2b));
CHECK(isEqual);
// Erase last.
data.assign(std::begin(initial), std::end(initial));
result = data.erase_range(5, 10);
CHECK_EQUAL(data[5], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check3a));
CHECK(isEqual);
data.assign(std::begin(initial), std::end(initial));
result = data.erase_range(5, 10, 99);
CHECK_EQUAL(data[5], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check3b));
CHECK(isEqual);
// first is greater than last
CHECK_THROW({ result = data.erase_range(6, 5, 99); }, etl::array_out_of_range);
// Erase out of range
CHECK_THROW({ result = data.erase_range(5, data.size() + 1, 99); }, etl::array_out_of_range);
}
//*************************************************************************
TEST(test_equal)
{
Data data1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Data data2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
CHECK(data1 == data2);
}
//*************************************************************************
TEST(test_equal_constexpr)
{
ETL_CONSTEXPR14 Data data1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
ETL_CONSTEXPR14 Data data2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
ETL_CONSTEXPR14 bool result = (data1 == data2);
CHECK(result);
}
//*************************************************************************
TEST(test_not_equal)
{
Data data1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Data data2 = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
CHECK(data1 != data2);
}
//*************************************************************************
TEST(test_not_equal_constexpr)
{
ETL_CONSTEXPR14 Data data1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
ETL_CONSTEXPR14 Data data2 = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
ETL_CONSTEXPR14 bool result = (data1 != data2);
CHECK(result);
}
//*************************************************************************
TEST(test_less_than)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Data greater = {0, 1, 2, 3, 5, 5, 6, 7, 8, 9};
Data lesser = {0, 1, 2, 3, 4, 4, 6, 7, 8, 9};
CHECK(lesser < data);
CHECK(!(data < data));
CHECK(!(greater < data));
}
//*************************************************************************
TEST(test_less_than_constexpr)
{
ETL_CONSTEXPR14 Data lesser = {0, 1, 2, 3, 4, 4, 6, 7, 8, 9};
ETL_CONSTEXPR14 Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
ETL_CONSTEXPR14 bool result = (lesser < data);
CHECK(result);
}
//*************************************************************************
TEST(test_less_than_equal)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Data greater = {0, 1, 2, 3, 5, 5, 6, 7, 8, 9};
Data lesser = {0, 1, 2, 3, 4, 4, 6, 7, 8, 9};
CHECK(lesser <= data);
CHECK(data <= data);
CHECK(!(greater <= data));
}
//*************************************************************************
TEST(test_less_than_equal_constexpr)
{
ETL_CONSTEXPR14 Data lesser = {0, 1, 2, 3, 4, 4, 6, 7, 8, 9};
ETL_CONSTEXPR14 Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
ETL_CONSTEXPR14 bool result = (lesser <= data);
CHECK(result);
}
//*************************************************************************
TEST(test_greater_than)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Data greater = {0, 1, 2, 3, 5, 5, 6, 7, 8, 9};
Data lesser = {0, 1, 2, 3, 4, 4, 6, 7, 8, 9};
CHECK(greater > data);
CHECK(!(data > data));
CHECK(!(lesser > data));
}
//*************************************************************************
TEST(test_greater_than_constexpr)
{
ETL_CONSTEXPR14 Data greater = {0, 1, 2, 3, 5, 5, 6, 7, 8, 9};
ETL_CONSTEXPR14 Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
ETL_CONSTEXPR14 bool result = (greater > data);
CHECK(result);
}
//*************************************************************************
TEST(test_greater_than_equal)
{
Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Data greater = {0, 1, 2, 3, 5, 5, 6, 7, 8, 9};
Data lesser = {0, 1, 2, 3, 4, 4, 6, 7, 8, 9};
CHECK(greater >= data);
CHECK(data >= data);
CHECK(!(lesser >= data));
}
//*************************************************************************
TEST(test_greater_than_equal_constexpr)
{
ETL_CONSTEXPR14 Data greater = {0, 1, 2, 3, 5, 5, 6, 7, 8, 9};
ETL_CONSTEXPR14 Data data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
ETL_CONSTEXPR14 bool result = (greater >= data);
CHECK(result);
}
//*************************************************************************
#if ETL_USING_CPP17 && ETL_HAS_INITIALIZER_LIST && !defined(ETL_TEMPLATE_DEDUCTION_GUIDE_TESTS_DISABLED)
TEST(test_array_template_deduction)
{
etl::array data{char(0), short(1), 2, long(3), 4, 5, 6, 7, 8, 9};
using Type = std::remove_reference_t<decltype(data[0])>;
CHECK((std::is_same_v<long, Type>));
CHECK_EQUAL(0, data[0]);
CHECK_EQUAL(1, data[1]);
CHECK_EQUAL(2, data[2]);
CHECK_EQUAL(3, data[3]);
CHECK_EQUAL(4, data[4]);
CHECK_EQUAL(5, data[5]);
CHECK_EQUAL(6, data[6]);
CHECK_EQUAL(7, data[7]);
CHECK_EQUAL(8, data[8]);
CHECK_EQUAL(9, data[9]);
}
#endif
//*************************************************************************
#if ETL_USING_CPP17 && ETL_HAS_INITIALIZER_LIST
TEST(test_array_template_deduction_for_movable)
{
etl::array data{Moveable(0), Moveable(1), Moveable(2), Moveable(3), Moveable(4),
Moveable(5), Moveable(6), Moveable(7), Moveable(8), Moveable(9)};
using Type = std::remove_reference_t<decltype(data[0])>;
CHECK((std::is_same_v<Moveable, Type>));
CHECK_EQUAL(Moveable(0), data[0]);
CHECK_EQUAL(Moveable(1), data[1]);
CHECK_EQUAL(Moveable(2), data[2]);
CHECK_EQUAL(Moveable(3), data[3]);
CHECK_EQUAL(Moveable(4), data[4]);
CHECK_EQUAL(Moveable(5), data[5]);
CHECK_EQUAL(Moveable(6), data[6]);
CHECK_EQUAL(Moveable(7), data[7]);
CHECK_EQUAL(Moveable(8), data[8]);
CHECK_EQUAL(Moveable(9), data[9]);
}
#endif
//*************************************************************************
#if ETL_HAS_INITIALIZER_LIST
TEST(test_make_array)
{
auto data = etl::make_array<char>(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
using Type = etl::remove_reference_t<decltype(data[0])>;
CHECK((std::is_same<char, Type>::value));
CHECK_EQUAL(0, data[0]);
CHECK_EQUAL(1, data[1]);
CHECK_EQUAL(2, data[2]);
CHECK_EQUAL(3, data[3]);
CHECK_EQUAL(4, data[4]);
CHECK_EQUAL(5, data[5]);
CHECK_EQUAL(6, data[6]);
CHECK_EQUAL(7, data[7]);
CHECK_EQUAL(8, data[8]);
CHECK_EQUAL(9, data[9]);
}
#endif
//*************************************************************************
#if ETL_HAS_INITIALIZER_LIST
TEST(test_make_array_for_movable)
{
auto data = etl::make_array<Moveable>(Moveable(0), Moveable(1), Moveable(2), Moveable(3), Moveable(4), Moveable(5), Moveable(6), Moveable(7),
Moveable(8), Moveable(9));
using Type = etl::remove_reference_t<decltype(data[0])>;
CHECK((std::is_same<Moveable, Type>::value));
CHECK_EQUAL(Moveable(0), data[0]);
CHECK_EQUAL(Moveable(1), data[1]);
CHECK_EQUAL(Moveable(2), data[2]);
CHECK_EQUAL(Moveable(3), data[3]);
CHECK_EQUAL(Moveable(4), data[4]);
CHECK_EQUAL(Moveable(5), data[5]);
CHECK_EQUAL(Moveable(6), data[6]);
CHECK_EQUAL(Moveable(7), data[7]);
CHECK_EQUAL(Moveable(8), data[8]);
CHECK_EQUAL(Moveable(9), data[9]);
}
#endif
//*************************************************************************
#if ETL_HAS_INITIALIZER_LIST
// Arguments that are const-qualified lvalues of the element type used to be rejected:
// the old implementation forwarded each argument as T, and forward<T> cannot accept a
// const T lvalue (it requires a non-const T&, and T&& cannot bind to a reference-related
// const lvalue).
TEST(test_make_array_from_const_lvalues_of_element_type)
{
static const char static_const_lvalue = 42;
const char local_const_lvalue = 43;
char mutable_lvalue = 44;
auto data = etl::make_array<char>(static_const_lvalue, local_const_lvalue, mutable_lvalue);
using Type = etl::remove_reference_t<decltype(data[0])>;
CHECK((std::is_same<char, Type>::value));
CHECK_EQUAL(42, data[0]);
CHECK_EQUAL(43, data[1]);
CHECK_EQUAL(44, data[2]);
}
#endif
//*************************************************************************
#if ETL_HAS_INITIALIZER_LIST
// The arguments are converted to the element type with static_cast, so narrowing
// initialisers (int literals and int lvalues narrowed to char) must keep working.
// Plain forwarding into the braced initialiser list would reject them.
TEST(test_make_array_from_narrowing_values)
{
static const int static_const_int = 45;
const int local_const_int = 46;
int mutable_int = 47;
auto data = etl::make_array<char>(0, 1, static_const_int, local_const_int, mutable_int);
using Type = etl::remove_reference_t<decltype(data[0])>;
CHECK((std::is_same<char, Type>::value));
CHECK_EQUAL(0, data[0]);
CHECK_EQUAL(1, data[1]);
CHECK_EQUAL(45, data[2]);
CHECK_EQUAL(46, data[3]);
CHECK_EQUAL(47, data[4]);
}
#endif
//*************************************************************************
#if ETL_HAS_INITIALIZER_LIST && ETL_USING_CPP14
TEST(test_make_array_is_constexpr)
{
static constexpr unsigned char constexpr_value = 0x18U;
constexpr auto data = etl::make_array<unsigned char>(constexpr_value, constexpr_value);
static_assert(data.size() == 2U, "make_array size");
static_assert(data[0] == 0x18U, "make_array element");
CHECK_EQUAL(0x18U, data[0]);
CHECK_EQUAL(0x18U, data[1]);
}
#endif
#if ETL_USING_CPP14
//*************************************************************************
using Array = etl::array<int, 10U>;
//*********************************
constexpr int BeginEnd(const Array& data) noexcept
{
return *(data.begin() + 5);
}
//*********************************
constexpr int CBeginCEnd(const Array& data) noexcept
{
return *(data.cbegin() + 5);
}
#if ETL_USING_CPP20 && ETL_USING_STL
//*********************************
constexpr int RBeginREnd(const Array& data) noexcept
{
return *(data.rbegin() + 5);
}
//*********************************
constexpr int CRBeginCREnd(const Array& data) noexcept
{
return *(data.crbegin() + 5);
}
#endif
//*********************************
constexpr int DataSize(const Array& data) noexcept
{
return *(data.data() + 5);
}
//*********************************
constexpr Array Fill(int i) noexcept
{
Array a{};
a.fill(i);
return a;
}
//*********************************
#if ETL_USING_CPP20 && ETL_USING_STL
constexpr Array Swap(Array data1, Array data2) noexcept
{
data1.swap(data2);
return data1;
}
#endif
TEST(test_cpp14_constexpr)
{
constexpr Array data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// [] operator
constexpr int i0 = data[0];
constexpr int i1 = data[1];
constexpr int i2 = data[2];
constexpr int i3 = data[3];
constexpr int i4 = data[4];
constexpr int i5 = data[5];
constexpr int i6 = data[6];
constexpr int i7 = data[7];
constexpr int i8 = data[8];
constexpr int i9 = data[9];
CHECK_EQUAL(data[0], i0);
CHECK_EQUAL(data[1], i1);
CHECK_EQUAL(data[2], i2);
CHECK_EQUAL(data[3], i3);
CHECK_EQUAL(data[4], i4);
CHECK_EQUAL(data[5], i5);
CHECK_EQUAL(data[6], i6);
CHECK_EQUAL(data[7], i7);
CHECK_EQUAL(data[8], i8);
CHECK_EQUAL(data[9], i9);
// front & back
constexpr int f0 = data.front();
constexpr int b9 = data.back();
CHECK_EQUAL(data[0], f0);
CHECK_EQUAL(data[9], b9);
// begin & end
constexpr int b5 = BeginEnd(data);
CHECK_EQUAL(data[5], b5);
// cbegin & cend
constexpr int cb5 = CBeginCEnd(data);
CHECK_EQUAL(data[5], cb5);
#if ETL_USING_CPP20 && ETL_USING_STL
// rbegin & rend
constexpr int rb5 = RBeginREnd(data);
CHECK_EQUAL(data[4], rb5);
// crbegin & crend
constexpr int crb5 = CRBeginCREnd(data);
CHECK_EQUAL(data[4], crb5);
#endif
// data
constexpr int d5 = DataSize(data);
CHECK_EQUAL(data[5], d5);
// empty
constexpr bool e = data.empty();
CHECK_FALSE(e);
// size
constexpr size_t s = data.size();
CHECK_EQUAL(data.size(), s);
// max_size
constexpr size_t ms = data.max_size();
CHECK_EQUAL(data.max_size(), ms);
// fill
constexpr Array a = Fill(5);
CHECK_EQUAL(5, a[0]);
CHECK_EQUAL(5, a[1]);
CHECK_EQUAL(5, a[2]);
CHECK_EQUAL(5, a[3]);
CHECK_EQUAL(5, a[4]);
CHECK_EQUAL(5, a[5]);
CHECK_EQUAL(5, a[6]);
CHECK_EQUAL(5, a[7]);
CHECK_EQUAL(5, a[8]);
CHECK_EQUAL(5, a[9]);
#if ETL_USING_CPP20 && ETL_USING_STL
// swap
constexpr Array data1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
constexpr Array data2{9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
constexpr Array data3 = Swap(data1, data2);
CHECK_ARRAY_EQUAL(data2.data(), data3.data(), data2.size());
#endif
}
#endif
}
} // namespace