File: iteration_allocator.cpp

package info (click to toggle)
foonathan-memory 0.7.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,644 kB
  • sloc: cpp: 12,425; xml: 139; sh: 48; makefile: 25
file content (57 lines) | stat: -rw-r--r-- 1,999 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
// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors
// SPDX-License-Identifier: Zlib

#include <foonathan/memory/iteration_allocator.hpp>

#include <doctest/doctest.h>

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

using namespace foonathan::memory;

TEST_CASE("iteration_allocator")
{
    SUBCASE("basic")
    {
        test_allocator                                              alloc;
        iteration_allocator<2, allocator_reference<test_allocator>> iter_alloc(100, alloc);
        REQUIRE(alloc.no_allocated() == 1u);
        REQUIRE(iter_alloc.max_iterations() == 2u);
        REQUIRE(iter_alloc.cur_iteration() == 0u);
        REQUIRE(iter_alloc.capacity_left(0u) == 50);
        REQUIRE(iter_alloc.capacity_left(1u) == 50);

        iter_alloc.allocate(10, 1);
        REQUIRE(iter_alloc.capacity_left() < 50);
        iter_alloc.allocate(4, 4);
        REQUIRE(iter_alloc.capacity_left() < 50);

        REQUIRE(iter_alloc.capacity_left(1u) == 50);
        iter_alloc.next_iteration();
        REQUIRE(iter_alloc.cur_iteration() == 1u);
        REQUIRE(iter_alloc.capacity_left() == 50);
        REQUIRE(iter_alloc.capacity_left(0u) < 50);

        iter_alloc.allocate(10, 1);
        REQUIRE(iter_alloc.capacity_left() < 50);

        iter_alloc.next_iteration();
        REQUIRE(iter_alloc.cur_iteration() == 0u);
        REQUIRE(iter_alloc.capacity_left() == 50);
        REQUIRE(iter_alloc.capacity_left(1u) < 50);

        iter_alloc.next_iteration();
        REQUIRE(iter_alloc.cur_iteration() == 1u);
        REQUIRE(iter_alloc.capacity_left() == 50);
    }
    SUBCASE("overaligned")
    {
        test_allocator                                              alloc;
        iteration_allocator<1, allocator_reference<test_allocator>> iter_alloc(100, alloc);

        auto align = 2 * detail::max_alignment;
        auto mem   = iter_alloc.allocate(align, align);
        REQUIRE(detail::is_aligned(mem, align));
    }
}