Skip to content

Commit

Permalink
Merge branch 'pull-request/#782-Set-is-full-error-when-adding-existin…
Browse files Browse the repository at this point in the history
…g-item' into development
  • Loading branch information
John Wellbelove committed Nov 24, 2023
2 parents b732b14 + c6fecd9 commit 4606e33
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 8 deletions.
56 changes: 50 additions & 6 deletions include/etl/set.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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));
Expand All @@ -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);
Expand All @@ -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));
Expand Down
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
30 changes: 30 additions & 0 deletions test/test_flat_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
30 changes: 30 additions & 0 deletions test/test_reference_flat_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
45 changes: 45 additions & 0 deletions test/test_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::iterator, bool> data_result;
ETL_OR_STD::pair<Compare_Data::iterator, bool> 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)
//{
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 4606e33

Please sign in to comment.