test/pool: make allocation tests more robust against allocator issues

This commit is contained in:
Danny Robson 2019-05-24 10:57:35 +10:00
parent 34a5f7d52b
commit f3e0744e18

View File

@ -48,21 +48,29 @@ check_unique_ptr (cruft::TAP::logger &tap)
//-----------------------------------------------------------------------------
// Test that every element in a pool points to a unique location.
void
check_keep_value (cruft::TAP::logger &tap)
{
// Ensure that items keep their values.
cruft::pool<std::size_t> uintpool(256);
// Allocate a source pool, and a storage vector.
cruft::pool<std::size_t > uintpool (64);
std::vector<std::size_t*> uintvector;
uintvector.reserve(uintpool.capacity ());
// Give every item a unique value
for (std::size_t i = 0; i < uintpool.capacity (); ++i) {
std::size_t *item = uintpool.allocate ();
*item = i;
// Generate a list of pointers to ints, then fill them with sequential values.
//
// Do this as a two step process rather than all at once. This separates
// the concerns of performing the allocation from whether the allocation
// points to a safe area. There's a tendency for the pool and vector to be
// adjacent in memory and overruns in the former impact the latter.
std::generate_n (
std::back_inserter (uintvector),
uintpool.capacity (),
[&] () { return uintpool.allocate (); }
);
uintvector.push_back(item);
}
for (std::size_t i = 0; i < uintpool.capacity (); ++i)
*uintvector[i] = i;
CHECK_EQ (uintvector.size (), uintpool.capacity ());