From d9589351e7193b709dbd675106e2a30a6547e22c Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Tue, 3 Jul 2018 22:13:11 +0100 Subject: [PATCH] Added specialisation for vector --- include/etl/private/ivectorpointer.h | 402 ++++++++++++ include/etl/version.h | 6 +- support/Release notes.txt | 4 + test/test_vector_pointer.cpp | 908 +++++++++++++++++++++++++-- 4 files changed, 1278 insertions(+), 42 deletions(-) diff --git a/include/etl/private/ivectorpointer.h b/include/etl/private/ivectorpointer.h index 6dcda8f39..9ef269777 100644 --- a/include/etl/private/ivectorpointer.h +++ b/include/etl/private/ivectorpointer.h @@ -446,6 +446,408 @@ namespace etl } }; + template + class ivector : public pvoidvector + { + public: + + typedef const T* value_type; + typedef const T*& reference; + typedef const T* const & const_reference; + typedef const T** pointer; + typedef const T* const * const_pointer; + typedef const T** iterator; + typedef const T* const * const_iterator; + typedef std::reverse_iterator reverse_iterator; + typedef std::reverse_iterator const_reverse_iterator; + typedef size_t size_type; + typedef typename std::iterator_traits::difference_type difference_type; + + protected: + + typedef value_type parameter_t; + + private: + + typedef pvoidvector base_t; + + public: + + //********************************************************************* + /// Returns an iterator to the beginning of the vector. + ///\return An iterator to the beginning of the vector. + //********************************************************************* + iterator begin() + { + return iterator(base_t::begin()); + } + + //********************************************************************* + /// Returns a const_iterator to the beginning of the vector. + ///\return A const iterator to the beginning of the vector. + //********************************************************************* + const_iterator begin() const + { + return const_iterator(base_t::begin()); + } + + //********************************************************************* + /// Returns an iterator to the end of the vector. + ///\return An iterator to the end of the vector. + //********************************************************************* + iterator end() + { + return iterator(base_t::end()); + } + + //********************************************************************* + /// Returns a const_iterator to the end of the vector. + ///\return A const iterator to the end of the vector. + //********************************************************************* + const_iterator end() const + { + return const_iterator(base_t::end()); + } + + //********************************************************************* + /// Returns a const_iterator to the beginning of the vector. + ///\return A const iterator to the beginning of the vector. + //********************************************************************* + const_iterator cbegin() const + { + return const_iterator(base_t::cbegin()); + } + + //********************************************************************* + /// Returns a const_iterator to the end of the vector. + ///\return A const iterator to the end of the vector. + //********************************************************************* + const_iterator cend() const + { + return const_iterator(base_t::cend()); + } + + //********************************************************************* + /// Returns an reverse iterator to the reverse beginning of the vector. + ///\return Iterator to the reverse beginning of the vector. + //********************************************************************* + reverse_iterator rbegin() + { + return reverse_iterator(iterator(base_t::end())); + } + + //********************************************************************* + /// Returns a const reverse iterator to the reverse beginning of the vector. + ///\return Const iterator to the reverse beginning of the vector. + //********************************************************************* + const_reverse_iterator rbegin() const + { + return const_reverse_iterator(const_iterator(base_t::end())); + } + + //********************************************************************* + /// Returns a reverse iterator to the end + 1 of the vector. + ///\return Reverse iterator to the end + 1 of the vector. + //********************************************************************* + reverse_iterator rend() + { + return reverse_iterator(iterator(base_t::begin())); + } + + //********************************************************************* + /// Returns a const reverse iterator to the end + 1 of the vector. + ///\return Const reverse iterator to the end + 1 of the vector. + //********************************************************************* + const_reverse_iterator rend() const + { + return const_reverse_iterator(const_iterator(base_t::begin())); + } + + //********************************************************************* + /// Returns a const reverse iterator to the reverse beginning of the vector. + ///\return Const reverse iterator to the reverse beginning of the vector. + //********************************************************************* + const_reverse_iterator crbegin() const + { + return const_reverse_iterator(const_iterator(base_t::cend())); + } + + //********************************************************************* + /// Returns a const reverse iterator to the end + 1 of the vector. + ///\return Const reverse iterator to the end + 1 of the vector. + //********************************************************************* + const_reverse_iterator crend() const + { + return const_reverse_iterator(const_iterator(base_t::cbegin())); + } + + //********************************************************************* + /// Resizes the vector. + /// If asserts or exceptions are enabled and the new size is larger than the + /// maximum then a vector_full is thrown. + ///\param new_size The new size. + //********************************************************************* + void resize(size_t new_size) + { + base_t::resize(new_size); + } + + //********************************************************************* + /// Resizes the vector. + /// If asserts or exceptions are enabled and the new size is larger than the + /// maximum then a vector_full is thrown. + ///\param new_size The new size. + ///\param value The value to fill new elements with. Default = default constructed value. + //********************************************************************* + void resize(size_t new_size, value_type value) + { + base_t::resize(new_size, const_cast(value)); + } + + //********************************************************************* + /// Returns a reference to the value at index 'i' + ///\param i The index. + ///\return A reference to the value at index 'i' + //********************************************************************* + reference operator [](size_t i) + { + return reference(base_t::operator[](i)); + } + + //********************************************************************* + /// Returns a const reference to the value at index 'i' + ///\param i The index. + ///\return A const reference to the value at index 'i' + //********************************************************************* + const_reference operator [](size_t i) const + { + return const_reference(base_t::operator[](i)); + } + + //********************************************************************* + /// Returns a reference to the value at index 'i' + /// If asserts or exceptions are enabled, emits an etl::vector_out_of_bounds if the index is out of range. + ///\param i The index. + ///\return A reference to the value at index 'i' + //********************************************************************* + reference at(size_t i) + { + return reference(base_t::at(i)); + } + + //********************************************************************* + /// Returns a const reference to the value at index 'i' + /// If asserts or exceptions are enabled, emits an etl::vector_out_of_bounds if the index is out of range. + ///\param i The index. + ///\return A const reference to the value at index 'i' + //********************************************************************* + const_reference at(size_t i) const + { + return const_reference(base_t::at(i)); + } + + //********************************************************************* + /// Returns a reference to the first element. + ///\return A reference to the first element. + //********************************************************************* + reference front() + { + return reference(base_t::front()); + } + + //********************************************************************* + /// Returns a const reference to the first element. + ///\return A const reference to the first element. + //********************************************************************* + const_reference front() const + { + return const_reference(base_t::front()); + } + + //********************************************************************* + /// Returns a reference to the last element. + ///\return A reference to the last element. + //********************************************************************* + reference back() + { + return reference(base_t::back()); + } + + //********************************************************************* + /// Returns a const reference to the last element. + ///\return A const reference to the last element. + //********************************************************************* + const_reference back() const + { + return const_reference(base_t::back()); + } + + //********************************************************************* + /// Returns a pointer to the beginning of the vector data. + ///\return A pointer to the beginning of the vector data. + //********************************************************************* + pointer data() + { + return pointer(base_t::data()); + } + + //********************************************************************* + /// Returns a const pointer to the beginning of the vector data. + ///\return A const pointer to the beginning of the vector data. + //********************************************************************* + const_pointer data() const + { + return const_pointer(base_t::data()); + } + + //********************************************************************* + /// Assigns values to the vector. + /// If asserts or exceptions are enabled, emits vector_full if the vector does not have enough free space. + /// If asserts or exceptions are enabled, emits vector_iterator if the iterators are reversed. + ///\param first The iterator to the first element. + ///\param last The iterator to the last element + 1. + //********************************************************************* + template + void assign(TIterator first, TIterator last) + { + base_t::initialise(); + + while (first != last) + { + *p_end++ = (void*)*first++; + } + } + + //********************************************************************* + /// Assigns values to the vector. + /// If asserts or exceptions are enabled, emits vector_full if the vector does not have enough free space. + ///\param n The number of elements to add. + ///\param value The value to insert for each element. + //********************************************************************* + void assign(size_t n, parameter_t value) + { + base_t::assign(n, const_cast(value)); + } + + //************************************************************************* + /// Clears the vector. + //************************************************************************* + void clear() + { + base_t::clear(); + } + + //************************************************************************* + /// Increases the size of the vector by one, but does not initialise the new element. + /// If asserts or exceptions are enabled, throws a vector_full if the vector is already full. + //************************************************************************* + void push_back() + { + base_t::push_back(); + } + + //********************************************************************* + /// Inserts a value at the end of the vector. + /// If asserts or exceptions are enabled, emits vector_full if the vector is already full. + ///\param value The value to add. + //********************************************************************* + void push_back(parameter_t value) + { + base_t::push_back(const_cast(value)); + } + + //************************************************************************* + /// Removes an element from the end of the vector. + /// Does nothing if the vector is empty. + //************************************************************************* + void pop_back() + { + base_t::pop_back(); + } + + //********************************************************************* + /// Inserts a value to the vector. + /// If asserts or exceptions are enabled, emits vector_full if the vector is already full. + ///\param position The position to insert before. + ///\param value The value to insert. + //********************************************************************* + iterator insert(iterator position, parameter_t value) + { + return iterator(base_t::insert(base_t::iterator(position), const_cast(value))); + } + + //********************************************************************* + /// Inserts 'n' values to the vector. + /// If asserts or exceptions are enabled, emits vector_full if the vector does not have enough free space. + ///\param position The position to insert before. + ///\param n The number of elements to add. + ///\param value The value to insert. + //********************************************************************* + void insert(iterator position, size_t n, parameter_t value) + { + base_t::insert(base_t::iterator(position), n, const_cast(value)); + } + + //********************************************************************* + /// Inserts a range of values to the vector. + /// If asserts or exceptions are enabled, emits vector_full if the vector does not have enough free space. + ///\param position The position to insert before. + ///\param first The first element to add. + ///\param last The last + 1 element to add. + //********************************************************************* + template + void insert(iterator position, TIterator first, TIterator last) + { + base_t::insert(base_t::iterator(position), first, last); + } + + //********************************************************************* + /// Erases an element. + ///\param i_element Iterator to the element. + ///\return An iterator pointing to the element that followed the erased element. + //********************************************************************* + iterator erase(iterator i_element) + { + return iterator(base_t::erase(base_t::iterator(i_element))); + } + + //********************************************************************* + /// Erases a range of elements. + /// The range includes all the elements between first and last, including the + /// element pointed by first, but not the one pointed by last. + ///\param first Iterator to the first element. + ///\param last Iterator to the last element. + ///\return An iterator pointing to the element that followed the erased element. + //********************************************************************* + iterator erase(iterator first, iterator last) + { + return iterator(base_t::erase(base_t::iterator(first), base_t::iterator(last))); + } + + //************************************************************************* + /// Assignment operator. + //************************************************************************* + ivector& operator = (const ivector& rhs) + { + if (&rhs != this) + { + assign(rhs.cbegin(), rhs.cend()); + } + + return *this; + } + + protected: + + //********************************************************************* + /// Constructor. + //********************************************************************* + ivector(const T** p_buffer_, size_t MAX_SIZE_) + : pvoidvector(reinterpret_cast(const_cast(p_buffer_)), MAX_SIZE_) + { + } + }; + //*************************************************************************** /// Equal operator. ///\param lhs Reference to the first vector. diff --git a/include/etl/version.h b/include/etl/version.h index cb8278229..9043d8f37 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -37,10 +37,10 @@ SOFTWARE. /// Definitions of the ETL version ///\ingroup utilities -#define ETL_VERSION "11.12.2" +#define ETL_VERSION "11.13.0" #define ETL_VERSION_MAJOR 11 -#define ETL_VERSION_MINOR 12 -#define ETL_VERSION_PATCH 2 +#define ETL_VERSION_MINOR 13 +#define ETL_VERSION_PATCH 0 #endif diff --git a/support/Release notes.txt b/support/Release notes.txt index 79e74a7df..315fa97c4 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,7 @@ +=============================================================================== +11.13.0 +Added specialisation for vector + =============================================================================== 11.12.2 Remove SFINAE from array_view. diff --git a/test/test_vector_pointer.cpp b/test/test_vector_pointer.cpp index 970d22638..eeac0e8cb 100644 --- a/test/test_vector_pointer.cpp +++ b/test/test_vector_pointer.cpp @@ -41,9 +41,12 @@ namespace { static const size_t SIZE = 10; - typedef etl::vector Data; - typedef etl::ivector IData; - typedef std::vector Compare_Data; + typedef etl::vector Data; + typedef etl::vector CData; + typedef etl::ivector IData; + typedef etl::ivector CIData; + typedef std::vector Compare_Data; + typedef std::vector CCompare_Data; Compare_Data initial_data; Compare_Data less_data; @@ -97,6 +100,17 @@ namespace CHECK_EQUAL(data.max_size(), SIZE); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_default_constructor) + { + CData data; + + CHECK_EQUAL(data.size(), size_t(0)); + CHECK(data.empty()); + CHECK_EQUAL(data.capacity(), SIZE); + CHECK_EQUAL(data.max_size(), SIZE); + } + //************************************************************************* TEST(test_iterator_comparison_empty) { @@ -108,6 +122,17 @@ namespace CHECK(data.crbegin() == data.crend()); } + //************************************************************************* + TEST(test_const_iterator_comparison_empty) + { + CData data; + + CHECK(data.begin() == data.end()); + CHECK(data.cbegin() == data.cend()); + CHECK(data.rbegin() == data.rend()); + CHECK(data.crbegin() == data.crend()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_constructor_size) { @@ -118,6 +143,16 @@ namespace CHECK(!data.empty()); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_constructor_size) + { + const size_t INITIAL_SIZE = 5; + CData data(INITIAL_SIZE); + + CHECK(data.size() == INITIAL_SIZE); + CHECK(!data.empty()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_constructor_size_value) { @@ -137,12 +172,37 @@ namespace CHECK(is_equal); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_constructor_size_value) + { + const size_t INITIAL_SIZE = 5; + int INITIAL_VALUE = 1; + + std::array compare_data; + compare_data.fill(&INITIAL_VALUE); + + CData data(INITIAL_SIZE, &INITIAL_VALUE); + + CHECK(data.size() == INITIAL_SIZE); + CHECK(!data.empty()); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_constructor_size_excess) { CHECK_THROW(Data data(SIZE + 1), etl::vector_full); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_constructor_size_excess) + { + CHECK_THROW(CData data(SIZE + 1), etl::vector_full); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_constructor_range) { @@ -155,6 +215,18 @@ namespace CHECK(std::equal(compare_data.begin(), compare_data.end(), data.begin())); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_constructor_range) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + CData data(compare_data.begin(), compare_data.end()); + + CHECK(data.size() == SIZE); + CHECK(!data.empty()); + CHECK(std::equal(compare_data.begin(), compare_data.end(), data.begin())); + } + //************************************************************************* TEST(test_constructor_initializer_list) { @@ -170,6 +242,21 @@ namespace CHECK(std::equal(compare_data.begin(), compare_data.end(), data.begin())); } + //************************************************************************* + TEST(test_const_constructor_initializer_list) + { + int a = 0; + int b = 1; + int c = 3; + int d = 4; + + CCompare_Data compare_data = { &a, &b, &c, &d }; + CData data = { &a, &b, &c, &d }; + + CHECK_EQUAL(compare_data.size(), data.size()); + CHECK(std::equal(compare_data.begin(), compare_data.end(), data.begin())); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_copy_constructor) { @@ -181,6 +268,17 @@ namespace CHECK(data2 != data); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_copy_constructor) + { + CData data(initial_data.begin(), initial_data.end()); + CData data2(data); + CHECK(data2 == data); + + data2[2] = nullptr; + CHECK(data2 != data); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_assignment) { @@ -194,6 +292,19 @@ namespace CHECK(is_equal); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_assignment) + { + CData data(initial_data.begin(), initial_data.end()); + CData other_data; + + other_data = data; + + bool is_equal = std::equal(data.begin(), data.end(), other_data.begin()); + + CHECK(is_equal); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_assignment_iterface) { @@ -210,6 +321,22 @@ namespace CHECK(is_equal); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_assignment_iterface) + { + CData data1(initial_data.begin(), initial_data.end()); + CData data2; + + CIData& idata1 = data1; + CIData& idata2 = data2; + + idata2 = idata1; + + bool is_equal = std::equal(data1.begin(), data1.end(), data2.begin()); + + CHECK(is_equal); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_self_assignment) { @@ -223,6 +350,19 @@ namespace CHECK(is_equal); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_self_assignment) + { + CData data(initial_data.begin(), initial_data.end()); + CData other_data(data); + + other_data = other_data; + + bool is_equal = std::equal(data.begin(), data.end(), other_data.begin()); + + CHECK(is_equal); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_begin) { @@ -233,6 +373,16 @@ namespace CHECK_EQUAL(&constData[0], constData.begin()); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_begin) + { + CData data(10); + const CData constData(10); + + CHECK_EQUAL(&data[0], data.begin()); + CHECK_EQUAL(&constData[0], constData.begin()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_end) { @@ -243,6 +393,16 @@ namespace CHECK_EQUAL(&constData[10], constData.end()); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_end) + { + CData data(10); + const CData constData(10); + + CHECK_EQUAL(&data[10], data.end()); + CHECK_EQUAL(&constData[10], constData.end()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_resize_up) { @@ -255,6 +415,18 @@ namespace CHECK_EQUAL(data.size(), NEW_SIZE); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_resize_up) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 8; + + CData data(INITIAL_SIZE); + data.resize(NEW_SIZE); + + CHECK_EQUAL(data.size(), NEW_SIZE); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_resize_up_value) { @@ -273,6 +445,24 @@ namespace CHECK(is_equal); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_resize_up_value) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 8; + int INITIAL_VALUE = 1; + + CData data(INITIAL_SIZE, &INITIAL_VALUE); + data.resize(NEW_SIZE, &INITIAL_VALUE); + + std::array compare_data; + compare_data.fill(&INITIAL_VALUE); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_resize_excess) { @@ -284,6 +474,17 @@ namespace CHECK_THROW(data.resize(NEW_SIZE), etl::vector_full); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_resize_excess) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = SIZE + 1; + + CData data(INITIAL_SIZE); + + CHECK_THROW(data.resize(NEW_SIZE), etl::vector_full); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_resize_down) { @@ -296,6 +497,18 @@ namespace CHECK_EQUAL(data.size(), NEW_SIZE); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_resize_down) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 2; + + CData data(INITIAL_SIZE); + data.resize(NEW_SIZE); + + CHECK_EQUAL(data.size(), NEW_SIZE); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_resize_down_value) { @@ -309,6 +522,19 @@ namespace CHECK_EQUAL(data.size(), NEW_SIZE); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_resize_down_value) + { + const size_t INITIAL_SIZE = 5; + const size_t NEW_SIZE = 2; + int INITIAL_VALUE = 1; + + CData data(INITIAL_SIZE); + data.resize(NEW_SIZE, &INITIAL_VALUE); + + CHECK_EQUAL(data.size(), NEW_SIZE); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_empty) { @@ -319,6 +545,16 @@ namespace CHECK(!data.empty()); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_empty) + { + CData data; + data.resize(data.max_size()); + + CHECK(data.full()); + CHECK(!data.empty()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_full) { @@ -328,6 +564,15 @@ namespace CHECK(data.empty()); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_full) + { + CData data; + + CHECK(!data.full()); + CHECK(data.empty()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_index) { @@ -341,6 +586,19 @@ namespace } } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_index) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + CData data(compare_data.begin(), compare_data.end()); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data[i], compare_data[i]); + } + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_index_const) { @@ -354,6 +612,19 @@ namespace } } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_index_const) + { + const CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + const CData data(compare_data.begin(), compare_data.end()); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data[i], compare_data[i]); + } + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_at) { @@ -368,6 +639,20 @@ namespace CHECK_THROW(data.at(data.size()), etl::vector_out_of_bounds); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_at) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + CData data(initial_data.begin(), initial_data.end()); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data.at(i), compare_data.at(i)); + } + + CHECK_THROW(data.at(data.size()), etl::vector_out_of_bounds); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_at_const) { @@ -382,6 +667,20 @@ namespace CHECK_THROW(data.at(data.size()), etl::vector_out_of_bounds); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_at_const) + { + const CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + const CData data(initial_data.begin(), initial_data.end()); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(data.at(i), compare_data.at(i)); + } + + CHECK_THROW(data.at(data.size()), etl::vector_out_of_bounds); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_front) { @@ -391,6 +690,15 @@ namespace CHECK(data.front() == compare_data.front()); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_front) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + CData data(initial_data.begin(), initial_data.end()); + + CHECK(data.front() == compare_data.front()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_front_const) { @@ -400,6 +708,15 @@ namespace CHECK(data.front() == compare_data.front()); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_front_const) + { + const CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + const CData data(initial_data.begin(), initial_data.end()); + + CHECK(data.front() == compare_data.front()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_back) { @@ -409,6 +726,15 @@ namespace CHECK(data.back() == compare_data.back()); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_back) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + CData data(initial_data.begin(), initial_data.end()); + + CHECK(data.back() == compare_data.back()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_back_const) { @@ -418,6 +744,14 @@ namespace CHECK(data.back() == compare_data.back()); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_back_const) + { + const CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + const CData data(initial_data.begin(), initial_data.end()); + + CHECK(data.back() == compare_data.back()); + } //************************************************************************* TEST_FIXTURE(SetupFixture, test_data) @@ -432,11 +766,35 @@ namespace } //************************************************************************* - TEST_FIXTURE(SetupFixture, test_data_const) + TEST_FIXTURE(SetupFixture, test_const_data) { - Compare_Data compare_data(initial_data.begin(), initial_data.end()); + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); - const Data data(compare_data.begin(), compare_data.end()); + CData data(compare_data.begin(), compare_data.end()); + + bool is_equal = std::equal(data.data(), data.data() + data.size(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_data_const) + { + Compare_Data compare_data(initial_data.begin(), initial_data.end()); + + const Data data(compare_data.begin(), compare_data.end()); + + bool is_equal = std::equal(data.data(), data.data() + data.size(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_data_const) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + const CData data(compare_data.begin(), compare_data.end()); bool is_equal = std::equal(data.data(), data.data() + data.size(), compare_data.begin()); @@ -457,6 +815,20 @@ namespace CHECK(is_equal); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_assign_range) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + CData data; + + data.assign(compare_data.begin(), compare_data.end()); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_assign_size_value) { @@ -473,6 +845,22 @@ namespace CHECK(is_equal); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_assign_size_value) + { + const size_t INITIAL_SIZE = 5; + int INITIAL_VALUE = 1; + std::array compare_data; + compare_data.fill(&INITIAL_VALUE); + + CData data; + data.assign(INITIAL_SIZE, &INITIAL_VALUE); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_assign_size_value_excess) { @@ -487,6 +875,20 @@ namespace CHECK_THROW(data.assign(EXCESS_SIZE, &INITIAL_VALUE), etl::vector_full); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_assign_size_value_excess) + { + const size_t INITIAL_SIZE = SIZE; + const size_t EXCESS_SIZE = SIZE + 1; + int INITIAL_VALUE = 1; + std::array compare_data; + compare_data.fill(&INITIAL_VALUE); + + CData data; + + CHECK_THROW(data.assign(EXCESS_SIZE, &INITIAL_VALUE), etl::vector_full); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_push_back) { @@ -510,6 +912,29 @@ namespace CHECK(is_equal); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_push_back) + { + CCompare_Data compare_data; + CData data; + + const int d = 0; + + for (size_t i = 0; i < SIZE; ++i) + { + compare_data.push_back(&d); + } + + for (size_t i = 0; i < SIZE; ++i) + { + data.push_back(&d); + } + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_push_back_null) { @@ -528,6 +953,24 @@ namespace CHECK(is_equal); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_push_back_null) + { + CCompare_Data compare_data; + CData data; + + const int d = 0; + + compare_data.push_back(&d); + + data.push_back(); + data[0] = &d; + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_push_back_excess) { @@ -543,6 +986,21 @@ namespace CHECK_THROW(data.push_back(&d), etl::vector_full); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_push_back_excess) + { + CData data; + + const int d = 0; + + for (size_t i = 0; i < SIZE; ++i) + { + data.push_back(&d); + } + + CHECK_THROW(data.push_back(&d), etl::vector_full); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_pop_back) { @@ -555,9 +1013,24 @@ namespace data.pop_back(); data.pop_back(); - bool is_equal = std::equal(data.begin(), - data.end(), - compare_data.begin()); + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_pop_back) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + CData data(initial_data.begin(), initial_data.end()); + + compare_data.pop_back(); + compare_data.pop_back(); + + data.pop_back(); + data.pop_back(); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); CHECK(is_equal); } @@ -575,6 +1048,19 @@ namespace CHECK_THROW(data.pop_back(), etl::vector_empty); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_pop_back_exception) + { + CData data; + + data.resize(2); + + data.pop_back(); + data.pop_back(); + + CHECK_THROW(data.pop_back(), etl::vector_empty); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_insert_position_value) { @@ -594,9 +1080,32 @@ namespace CHECK_EQUAL(compare_data.size(), data.size()); - bool is_equal = std::equal(data.begin(), - data.end(), - compare_data.begin()); + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_insert_position_value) + { + const size_t INITIAL_SIZE = 5; + int INITIAL_VALUE = 1; + + for (size_t offset = 0; offset <= INITIAL_SIZE; ++offset) + { + CCompare_Data compare_data; + CData data; + + data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + compare_data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + + data.insert(data.begin() + offset, &INITIAL_VALUE); + compare_data.insert(compare_data.begin() + offset, &INITIAL_VALUE); + + CHECK_EQUAL(compare_data.size(), data.size()); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); CHECK(is_equal); } @@ -640,9 +1149,30 @@ namespace data.insert(data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE); compare_data.insert(compare_data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE); - bool is_equal = std::equal(data.begin(), - data.end(), - compare_data.begin()); + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_insert_position_n_value) + { + const size_t INITIAL_SIZE = 5; + const size_t INSERT_SIZE = 3; + int INITIAL_VALUE = 11; + + for (size_t offset = 0; offset <= INITIAL_SIZE; ++offset) + { + CCompare_Data compare_data; + CData data; + + data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + compare_data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + data.insert(data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE); + compare_data.insert(compare_data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); CHECK(is_equal); } @@ -674,6 +1204,32 @@ namespace CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE), etl::vector_full); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_insert_position_n_value_excess) + { + const size_t INITIAL_SIZE = SIZE; + const size_t INSERT_SIZE = 4; + const int INITIAL_VALUE = 1; + + CData data(INITIAL_SIZE, &INITIAL_VALUE); + + size_t offset = 0; + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE), etl::vector_full); + + offset = 2; + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE), etl::vector_full); + + offset = 4; + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE), etl::vector_full); + + offset = data.size(); + + CHECK_THROW(data.insert(data.begin() + offset, INSERT_SIZE, &INITIAL_VALUE), etl::vector_full); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_insert_position_range) { @@ -698,6 +1254,30 @@ namespace } } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_insert_position_range) + { + const size_t INITIAL_SIZE = 5; + const int INITIAL_VALUE = 1; + + for (size_t offset = 0; offset <= INITIAL_SIZE; ++offset) + { + CCompare_Data compare_data; + CData data; + + data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + compare_data.assign(initial_data.begin(), initial_data.begin() + INITIAL_SIZE); + data.insert(data.begin() + offset, insert_data.begin(), insert_data.end()); + compare_data.insert(compare_data.begin() + offset, insert_data.begin(), insert_data.end()); + + bool is_equal = std::equal(data.begin(), + data.end(), + compare_data.begin()); + + CHECK(is_equal); + } + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_insert_position_range_excess) { @@ -722,7 +1302,31 @@ namespace CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full); } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_insert_position_range_excess) + { + const size_t INITIAL_SIZE = 5; + const int INITIAL_VALUE = 1; + + CData data(INITIAL_SIZE, &INITIAL_VALUE); + + size_t offset = 0; + + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full); + + offset = 2; + + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full); + + offset = 4; + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full); + + offset = data.size(); + + CHECK_THROW(data.insert(data.begin() + offset, initial_data.begin(), initial_data.end()), etl::vector_full); + } //************************************************************************* TEST_FIXTURE(SetupFixture, test_erase_single) @@ -734,9 +1338,22 @@ namespace data.erase(data.begin() + 2); - bool is_equal = std::equal(data.begin(), - data.end(), - compare_data.begin()); + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_erase_single) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + CData data(initial_data.begin(), initial_data.end()); + + compare_data.erase(compare_data.begin() + 2); + + data.erase(data.begin() + 2); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); CHECK(is_equal); } @@ -751,9 +1368,22 @@ namespace data.erase(data.begin() + 2, data.begin() + 4); - bool is_equal = std::equal(data.begin(), - data.end(), - compare_data.begin()); + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_erase_range) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + CData data(initial_data.begin(), initial_data.end()); + + compare_data.erase(compare_data.begin() + 2, compare_data.begin() + 4); + + data.erase(data.begin() + 2, data.begin() + 4); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); CHECK(is_equal); } @@ -769,6 +1399,17 @@ namespace CHECK_EQUAL(data.size(), size_t(0)); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_clear) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + CData data(compare_data.begin(), compare_data.end()); + data.clear(); + + CHECK_EQUAL(data.size(), size_t(0)); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_iterator) { @@ -776,9 +1417,19 @@ namespace Data data(compare_data.begin(), compare_data.end()); - bool is_equal = std::equal(data.begin(), - data.end(), - compare_data.begin()); + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_iterator_const) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + CData data(compare_data.begin(), compare_data.end()); + + bool is_equal = std::equal(data.begin(), data.end(), compare_data.begin()); CHECK(is_equal); } @@ -790,9 +1441,19 @@ namespace Data data(compare_data.begin(), compare_data.end()); - bool is_equal = std::equal(data.cbegin(), - data.cend(), - compare_data.cbegin()); + bool is_equal = std::equal(data.cbegin(), data.cend(), compare_data.cbegin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_iterator_const) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + CData data(compare_data.begin(), compare_data.end()); + + bool is_equal = std::equal(data.cbegin(), data.cend(), compare_data.cbegin()); CHECK(is_equal); } @@ -804,9 +1465,19 @@ namespace Data data(compare_data.begin(), compare_data.end()); - bool is_equal = std::equal(data.rbegin(), - data.rend(), - compare_data.rbegin()); + bool is_equal = std::equal(data.rbegin(), data.rend(), compare_data.rbegin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_reverse_iterator_const) + { + CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + CData data(compare_data.begin(), compare_data.end()); + + bool is_equal = std::equal(data.rbegin(), data.rend(), compare_data.rbegin()); CHECK(is_equal); } @@ -818,9 +1489,19 @@ namespace const Data data(compare_data.begin(), compare_data.end()); - bool is_equal = std::equal(data.crbegin(), - data.crend(), - compare_data.crbegin()); + bool is_equal = std::equal(data.crbegin(), data.crend(), compare_data.crbegin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_reverse_iterator_const) + { + const CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + const CData data(compare_data.begin(), compare_data.end()); + + bool is_equal = std::equal(data.crbegin(), data.crend(), compare_data.crbegin()); CHECK(is_equal); } @@ -832,9 +1513,19 @@ namespace const Data data(compare_data.begin(), compare_data.end()); - bool is_equal = std::equal(data.rbegin(), - data.rend(), - compare_data.rbegin()); + bool is_equal = std::equal(data.rbegin(), data.rend(), compare_data.rbegin()); + + CHECK(is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_reverse_iterator2_const) + { + const CCompare_Data compare_data(initial_data.begin(), initial_data.end()); + + const CData data(compare_data.begin(), compare_data.end()); + + bool is_equal = std::equal(data.rbegin(), data.rend(), compare_data.rbegin()); CHECK(is_equal); } @@ -856,6 +1547,23 @@ namespace CHECK(!(shorter == initial1)); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_equal) + { + const CData initial1(initial_data.begin(), initial_data.end()); + const CData initial2(initial_data.begin(), initial_data.end()); + + CHECK(initial1 == initial2); + + const CData different(different_data.begin(), different_data.end()); + + CHECK(!(initial1 == different)); + + const CData shorter(shorter_data.begin(), shorter_data.end()); + + CHECK(!(shorter == initial1)); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_not_equal) { @@ -873,6 +1581,23 @@ namespace CHECK(shorter != initial1); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_not_equal) + { + const CData initial1(initial_data.begin(), initial_data.end()); + const CData initial2(initial_data.begin(), initial_data.end()); + + CHECK(!(initial1 != initial2)); + + const CData different(different_data.begin(), different_data.end()); + + CHECK(initial1 != different); + + const CData shorter(shorter_data.begin(), shorter_data.end()); + + CHECK(shorter != initial1); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_less_than) { @@ -891,6 +1616,24 @@ namespace CHECK((initial < shorter) == (initial_data < shorter_data)); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_less_than) + { + const CData less(less_data.begin(), less_data.end()); + const CData initial(initial_data.begin(), initial_data.end()); + + CHECK((less < initial) == (less_data < initial_data)); + + const CData greater(greater_data.begin(), greater_data.end()); + + CHECK((greater < initial) == (greater_data < initial_data)); + + const CData shorter(shorter_data.begin(), shorter_data.end()); + + CHECK((shorter < initial) == (shorter_data < initial_data)); + CHECK((initial < shorter) == (initial_data < shorter_data)); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_less_than_or_equal) { @@ -912,6 +1655,27 @@ namespace CHECK((initial <= initial2) == (initial_data <= initial_data)); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_less_than_or_equal) + { + const CData less(less_data.begin(), less_data.end()); + const CData initial(initial_data.begin(), initial_data.end()); + + CHECK((less <= initial) == (less_data <= initial_data)); + + const CData greater(greater_data.begin(), greater_data.end()); + + CHECK((greater <= initial) == (greater_data <= initial_data)); + + const CData shorter(shorter_data.begin(), shorter_data.end()); + + CHECK((shorter <= initial) == (shorter_data <= initial_data)); + CHECK((initial <= shorter) == (initial_data <= shorter_data)); + + const CData initial2(initial_data.begin(), initial_data.end()); + CHECK((initial <= initial2) == (initial_data <= initial_data)); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_greater_than) { @@ -930,6 +1694,24 @@ namespace CHECK((initial > shorter) == (initial_data > shorter_data)); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_greater_than) + { + const CData less(less_data.begin(), less_data.end()); + const CData initial(initial_data.begin(), initial_data.end()); + + CHECK((less > initial) == (less_data > initial_data)); + + const CData greater(greater_data.begin(), greater_data.end()); + + CHECK((greater > initial) == (greater_data > initial_data)); + + const CData shorter(shorter_data.begin(), shorter_data.end()); + + CHECK((shorter > initial) == (shorter_data > initial_data)); + CHECK((initial > shorter) == (initial_data > shorter_data)); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_greater_than_or_equal) { @@ -951,6 +1733,27 @@ namespace CHECK((initial >= initial2) == (initial_data >= initial_data)); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_greater_than_or_equal) + { + const CData less(less_data.begin(), less_data.end()); + const CData initial(initial_data.begin(), initial_data.end()); + + CHECK((less >= initial) == (less_data >= initial_data)); + + const CData greater(greater_data.begin(), greater_data.end()); + + CHECK((greater >= initial) == (greater_data >= initial_data)); + + const CData shorter(shorter_data.begin(), shorter_data.end()); + + CHECK((shorter >= initial) == (shorter_data >= initial_data)); + CHECK((initial >= shorter) == (initial_data > shorter_data)); + + const CData initial2(initial_data.begin(), initial_data.end()); + CHECK((initial >= initial2) == (initial_data >= initial_data)); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_memcpy_repair) { @@ -977,9 +1780,36 @@ namespace // Modify the original and check that the memcpy'd vector is not the same. std::reverse(data.begin(), data.end()); - is_equal = std::equal(rdata.begin(), - rdata.end(), - data.begin()); + is_equal = std::equal(rdata.begin(), rdata.end(), data.begin()); + + CHECK(!is_equal); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_const_memcpy_repair) + { + CData data(initial_data.begin(), initial_data.end()); + + char buffer[sizeof(CData)]; + + memcpy(&buffer, &data, sizeof(data)); + + CData& rdata(*reinterpret_cast(buffer)); + rdata.repair(); + + // Check that the memcpy'd vector is the same. + CHECK_EQUAL(data.size(), rdata.size()); + CHECK(!rdata.empty()); + CHECK(rdata.full()); + + bool is_equal = std::equal(rdata.begin(), rdata.end(), data.begin()); + + CHECK(is_equal); + + // Modify the original and check that the memcpy'd vector is not the same. + std::reverse(data.begin(), data.end()); + + is_equal = std::equal(rdata.begin(), rdata.end(), data.begin()); CHECK(!is_equal); }