From 3206ac9feb44a22e0fb27bbbec5eecf156ee6231 Mon Sep 17 00:00:00 2001 From: mike919192 <91038685+mike919192@users.noreply.github.com> Date: Sun, 11 Jan 2026 12:05:21 -0500 Subject: [PATCH] ivector swap (#1256) * Etl vector swap implementation * Implement swap function overload * Fix nitpicks * Add size check * Remove unnessecary etl swap overloads * Use swap_range and move range --------- Co-authored-by: John Wellbelove --- include/etl/private/ivectorpointer.h | 50 +++++ include/etl/vector.h | 36 +++ test/test_vector.cpp | 114 ++++++++++ test/test_vector_external_buffer.cpp | 114 ++++++++++ test/test_vector_non_trivial.cpp | 81 +++++++ test/test_vector_pointer.cpp | 222 ++++++++++++++++++ test/test_vector_pointer_external_buffer.cpp | 223 +++++++++++++++++++ 7 files changed, 840 insertions(+) diff --git a/include/etl/private/ivectorpointer.h b/include/etl/private/ivectorpointer.h index 52f938fe..f9f6dd7e 100644 --- a/include/etl/private/ivectorpointer.h +++ b/include/etl/private/ivectorpointer.h @@ -467,6 +467,31 @@ namespace etl return iterator(base_t::erase(base_t::const_iterator(first), base_t::const_iterator(last))); } + //********************************************************************* + /// Swap contents with another vector. Performs operation on each individual element. + ///\param other The other vector to swap with. + //********************************************************************* + void swap(ivector& other) + { + if (this == &other) + { + return; + } + + ETL_ASSERT_OR_RETURN(this->max_size() >= other.size() && other.max_size() >= this->size(), ETL_ERROR(vector_full)); + + ivector& smaller = other.size() > this->size() ? *this : other; + ivector& larger = other.size() > this->size() ? other : *this; + + ETL_OR_STD::swap_ranges(smaller.begin(), smaller.end(), larger.begin()); + + typename ivector::iterator larger_itr = etl::next(larger.begin(), smaller.size()); + + etl::move(larger_itr, larger.end(), etl::back_inserter(smaller)); + + larger.erase(larger_itr, larger.end()); + } + //************************************************************************* /// Assignment operator. //************************************************************************* @@ -890,6 +915,31 @@ namespace etl return iterator(base_t::erase(base_t::iterator(first), base_t::iterator(last))); } + //********************************************************************* + /// Swap contents with another vector. Performs operation on each individual element. + ///\param other The other vector to swap with. + //********************************************************************* + void swap(ivector& other) + { + if (this == &other) + { + return; + } + + ETL_ASSERT_OR_RETURN(this->max_size() >= other.size() && other.max_size() >= this->size(), ETL_ERROR(vector_full)); + + ivector& smaller = other.size() > this->size() ? *this : other; + ivector& larger = other.size() > this->size() ? other : *this; + + ETL_OR_STD::swap_ranges(smaller.begin(), smaller.end(), larger.begin()); + + typename ivector::iterator larger_itr = etl::next(larger.begin(), smaller.size()); + + etl::move(larger_itr, larger.end(), etl::back_inserter(smaller)); + + larger.erase(larger_itr, larger.end()); + } + //************************************************************************* /// Assignment operator. //************************************************************************* diff --git a/include/etl/vector.h b/include/etl/vector.h index c36f9b55..92caf40c 100644 --- a/include/etl/vector.h +++ b/include/etl/vector.h @@ -947,6 +947,31 @@ namespace etl return first_; } + //********************************************************************* + /// Swap contents with another vector. Performs operation on each individual element. + ///\param other The other vector to swap with. + //********************************************************************* + void swap(ivector& other) + { + if (this == &other) + { + return; + } + + ETL_ASSERT_OR_RETURN(this->max_size() >= other.size() && other.max_size() >= this->size(), ETL_ERROR(vector_full)); + + ivector& smaller = other.size() > this->size() ? *this : other; + ivector& larger = other.size() > this->size() ? other : *this; + + ETL_OR_STD::swap_ranges(smaller.begin(), smaller.end(), larger.begin()); + + typename ivector::iterator larger_itr = etl::next(larger.begin(), smaller.size()); + + etl::move(larger_itr, larger.end(), etl::back_inserter(smaller)); + + larger.erase(larger_itr, larger.end()); + } + //************************************************************************* /// Assignment operator. //************************************************************************* @@ -1863,6 +1888,17 @@ namespace etl return d; } + + //********************************************************************* + /// Overloaded swap for etl::ivector + ///\param lhs The first vector to swap with. + ///\param rhs The second vector to swap with. + //********************************************************************* + template + void swap(ivector& lhs, ivector& rhs) + { + lhs.swap(rhs); + } } #endif diff --git a/test/test_vector.cpp b/test/test_vector.cpp index 03f6a615..c1e679a4 100644 --- a/test/test_vector.cpp +++ b/test/test_vector.cpp @@ -52,6 +52,8 @@ namespace Compare_Data different_data; Compare_Data insert_data; Compare_Data blank_data; + Compare_Data swap_data; + Compare_Data swap_other_data; //************************************************************************* struct SetupFixture @@ -63,6 +65,8 @@ namespace int n_less[] = { 0, 1, 2, 3, 3, 5, 6, 7, 8, 9 }; int n_greater[] = { 0, 1, 2, 4, 4, 5, 6, 7, 8, 9 }; int n_blank[] = { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99 }; + int n_swap[] = { 0, 1, 2, 3, 4, 5 }; + int n_swap_other[] = { 6, 7, 8, 9 }; initial_data.assign(std::begin(n), std::end(n)); insert_data.assign(std::begin(n_insert), std::end(n_insert)); @@ -71,6 +75,8 @@ namespace shorter_data.assign(std::begin(n_greater), std::end(n_greater) - 1); different_data.assign(initial_data.rbegin(), initial_data.rend()); blank_data.assign(std::begin(n_blank), std::end(n_blank)); + swap_data.assign(std::begin(n_swap), std::end(n_swap)); + swap_other_data.assign(std::begin(n_swap_other), std::end(n_swap_other)); } }; @@ -1620,5 +1626,113 @@ namespace CHECK(std::equal(blank_data.begin(), blank_data.end(), data.begin())); } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_swap_same_capacity) + { + Data etl_data(swap_data.begin(), swap_data.end()); + Data etl_data2(swap_other_data.begin(), swap_other_data.end()); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_swap_different_capacity) + { + const size_t other_size = 6; + Data etl_data(swap_data.begin(), swap_data.end()); + etl::vector etl_data2(swap_other_data.begin(), swap_other_data.end()); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == other_size); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + } + + //************************************************************************* + TEST(test_swap_insufficient_capacity) + { + etl::vector etl_data(4); + etl::vector etl_data2(6); + + CHECK_THROW(etl_data.swap(etl_data2), etl::vector_full); + CHECK_THROW(etl_data2.swap(etl_data), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_etl_swap_different_capacity) + { + const size_t other_size = 6; + Data etl_data(swap_data.begin(), swap_data.end()); + etl::vector etl_data2(swap_other_data.begin(), swap_other_data.end()); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + + etl::swap(etl_data, etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == other_size); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl::swap(etl_data, etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + } } } diff --git a/test/test_vector_external_buffer.cpp b/test/test_vector_external_buffer.cpp index 912ebc29..a65648fe 100644 --- a/test/test_vector_external_buffer.cpp +++ b/test/test_vector_external_buffer.cpp @@ -58,6 +58,8 @@ namespace Compare_Data shorter_data; Compare_Data different_data; Compare_Data insert_data; + Compare_Data swap_data; + Compare_Data swap_other_data; //************************************************************************* struct SetupFixture @@ -68,6 +70,8 @@ namespace int n_insert[] = { 11, 12, 13 }; int n_less[] = { 0, 1, 2, 3, 3, 5, 6, 7, 8, 9 }; int n_greater[] = { 0, 1, 2, 4, 4, 5, 6, 7, 8, 9 }; + int n_swap[] = { 0, 1, 2, 3, 4, 5 }; + int n_swap_other[] = { 6, 7, 8, 9 }; initial_data.assign(std::begin(n), std::end(n)); insert_data.assign(std::begin(n_insert), std::end(n_insert)); @@ -75,6 +79,8 @@ namespace greater_data.assign(std::begin(n_greater), std::end(n_greater)); shorter_data.assign(std::begin(n_greater), std::end(n_greater) - 1); different_data.assign(initial_data.rbegin(), initial_data.rend()); + swap_data.assign(std::begin(n_swap), std::end(n_swap)); + swap_other_data.assign(std::begin(n_swap_other), std::end(n_swap_other)); std::fill_n(buffer1, SIZE, -1); std::fill_n(buffer2, SIZE, -1); @@ -1320,5 +1326,113 @@ namespace CHECK_EQUAL(raw[4].i, dest[6].i); CHECK_EQUAL(raw[5].i, dest[7].i); } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_swap_same_capacity) + { + Data etl_data(swap_data.begin(), swap_data.end(), buffer1, SIZE); + Data etl_data2(swap_other_data.begin(), swap_other_data.end(), buffer2, SIZE); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_swap_different_capacity) + { + const size_t other_size = 6; + Data etl_data(swap_data.begin(), swap_data.end(), buffer1, SIZE); + Data etl_data2(swap_other_data.begin(), swap_other_data.end(), buffer2, other_size); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == other_size); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + } + + //************************************************************************* + TEST(test_swap_insufficient_capacity) + { + Data etl_data(4, buffer1, 4); + etl::vector etl_data2(6); + + CHECK_THROW(etl_data.swap(etl_data2), etl::vector_full); + CHECK_THROW(etl_data2.swap(etl_data), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_swap_between_internal_and_external) + { + const size_t other_size = 6; + Data etl_data(swap_data.begin(), swap_data.end(), buffer1, SIZE); + etl::vector etl_data2(swap_other_data.begin(), swap_other_data.end()); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == other_size); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + } } } diff --git a/test/test_vector_non_trivial.cpp b/test/test_vector_non_trivial.cpp index aabd0e4a..1a10ac77 100644 --- a/test/test_vector_non_trivial.cpp +++ b/test/test_vector_non_trivial.cpp @@ -62,6 +62,8 @@ namespace CompareDataNDC shorter_data; CompareDataNDC different_data; CompareDataNDC insert_data; + CompareDataNDC swap_data; + CompareDataNDC swap_other_data; //************************************************************************* struct SetupFixture @@ -72,6 +74,8 @@ namespace NDC n_insert[] = { NDC("11"), NDC("12"), NDC("13") }; NDC n_less[] = { NDC("0"), NDC("1"), NDC("2"), NDC("3"), NDC("3"), NDC("5"), NDC("6"), NDC("7"), NDC("8"), NDC("9") }; NDC n_greater[] = { NDC("0"), NDC("1"), NDC("2"), NDC("4"), NDC("4"), NDC("5"), NDC("6"), NDC("7"), NDC("8"), NDC("9") }; + NDC n_swap[] = { NDC("0"), NDC("1"), NDC("2"), NDC("4"), NDC("4"), NDC("5") }; + NDC n_swap_other[] = { NDC("6"), NDC("7"), NDC("8"), NDC("9") }; initial_data.assign(std::begin(n), std::end(n)); insert_data.assign(std::begin(n_insert), std::end(n_insert)); @@ -79,6 +83,8 @@ namespace greater_data.assign(std::begin(n_greater), std::end(n_greater)); shorter_data.assign(std::begin(n_greater), std::end(n_greater) - 1); different_data.assign(initial_data.rbegin(), initial_data.rend()); + swap_data.assign(std::begin(n_swap), std::end(n_swap)); + swap_other_data.assign(std::begin(n_swap_other), std::end(n_swap_other)); } }; @@ -1407,5 +1413,80 @@ namespace const DataNDC initial2(initial_data.begin(), initial_data.end()); CHECK((initial >= initial2) == (initial_data >= initial_data)); } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_swap_same_capacity) + { + DataNDC etl_data(swap_data.begin(), swap_data.end()); + DataNDC etl_data2(swap_other_data.begin(), swap_other_data.end()); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_swap_different_capacity) + { + const size_t other_size = 6; + DataNDC etl_data(swap_data.begin(), swap_data.end()); + etl::vector etl_data2(swap_other_data.begin(), swap_other_data.end()); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == other_size); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + } + + //************************************************************************* + TEST(test_swap_insufficient_capacity) + { + etl::vector etl_data(4); + etl::vector etl_data2(6); + + CHECK_THROW(etl_data.swap(etl_data2), etl::vector_full); + CHECK_THROW(etl_data2.swap(etl_data), etl::vector_full); + } } } diff --git a/test/test_vector_pointer.cpp b/test/test_vector_pointer.cpp index ee6ce915..78d2b0db 100644 --- a/test/test_vector_pointer.cpp +++ b/test/test_vector_pointer.cpp @@ -54,6 +54,8 @@ namespace Compare_Data shorter_data; Compare_Data different_data; Compare_Data insert_data; + Compare_Data swap_data; + Compare_Data swap_other_data; int n0 = 0; int n1 = 1; @@ -78,6 +80,8 @@ namespace int* n_insert[] = { &n11, &n12, &n13 }; int* n_less[] = { &n0, &n1, &n2, &n3, &n3, &n5, &n6, &n7, &n8, &n9 }; int* n_greater[] = { &n0, &n1, &n2, &n4, &n4, &n5, &n6, &n7, &n8, &n9 }; + int* n_swap[] = { &n0, &n1, &n2, &n3, &n4, &n5 }; + int* n_swap_other[] = { &n6, &n7, &n8, &n9 }; initial_data.assign(std::begin(n), std::end(n)); insert_data.assign(std::begin(n_insert), std::end(n_insert)); @@ -85,6 +89,8 @@ namespace greater_data.assign(std::begin(n_greater), std::end(n_greater)); shorter_data.assign(std::begin(n_greater), std::end(n_greater) - 1); different_data.assign(initial_data.rbegin(), initial_data.rend()); + swap_data.assign(std::begin(n_swap), std::end(n_swap)); + swap_other_data.assign(std::begin(n_swap_other), std::end(n_swap_other)); } }; @@ -2199,5 +2205,221 @@ namespace CHECK_EQUAL(9, *data[9]); } #endif + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_swap_same_capacity) + { + Data etl_data(swap_data.begin(), swap_data.end()); + Data etl_data2(swap_other_data.begin(), swap_other_data.end()); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_swap_const_same_capacity) + { + CData etl_data(swap_data.begin(), swap_data.end()); + CData etl_data2(swap_other_data.begin(), swap_other_data.end()); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_swap_different_capacity) + { + const size_t other_size = 6; + Data etl_data(swap_data.begin(), swap_data.end()); + etl::vector etl_data2(swap_other_data.begin(), swap_other_data.end()); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == other_size); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_swap_const_different_capacity) + { + const size_t other_size = 6; + CData etl_data(swap_data.begin(), swap_data.end()); + etl::vector etl_data2(swap_other_data.begin(), swap_other_data.end()); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == other_size); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + } + + //************************************************************************* + TEST(test_swap_insufficient_capacity) + { + etl::vector etl_data(4); + etl::vector etl_data2(6); + + CHECK_THROW(etl_data.swap(etl_data2), etl::vector_full); + CHECK_THROW(etl_data2.swap(etl_data), etl::vector_full); + } + + //************************************************************************* + TEST(test_swap_const_insufficient_capacity) + { + etl::vector etl_data(4); + etl::vector etl_data2(6); + + CHECK_THROW(etl_data.swap(etl_data2), etl::vector_full); + CHECK_THROW(etl_data2.swap(etl_data), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_etl_swap_different_capacity) + { + const size_t other_size = 6; + Data etl_data(swap_data.begin(), swap_data.end()); + etl::vector etl_data2(swap_other_data.begin(), swap_other_data.end()); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + + etl::swap(etl_data, etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == other_size); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl::swap(etl_data, etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_etl_swap_const_different_capacity) + { + const size_t other_size = 6; + CData etl_data(swap_data.begin(), swap_data.end()); + etl::vector etl_data2(swap_other_data.begin(), swap_other_data.end()); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + + etl::swap(etl_data, etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == other_size); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl::swap(etl_data, etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + } } } diff --git a/test/test_vector_pointer_external_buffer.cpp b/test/test_vector_pointer_external_buffer.cpp index e6be4831..7fa4af32 100644 --- a/test/test_vector_pointer_external_buffer.cpp +++ b/test/test_vector_pointer_external_buffer.cpp @@ -62,6 +62,8 @@ namespace Compare_Data shorter_data; Compare_Data different_data; Compare_Data insert_data; + Compare_Data swap_data; + Compare_Data swap_other_data; //************************************************************************* struct SetupFixture @@ -87,6 +89,8 @@ namespace int* n_insert[] = { &n11, &n12, &n13 }; int* n_less[] = { &n0, &n1, &n2, &n3, &n3, &n5, &n6, &n7, &n8, &n9 }; int* n_greater[] = { &n0, &n1, &n2, &n4, &n4, &n5, &n6, &n7, &n8, &n9 }; + int* n_swap[] = { &n0, &n1, &n2, &n3, &n4, &n5 }; + int* n_swap_other[] = { &n6, &n7, &n8, &n9 }; initial_data.assign(std::begin(n), std::end(n)); insert_data.assign(std::begin(n_insert), std::end(n_insert)); @@ -94,6 +98,8 @@ namespace greater_data.assign(std::begin(n_greater), std::end(n_greater)); shorter_data.assign(std::begin(n_greater), std::end(n_greater) - 1); different_data.assign(initial_data.rbegin(), initial_data.rend()); + swap_data.assign(std::begin(n_swap), std::end(n_swap)); + swap_other_data.assign(std::begin(n_swap_other), std::end(n_swap_other)); std::fill_n(buffer1, SIZE, nullptr); std::fill_n(buffer2, SIZE, nullptr); @@ -2013,5 +2019,222 @@ namespace CHECK(i1 == *i2); CHECK(&i1 == i2); } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_swap_same_capacity) + { + Data etl_data(swap_data.begin(), swap_data.end(), buffer1, SIZE); + Data etl_data2(swap_other_data.begin(), swap_other_data.end(), buffer2, SIZE); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_swap_const_same_capacity) + { + CData etl_data(swap_data.begin(), swap_data.end(), buffer1, SIZE); + CData etl_data2(swap_other_data.begin(), swap_other_data.end(), buffer2, SIZE); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == SIZE); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_swap_different_capacity) + { + const size_t other_size = 6; + Data etl_data(swap_data.begin(), swap_data.end(), buffer1, SIZE); + Data etl_data2(swap_other_data.begin(), swap_other_data.end(), buffer2, other_size); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == other_size); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_swap_const_different_capacity) + { + const size_t other_size = 6; + CData etl_data(swap_data.begin(), swap_data.end(), buffer1, SIZE); + CData etl_data2(swap_other_data.begin(), swap_other_data.end(), buffer2, other_size); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == other_size); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + } + + //************************************************************************* + TEST(test_swap_insufficient_capacity) + { + Data etl_data(4, buffer1, 4); + etl::vector etl_data2(6); + + CHECK_THROW(etl_data.swap(etl_data2), etl::vector_full); + CHECK_THROW(etl_data2.swap(etl_data), etl::vector_full); + } + + //************************************************************************* + TEST(test_swap_const_insufficient_capacity) + { + CData etl_data(4, buffer1, 4); + etl::vector etl_data2(6); + + CHECK_THROW(etl_data.swap(etl_data2), etl::vector_full); + CHECK_THROW(etl_data2.swap(etl_data), etl::vector_full); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_swap_between_internal_and_external) + { + const size_t other_size = 6; + Data etl_data(swap_data.begin(), swap_data.end(), buffer1, SIZE); + etl::vector etl_data2(swap_other_data.begin(), swap_other_data.end()); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == other_size); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_swap_const_between_internal_and_external) + { + const size_t other_size = 6; + CData etl_data(swap_data.begin(), swap_data.end(), buffer1, SIZE); + etl::vector etl_data2(swap_other_data.begin(), swap_other_data.end()); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_data.size()); + CHECK(etl_data2.max_size() == other_size); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_other_data.size()); + CHECK(etl_data.max_size() == SIZE); + + etl_data.swap(etl_data2); + + CHECK(std::equal(swap_data.begin(), swap_data.end(), etl_data.begin())); + CHECK(etl_data.size() == swap_data.size()); + CHECK(etl_data.max_size() == SIZE); + CHECK(std::equal(swap_other_data.begin(), swap_other_data.end(), etl_data2.begin())); + CHECK(etl_data2.size() == swap_other_data.size()); + CHECK(etl_data2.max_size() == other_size); + } } }