11#include <boost/test/unit_test.hpp>
15#include <unordered_map>
26 size_t expected_bytes_available = resource.ChunkSizeBytes();
30 void* block = resource.Allocate(8, 8);
31 expected_bytes_available -= 8;
35 resource.Deallocate(block, 8, 8);
40 void* b = resource.Allocate(8, 1);
41 BOOST_TEST(b == block);
45 resource.Deallocate(block, 8, 1);
51 b = resource.Allocate(8, 16);
52 BOOST_TEST(b != block);
58 resource.Deallocate(block, 8, 16);
64 block = resource.Allocate(16, 8);
69 resource.Deallocate(block, 16, 8);
76 void* p = resource.Allocate(0, 1);
80 resource.Deallocate(p, 0, 1);
91 uint8_t num_allocs = 200;
93 auto data = std::vector<Span<uint8_t>>();
96 for (uint8_t num_bytes = 0; num_bytes < num_allocs; ++num_bytes) {
97 uint8_t* bytes =
new (resource.Allocate(num_bytes, 1)) uint8_t[num_bytes];
98 BOOST_TEST(bytes !=
nullptr);
99 data.emplace_back(bytes, num_bytes);
102 std::fill(bytes, bytes + num_bytes, num_bytes);
107 for (
auto const& span :
data) {
108 for (
auto x : span) {
109 BOOST_TEST(val == x);
111 std::destroy(span.data(), span.data() + span.size());
112 resource.Deallocate(span.data(), span.size(), 1);
121 struct PtrSizeAlignment {
129 std::vector<PtrSizeAlignment> ptr_size_alignment{};
130 for (
size_t i = 0; i < 1000; ++i) {
132 if (ptr_size_alignment.empty() || 0 != m_rng.randrange(4)) {
134 std::size_t alignment = std::size_t{1} << m_rng.randrange(8);
135 std::size_t size = (m_rng.randrange(200) / alignment + 1) * alignment;
136 void* ptr = resource.Allocate(size, alignment);
137 BOOST_TEST(ptr !=
nullptr);
138 BOOST_TEST((
reinterpret_cast<uintptr_t
>(ptr) & (alignment - 1)) == 0);
139 ptr_size_alignment.push_back({ptr, size, alignment});
142 auto& x = ptr_size_alignment[m_rng.randrange(ptr_size_alignment.size())];
143 resource.Deallocate(x.ptr, x.bytes, x.alignment);
144 x = ptr_size_alignment.back();
145 ptr_size_alignment.pop_back();
150 for (
auto const& x : ptr_size_alignment) {
151 resource.Deallocate(x.ptr, x.bytes, x.alignment);
159 auto std_map = std::unordered_map<int64_t, int64_t>{};
161 using Map = std::unordered_map<int64_t,
164 std::equal_to<int64_t>,
166 sizeof(std::pair<const int64_t, int64_t>) +
sizeof(
void*) * 4>>;
167 auto resource = Map::allocator_type::ResourceType(1024);
172 auto resource_map = Map{0, std::hash<int64_t>{}, std::equal_to<int64_t>{}, &resource};
177 for (
size_t i = 0; i < 10000; ++i) {
186 auto max_nodes_per_chunk = resource.ChunkSizeBytes() /
sizeof(Map::value_type);
187 auto min_num_allocated_chunks = resource_map.size() / max_nodes_per_chunk + 1;
188 BOOST_TEST(resource.NumAllocatedChunks() >= min_num_allocated_chunks);
Forwards all allocations/deallocations to the PoolResource.
A memory resource similar to std::pmr::unsynchronized_pool_resource, but optimized for node-based con...
static void CheckAllDataAccountedFor(const PoolResource< MAX_BLOCK_SIZE_BYTES, ALIGN_BYTES > &resource)
Once all blocks are given back to the resource, tests that the freelists are consistent:
static std::size_t AvailableMemoryFromChunk(const PoolResource< MAX_BLOCK_SIZE_BYTES, ALIGN_BYTES > &resource)
How many bytes are still available from the last allocated chunk.
static std::vector< std::size_t > FreeListSizes(const PoolResource< MAX_BLOCK_SIZE_BYTES, ALIGN_BYTES > &resource)
Extracts the number of elements per freelist.
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
BOOST_AUTO_TEST_CASE(basic_allocating)