Skip to content

Commit

Permalink
Add shrink_to_fit tests to small_vector
Browse files Browse the repository at this point in the history
  • Loading branch information
igaztanaga committed Jun 2, 2024
1 parent dad179d commit aa35950
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion test/map_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ int map_test_range()

::boost::movelib::unique_ptr<MyBoostMultiMap> const pboostmultimap = ::boost::movelib::make_unique<MyBoostMultiMap>
( boost::make_move_iterator(&aux_vect3[0])
, boost::make_move_iterator(&aux_vect3[0] + MaxElem), typename MyBoostMap::allocator_type());
, boost::make_move_iterator(&aux_vect3[0] + MaxElem));
::boost::movelib::unique_ptr<MyStdMultiMap> const pstdmultimap = ::boost::movelib::make_unique<MyStdMultiMap>
(&aux_vect2[0], &aux_vect2[0] + MaxElem, typename MyStdMap::key_compare());
if(!CheckEqualContainers(*pboostmultimap, *pstdmultimap)) return 1;
Expand Down
67 changes: 66 additions & 1 deletion test/small_vector_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,71 @@ struct alloc_propagate_base<boost_container_small_vector>

}}} //namespace boost::container::test


bool test_small_vector_shrink_to_fit()
{
boost::container::small_vector<int, 5> sm5;
boost::container::vector<int> v;
sm5.push_back(1);
sm5.push_back(2);
sm5.push_back(3);
sm5.push_back(4);
sm5.push_back(5);

v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);

if (!sm5.is_small())
return false;
if (!boost::container::algo_equal(sm5.begin(), sm5.end(), v.begin(), v.end()))
return false;

//Shrinking a when internal storage is used is a no-op
sm5.shrink_to_fit();

if (!sm5.is_small())
return false;
if (!boost::container::algo_equal(sm5.begin(), sm5.end(), v.begin(), v.end()))
return false;

//If dynamic memory is used, shrink_to_fit will move elements to the internal storage
sm5.push_back(6);
v.push_back(6);
if (sm5.is_small())
return false;
sm5.pop_back();
v.pop_back();
if (sm5.is_small())
return false;

sm5.shrink_to_fit();
if (!sm5.is_small())
return false;
if (!boost::container::algo_equal(sm5.begin(), sm5.end(), v.begin(), v.end()))
return false;

//If dynamic memory is used, and size is zero the dynamic storage is deallocated
sm5.push_back(6);
v.push_back(6);
if (sm5.is_small())
return false;
sm5.clear();
v.clear();
if (sm5.is_small())
return false;

sm5.shrink_to_fit();
if (!sm5.is_small())
return false;
if (!boost::container::algo_equal(sm5.begin(), sm5.end(), v.begin(), v.end()))
return false;

return true;
}

bool test_small_vector_base_test()
{
typedef boost::container::small_vector_base<int> smb_t;
Expand Down Expand Up @@ -452,7 +517,7 @@ int main()
////////////////////////////////////
// Small vector base
////////////////////////////////////
if (!test_small_vector_base_test()){
if (!test_small_vector_shrink_to_fit()){
return 1;
}

Expand Down

0 comments on commit aa35950

Please sign in to comment.