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 void
check_keep_value (cruft::TAP::logger &tap) check_keep_value (cruft::TAP::logger &tap)
{ {
// Ensure that items keep their values. // Allocate a source pool, and a storage vector.
cruft::pool<std::size_t> uintpool(256); cruft::pool<std::size_t > uintpool (64);
std::vector<std::size_t*> uintvector; std::vector<std::size_t*> uintvector;
uintvector.reserve(uintpool.capacity ()); uintvector.reserve(uintpool.capacity ());
// Give every item a unique value // Generate a list of pointers to ints, then fill them with sequential values.
for (std::size_t i = 0; i < uintpool.capacity (); ++i) { //
std::size_t *item = uintpool.allocate (); // Do this as a two step process rather than all at once. This separates
*item = i; // 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 ()); CHECK_EQ (uintvector.size (), uintpool.capacity ());