From c6fecd9d7cec89142acc3f30d9748520884cec83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0smail=20T=C3=BCz=C3=BCn?= Date: Fri, 24 Nov 2023 14:24:52 +0300 Subject: [PATCH] Set is full error when adding existing item (#782) * Fixed set_full error when inserting existing item. * Added test inserting existing value to full set * Added test inserting existing value to full flat_set and reference_flat_set * Fixed unordered_set_full error when inserting existing item. --- include/etl/set.h | 56 ++++++++++++++++++++++++++++---- include/etl/unordered_set.h | 30 +++++++++++++++-- test/test_flat_set.cpp | 30 +++++++++++++++++ test/test_reference_flat_set.cpp | 30 +++++++++++++++++ test/test_set.cpp | 45 +++++++++++++++++++++++++ test/test_unordered_set.cpp | 31 ++++++++++++++++++ 6 files changed, 214 insertions(+), 8 deletions(-) diff --git a/include/etl/set.h b/include/etl/set.h index 6825d78c1..b3babaa2e 100644 --- a/include/etl/set.h +++ b/include/etl/set.h @@ -1115,8 +1115,19 @@ namespace etl Node* inserted_node = ETL_NULLPTR; bool inserted = false; - ETL_ASSERT(!full(), ETL_ERROR(set_full)); - + if (full()) + { + iterator iter = find(value); + if (iter == end()) + { + ETL_ASSERT_FAIL(ETL_ERROR(set_full)); + } + else + { + return ETL_OR_STD::make_pair(iter, false); + } + } + // Get next available free node Data_Node& node = allocate_data_node(value); @@ -1139,8 +1150,19 @@ namespace etl // Default to no inserted node Node* inserted_node = ETL_NULLPTR; bool inserted = false; - - ETL_ASSERT(!full(), ETL_ERROR(set_full)); + + if (full()) + { + iterator iter = find(value); + if (iter == end()) + { + ETL_ASSERT_FAIL(ETL_ERROR(set_full)); + } + else + { + return ETL_OR_STD::make_pair(iter, false); + } + } // Get next available free node Data_Node& node = allocate_data_node(etl::move(value)); @@ -1165,7 +1187,18 @@ namespace etl // Default to no inserted node Node* inserted_node = ETL_NULLPTR; - ETL_ASSERT(!full(), ETL_ERROR(set_full)); + if (full()) + { + iterator iter = find(value); + if (iter == end()) + { + ETL_ASSERT_FAIL(ETL_ERROR(set_full)); + } + else + { + return iter; + } + } // Get next available free node Data_Node& node = allocate_data_node(value); @@ -1189,7 +1222,18 @@ namespace etl // Default to no inserted node Node* inserted_node = ETL_NULLPTR; - ETL_ASSERT(!full(), ETL_ERROR(set_full)); + if (full()) + { + iterator iter = find(value); + if (iter == end()) + { + ETL_ASSERT_FAIL(ETL_ERROR(set_full)); + } + else + { + return iter; + } + } // Get next available free node Data_Node& node = allocate_data_node(etl::move(value)); diff --git a/include/etl/unordered_set.h b/include/etl/unordered_set.h index 788de85f9..35dce097c 100644 --- a/include/etl/unordered_set.h +++ b/include/etl/unordered_set.h @@ -652,7 +652,20 @@ namespace etl { ETL_OR_STD::pair result(end(), false); - ETL_ASSERT(!full(), ETL_ERROR(unordered_set_full)); + if (full()) + { + iterator iter = find(key); + if (iter == end()) + { + ETL_ASSERT_FAIL(ETL_ERROR(unordered_set_full)); + } + else + { + result.first = iter; + result.second = false; + return result; + } + } // Get the hash index. size_t index = get_bucket_index(key); @@ -727,7 +740,20 @@ namespace etl { ETL_OR_STD::pair result(end(), false); - ETL_ASSERT(!full(), ETL_ERROR(unordered_set_full)); + if (full()) + { + iterator iter = find(key); + if (iter == end()) + { + ETL_ASSERT_FAIL(ETL_ERROR(unordered_set_full)); + } + else + { + result.first = iter; + result.second = false; + return result; + } + } // Get the hash index. size_t index = get_bucket_index(key); diff --git a/test/test_flat_set.cpp b/test/test_flat_set.cpp index 19e6d0398..873539a97 100644 --- a/test/test_flat_set.cpp +++ b/test/test_flat_set.cpp @@ -613,6 +613,36 @@ namespace CHECK(std::is_sorted(data.begin(), data.end())); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_existing_value_when_full) + { + DataNDC data; + + data.insert(N0); + data.insert(N1); + data.insert(N2); + data.insert(N3); + data.insert(N4); + data.insert(N5); + data.insert(N6); + data.insert(N7); + data.insert(N8); + data.insert(N9); + + CHECK_NO_THROW(data.insert(N0)); + CHECK_NO_THROW(data.insert(N1)); + CHECK_NO_THROW(data.insert(N2)); + CHECK_NO_THROW(data.insert(N3)); + CHECK_NO_THROW(data.insert(N4)); + CHECK_NO_THROW(data.insert(N5)); + CHECK_NO_THROW(data.insert(N6)); + CHECK_NO_THROW(data.insert(N7)); + CHECK_NO_THROW(data.insert(N8)); + CHECK_NO_THROW(data.insert(N9)); + + CHECK(std::is_sorted(data.begin(), data.end())); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_emplace_default_value) { diff --git a/test/test_reference_flat_set.cpp b/test/test_reference_flat_set.cpp index a48582976..bfe8cdb19 100644 --- a/test/test_reference_flat_set.cpp +++ b/test/test_reference_flat_set.cpp @@ -363,6 +363,36 @@ namespace CHECK(std::is_sorted(data.begin(), data.end())); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_existing_value_when_full) + { + DataNDC data; + + data.insert(N0); + data.insert(N1); + data.insert(N2); + data.insert(N3); + data.insert(N4); + data.insert(N5); + data.insert(N6); + data.insert(N7); + data.insert(N8); + data.insert(N9); + + CHECK_NO_THROW(data.insert(N0)); + CHECK_NO_THROW(data.insert(N1)); + CHECK_NO_THROW(data.insert(N2)); + CHECK_NO_THROW(data.insert(N3)); + CHECK_NO_THROW(data.insert(N4)); + CHECK_NO_THROW(data.insert(N5)); + CHECK_NO_THROW(data.insert(N6)); + CHECK_NO_THROW(data.insert(N7)); + CHECK_NO_THROW(data.insert(N8)); + CHECK_NO_THROW(data.insert(N9)); + + CHECK(std::is_sorted(data.begin(), data.end())); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_erase_key) { diff --git a/test/test_set.cpp b/test/test_set.cpp index aa25fc2a6..41be506c8 100644 --- a/test/test_set.cpp +++ b/test/test_set.cpp @@ -594,6 +594,51 @@ namespace CHECK_EQUAL(4, data.find(ItemM(4))->value); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_existing_value_when_full) + { + Compare_Data compare_data; + Data data; + ETL_OR_STD::pair data_result; + ETL_OR_STD::pair compare_result; + + for (size_t i = 0; i < MAX_SIZE; ++i) + { + data_result = data.insert(i); + compare_result = compare_data.insert(i); + + // Check that both return successful return results + CHECK_EQUAL(*data_result.first, *compare_result.first); + } + + // Try to insert when set is full should throw etl::set_full + try + { + data_result = data.insert(MAX_SIZE); + } + catch(const etl::set_full &e) + {} + + // Try adding a duplicate (should return iterator pointing to duplicate) not throw error + for (size_t i = 0; i < MAX_SIZE; ++i) + { + data_result = data.insert(i); + compare_result = compare_data.insert(i); + + // Check that both return successful return results + CHECK_EQUAL(*data_result.first, *compare_result.first); + } + + + + // Check that elements in set are the same + bool isEqual = Check_Equal(data.begin(), + data.end(), + compare_data.begin()); + CHECK(isEqual); + + } + //************************************************************************* //TEST_FIXTURE(SetupFixture, test_emplace_value) //{ diff --git a/test/test_unordered_set.cpp b/test/test_unordered_set.cpp index a5fe32c84..8ef20ad3f 100644 --- a/test/test_unordered_set.cpp +++ b/test/test_unordered_set.cpp @@ -464,6 +464,37 @@ namespace CHECK_EQUAL(4, data.find(ItemM(4))->value); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_existing_value_when_full) + { + DataNDC data; + + data.insert(N0); // Inserted + data.insert(N1); // Inserted + data.insert(N2); // Inserted + data.insert(N3); // Inserted + data.insert(N4); // Inserted + data.insert(N5); // Inserted + data.insert(N6); // Inserted + data.insert(N7); // Inserted + data.insert(N8); // Inserted + data.insert(N9); // Inserted + + // Try to insert existing item when unordered_set is full should not fail + CHECK_NO_THROW(data.insert(N0)); + CHECK_NO_THROW(data.insert(N1)); + CHECK_NO_THROW(data.insert(N2)); + CHECK_NO_THROW(data.insert(N3)); + CHECK_NO_THROW(data.insert(N4)); + CHECK_NO_THROW(data.insert(N5)); + CHECK_NO_THROW(data.insert(N6)); + CHECK_NO_THROW(data.insert(N7)); + CHECK_NO_THROW(data.insert(N8)); + CHECK_NO_THROW(data.insert(N9)); + + CHECK(data.size() == SIZE); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_erase_key) {