forked from foonathan/memory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart_ptr.cpp
More file actions
53 lines (44 loc) · 1.41 KB
/
Copy pathsmart_ptr.cpp
File metadata and controls
53 lines (44 loc) · 1.41 KB
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
53
// Copyright (C) 2018 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#include "smart_ptr.hpp"
#include <catch.hpp>
#include "container.hpp"
#include "memory_pool.hpp"
using namespace foonathan::memory;
struct dummy_allocator
{
static std::size_t size;
void* allocate_node(std::size_t size, std::size_t)
{
size = size;
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", "[adapter]")
{
SECTION("stateless")
{
dummy_allocator::size = 0;
auto ptr = allocate_shared<int>(dummy_allocator{}, 42);
REQUIRE(*ptr == 42);
REQUIRE((dummy_allocator::size <= allocate_shared_node_size<int, dummy_allocator>::value));
}
SECTION("stateful no mutex")
{
memory_pool<> pool(allocate_shared_node_size<int, memory_pool<>, no_mutex>::value, 1024);
auto ptr = allocate_shared<int, no_mutex>(pool, 42);
REQUIRE(*ptr == 42);
}
SECTION("stateful mutex")
{
memory_pool<> pool(allocate_shared_node_size<int, memory_pool<>>::value, 1024);
auto ptr = allocate_shared<int>(pool, 42);
REQUIRE(*ptr == 42);
}
}