File: smart_ptr.cpp

package info (click to toggle)
foonathan-memory 0.7-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,748 kB
  • sloc: cpp: 12,014; xml: 139; sh: 49; makefile: 22
file content (53 lines) | stat: -rw-r--r-- 1,404 bytes parent folder | download
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 <foonathan/memory/smart_ptr.hpp>

#include <catch.hpp>

#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", "[adapter]")
{
    SECTION("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
    }
    SECTION("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);
    }
}