1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors
// SPDX-License-Identifier: Zlib
#include <foonathan/memory/smart_ptr.hpp>
#include <doctest/doctest.h>
#include <foonathan/memory/container.hpp>
#include <foonathan/memory/memory_pool.hpp>
using namespace foonathan::memory;
struct dummy_allocator
{
static std::size_t size;
void* allocate_node(std::size_t s, std::size_t)
{
size = s;
return ::operator new(size);
}
void deallocate_node(void* ptr, std::size_t, std::size_t)
{
::operator delete(ptr);
}
};
std::size_t dummy_allocator::size = 0;
TEST_CASE("allocate_shared")
{
SUBCASE("stateless")
{
dummy_allocator::size = 0;
auto ptr = allocate_shared<int>(dummy_allocator{}, 42);
REQUIRE(*ptr == 42);
#if !defined(FOONATHAN_MEMORY_NO_NODE_SIZE)
REQUIRE((dummy_allocator::size <= allocate_shared_node_size<int, dummy_allocator>::value));
#endif
}
SUBCASE("stateful")
{
#if defined(FOONATHAN_MEMORY_NO_NODE_SIZE)
memory_pool<> pool(128, 1024); // hope that's enough
#else
memory_pool<> pool(allocate_shared_node_size<int, memory_pool<>>::value, 1024);
#endif
auto ptr = allocate_shared<int>(pool, 42);
REQUIRE(*ptr == 42);
}
}
|