Skip to content

Commit

Permalink
Fixed unordered_set_full error when inserting existing item.
Browse files Browse the repository at this point in the history
  • Loading branch information
ismailtzn committed Nov 4, 2023
1 parent 4a926dd commit a4e0650
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
30 changes: 28 additions & 2 deletions include/etl/unordered_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,20 @@ namespace etl
{
ETL_OR_STD::pair<iterator, bool> 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);
Expand Down Expand Up @@ -727,7 +740,20 @@ namespace etl
{
ETL_OR_STD::pair<iterator, bool> 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);
Expand Down
31 changes: 31 additions & 0 deletions test/test_unordered_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit a4e0650

Please sign in to comment.