File: joint_allocator.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 (278 lines) | stat: -rw-r--r-- 7,988 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (C) 2016 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/joint_allocator.hpp>

#include <catch.hpp>

#include <foonathan/memory/container.hpp>
#include "test_allocator.hpp"

using namespace foonathan::memory;

template <typename T, class RawAllocator>
void verify(const joint_ptr<T, RawAllocator>& ptr, const RawAllocator& alloc, int value)
{
    REQUIRE(ptr);
    REQUIRE(ptr.get());
    REQUIRE(ptr.get() == ptr.operator->());
    REQUIRE(ptr.get() == &*ptr);
    REQUIRE(&ptr.get_allocator() == &alloc);
    REQUIRE(ptr->value == value);
}

template <typename T, class RawAllocator>
void verify_null(const joint_ptr<T, RawAllocator>& ptr, const RawAllocator& alloc)
{
    REQUIRE(!ptr);
    REQUIRE(ptr.get() == nullptr);
    REQUIRE(&ptr.get_allocator() == &alloc);
}

TEST_CASE("joint_ptr", "[allocator]")
{
    struct joint_test : joint_type<joint_test>
    {
        int value;

        joint_test(joint tag, int v) : joint_type(tag), value(v) {}

        joint_test(joint tag, const joint_test& other) : joint_type(tag), value(other.value) {}
    };

    test_allocator alloc;

    SECTION("allocator constructor")
    {
        joint_ptr<joint_test, test_allocator> ptr(alloc);
        verify_null(ptr, alloc);

        REQUIRE(alloc.no_allocated() == 0u);
        REQUIRE(alloc.no_deallocated() == 0u);
    }
    SECTION("creation constructor")
    {
        joint_ptr<joint_test, test_allocator> ptr(alloc, joint_size(10u), 5);
        verify(ptr, alloc, 5);

        REQUIRE(alloc.no_allocated() == 1u);
        REQUIRE(alloc.last_allocated().size == sizeof(joint_test) + 10u);
        REQUIRE(alloc.last_allocated().alignment == alignof(joint_test));
        REQUIRE(alloc.no_deallocated() == 0u);
    }
    SECTION("move constructor")
    {
        auto ptr1 = allocate_joint<joint_test>(alloc, joint_size(10u), 5);
        verify(ptr1, alloc, 5);

        auto ptr2 = std::move(ptr1);
        verify_null(ptr1, alloc);
        verify(ptr2, alloc, 5);

        auto ptr3 = std::move(ptr1);
        verify_null(ptr1, alloc);

        REQUIRE(alloc.no_allocated() == 1u);
    }
    SECTION("move assignment")
    {
        joint_ptr<joint_test, test_allocator> ptr1(alloc);
        verify_null(ptr1, alloc);

        auto ptr2 = allocate_joint<joint_test>(alloc, joint_size(10u), 5);
        verify(ptr2, alloc, 5);
        REQUIRE(alloc.no_allocated() == 1u);

        ptr1 = std::move(ptr2);
        verify_null(ptr2, alloc);
        verify(ptr1, alloc, 5);
        REQUIRE(alloc.no_allocated() == 1u);

        ptr1 = std::move(ptr2);
        verify_null(ptr1, alloc);
        verify_null(ptr2, alloc);
        REQUIRE(alloc.no_allocated() == 0u);
    }
    SECTION("swap")
    {
        joint_ptr<joint_test, test_allocator> ptr1(alloc);
        verify_null(ptr1, alloc);

        auto ptr2 = allocate_joint<joint_test>(alloc, joint_size(10u), 5);
        verify(ptr2, alloc, 5);

        swap(ptr1, ptr2);
        verify(ptr1, alloc, 5);
        verify_null(ptr2, alloc);

        REQUIRE(alloc.no_allocated() == 1u);
    }
    SECTION("reset")
    {
        joint_ptr<joint_test, test_allocator> ptr1(alloc);
        verify_null(ptr1, alloc);
        ptr1.reset();
        verify_null(ptr1, alloc);

        auto ptr2 = allocate_joint<joint_test>(alloc, joint_size(10u), 5);
        verify(ptr2, alloc, 5);
        REQUIRE(alloc.no_allocated() == 1u);

        ptr2.reset();
        verify_null(ptr2, alloc);
        REQUIRE(alloc.no_allocated() == 0u);
    }
    SECTION("compare")
    {
        joint_ptr<joint_test, test_allocator> ptr1(alloc);
        verify_null(ptr1, alloc);

        auto ptr2 = allocate_joint<joint_test>(alloc, joint_size(10u), 5);
        verify(ptr2, alloc, 5);

        REQUIRE(ptr1 == nullptr);
        REQUIRE(nullptr == ptr1);
        REQUIRE_FALSE(ptr1 != nullptr);
        REQUIRE_FALSE(nullptr != ptr1);

        REQUIRE_FALSE(ptr1 == ptr2.get());
        REQUIRE_FALSE(ptr2.get() == ptr1);
        REQUIRE(ptr1 != ptr2.get());
        REQUIRE(ptr2.get() != ptr1);

        REQUIRE_FALSE(ptr2 == nullptr);
        REQUIRE_FALSE(nullptr == ptr2);
        REQUIRE(ptr2 != nullptr);
        REQUIRE(nullptr != ptr2);

        REQUIRE(ptr2 == ptr2.get());
        REQUIRE(ptr2.get() == ptr2);
        REQUIRE_FALSE(ptr2 != ptr2.get());
        REQUIRE_FALSE(ptr2.get() != ptr2);
    }
    SECTION("clone")
    {
        auto ptr1 = allocate_joint<joint_test>(alloc, joint_size(10u), 5);
        verify(ptr1, alloc, 5);

        REQUIRE(alloc.no_allocated() == 1u);
        REQUIRE(alloc.last_allocated().size == sizeof(joint_test) + 10u);
        REQUIRE(alloc.last_allocated().alignment == alignof(joint_test));
        REQUIRE(alloc.no_deallocated() == 0u);

        auto ptr2 = clone_joint(alloc, *ptr1);
        verify(ptr2, alloc, 5);

        REQUIRE(alloc.no_allocated() == 2u);
        REQUIRE(alloc.last_allocated().size == sizeof(joint_test));
        REQUIRE(alloc.last_allocated().alignment == alignof(joint_test));
        REQUIRE(alloc.no_deallocated() == 0u);
    }

    REQUIRE(alloc.no_allocated() == 0u);
}

