etl/test/test_array.cpp
rleigh-electradx 25e2d6e8a3
Adopt the Library Fundamentals TS make_array design for the sequence factories (#1553)
* Adopt the Library Fundamentals TS make_array design for the sequence factories

make_array, make_deque, make_vector, make_list, make_forward_list and
make_indirect_vector now follow the Library Fundamentals TS
std::experimental::make_array design: a defaulted leading template
parameter selects the element type explicitly, falling back to the
decayed common type of the arguments when it is not supplied. A shared
trait (etl::private_make::element_type) performs the selection lazily,
so common_type is not instantiated when the element type is explicit.

This fixes the explicit-element-type calling style for the four
common-type factories, where make_vector<char>(lvalue) never compiled:
the explicit argument pinned the first pack parameter to char, making
it a char&& that no lvalue can bind to. It also gives make_array and
make_deque the deduced form (e.g. make_array(1, 2L) is array<long, 2>)
for free. All arguments are converted with static_cast, as in the
make_array const-lvalue fix, so const-qualified lvalues and narrowing
initialisers work in both forms.

Intentional behaviour change: an explicit element type now converts
every argument instead of pinning only the first. make_vector<char>(0,
1, ...) previously produced vector<int> - the explicit char affected
only the first argument and the element type was still the common type
- and now produces vector<char>. test_make_vector documented the old
behaviour and has been updated.

The keyed set and map factories are unchanged: their key/mapped types
are separate mandatory template parameters ahead of a fully-deduced
pack, so the explicit-type style already works, and retrofitting
deduction there would interact with the defaulted comparator/hash
parameters.

Adds explicit-element-type tests for the four common-type factories
(const/non-const lvalues plus narrowing initialisers) and
deduced-element-type tests for make_array and make_deque, and updates
the four documented signatures.

* Address review comments on the make_xxx factory design

- Forward same-type arguments in the make_xxx factories instead of
  casting them. static_cast produced a prvalue that, before C++17, had
  to be materialised through the move constructor, rejecting
  copyable-but-non-movable element types. The new
  private_make::convert helper forwards arguments that already have
  the element type and static_casts the rest.
- Drop ETL_NOEXCEPT from make_array: conversions can throw, and the
  other factories do not declare it.
- Remove the redundant etl::decay in element_type<void, ...>;
  etl::common_type already decays its arguments.
- Stop leaking the etl::private_make implementation namespace into the
  documented factory signatures.

* Add a test for make_array with an explicit element type and no arguments

etl::make_array<int>() produces an empty etl::array<int, 0> and now has
a regression test. A call with neither an element type nor arguments
remains ill-formed, matching the Library Fundamentals TS: there is
nothing to deduce the element type from. The array documentation now
states this explicitly.

---------

Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>
2026-08-24 08:53:25 +01:00

1229 lines
38 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
// With no explicit element type, the element type is deduced as the
// decayed common type of the arguments (Library Fundamentals TS
// make_array design).
TEST(test_make_array_deduced_element_type)
{
static const int static_const_lvalue = 1;
auto data = etl::make_array(static_const_lvalue, 2L, 3);
CHECK((std::is_same<etl::array<long, 3U>, decltype(data)>::value));
CHECK_EQUAL(1L, data[0]);
CHECK_EQUAL(2L, data[1]);
CHECK_EQUAL(3L, data[2]);
}
#endif
//*************************************************************************
#if ETL_HAS_INITIALIZER_LIST
// An empty array requires the element type to be supplied explicitly.
// etl::make_array() with no element type and no arguments is ill-formed,
// as in the Library Fundamentals TS: there is nothing to deduce the
// element type from.
TEST(test_make_array_empty)
{
auto data = etl::make_array<int>();
CHECK((std::is_same<etl::array<int, 0U>, decltype(data)>::value));
CHECK_TRUE(data.empty());
CHECK_EQUAL(0U, data.size());
}
#endif
//*************************************************************************
#if ETL_HAS_INITIALIZER_LIST
// Arguments that already have the element type are forwarded rather than
// cast. Casting created a prvalue that, before C++17, had to be
// materialised through the move constructor, so a copyable type with a
// deleted move constructor was rejected.
TEST(test_make_array_for_copyable_non_movable)
{
struct NonMovable
{
explicit NonMovable(int v_)
: v(v_)
{
}
NonMovable(const NonMovable&) = default;
NonMovable(NonMovable&&) = delete;
int v;
};
NonMovable mutable_lvalue(1);
const NonMovable const_lvalue(2);
auto data = etl::make_array<NonMovable>(mutable_lvalue, const_lvalue);
using Type = etl::remove_reference_t<decltype(data[0])>;
CHECK((std::is_same<NonMovable, Type>::value));
CHECK_EQUAL(1, data[0].v);
CHECK_EQUAL(2, data[1].v);
}
#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