mirror of
https://github.com/ETLCPP/etl.git
synced 2026-10-28 18:07:10 +08:00
* 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>
1583 lines
52 KiB
C++
1583 lines
52 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/forward_list.h"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <forward_list>
|
|
#include <functional>
|
|
#include <list>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
#include "etl/private/diagnostic_null_dereference_push.h"
|
|
SUITE(test_forward_list)
|
|
{
|
|
const size_t SIZE = 10UL;
|
|
|
|
typedef TestDataDC<std::string> ItemDC;
|
|
typedef TestDataNDC<std::string> ItemNDC;
|
|
typedef TestDataM<uint32_t> ItemM;
|
|
|
|
typedef etl::forward_list<ItemDC, SIZE> DataDC;
|
|
typedef etl::forward_list<ItemNDC, SIZE> DataNDC;
|
|
typedef etl::iforward_list<ItemNDC> IDataNDC;
|
|
|
|
typedef etl::forward_list<ItemM, SIZE> DataM;
|
|
typedef etl::iforward_list<ItemM> IDataM;
|
|
|
|
typedef etl::forward_list<int, SIZE> DataInt;
|
|
|
|
typedef std::forward_list<ItemDC> CompareDataDC;
|
|
typedef std::forward_list<ItemNDC> CompareDataNDC;
|
|
typedef std::vector<ItemNDC> InitialDataNDC;
|
|
|
|
InitialDataNDC stable_sort_data;
|
|
InitialDataNDC unsorted_data;
|
|
InitialDataNDC sorted_data;
|
|
InitialDataNDC non_unique_data;
|
|
InitialDataNDC small_data;
|
|
|
|
bool are_equal;
|
|
|
|
//*************************************************************************
|
|
struct SetupFixture
|
|
{
|
|
SetupFixture()
|
|
{
|
|
stable_sort_data = {ItemNDC("1", 1), ItemNDC("2", 2), ItemNDC("3", 3), ItemNDC("2", 4), ItemNDC("0", 5),
|
|
ItemNDC("2", 6), ItemNDC("7", 7), ItemNDC("4", 8), ItemNDC("4", 9), ItemNDC("8", 10)};
|
|
unsorted_data = {ItemNDC("1"), ItemNDC("0"), ItemNDC("3"), ItemNDC("2"), ItemNDC("5"),
|
|
ItemNDC("4"), ItemNDC("7"), ItemNDC("6"), ItemNDC("9"), ItemNDC("8")};
|
|
sorted_data = {ItemNDC("0"), ItemNDC("1"), ItemNDC("2"), ItemNDC("3"), ItemNDC("4"),
|
|
ItemNDC("5"), ItemNDC("6"), ItemNDC("7"), ItemNDC("8"), ItemNDC("9")};
|
|
non_unique_data = {ItemNDC("0"), ItemNDC("0"), ItemNDC("1"), ItemNDC("1"), ItemNDC("2"),
|
|
ItemNDC("3"), ItemNDC("3"), ItemNDC("3"), ItemNDC("4"), ItemNDC("5")};
|
|
small_data = {ItemNDC("0"), ItemNDC("1"), ItemNDC("2"), ItemNDC("3"), ItemNDC("4"), ItemNDC("5")};
|
|
}
|
|
};
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_default_constructor)
|
|
{
|
|
DataDC data;
|
|
|
|
CHECK(data.empty());
|
|
CHECK_EQUAL(data.max_size(), SIZE);
|
|
CHECK(data.begin() == data.end());
|
|
}
|
|
|
|
#if ETL_USING_CPP17 && ETL_HAS_INITIALIZER_LIST && !defined(ETL_TEMPLATE_DEDUCTION_GUIDE_TESTS_DISABLED)
|
|
//*************************************************************************
|
|
TEST(test_cpp17_deduced_constructor)
|
|
{
|
|
etl::forward_list data{ItemNDC("1", 1), ItemNDC("2", 2), ItemNDC("3", 3), ItemNDC("2", 4), ItemNDC("0", 5),
|
|
ItemNDC("2", 6), ItemNDC("7", 7), ItemNDC("4", 8), ItemNDC("4", 9), ItemNDC("8", 10)};
|
|
etl::forward_list< ItemNDC, 10> check = {ItemNDC("1", 1), ItemNDC("2", 2), ItemNDC("3", 3), ItemNDC("2", 4), ItemNDC("0", 5),
|
|
ItemNDC("2", 6), ItemNDC("7", 7), ItemNDC("4", 8), ItemNDC("4", 9), ItemNDC("8", 10)};
|
|
|
|
CHECK(!data.empty());
|
|
CHECK(data.full());
|
|
CHECK(data.begin() != data.end());
|
|
CHECK_EQUAL(0U, data.available());
|
|
CHECK_EQUAL(10U, data.capacity());
|
|
CHECK_EQUAL(10U, data.size());
|
|
CHECK_EQUAL(10U, data.max_size());
|
|
CHECK(data == check);
|
|
}
|
|
#endif
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_constructor_size)
|
|
{
|
|
const size_t INITIAL_SIZE = 4UL;
|
|
DataDC data(INITIAL_SIZE);
|
|
|
|
CHECK(!data.empty());
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_constructor_size_excess)
|
|
{
|
|
CHECK_THROW(DataDC data(SIZE + 1), etl::forward_list_full);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_constructor_size_value)
|
|
{
|
|
const size_t INITIAL_SIZE = 4UL;
|
|
const ItemNDC INITIAL_VALUE("1");
|
|
|
|
std::array<ItemNDC, INITIAL_SIZE> compare_data = {ItemNDC("1"), ItemNDC("1"), ItemNDC("1"), ItemNDC("1")};
|
|
|
|
DataNDC data(INITIAL_SIZE, INITIAL_VALUE);
|
|
|
|
CHECK(!data.empty());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_constructor_range)
|
|
{
|
|
CompareDataNDC compare(sorted_data.begin(), sorted_data.end());
|
|
DataNDC data(sorted_data.begin(), sorted_data.end());
|
|
|
|
CHECK(!data.empty());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare.begin());
|
|
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
#if ETL_HAS_INITIALIZER_LIST
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_constructor_initializer_list)
|
|
{
|
|
CompareDataNDC compare = {ItemNDC("0"), ItemNDC("1"), ItemNDC("2"), ItemNDC("3")};
|
|
DataNDC data = {ItemNDC("0"), ItemNDC("1"), ItemNDC("2"), ItemNDC("3")};
|
|
|
|
CHECK(!data.empty());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
#endif
|
|
|
|
//*************************************************************************
|
|
TEST(test_destruct_via_iforward_list)
|
|
{
|
|
int current_count = ItemNDC::get_instance_count();
|
|
|
|
DataNDC* pdata = new DataNDC(sorted_data.begin(), sorted_data.end());
|
|
CHECK_EQUAL(int(current_count + int(sorted_data.size())), ItemNDC::get_instance_count());
|
|
|
|
IDataNDC* pidata = pdata;
|
|
delete pidata;
|
|
CHECK_EQUAL(current_count, ItemNDC::get_instance_count());
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_copy_constructor)
|
|
{
|
|
CompareDataNDC compare_data(sorted_data.begin(), sorted_data.end());
|
|
DataNDC data(sorted_data.begin(), sorted_data.end());
|
|
DataNDC other_data(data);
|
|
|
|
CHECK(std::equal(data.begin(), data.end(), other_data.begin()));
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST(test_move_constructor)
|
|
{
|
|
ItemM p1(1U);
|
|
ItemM p2(2U);
|
|
ItemM p3(3U);
|
|
ItemM p4(4U);
|
|
|
|
DataM data1;
|
|
data1.push_front(std::move(p1));
|
|
data1.push_front(std::move(p2));
|
|
data1.push_front(std::move(p3));
|
|
data1.push_front(std::move(p4));
|
|
|
|
CHECK(!bool(p1));
|
|
CHECK(!bool(p2));
|
|
CHECK(!bool(p3));
|
|
CHECK(!bool(p4));
|
|
|
|
DataM data2(std::move(data1));
|
|
|
|
CHECK_EQUAL(0U, data1.size());
|
|
CHECK_EQUAL(4U, data2.size());
|
|
|
|
DataM::const_iterator itr = data2.begin();
|
|
|
|
CHECK_EQUAL(4U, (*itr++).value);
|
|
CHECK_EQUAL(3U, (*itr++).value);
|
|
CHECK_EQUAL(2U, (*itr++).value);
|
|
CHECK_EQUAL(1U, (*itr++).value);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST(test_move_assignment)
|
|
{
|
|
ItemM p1(1U);
|
|
ItemM p2(2U);
|
|
ItemM p3(3U);
|
|
ItemM p4(4U);
|
|
|
|
DataM data1;
|
|
data1.push_front(std::move(p1));
|
|
data1.push_front(std::move(p2));
|
|
data1.push_front(std::move(p3));
|
|
data1.push_front(std::move(p4));
|
|
|
|
DataM data2;
|
|
data2.push_front(ItemM(5U));
|
|
data2 = std::move(data1);
|
|
|
|
CHECK_EQUAL(0U, data1.size());
|
|
CHECK_EQUAL(4U, data2.size());
|
|
|
|
DataM::const_iterator itr = data2.begin();
|
|
|
|
CHECK_EQUAL(4U, (*itr++).value);
|
|
CHECK_EQUAL(3U, (*itr++).value);
|
|
CHECK_EQUAL(2U, (*itr++).value);
|
|
CHECK_EQUAL(1U, (*itr++).value);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST(test_move_assignment_interface)
|
|
{
|
|
ItemM p1(1U);
|
|
ItemM p2(2U);
|
|
ItemM p3(3U);
|
|
ItemM p4(4U);
|
|
|
|
DataM data1;
|
|
data1.push_front(std::move(p1));
|
|
data1.push_front(std::move(p2));
|
|
data1.push_front(std::move(p3));
|
|
data1.push_front(std::move(p4));
|
|
|
|
DataM data2;
|
|
data2.push_front(ItemM(5U));
|
|
|
|
IDataM& idata1 = data1;
|
|
IDataM& idata2 = data2;
|
|
|
|
idata2 = std::move(idata1);
|
|
|
|
CHECK_EQUAL(0U, data1.size());
|
|
CHECK_EQUAL(4U, data2.size());
|
|
|
|
DataM::const_iterator itr = data2.begin();
|
|
|
|
CHECK_EQUAL(4U, (*itr++).value);
|
|
CHECK_EQUAL(3U, (*itr++).value);
|
|
CHECK_EQUAL(2U, (*itr++).value);
|
|
CHECK_EQUAL(1U, (*itr++).value);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_iterator)
|
|
{
|
|
CompareDataNDC compare_data(sorted_data.begin(), sorted_data.end());
|
|
DataNDC data(compare_data.begin(), compare_data.end());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_const_iterator)
|
|
{
|
|
CompareDataNDC compare_data(sorted_data.begin(), sorted_data.end());
|
|
const DataNDC data(compare_data.begin(), compare_data.end());
|
|
|
|
are_equal = std::equal(data.cbegin(), data.cend(), compare_data.cbegin());
|
|
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_resize_up)
|
|
{
|
|
const size_t INITIAL_SIZE = 4UL;
|
|
const size_t NEW_SIZE = 8UL;
|
|
const ItemDC VALUE("1");
|
|
|
|
DataDC data(INITIAL_SIZE, VALUE);
|
|
data.resize(NEW_SIZE);
|
|
|
|
CompareDataDC compare_data(INITIAL_SIZE, VALUE);
|
|
compare_data.resize(NEW_SIZE);
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_resize_up_value)
|
|
{
|
|
const size_t INITIAL_SIZE = 4UL;
|
|
const size_t NEW_SIZE = 8UL;
|
|
const ItemNDC VALUE("1");
|
|
|
|
DataNDC data(INITIAL_SIZE, VALUE);
|
|
data.resize(NEW_SIZE, VALUE);
|
|
|
|
CompareDataNDC compare_data(INITIAL_SIZE, VALUE);
|
|
compare_data.resize(NEW_SIZE, VALUE);
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_resize_excess)
|
|
{
|
|
const size_t INITIAL_SIZE = 4UL;
|
|
DataDC data(INITIAL_SIZE);
|
|
|
|
CHECK_THROW(data.resize(data.max_size() + 1), etl::forward_list_full);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_resize_down)
|
|
{
|
|
const size_t INITIAL_SIZE = 4UL;
|
|
const size_t NEW_SIZE = 2UL;
|
|
const ItemDC VALUE("1");
|
|
|
|
DataDC data(INITIAL_SIZE, VALUE);
|
|
data.resize(NEW_SIZE);
|
|
|
|
CompareDataDC compare_data(INITIAL_SIZE, VALUE);
|
|
compare_data.resize(NEW_SIZE);
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_resize_down_value)
|
|
{
|
|
const size_t INITIAL_SIZE = 4UL;
|
|
const size_t NEW_SIZE = 2UL;
|
|
const ItemNDC VALUE("1");
|
|
|
|
DataNDC data(INITIAL_SIZE, VALUE);
|
|
data.resize(NEW_SIZE, VALUE);
|
|
|
|
CompareDataNDC compare_data(INITIAL_SIZE, VALUE);
|
|
compare_data.resize(NEW_SIZE, VALUE);
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_resize_zero)
|
|
{
|
|
const size_t INITIAL_SIZE = 4UL;
|
|
const size_t NEW_SIZE = 0UL;
|
|
const ItemDC VALUE("1");
|
|
|
|
DataDC data(INITIAL_SIZE, VALUE);
|
|
data.resize(NEW_SIZE);
|
|
|
|
CompareDataDC compare_data(INITIAL_SIZE, VALUE);
|
|
compare_data.resize(NEW_SIZE);
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_clear)
|
|
{
|
|
DataNDC data(sorted_data.begin(), sorted_data.end());
|
|
data.clear();
|
|
CHECK(data.empty());
|
|
|
|
// Do it again to check that clear() didn't screw up the internals.
|
|
data.assign(sorted_data.begin(), sorted_data.end());
|
|
CHECK_EQUAL(SIZE, data.size());
|
|
data.clear();
|
|
CHECK_EQUAL(0UL, data.size());
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_clear_pod)
|
|
{
|
|
DataInt data(SIZE, 1);
|
|
|
|
data.clear();
|
|
CHECK(data.empty());
|
|
|
|
// Do it again to check that clear() didn't screw up the internals.
|
|
data.resize(SIZE);
|
|
CHECK_EQUAL(SIZE, data.size());
|
|
data.clear();
|
|
CHECK_EQUAL(0UL, data.size());
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_assign_range)
|
|
{
|
|
CompareDataNDC compare_data(sorted_data.begin(), sorted_data.end());
|
|
DataNDC data;
|
|
|
|
// Do it twice. We should only get one copy.
|
|
data.assign(compare_data.begin(), compare_data.end());
|
|
data.assign(compare_data.begin(), compare_data.end());
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_assign_size_value)
|
|
{
|
|
const size_t INITIAL_SIZE = 4UL;
|
|
const ItemNDC VALUE("1");
|
|
|
|
CompareDataNDC compare_data(INITIAL_SIZE, VALUE);
|
|
DataNDC data;
|
|
|
|
// Do it twice. We should only get one copy.
|
|
data.assign(INITIAL_SIZE, VALUE);
|
|
data.assign(INITIAL_SIZE, VALUE);
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_assign_size_value_excess)
|
|
{
|
|
const ItemNDC VALUE("1");
|
|
|
|
DataNDC data;
|
|
|
|
CHECK_THROW(data.assign(data.max_size() + 1, VALUE), etl::forward_list_full);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_insert_after_position_value)
|
|
{
|
|
const size_t INITIAL_SIZE = 4UL;
|
|
const ItemNDC VALUE("1");
|
|
const ItemNDC INSERT_VALUE("2");
|
|
|
|
CompareDataNDC compare_data(INITIAL_SIZE, VALUE);
|
|
DataNDC data(INITIAL_SIZE, VALUE);
|
|
|
|
size_t offset = 2UL;
|
|
|
|
DataNDC::iterator i_data = data.begin();
|
|
std::advance(i_data, offset);
|
|
|
|
CompareDataNDC::iterator i_compare_data = compare_data.begin();
|
|
std::advance(i_compare_data, offset);
|
|
|
|
data.insert_after(i_data, INSERT_VALUE);
|
|
compare_data.insert_after(i_compare_data, INSERT_VALUE);
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
|
|
offset = 0;
|
|
|
|
i_data = data.begin();
|
|
std::advance(i_data, offset);
|
|
|
|
i_compare_data = compare_data.begin();
|
|
std::advance(i_compare_data, offset);
|
|
|
|
data.insert_after(i_data, INSERT_VALUE);
|
|
compare_data.insert_after(i_compare_data, INSERT_VALUE);
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_insert_after_position_size_value)
|
|
{
|
|
const size_t INITIAL_SIZE = 4UL;
|
|
const ItemNDC VALUE("1");
|
|
const ItemNDC INSERT_VALUE("2");
|
|
|
|
CompareDataNDC compare_data(INITIAL_SIZE, VALUE);
|
|
DataNDC data(INITIAL_SIZE, VALUE);
|
|
|
|
size_t offset = 2UL;
|
|
|
|
DataNDC::iterator i_data = data.begin();
|
|
std::advance(i_data, offset);
|
|
|
|
CompareDataNDC::iterator i_compare_data = compare_data.begin();
|
|
std::advance(i_compare_data, offset);
|
|
|
|
data.insert_after(i_data, 2, INSERT_VALUE);
|
|
compare_data.insert_after(i_compare_data, 2, INSERT_VALUE);
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
|
|
offset = 0;
|
|
|
|
i_data = data.begin();
|
|
std::advance(i_data, offset);
|
|
|
|
i_compare_data = compare_data.begin();
|
|
std::advance(i_compare_data, offset);
|
|
|
|
data.insert_after(i_data, 2, INSERT_VALUE);
|
|
compare_data.insert_after(i_compare_data, 2, INSERT_VALUE);
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_insert_after_range)
|
|
{
|
|
std::vector<ItemNDC> test1 = {ItemNDC("0"), ItemNDC("1"), ItemNDC("2"), ItemNDC("3"), ItemNDC("4")};
|
|
std::vector<ItemNDC> test2 = {ItemNDC("5"), ItemNDC("6"), ItemNDC("7"), ItemNDC("8"), ItemNDC("9")};
|
|
|
|
CompareDataNDC compare_data(test1.begin(), test1.end());
|
|
DataNDC data(test1.begin(), test1.end());
|
|
|
|
compare_data.insert_after(compare_data.before_begin(), test2.begin(), test2.end());
|
|
data.insert_after(data.before_begin(), test2.begin(), test2.end());
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
|
|
compare_data.assign(test1.begin(), test1.end());
|
|
data.assign(test1.begin(), test1.end());
|
|
|
|
CompareDataNDC::iterator icd = compare_data.begin();
|
|
DataNDC::iterator id = data.begin();
|
|
|
|
std::advance(icd, 3);
|
|
std::advance(id, 3);
|
|
|
|
compare_data.insert_after(icd, test2.begin(), test2.end());
|
|
data.insert_after(id, test2.begin(), test2.end());
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_push_front)
|
|
{
|
|
CompareDataNDC compare_data;
|
|
DataNDC data;
|
|
|
|
compare_data.push_front(ItemNDC("1"));
|
|
compare_data.push_front(ItemNDC("2"));
|
|
compare_data.push_front(ItemNDC("3"));
|
|
compare_data.push_front(ItemNDC("4"));
|
|
compare_data.push_front(ItemNDC("5"));
|
|
compare_data.push_front(ItemNDC("6"));
|
|
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("1")));
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("2")));
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("3")));
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("4")));
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("5")));
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("6")));
|
|
|
|
CHECK_EQUAL(6U, data.size());
|
|
CHECK_EQUAL(6, std::distance(data.begin(), data.end()));
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_push_front_move)
|
|
{
|
|
ItemM p1(1U);
|
|
ItemM p2(2U);
|
|
ItemM p3(3U);
|
|
ItemM p4(4U);
|
|
|
|
DataM data;
|
|
data.push_front(std::move(p1));
|
|
data.push_front(std::move(p2));
|
|
data.push_front(std::move(p3));
|
|
data.push_front(std::move(p4));
|
|
|
|
CHECK_EQUAL(4U, data.size());
|
|
|
|
CHECK(!bool(p1));
|
|
CHECK(!bool(p2));
|
|
CHECK(!bool(p3));
|
|
CHECK(!bool(p4));
|
|
|
|
DataM::const_iterator itr = data.begin();
|
|
|
|
CHECK_EQUAL(4U, (*itr++).value);
|
|
CHECK_EQUAL(3U, (*itr++).value);
|
|
CHECK_EQUAL(2U, (*itr++).value);
|
|
CHECK_EQUAL(1U, (*itr++).value);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_emplace_front)
|
|
{
|
|
CompareDataNDC compare_data;
|
|
DataNDC data;
|
|
|
|
compare_data.emplace_front("1");
|
|
compare_data.emplace_front("2");
|
|
compare_data.emplace_front("3");
|
|
compare_data.emplace_front("4");
|
|
compare_data.emplace_front("5");
|
|
compare_data.emplace_front("6");
|
|
|
|
CHECK_NO_THROW(data.emplace_front("1"));
|
|
CHECK_NO_THROW(data.emplace_front("2"));
|
|
CHECK_NO_THROW(data.emplace_front("3"));
|
|
CHECK_NO_THROW(data.emplace_front("4"));
|
|
CHECK_NO_THROW(data.emplace_front("5"));
|
|
CHECK_NO_THROW(data.emplace_front("6"));
|
|
|
|
CHECK_EQUAL(6U, data.size());
|
|
CHECK_EQUAL(6, std::distance(data.begin(), data.end()));
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_emplace_front_return)
|
|
{
|
|
DataNDC data;
|
|
|
|
data.emplace_front("24");
|
|
auto& front = data.emplace_front("42");
|
|
CHECK_EQUAL(front, data.front());
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_emplace_after)
|
|
{
|
|
CompareDataNDC compare_data;
|
|
DataNDC data;
|
|
|
|
CompareDataNDC::iterator itc;
|
|
|
|
itc = compare_data.before_begin();
|
|
compare_data.emplace_after(itc, "1");
|
|
compare_data.emplace_after(itc, "2");
|
|
|
|
++itc;
|
|
compare_data.emplace_after(itc, "3");
|
|
compare_data.emplace_after(itc, "4");
|
|
|
|
++itc;
|
|
compare_data.emplace_after(itc, "5");
|
|
compare_data.emplace_after(itc, "6");
|
|
|
|
DataNDC::iterator itd;
|
|
|
|
itd = data.before_begin();
|
|
CHECK_NO_THROW(data.emplace_after(itd, "1"));
|
|
CHECK_NO_THROW(data.emplace_after(itd, "2"));
|
|
|
|
++itd;
|
|
CHECK_NO_THROW(data.emplace_after(itd, "3"));
|
|
CHECK_NO_THROW(data.emplace_after(itd, "4"));
|
|
|
|
++itd;
|
|
CHECK_NO_THROW(data.emplace_after(itd, "5"));
|
|
CHECK_NO_THROW(data.emplace_after(itd, "6"));
|
|
|
|
CHECK_EQUAL(6U, data.size());
|
|
CHECK_EQUAL(6, std::distance(data.begin(), data.end()));
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_front_const)
|
|
{
|
|
const DataNDC data(sorted_data.begin(), sorted_data.end());
|
|
|
|
CHECK_EQUAL(ItemNDC("0"), data.front());
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_front_const_exception)
|
|
{
|
|
const DataNDC data;
|
|
|
|
CHECK(data.empty());
|
|
CHECK_THROW(data.front(), etl::forward_list_empty);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_before_begin_const)
|
|
{
|
|
const DataNDC data(sorted_data.begin(), sorted_data.end());
|
|
|
|
DataNDC::const_iterator itr = data.before_begin();
|
|
++itr;
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), itr);
|
|
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_push_front_excess)
|
|
{
|
|
DataNDC data;
|
|
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("0")));
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("1")));
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("2")));
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("3")));
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("4")));
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("5")));
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("6")));
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("7")));
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("8")));
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("9")));
|
|
|
|
CHECK_THROW(data.push_front(ItemNDC("10")), etl::forward_list_full);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_push_front_pop_front)
|
|
{
|
|
DataNDC data;
|
|
|
|
for (size_t i = 0UL; i < 2UL * data.max_size(); ++i)
|
|
{
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("1")));
|
|
data.pop_front();
|
|
}
|
|
|
|
CHECK(data.empty());
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_pop_front_empty)
|
|
{
|
|
DataNDC data;
|
|
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("0")));
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("1")));
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("2")));
|
|
CHECK_NO_THROW(data.push_front(ItemNDC("3")));
|
|
|
|
data.pop_front();
|
|
data.pop_front();
|
|
data.pop_front();
|
|
data.pop_front();
|
|
|
|
CHECK_THROW(data.pop_front(), etl::forward_list_empty);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_erase_after_single)
|
|
{
|
|
CompareDataNDC compare_data(sorted_data.begin(), sorted_data.end());
|
|
DataNDC data(sorted_data.begin(), sorted_data.end());
|
|
|
|
DataNDC::iterator i_data = data.begin();
|
|
std::advance(i_data, 2);
|
|
|
|
CompareDataNDC::iterator i_compare_data = compare_data.begin();
|
|
std::advance(i_compare_data, 2);
|
|
|
|
i_compare_data = compare_data.erase_after(i_compare_data);
|
|
i_data = data.erase_after(i_data);
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
CHECK(*i_compare_data == *i_data);
|
|
|
|
i_compare_data = compare_data.erase_after(compare_data.begin());
|
|
i_data = data.erase_after(data.begin());
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
|
|
are_equal = *i_data == *i_compare_data;
|
|
CHECK(are_equal);
|
|
|
|
// Move to the last value and erase.
|
|
i_compare_data = compare_data.begin();
|
|
// std::advance(i_compare_data, compare_data.size() - 1);
|
|
i_compare_data = compare_data.erase_after(i_compare_data);
|
|
|
|
i_data = data.begin();
|
|
// std::advance(i_data, data.size() - 1);
|
|
i_data = data.erase_after(i_data);
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
|
|
CHECK(are_equal);
|
|
are_equal = *i_data == *i_compare_data;
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_erase_after_range)
|
|
{
|
|
CompareDataNDC compare_data(sorted_data.begin(), sorted_data.end());
|
|
DataNDC data(sorted_data.begin(), sorted_data.end());
|
|
|
|
DataNDC::iterator i_data_1 = data.begin();
|
|
std::advance(i_data_1, 2);
|
|
|
|
DataNDC::iterator i_data_2 = data.begin();
|
|
std::advance(i_data_2, 4);
|
|
|
|
CompareDataNDC::iterator i_compare_data_1 = compare_data.begin();
|
|
std::advance(i_compare_data_1, 2);
|
|
|
|
CompareDataNDC::iterator i_compare_data_2 = compare_data.begin();
|
|
std::advance(i_compare_data_2, 4);
|
|
|
|
CompareDataNDC::iterator i_compare_result = compare_data.erase_after(i_compare_data_1, i_compare_data_2);
|
|
|
|
DataNDC::iterator i_result = data.erase_after(i_data_1, i_data_2);
|
|
|
|
CHECK_EQUAL(*i_compare_result, *i_result);
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_erase_after_range_end)
|
|
{
|
|
CompareDataNDC compare_data(sorted_data.begin(), sorted_data.end());
|
|
DataNDC data(sorted_data.begin(), sorted_data.end());
|
|
|
|
DataNDC::iterator i_data = data.begin();
|
|
std::advance(i_data, 4);
|
|
|
|
CompareDataNDC::iterator i_compare_data = compare_data.begin();
|
|
std::advance(i_compare_data, 4);
|
|
|
|
compare_data.erase_after(i_compare_data, compare_data.end());
|
|
|
|
DataNDC::iterator i_result = data.erase_after(i_data, data.end());
|
|
|
|
CHECK(i_result == data.end());
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_erase_after_all)
|
|
{
|
|
DataNDC data(sorted_data.begin(), sorted_data.end());
|
|
|
|
data.erase_after(data.before_begin(), data.end());
|
|
|
|
CHECK(data.empty());
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_front)
|
|
{
|
|
CompareDataNDC compare_data(sorted_data.begin(), sorted_data.end());
|
|
DataNDC data(sorted_data.begin(), sorted_data.end());
|
|
|
|
CHECK_EQUAL(compare_data.front(), data.front());
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_front_exception)
|
|
{
|
|
DataNDC data;
|
|
|
|
CHECK(data.empty());
|
|
CHECK_THROW(data.front(), etl::forward_list_empty);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_assignment)
|
|
{
|
|
CompareDataNDC compare_data(sorted_data.begin(), sorted_data.end());
|
|
DataNDC data(sorted_data.begin(), sorted_data.end());
|
|
DataNDC other_data;
|
|
|
|
other_data = data;
|
|
|
|
CHECK_EQUAL(data.size(), other_data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), other_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_assignment_interface)
|
|
{
|
|
DataNDC data1(sorted_data.begin(), sorted_data.end());
|
|
DataNDC data2;
|
|
|
|
IDataNDC& idata1 = data1;
|
|
IDataNDC& idata2 = data2;
|
|
|
|
idata2 = idata1;
|
|
|
|
bool isEqual = std::equal(data1.begin(), data1.end(), data2.begin());
|
|
CHECK(isEqual);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_self_assignment)
|
|
{
|
|
CompareDataNDC compare_data(sorted_data.begin(), sorted_data.end());
|
|
DataNDC data(sorted_data.begin(), sorted_data.end());
|
|
DataNDC other_data(data);
|
|
|
|
#include "etl/private/diagnostic_self_assign_overloaded_push.h"
|
|
other_data = other_data;
|
|
#include "etl/private/diagnostic_pop.h"
|
|
|
|
CHECK_EQUAL(data.size(), other_data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), other_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_unique_empty)
|
|
{
|
|
CompareDataNDC compare_data;
|
|
DataNDC data;
|
|
|
|
compare_data.unique();
|
|
data.unique();
|
|
|
|
if ((compare_data.begin() != compare_data.end()) && (data.begin() != data.end()))
|
|
{
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_unique)
|
|
{
|
|
CompareDataNDC compare_data(non_unique_data.begin(), non_unique_data.end());
|
|
DataNDC data(non_unique_data.begin(), non_unique_data.end());
|
|
|
|
compare_data.unique();
|
|
data.unique();
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_remove)
|
|
{
|
|
CompareDataNDC compare_data(sorted_data.begin(), sorted_data.end());
|
|
DataNDC data(sorted_data.begin(), sorted_data.end());
|
|
|
|
compare_data.remove(ItemNDC("7"));
|
|
data.remove(ItemNDC("7"));
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_remove_if)
|
|
{
|
|
CompareDataNDC compare_data(sorted_data.begin(), sorted_data.end());
|
|
DataNDC data(sorted_data.begin(), sorted_data.end());
|
|
|
|
compare_data.remove_if(std::bind(std::equal_to<ItemNDC>(), std::placeholders::_1, ItemNDC("7")));
|
|
data.remove_if(std::bind(std::equal_to<ItemNDC>(), std::placeholders::_1, ItemNDC("7")));
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_reverse)
|
|
{
|
|
CompareDataNDC compare_data(sorted_data.begin(), sorted_data.end());
|
|
DataNDC data(sorted_data.begin(), sorted_data.end());
|
|
|
|
compare_data.reverse();
|
|
data.reverse();
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
CHECK_EQUAL(data.size(), size_t(std::distance(data.begin(), data.end())));
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_reverse_empty)
|
|
{
|
|
DataNDC data;
|
|
|
|
data.reverse();
|
|
|
|
CHECK_TRUE(data.empty());
|
|
CHECK_FALSE(data.full());
|
|
CHECK_EQUAL(0, data.size());
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_sort)
|
|
{
|
|
CompareDataNDC compare_data(unsorted_data.begin(), unsorted_data.end());
|
|
DataNDC data(unsorted_data.begin(), unsorted_data.end());
|
|
|
|
compare_data.sort();
|
|
data.sort();
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_stable_sort)
|
|
{
|
|
std::list<ItemNDC> compare_data(stable_sort_data.begin(), stable_sort_data.end());
|
|
DataNDC data(stable_sort_data.begin(), stable_sort_data.end());
|
|
|
|
compare_data.sort();
|
|
data.sort();
|
|
|
|
std::list<ItemNDC>::const_iterator citr = compare_data.begin();
|
|
DataNDC::const_iterator ditr = data.begin();
|
|
|
|
while (ditr != data.end())
|
|
{
|
|
CHECK_EQUAL(citr->index, ditr->index);
|
|
|
|
++citr;
|
|
++ditr;
|
|
}
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_sort_empty)
|
|
{
|
|
CompareDataNDC compare_data;
|
|
DataNDC data;
|
|
|
|
compare_data.sort();
|
|
data.sort();
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_move)
|
|
{
|
|
CompareDataNDC compare_data(sorted_data.begin(), sorted_data.end());
|
|
DataNDC data(sorted_data.begin(), sorted_data.end());
|
|
|
|
DataNDC::const_iterator i_from_before;
|
|
DataNDC::const_iterator i_to_before;
|
|
CompareDataNDC::const_iterator i_compare_from_before;
|
|
CompareDataNDC::const_iterator i_compare_to_before;
|
|
|
|
// Move to the beginning.
|
|
i_from_before = data.begin();
|
|
std::advance(i_from_before, 5);
|
|
|
|
i_to_before = data.before_begin();
|
|
|
|
i_compare_from_before = compare_data.begin();
|
|
std::advance(i_compare_from_before, 5);
|
|
|
|
i_compare_to_before = compare_data.before_begin();
|
|
|
|
compare_data.splice_after(i_compare_to_before, compare_data, i_compare_from_before);
|
|
data.move_after(i_from_before, i_to_before);
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
|
|
// Move to the end.
|
|
i_from_before = data.begin();
|
|
std::advance(i_from_before, 5);
|
|
|
|
i_to_before = data.begin();
|
|
std::advance(i_to_before, 9);
|
|
|
|
i_compare_from_before = compare_data.begin();
|
|
std::advance(i_compare_from_before, 5);
|
|
|
|
i_compare_to_before = compare_data.begin();
|
|
std::advance(i_compare_to_before, 9);
|
|
|
|
compare_data.splice_after(i_compare_to_before, compare_data, i_compare_from_before);
|
|
data.move_after(i_from_before, i_to_before);
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
|
|
// Move to nearby.
|
|
i_from_before = data.begin();
|
|
std::advance(i_from_before, 3);
|
|
|
|
i_to_before = data.begin();
|
|
std::advance(i_to_before, 7);
|
|
|
|
i_compare_from_before = compare_data.begin();
|
|
std::advance(i_compare_from_before, 3);
|
|
|
|
i_compare_to_before = compare_data.begin();
|
|
std::advance(i_compare_to_before, 7);
|
|
|
|
compare_data.splice_after(i_compare_to_before, compare_data, i_compare_from_before);
|
|
data.move_after(i_from_before, i_to_before);
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
|
|
// Move to same.
|
|
i_from_before = data.begin();
|
|
std::advance(i_from_before, 3);
|
|
|
|
i_to_before = data.begin();
|
|
std::advance(i_to_before, 3);
|
|
|
|
i_compare_from_before = compare_data.begin();
|
|
std::advance(i_compare_from_before, 3);
|
|
|
|
i_compare_to_before = compare_data.begin();
|
|
std::advance(i_compare_to_before, 3);
|
|
|
|
compare_data.splice_after(i_compare_to_before, compare_data, i_compare_from_before);
|
|
data.move_after(i_from_before, i_to_before);
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_move_range)
|
|
{
|
|
CompareDataNDC compare_data(sorted_data.begin(), sorted_data.end());
|
|
DataNDC data(sorted_data.begin(), sorted_data.end());
|
|
|
|
DataNDC::const_iterator i_first_before;
|
|
DataNDC::const_iterator i_last;
|
|
DataNDC::const_iterator i_to_before;
|
|
CompareDataNDC::const_iterator i_compare_first_before;
|
|
CompareDataNDC::const_iterator i_compare_last;
|
|
CompareDataNDC::const_iterator i_compare_to_before;
|
|
|
|
// Move to the beginning.
|
|
i_first_before = data.begin();
|
|
std::advance(i_first_before, 4);
|
|
|
|
i_last = i_first_before;
|
|
std::advance(i_last, 4);
|
|
|
|
i_to_before = data.before_begin();
|
|
|
|
i_compare_first_before = compare_data.begin();
|
|
std::advance(i_compare_first_before, 4);
|
|
|
|
i_compare_last = i_compare_first_before;
|
|
std::advance(i_compare_last, 4);
|
|
|
|
i_compare_to_before = compare_data.before_begin();
|
|
|
|
compare_data.splice_after(i_compare_to_before, compare_data, i_compare_first_before, i_compare_last);
|
|
data.move_after(i_first_before, i_last, i_to_before);
|
|
|
|
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data.size());
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
|
|
// Move to the end.
|
|
i_first_before = data.begin();
|
|
std::advance(i_first_before, 4);
|
|
|
|
i_last = i_first_before;
|
|
std::advance(i_last, 4);
|
|
|
|
i_to_before = data.begin();
|
|
std::advance(i_to_before, 9);
|
|
|
|
i_compare_first_before = compare_data.begin();
|
|
std::advance(i_compare_first_before, 4);
|
|
|
|
i_compare_last = i_compare_first_before;
|
|
std::advance(i_compare_last, 4);
|
|
|
|
i_compare_to_before = compare_data.begin();
|
|
std::advance(i_compare_to_before, 9);
|
|
|
|
compare_data.splice_after(i_compare_to_before, compare_data, i_compare_first_before, i_compare_last);
|
|
data.move_after(i_first_before, i_last, i_to_before);
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
|
|
// Move to nearby.
|
|
i_first_before = data.begin();
|
|
std::advance(i_first_before, 4);
|
|
|
|
i_last = i_first_before;
|
|
std::advance(i_last, 4);
|
|
|
|
i_to_before = data.begin();
|
|
std::advance(i_to_before, 2);
|
|
|
|
i_compare_first_before = compare_data.begin();
|
|
std::advance(i_compare_first_before, 4);
|
|
|
|
i_compare_last = i_compare_first_before;
|
|
std::advance(i_compare_last, 4);
|
|
|
|
i_compare_to_before = compare_data.begin();
|
|
std::advance(i_compare_to_before, 2);
|
|
|
|
compare_data.splice_after(i_compare_to_before, compare_data, i_compare_first_before, i_compare_last);
|
|
data.move_after(i_first_before, i_last, i_to_before);
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
|
|
// Move to same.
|
|
i_first_before = data.begin();
|
|
std::advance(i_first_before, 4);
|
|
|
|
i_last = i_first_before;
|
|
std::advance(i_last, 4);
|
|
|
|
i_to_before = data.begin();
|
|
std::advance(i_to_before, 4);
|
|
|
|
i_compare_first_before = compare_data.begin();
|
|
std::advance(i_compare_first_before, 4);
|
|
|
|
i_compare_last = i_compare_first_before;
|
|
std::advance(i_compare_last, 4);
|
|
|
|
i_compare_to_before = compare_data.begin();
|
|
std::advance(i_compare_to_before, 4);
|
|
|
|
compare_data.splice_after(i_compare_to_before, compare_data, i_compare_first_before, i_compare_last);
|
|
data.move_after(i_first_before, i_last, i_to_before);
|
|
|
|
are_equal = std::equal(data.begin(), data.end(), compare_data.begin());
|
|
CHECK(are_equal);
|
|
|
|
// Move to illegal place.
|
|
i_first_before = data.begin();
|
|
std::advance(i_first_before, 2);
|
|
i_last = i_first_before;
|
|
std::advance(i_last, 3);
|
|
i_to_before = i_first_before;
|
|
std::advance(i_to_before, 2);
|
|
|
|
CHECK_THROW(data.move_after(i_first_before, i_last, i_to_before), etl::forward_list_iterator);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST_FIXTURE(SetupFixture, test_binary_comparisons)
|
|
{
|
|
DataNDC data1(sorted_data.begin(), sorted_data.end());
|
|
DataNDC data2(sorted_data.begin(), sorted_data.end());
|
|
DataNDC data3(unsorted_data.begin(), unsorted_data.end());
|
|
|
|
CHECK(data1 == data2);
|
|
CHECK(data1 != data3);
|
|
CHECK(data1 < data3);
|
|
CHECK(data3 > data1);
|
|
}
|
|
|
|
//*************************************************************************
|
|
TEST(test_two_parameter_same_type_non_iterator)
|
|
{
|
|
// No compile error.
|
|
etl::forward_list<int, 10> fl(10, 1);
|
|
CHECK(fl.size() == 10);
|
|
fl.assign(5, 2);
|
|
CHECK(fl.size() == 5);
|
|
fl.insert_after(fl.before_begin(), 5, 3);
|
|
CHECK(fl.size() == fl.max_size());
|
|
}
|
|
|
|
//*************************************************************************
|
|
#if ETL_USING_CPP17 && ETL_HAS_INITIALIZER_LIST && !defined(ETL_TEMPLATE_DEDUCTION_GUIDE_TESTS_DISABLED)
|
|
TEST(test_forward_list_template_deduction)
|
|
{
|
|
etl::forward_list data{ItemNDC("A"), ItemNDC("B"), ItemNDC("C"), ItemNDC("D"), ItemNDC("E"), ItemNDC("F")};
|
|
|
|
auto v = *data.begin();
|
|
using Type = decltype(v);
|
|
CHECK((std::is_same_v<ItemNDC, Type>));
|
|
|
|
decltype(data)::const_iterator itr = data.begin();
|
|
|
|
CHECK_EQUAL(ItemNDC("A"), *itr++);
|
|
CHECK_EQUAL(ItemNDC("B"), *itr++);
|
|
CHECK_EQUAL(ItemNDC("C"), *itr++);
|
|
CHECK_EQUAL(ItemNDC("D"), *itr++);
|
|
CHECK_EQUAL(ItemNDC("E"), *itr++);
|
|
CHECK_EQUAL(ItemNDC("F"), *itr++);
|
|
}
|
|
#endif
|
|
|
|
//*************************************************************************
|
|
#if ETL_HAS_INITIALIZER_LIST
|
|
TEST(test_make_forward_list)
|
|
{
|
|
auto data = etl::make_forward_list<ItemNDC>(ItemNDC("A"), ItemNDC("B"), ItemNDC("C"), ItemNDC("D"), ItemNDC("E"), ItemNDC("F"));
|
|
|
|
auto v = *data.begin();
|
|
using Type = decltype(v);
|
|
CHECK((std::is_same<ItemNDC, Type>::value));
|
|
|
|
decltype(data)::const_iterator itr = data.begin();
|
|
|
|
CHECK_EQUAL(ItemNDC("A"), *itr++);
|
|
CHECK_EQUAL(ItemNDC("B"), *itr++);
|
|
CHECK_EQUAL(ItemNDC("C"), *itr++);
|
|
CHECK_EQUAL(ItemNDC("D"), *itr++);
|
|
CHECK_EQUAL(ItemNDC("E"), *itr++);
|
|
CHECK_EQUAL(ItemNDC("F"), *itr++);
|
|
}
|
|
#endif
|
|
|
|
//*************************************************************************
|
|
#if ETL_HAS_INITIALIZER_LIST
|
|
// Arguments that are const-qualified lvalues must be accepted and must not
|
|
// affect the deduced element type. Guards against the defect fixed for
|
|
// make_array and make_deque, which forwarded each argument as the element
|
|
// type rather than as its own deduced type and so rejected const lvalues.
|
|
TEST(test_make_forward_list_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_forward_list(static_const_lvalue, local_const_lvalue, mutable_lvalue);
|
|
|
|
auto v = *data.begin();
|
|
using Type = decltype(v);
|
|
CHECK((std::is_same<char, Type>::value));
|
|
|
|
decltype(data)::const_iterator itr = data.begin();
|
|
|
|
CHECK_EQUAL(42, *itr++);
|
|
CHECK_EQUAL(43, *itr++);
|
|
CHECK_EQUAL(44, *itr++);
|
|
}
|
|
#endif
|
|
|
|
//*************************************************************************
|
|
#if ETL_HAS_INITIALIZER_LIST
|
|
TEST(test_make_forward_list_common_type_from_mixed_types)
|
|
{
|
|
auto data = etl::make_forward_list(1, 2L, 3);
|
|
|
|
CHECK((std::is_same<etl::forward_list<long, 3U>, decltype(data)>::value));
|
|
|
|
auto itr = data.begin();
|
|
CHECK_EQUAL(1L, *itr++);
|
|
CHECK_EQUAL(2L, *itr++);
|
|
CHECK_EQUAL(3L, *itr++);
|
|
}
|
|
#endif
|
|
|
|
//*************************************************************************
|
|
#if ETL_HAS_INITIALIZER_LIST
|
|
struct CopyMoveCounted
|
|
{
|
|
explicit CopyMoveCounted(int value_)
|
|
: value(value_)
|
|
{
|
|
}
|
|
|
|
CopyMoveCounted(const CopyMoveCounted& other)
|
|
: value(other.value)
|
|
{
|
|
++copy_count;
|
|
}
|
|
|
|
CopyMoveCounted(CopyMoveCounted&& other)
|
|
: value(other.value)
|
|
{
|
|
++move_count;
|
|
}
|
|
|
|
int value;
|
|
|
|
static size_t copy_count;
|
|
static size_t move_count;
|
|
};
|
|
|
|
size_t CopyMoveCounted::copy_count = 0;
|
|
size_t CopyMoveCounted::move_count = 0;
|
|
|
|
// The arguments must be forwarded into the initializer list, not taken by
|
|
// value: a by-value parameter pack would cost an extra move per argument
|
|
// (the defect fixed for make_list). Each element is then copied from the
|
|
// initializer_list backing array into its node, which is why lvalue
|
|
// arguments count two copies.
|
|
TEST(test_make_forward_list_argument_copy_and_move_counts)
|
|
{
|
|
const CopyMoveCounted const_lvalue(1);
|
|
CopyMoveCounted mutable_lvalue(2);
|
|
|
|
CopyMoveCounted::copy_count = 0;
|
|
CopyMoveCounted::move_count = 0;
|
|
|
|
auto data = etl::make_forward_list(const_lvalue, mutable_lvalue);
|
|
|
|
CHECK_EQUAL(4U, CopyMoveCounted::copy_count);
|
|
#if ETL_USING_CPP17
|
|
// The exact move counts rely on guaranteed copy elision. Before C++17 the
|
|
// returned forward_list may be move-constructed once more, which moves each
|
|
// element, so only the copy counts are checked there.
|
|
CHECK_EQUAL(0U, CopyMoveCounted::move_count);
|
|
#endif
|
|
|
|
CopyMoveCounted::copy_count = 0;
|
|
CopyMoveCounted::move_count = 0;
|
|
|
|
auto data2 = etl::make_forward_list(CopyMoveCounted(3));
|
|
|
|
CHECK_EQUAL(1U, CopyMoveCounted::copy_count);
|
|
#if ETL_USING_CPP17
|
|
CHECK_EQUAL(1U, CopyMoveCounted::move_count);
|
|
#endif
|
|
|
|
auto itr = data.begin();
|
|
CHECK_EQUAL(1, (*itr++).value);
|
|
CHECK_EQUAL(2, (*itr).value);
|
|
CHECK_EQUAL(3, data2.begin()->value);
|
|
}
|
|
#endif
|
|
|
|
//*************************************************************************
|
|
TEST(test_forward_list_exception_types)
|
|
{
|
|
// Verify exception class hierarchy is correct
|
|
CHECK(true == (std::is_base_of<etl::exception, etl::forward_list_exception>::value));
|
|
CHECK(true == (std::is_base_of<etl::forward_list_exception, etl::forward_list_full>::value));
|
|
CHECK(true == (std::is_base_of<etl::forward_list_exception, etl::forward_list_empty>::value));
|
|
CHECK(true == (std::is_base_of<etl::forward_list_exception, etl::forward_list_iterator>::value));
|
|
|
|
#if defined(ETL_VERBOSE_ERRORS)
|
|
// When verbose errors are enabled, check the error text contains "forward_list"
|
|
etl::forward_list_full ex_full(__FILE__, __LINE__);
|
|
etl::forward_list_empty ex_empty(__FILE__, __LINE__);
|
|
|
|
std::string full_msg(ex_full.what());
|
|
std::string empty_msg(ex_empty.what());
|
|
|
|
CHECK(full_msg.find("forward_list") != std::string::npos);
|
|
CHECK(empty_msg.find("forward_list") != std::string::npos);
|
|
#endif
|
|
}
|
|
}
|
|
#include "etl/private/diagnostic_pop.h"
|
|
} // namespace
|