/****************************************************************************** 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 #include #include #include #include "etl/iterator.h" #include "iterators_for_unit_tests.h" #include "data.h" namespace { using Item = TestDataM; struct input : public etl::iterator { }; struct output : public etl::iterator { }; struct forward : public etl::iterator { }; struct bidirectional : public etl::iterator { }; struct random : public etl::iterator { }; typedef int* pointer; typedef const int* const_pointer; const size_t SIZE = 10UL; int dataA[SIZE] = { 2, 1, 4, 3, 6, 5, 8, 7, 10, 9 }; SUITE(test_iterator) { // NOTE '!!' is required to keep GCC happy. //************************************************************************* TEST(distance_non_random) { ptrdiff_t d = etl::distance(non_random_iterator(&dataA[0]), non_random_iterator(dataA + SIZE)); CHECK_EQUAL(SIZE, d); } //************************************************************************* TEST(distance_random) { ptrdiff_t d = etl::distance(random_iterator(&dataA[0]), random_iterator(dataA + SIZE)); CHECK_EQUAL(SIZE, d); } //************************************************************************* TEST(advance_non_random) { int* itr1 = std::begin(dataA); non_random_iterator itr2 = std::begin(dataA); std::advance(itr1, 4); etl::advance(itr2, 4); CHECK_EQUAL(*itr1, *itr2); std::advance(itr1, -3); etl::advance(itr2, -3); CHECK_EQUAL(*itr1, *itr2); } //************************************************************************* TEST(advance_random) { int* itr1 = std::begin(dataA); random_iterator itr2 = std::begin(dataA); std::advance(itr1, 4); etl::advance(itr2, 4); CHECK_EQUAL(*itr1, *itr2); std::advance(itr1, -3); etl::advance(itr2, -3); CHECK_EQUAL(*itr1, *itr2); } //************************************************************************* TEST(prev) { int data[] = { 1, 2, 3, 4, 5, 6, 7 }; size_t length = 6UL; int* itr = &data[0] + length; for (size_t i = 1UL; i <= length; ++i) { CHECK_EQUAL(data[length - i], *etl::prev(itr, i)); } } //************************************************************************* TEST(next) { int data[] = { 1, 2, 3, 4, 5, 6, 7 }; size_t length = 6UL; int* itr = &data[0]; for (size_t i = 1UL; i <= length; ++i) { CHECK_EQUAL(data[i], *etl::next(itr, i)); } } //************************************************************************* TEST(reverse_iterator) { int data[] = { 1, 2, 3, 4, 5, 6, 7 }; std::reverse_iterator sri(&data[7]); etl::reverse_iterator eri(&data[7]); CHECK_EQUAL(*sri++, *eri++); CHECK_EQUAL(*sri++, *eri++); CHECK_EQUAL(*sri++, *eri++); CHECK(etl::reverse_iterator(&data[5]) < eri); CHECK(etl::reverse_iterator(&data[3]) > eri); CHECK(etl::reverse_iterator(&data[4]) <= eri); CHECK(etl::reverse_iterator(&data[5]) <= eri); CHECK(etl::reverse_iterator(&data[4]) >= eri); CHECK(etl::reverse_iterator(&data[3]) >= eri); CHECK_EQUAL(*sri++, *eri++); CHECK_EQUAL(*sri++, *eri++); CHECK_EQUAL(*sri++, *eri++); CHECK_EQUAL(*sri, *eri); CHECK(etl::reverse_iterator(&data[1]) == eri); CHECK(etl::reverse_iterator(&data[2]) != eri); CHECK(etl::reverse_iterator(&data[3]) != eri); CHECK(etl::reverse_iterator(&data[4]) != eri); CHECK(etl::reverse_iterator(&data[5]) != eri); CHECK(etl::reverse_iterator(&data[6]) != eri); CHECK(etl::reverse_iterator(&data[7]) != eri); sri = std::reverse_iterator(&data[7]); eri = etl::reverse_iterator(&data[7]); CHECK_EQUAL(sri[0], eri[0]); CHECK_EQUAL(sri[1], eri[1]); CHECK_EQUAL(sri[2], eri[2]); CHECK_EQUAL(sri[3], eri[3]); CHECK_EQUAL(sri[4], eri[4]); CHECK_EQUAL(sri[5], eri[5]); CHECK_EQUAL(sri[6], eri[6]); sri += 4; eri += 4; CHECK_EQUAL(*sri, *eri); sri -= 2; eri -= 2; CHECK_EQUAL(*sri, *eri); std::reverse_iterator sri2 = sri + 3; etl::reverse_iterator eri2 = eri + 3; CHECK_EQUAL(*sri, *eri); sri2 = sri - 3; eri2 = eri - 3; CHECK_EQUAL(*sri, *eri); } //************************************************************************* TEST(reverse_iterator_const) { const int data[] = { 1, 2, 3, 4, 5, 6, 7 }; std::reverse_iterator sri(&data[7]); etl::reverse_iterator eri(&data[7]); CHECK_EQUAL(*sri++, *eri++); CHECK_EQUAL(*sri++, *eri++); CHECK_EQUAL(*sri++, *eri++); CHECK(etl::reverse_iterator(&data[5]) < eri); CHECK(etl::reverse_iterator(&data[3]) > eri); CHECK(etl::reverse_iterator(&data[4]) <= eri); CHECK(etl::reverse_iterator(&data[5]) <= eri); CHECK(etl::reverse_iterator(&data[4]) >= eri); CHECK(etl::reverse_iterator(&data[3]) >= eri); CHECK_EQUAL(*sri++, *eri++); CHECK_EQUAL(*sri++, *eri++); CHECK_EQUAL(*sri++, *eri++); CHECK_EQUAL(*sri, *eri); CHECK(etl::reverse_iterator(&data[1]) == eri); CHECK(etl::reverse_iterator(&data[2]) != eri); CHECK(etl::reverse_iterator(&data[3]) != eri); CHECK(etl::reverse_iterator(&data[4]) != eri); CHECK(etl::reverse_iterator(&data[5]) != eri); CHECK(etl::reverse_iterator(&data[6]) != eri); CHECK(etl::reverse_iterator(&data[7]) != eri); sri = std::reverse_iterator(&data[7]); eri = etl::reverse_iterator(&data[7]); CHECK_EQUAL(sri[0], eri[0]); CHECK_EQUAL(sri[1], eri[1]); CHECK_EQUAL(sri[2], eri[2]); CHECK_EQUAL(sri[3], eri[3]); CHECK_EQUAL(sri[4], eri[4]); CHECK_EQUAL(sri[5], eri[5]); CHECK_EQUAL(sri[6], eri[6]); CHECK_EQUAL(*sri, *eri); CHECK_EQUAL(*(sri + 1), *(eri + 1)); CHECK_EQUAL(*(sri + 2), *(eri + 2)); CHECK_EQUAL(*(sri + 3), *(eri + 3)); CHECK_EQUAL(*(sri + 4), *(eri + 4)); CHECK_EQUAL(*(sri + 5), *(eri + 5)); CHECK_EQUAL(*(sri + 6), *(eri + 6)); sri += 4; eri += 4; CHECK_EQUAL(*sri, *eri); sri -= 2; eri -= 2; CHECK_EQUAL(*sri, *eri); std::reverse_iterator sri2 = sri + 3; etl::reverse_iterator eri2 = eri + 3; CHECK_EQUAL(*sri, *eri); sri2 = sri - 3; eri2 = eri - 3; CHECK_EQUAL(*sri, *eri); } //************************************************************************* TEST(test_input) { CHECK(!!etl::is_input_iterator::value); CHECK(!etl::is_output_iterator::value); CHECK(!etl::is_forward_iterator::value); CHECK(!etl::is_bidirectional_iterator::value); CHECK(!etl::is_random_iterator::value); CHECK(!!etl::is_input_iterator_concept::value); CHECK(!etl::is_output_iterator_concept::value); CHECK(!etl::is_forward_iterator_concept::value); CHECK(!etl::is_bidirectional_iterator_concept::value); CHECK(!etl::is_random_iterator_concept::value); } //************************************************************************* TEST(test_output) { CHECK(!etl::is_input_iterator::value); CHECK(!!etl::is_output_iterator::value); CHECK(!etl::is_forward_iterator::value); CHECK(!etl::is_bidirectional_iterator::value); CHECK(!etl::is_random_iterator::value); CHECK(!etl::is_input_iterator_concept::value); CHECK(etl::is_output_iterator_concept::value == true); CHECK(!etl::is_forward_iterator_concept::value); CHECK(!etl::is_bidirectional_iterator_concept::value); CHECK(!etl::is_random_iterator_concept::value); } //************************************************************************* TEST(test_forward) { CHECK(!etl::is_input_iterator::value); CHECK(!etl::is_output_iterator::value); CHECK(!!etl::is_forward_iterator::value); CHECK(!etl::is_bidirectional_iterator::value); CHECK(!etl::is_random_iterator::value); CHECK(!!etl::is_input_iterator_concept::value); CHECK(!!etl::is_output_iterator_concept::value); CHECK(!!etl::is_forward_iterator_concept::value); CHECK(!etl::is_bidirectional_iterator_concept::value); CHECK(!etl::is_random_iterator_concept::value); } //************************************************************************* TEST(test_bidirectional) { CHECK(!etl::is_input_iterator::value); CHECK(!etl::is_output_iterator::value); CHECK(!etl::is_forward_iterator::value); CHECK(!!etl::is_bidirectional_iterator::value); CHECK(!etl::is_random_iterator::value); CHECK(!!etl::is_input_iterator_concept::value); CHECK(!!etl::is_output_iterator_concept::value); CHECK(!!etl::is_forward_iterator_concept::value); CHECK(!!etl::is_bidirectional_iterator_concept::value); CHECK(!etl::is_random_iterator_concept::value); } //************************************************************************* TEST(test_random) { CHECK(!etl::is_input_iterator::value); CHECK(!etl::is_output_iterator::value); CHECK(!etl::is_forward_iterator::value); CHECK(!etl::is_bidirectional_iterator::value); CHECK(!!etl::is_random_iterator::value); CHECK(!!etl::is_input_iterator_concept::value); CHECK(!!etl::is_output_iterator_concept::value); CHECK(!!etl::is_forward_iterator_concept::value); CHECK(!!etl::is_bidirectional_iterator_concept::value); CHECK(!!etl::is_random_iterator_concept::value); } //************************************************************************* TEST(test_pointer) { CHECK(!etl::is_input_iterator::value); CHECK(!etl::is_output_iterator::value); CHECK(!etl::is_forward_iterator::value); CHECK(!etl::is_bidirectional_iterator::value); CHECK(!!etl::is_random_iterator::value); CHECK(!!etl::is_input_iterator_concept::value); CHECK(!!etl::is_output_iterator_concept::value); CHECK(!!etl::is_forward_iterator_concept::value); CHECK(!!etl::is_bidirectional_iterator_concept::value); CHECK(!!etl::is_random_iterator_concept::value); } //************************************************************************* TEST(test_const_pointer) { CHECK(!etl::is_input_iterator::value); CHECK(!etl::is_output_iterator::value); CHECK(!etl::is_forward_iterator::value); CHECK(!etl::is_bidirectional_iterator::value); CHECK(!!etl::is_random_iterator::value); CHECK(!!etl::is_input_iterator_concept::value); CHECK(!!etl::is_output_iterator_concept::value); CHECK(!!etl::is_forward_iterator_concept::value); CHECK(!!etl::is_bidirectional_iterator_concept::value); CHECK(!!etl::is_random_iterator_concept::value); } //************************************************************************* TEST(test_move_iterator_constructors) { Item list[] = { Item("1"), Item("2"), Item("3") }; etl::move_iterator mitr1(&list[0]); etl::move_iterator mitr2(&list[1]); etl::move_iterator mitr3(&list[1]); etl::move_iterator mitr4 = etl::make_move_iterator(&list[2]); etl::move_iterator mitr5(mitr4); CHECK(mitr1.base() == &list[0]); CHECK(mitr2.base() == &list[1]); CHECK(mitr3.base() == &list[1]); CHECK(mitr4.base() == &list[2]); CHECK(mitr5.base() == &list[2]); } //************************************************************************* TEST(test_move_iterator_relational_operators) { Item list[] = { Item("1"), Item("2"), Item("3") }; etl::move_iterator mitr1(&list[0]); etl::move_iterator mitr2(&list[1]); etl::move_iterator mitr3(&list[1]); etl::move_iterator mitr4 = etl::make_move_iterator(&list[2]); etl::move_iterator mitr5(mitr4); CHECK(mitr1 < mitr2); CHECK(!(mitr2 < mitr1)); CHECK(!(mitr2 < mitr3)); CHECK(mitr1 <= mitr2); CHECK(mitr2 <= mitr3); CHECK(!(mitr2 <= mitr1)); CHECK(mitr2 > mitr1); CHECK(!(mitr1 > mitr2)); CHECK(!(mitr3 > mitr2)); CHECK(mitr2 >= mitr1); CHECK(mitr3 >= mitr2); CHECK(!(mitr1 >= mitr2)); CHECK(mitr4 == mitr5); CHECK(mitr3 != mitr5); } //************************************************************************* TEST(test_move_iterator_access_operators) { Item item1("1"); etl::move_iterator mitr(&item1); CHECK_EQUAL("1", mitr->value); CHECK_EQUAL(true, mitr->valid); CHECK_EQUAL("1", (*mitr).value); CHECK_EQUAL(true, (*mitr).valid); Item item2 = *mitr; // Move item1 CHECK_EQUAL(false, item1.valid); CHECK_EQUAL("1", item2.value); CHECK_EQUAL(true, item2.valid); } //************************************************************************* TEST(test_move_iterator_index) { Item list[] = { Item("1"), Item("2"), Item("3") }; etl::move_iterator mitr(&list[0]); CHECK_EQUAL("3", mitr[2].value); CHECK_EQUAL(true, mitr[2].valid); } //************************************************************************* TEST(test_move_iterator_increment_decrement) { Item list[] = { Item("1"), Item("2"), Item("3") }; etl::move_iterator mitr(&list[0]); mitr++; ++mitr; CHECK_EQUAL("3", mitr->value); CHECK_EQUAL(true, mitr->valid); mitr--; --mitr; CHECK_EQUAL("1", mitr->value); CHECK_EQUAL(true, mitr->valid); mitr += 1; CHECK_EQUAL("2", mitr->value); CHECK_EQUAL(true, mitr->valid); mitr -= 1; CHECK_EQUAL("1", mitr->value); CHECK_EQUAL(true, mitr->valid); mitr = mitr + 1; CHECK_EQUAL("2", mitr->value); CHECK_EQUAL(true, mitr->valid); mitr = mitr - 1; CHECK_EQUAL("1", mitr->value); CHECK_EQUAL(true, mitr->valid); } //************************************************************************* TEST(test_move_iterator_subtraction) { Item list[] = { Item("1"), Item("2"), Item("3") }; etl::move_iterator mitr1(&list[0]); etl::move_iterator mitr2(&list[1]); etl::move_iterator::difference_type d = mitr2 - mitr1; CHECK_EQUAL(1, d); } //************************************************************************* TEST(test_front_insert_iterator) { std::list input = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; std::list expected = { 81, 64, 49, 36, 25, 16, 9, 4, 1, 0 }; std::list output; auto squared = [](int value) { return value * value; }; std::transform(input.cbegin(), input.cend(), etl::front_inserter(output), squared); CHECK_EQUAL(expected.size(), output.size()); CHECK(std::equal(output.begin(), output.end(), expected.begin())); } //************************************************************************* TEST(test_back_insert_iterator) { std::list input = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; std::list expected = { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 }; std::list output; auto squared = [](int value) { return value * value; }; std::transform(input.cbegin(), input.cend(), etl::back_inserter(output), squared); CHECK_EQUAL(expected.size(), output.size()); CHECK(std::equal(output.begin(), output.end(), expected.begin())); } //************************************************************************* //TEST(test_push_insert_iterator) //{ // std::list input = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // std::list expected = { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 }; // std::queue output; // auto squared = [](int value) // { // return value * value; // }; // std::transform(input.begin(), input.cend(), etl::push_inserter(output), squared); // CHECK_EQUAL(expected.size(), output.size()); // CHECK(std::equal(output.begin(), output.end(), expected.begin())); //} }; }