#include "debug.hpp" #include "pool.hpp" #include #include #include using namespace std; using namespace util; void check_single (void) { // Ensure a single element doesn't break the circular linked list pool single(1); single.release (single.acquire ()); } void check_unique_ptr (void) { pool uintpool (1025); set uintset; // Take all pointers out, checking they are unique, then replace for destruction. for (unsigned int i = 0; i < uintpool.capacity (); ++i) { bool success = uintset.insert (uintpool.acquire ()).second; CHECK_HARD (success); } for (auto i = uintset.begin (); i != uintset.end (); ++i) uintpool.release (*i); uintset.clear (); // Do the above one more time to ensure that releasing works right for (unsigned int i = 0; i < uintpool.capacity (); ++i) { bool success = uintset.insert (uintpool.acquire ()).second; CHECK_HARD (success); } for (auto i = uintset.begin (); i != uintset.end (); ++i) uintpool.release (*i); } void check_keep_value (void) { // Ensure that items keep their values. pool uintpool(256); std::vector uintvector; uintvector.reserve(uintpool.capacity ()); // Give every item a unique value for (unsigned int i = 0; i < uintpool.capacity (); ++i) { uint64_t *item = uintpool.acquire (); *item = i; uintvector.push_back(item); } CHECK (uintvector.size () == uintpool.capacity ()); // Ensure they're all still present vector present(uintpool.capacity (), false); for (auto i = uintvector.begin (); i != uintvector.end (); ++i) { CHECK_HARD (**i < uintpool.capacity ()); CHECK_HARD (present[**i] != true); present[**i] = true; } // All must have been marked as present... CHECK_HARD (find (present.begin (), present.end (), false) == present.end ()); // Release all back into the pool for destruction for (auto i = uintvector.begin (); i != uintvector.end (); ++i) uintpool.release (*i); uintvector.clear (); } int main (int, char **) { check_single (); check_unique_ptr (); check_keep_value (); }