libcruft-util/test/pool.cpp

135 lines
3.6 KiB
C++
Raw Normal View History

#include "../pool.hpp"
2016-01-19 18:31:49 +11:00
#include "../tap.hpp"
#include <set>
#include <vector>
#include <algorithm>
2016-01-19 18:31:49 +11:00
//-----------------------------------------------------------------------------
void
check_single (cruft::TAP::logger &tap)
2016-01-19 18:31:49 +11:00
{
// Ensure a single element doesn't break the circular linked list
cruft::pool<uint64_t> single(1);
2016-01-19 18:31:49 +11:00
tap.expect_nothrow ([&] {
single.deallocate (single.allocate ());
2016-01-19 18:31:49 +11:00
}, "single element acquire-release");
}
2016-01-19 18:31:49 +11:00
//-----------------------------------------------------------------------------
void
check_unique_ptr (cruft::TAP::logger &tap)
2016-01-19 18:31:49 +11:00
{
cruft::pool<uint64_t> uintpool (1025);
2016-01-19 18:31:49 +11:00
std::set<uint64_t *> uintset;
// Take all pointers out, checking they are unique, then replace for destruction.
2016-01-19 18:31:49 +11:00
while (!uintpool.empty ())
uintset.insert (uintpool.allocate ());
2016-01-19 18:31:49 +11:00
tap.expect_eq (uintset.size (), uintpool.capacity (), "extracted maximum elements");
for (auto i: uintset)
uintpool.deallocate (i);
2016-01-19 18:31:49 +11:00
tap.expect_eq (uintpool.size (), 0u, "re-inserted maximum elements");
uintset.clear ();
// Do the above one more time to ensure that releasing works right
2016-01-19 18:31:49 +11:00
while (!uintpool.empty ())
uintset.insert (uintpool.allocate ());
2016-01-19 18:31:49 +11:00
tap.expect_eq (uintset.size (), uintpool.capacity (), "re-extracted maximum elements");
}
2016-01-19 18:31:49 +11:00
//-----------------------------------------------------------------------------
void
check_keep_value (cruft::TAP::logger &tap)
2016-01-19 18:31:49 +11:00
{
// Ensure that items keep their values.
cruft::pool<uint64_t> uintpool(256);
std::vector<uint64_t*> uintvector;
uintvector.reserve(uintpool.capacity ());
// Give every item a unique value
for (unsigned int i = 0; i < uintpool.capacity (); ++i) {
uint64_t *item = uintpool.allocate ();
*item = i;
uintvector.push_back(item);
}
2016-01-19 18:31:49 +11:00
CHECK_EQ (uintvector.size (), uintpool.capacity ());
// Ensure they're all still present
2016-01-19 18:31:49 +11:00
std::vector<bool> present(uintpool.capacity (), false);
for (auto i = uintvector.begin (); i != uintvector.end (); ++i) {
CHECK (**i < uintpool.capacity ());
CHECK (present[**i] != true);
present[**i] = true;
}
// All must have been marked as present...
2016-01-19 18:31:49 +11:00
tap.expect (std::find (present.begin (), present.end (), false) == present.end (), "values retained");
// Release all back into the pool for destruction
2016-01-19 18:31:49 +11:00
//for (auto i = uintvector.begin (); i != uintvector.end (); ++i)
// uintpool.release (*i);
//uintvector.clear ();
}
//-----------------------------------------------------------------------------
void
check_keep_variadic_value (cruft::TAP::logger &tap)
{
struct foo_t {
foo_t (uint64_t _a, uint64_t _b, uint64_t _c, uint64_t _d):
a (_a),
b (_b),
c (_c),
d (_d)
{ ; }
uint64_t a, b, c, d;
};
cruft::pool<foo_t> alloc (512);
bool success = true;
for (uint64_t a = 0; a < 8; ++a)
for (uint64_t b = 0; b < 8; ++b)
for (uint64_t c = 0; c < 8; ++c) {
uint64_t d = (a << 24) | (b << 16) | (c << 8);
auto ptr = alloc.construct (a, b, c, d);
if (ptr->a != a || ptr->b != b || ptr->c != c || ptr->d != d) {
success = false;
goto done;
}
}
done:
tap.expect (success, "variadiac construction retains values");
}
2016-01-19 18:31:49 +11:00
//-----------------------------------------------------------------------------
2015-04-13 18:06:08 +10:00
int
2016-01-19 18:31:49 +11:00
main (int, char **)
{
cruft::TAP::logger tap;
2016-01-19 18:31:49 +11:00
check_single (tap);
check_unique_ptr (tap);
check_keep_value (tap);
check_keep_variadic_value (tap);
2016-01-19 18:31:49 +11:00
return tap.status ();
}