2011-07-01 13:46:39 +10:00
|
|
|
|
|
|
|
#include "debug.hpp"
|
|
|
|
#include "pool.hpp"
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using namespace std;
|
2011-11-04 17:28:04 +11:00
|
|
|
using namespace util;
|
2011-07-01 13:46:39 +10:00
|
|
|
|
|
|
|
void
|
|
|
|
check_single (void) {
|
|
|
|
// Ensure a single element doesn't break the circular linked list
|
|
|
|
pool<uint64_t> single(1);
|
|
|
|
single.release (single.acquire ());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
check_unique_ptr (void) {
|
|
|
|
pool<uint64_t> uintpool (1025);
|
|
|
|
set<uint64_t *> 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;
|
2012-05-11 12:34:21 +10:00
|
|
|
CHECK_HARD (success);
|
2011-07-01 13:46:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2012-05-11 12:34:21 +10:00
|
|
|
CHECK_HARD (success);
|
2011-07-01 13:46:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto i = uintset.begin (); i != uintset.end (); ++i)
|
|
|
|
uintpool.release (*i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
check_keep_value (void) {
|
|
|
|
// Ensure that items keep their values.
|
|
|
|
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) {
|
2012-04-19 16:54:36 +10:00
|
|
|
uint64_t *item = uintpool.acquire ();
|
|
|
|
*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
|
|
|
}
|
2012-05-11 12:34:21 +10:00
|
|
|
CHECK (uintvector.size () == uintpool.capacity ());
|
2011-07-01 13:46:39 +10:00
|
|
|
|
|
|
|
// Ensure they're all still present
|
|
|
|
vector<bool> present(uintpool.capacity (), false);
|
|
|
|
for (auto i = uintvector.begin (); i != uintvector.end (); ++i) {
|
2012-05-11 12:34:21 +10:00
|
|
|
CHECK_HARD (**i < uintpool.capacity ());
|
|
|
|
CHECK_HARD (present[**i] != true);
|
2011-07-01 13:46:39 +10:00
|
|
|
|
|
|
|
present[**i] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All must have been marked as present...
|
2012-05-11 12:34:21 +10:00
|
|
|
CHECK_HARD (find (present.begin (), present.end (), false) == present.end ());
|
2011-07-01 13:46:39 +10:00
|
|
|
|
|
|
|
// Release all back into the pool for destruction
|
|
|
|
for (auto i = uintvector.begin (); i != uintvector.end (); ++i)
|
|
|
|
uintpool.release (*i);
|
|
|
|
uintvector.clear ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2012-04-19 16:54:36 +10:00
|
|
|
main (int, char **) {
|
2011-07-01 13:46:39 +10:00
|
|
|
check_single ();
|
|
|
|
check_unique_ptr ();
|
|
|
|
check_keep_value ();
|
|
|
|
}
|