2017-11-22 16:49:37 +11:00
|
|
|
#include "../pool.hpp"
|
2016-01-19 18:31:49 +11:00
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "../tap.hpp"
|
2011-07-01 13:46:39 +10:00
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-07-01 13:46:39 +10:00
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
check_single (cruft::TAP::logger &tap)
|
2016-01-19 18:31:49 +11:00
|
|
|
{
|
2011-07-01 13:46:39 +10:00
|
|
|
// Ensure a single element doesn't break the circular linked list
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::pool<uint64_t> single(1);
|
2016-01-19 18:31:49 +11:00
|
|
|
tap.expect_nothrow ([&] {
|
2018-03-14 18:15:07 +11:00
|
|
|
single.deallocate (single.allocate ());
|
2016-01-19 18:31:49 +11:00
|
|
|
}, "single element acquire-release");
|
2011-07-01 13:46:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-07-01 13:46:39 +10:00
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
check_unique_ptr (cruft::TAP::logger &tap)
|
2016-01-19 18:31:49 +11:00
|
|
|
{
|
2018-12-17 12:56:07 +11:00
|
|
|
constexpr std::size_t element_count = 1025;
|
|
|
|
cruft::pool<uint64_t> uintpool (element_count);
|
2016-01-19 18:31:49 +11:00
|
|
|
std::set<uint64_t *> uintset;
|
2011-07-01 13:46:39 +10:00
|
|
|
|
|
|
|
// Take all pointers out, checking they are unique, then replace for destruction.
|
2019-01-21 17:13:47 +11:00
|
|
|
while (!uintpool.full ())
|
2018-03-14 18:15:07 +11:00
|
|
|
uintset.insert (uintpool.allocate ());
|
2011-07-01 13:46:39 +10:00
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
tap.expect_eq (uintset.size (), uintpool.capacity (), "extracted maximum elements");
|
|
|
|
|
|
|
|
for (auto i: uintset)
|
2018-03-14 18:15:07 +11:00
|
|
|
uintpool.deallocate (i);
|
2016-01-19 18:31:49 +11:00
|
|
|
|
2016-02-04 12:41:23 +11:00
|
|
|
tap.expect_eq (uintpool.size (), 0u, "re-inserted maximum elements");
|
2011-07-01 13:46:39 +10:00
|
|
|
uintset.clear ();
|
|
|
|
|
|
|
|
// Do the above one more time to ensure that releasing works right
|
2019-01-21 17:13:47 +11:00
|
|
|
while (!uintpool.full ())
|
2018-03-14 18:15:07 +11:00
|
|
|
uintset.insert (uintpool.allocate ());
|
2016-01-19 18:31:49 +11:00
|
|
|
tap.expect_eq (uintset.size (), uintpool.capacity (), "re-extracted maximum elements");
|
2011-07-01 13:46:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-07-01 13:46:39 +10:00
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
check_keep_value (cruft::TAP::logger &tap)
|
2016-01-19 18:31:49 +11:00
|
|
|
{
|
2011-07-01 13:46:39 +10:00
|
|
|
// Ensure that items keep their values.
|
2019-04-15 10:36:53 +10:00
|
|
|
cruft::pool<std::size_t> uintpool(256);
|
|
|
|
std::vector<std::size_t*> uintvector;
|
2011-07-01 13:46:39 +10:00
|
|
|
uintvector.reserve(uintpool.capacity ());
|
|
|
|
|
|
|
|
// Give every item a unique value
|
2019-04-15 10:36:53 +10:00
|
|
|
for (std::size_t i = 0; i < uintpool.capacity (); ++i) {
|
|
|
|
std::size_t *item = uintpool.allocate ();
|
2012-04-19 16:54:36 +10:00
|
|
|
*item = i;
|
2011-07-01 13:46:39 +10:00
|
|
|
|
2012-04-19 16:54:36 +10:00
|
|
|
uintvector.push_back(item);
|
2011-07-01 13:46:39 +10:00
|
|
|
}
|
2016-01-19 18:31:49 +11:00
|
|
|
|
2015-01-28 14:49:34 +11:00
|
|
|
CHECK_EQ (uintvector.size (), uintpool.capacity ());
|
2011-07-01 13:46:39 +10:00
|
|
|
|
|
|
|
// Ensure they're all still present
|
2016-01-19 18:31:49 +11:00
|
|
|
std::vector<bool> present(uintpool.capacity (), false);
|
2011-07-01 13:46:39 +10:00
|
|
|
for (auto i = uintvector.begin (); i != uintvector.end (); ++i) {
|
2015-01-28 14:49:34 +11:00
|
|
|
CHECK (**i < uintpool.capacity ());
|
|
|
|
CHECK (present[**i] != true);
|
2011-07-01 13:46:39 +10:00
|
|
|
|
|
|
|
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");
|
2011-07-01 13:46:39 +10:00
|
|
|
|
|
|
|
// 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 ();
|
2011-07-01 13:46:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-14 18:15:07 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
check_keep_variadic_value (cruft::TAP::logger &tap)
|
2018-03-14 18:15:07 +11:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::pool<foo_t> alloc (512);
|
2018-03-14 18:15:07 +11:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-21 17:13:47 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
check_size_queries (cruft::TAP::logger &tap)
|
|
|
|
{
|
|
|
|
cruft::pool<int> data (8);
|
|
|
|
|
|
|
|
tap.expect_eq (data.size (), 0u, "initial size is zero");
|
|
|
|
tap.expect (data.empty (), "initial object is empty");
|
|
|
|
|
|
|
|
auto first = data.allocate ();
|
|
|
|
tap.expect_eq (data.size (), 1u, "1 allocation has size of 1");
|
|
|
|
tap.expect (!data.empty (), "1 allocation is not empty");
|
|
|
|
|
|
|
|
data.deallocate (first);
|
|
|
|
tap.expect (data.empty (), "full deallocation is empty");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 **)
|
|
|
|
{
|
2019-01-03 15:48:34 +11:00
|
|
|
return cruft::TAP::logger::run ([] (auto &tap) {
|
|
|
|
check_single (tap);
|
|
|
|
check_unique_ptr (tap);
|
|
|
|
check_keep_value (tap);
|
|
|
|
check_keep_variadic_value (tap);
|
2019-01-21 17:13:47 +11:00
|
|
|
check_size_queries (tap);
|
2019-01-03 15:48:34 +11:00
|
|
|
});
|
2011-07-01 13:46:39 +10:00
|
|
|
}
|