TEST_CASE("joint_allocator", "[allocator]")
{
    struct joint_test : joint_type<joint_test>
    {
        vector<int, joint_allocator> vec;
        int                          value;

        joint_test(joint tag, int val, std::size_t size) : joint_type(tag), vec(*this), value(val)
        {
            vec.reserve(size);
            vec.push_back(42);
            vec.push_back(-1);
        }
    };

    test_allocator alloc;

    auto ptr = allocate_joint<joint_test>(alloc, joint_size(10 * sizeof(int)), 5, 3);
    verify(ptr, alloc, 5);
    REQUIRE(ptr->vec[0] == 42);
    REQUIRE(ptr->vec[1] == -1);

    ptr->vec.push_back(5);
    REQUIRE(ptr->vec.back() == 5);

    REQUIRE(alloc.no_allocated() == 1u);
    REQUIRE(alloc.last_allocated().size == sizeof(joint_test) + 10 * sizeof(int));
    REQUIRE(alloc.last_allocated().alignment == alignof(joint_test));
}

template <typename T>
void verify(const joint_array<T>& array, std::size_t size)
{
    REQUIRE(array.data() == array.begin());
    REQUIRE(array.size() == size);
    REQUIRE(!array.empty());

    auto iter = array.begin();
    for (auto i = 0u; i != array.size(); ++i)
    {
        REQUIRE(&array[i] == array.data() + i);
        REQUIRE(&array[i] == iter);
        REQUIRE(iter - array.begin() == i);
        ++iter;
    }
}

TEST_CASE("joint_array", "[allocator]")
{
    struct joint_test : joint_type<joint_test>
    {
        int value;

        joint_test(joint tag, int v) : joint_type(tag), value(v) {}
    };

    test_allocator alloc;
    auto           ptr = allocate_joint<joint_test>(alloc, joint_size(20 * sizeof(int)), 5);
    verify(ptr, alloc, 5);

    SECTION("size constructor")
    {
        joint_array<int> arr(5, *ptr);
        verify(arr, 5);
        for (auto el : arr)
            REQUIRE(el == 0);
    }
    SECTION("size value constructor")
    {
        joint_array<int> arr(5, 1, *ptr);
        verify(arr, 5);
        for (auto el : arr)
            REQUIRE(el == 1);
    }
    SECTION("ilist constructor")
    {
        joint_array<int> arr({1, 2, 3}, *ptr);
        verify(arr, 3);
        REQUIRE(arr[0] == 1);
        REQUIRE(arr[1] == 2);
        REQUIRE(arr[2] == 3);
    }
    SECTION("iterator constructor")
    {
        int              input[] = {1, 2, 3};
        joint_array<int> arr(std::begin(input), std::end(input), *ptr);
        verify(arr, 3);
        REQUIRE(arr[0] == 1);
        REQUIRE(arr[1] == 2);
        REQUIRE(arr[2] == 3);
    }
    SECTION("copy/move constructor")
    {
        joint_array<int> arr1({1, 2, 3}, *ptr);
        verify(arr1, 3);

        joint_array<int> arr2(arr1, *ptr);
        verify(arr2, 3);
        REQUIRE(arr2[0] == 1);
        REQUIRE(arr2[1] == 2);
        REQUIRE(arr2[2] == 3);
    }
}