File: iteration_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 (58 lines) | stat: -rw-r--r-- 2,090 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
// Copyright (C) 2015-2020 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/iteration_allocator.hpp>

#include <catch.hpp>

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

using namespace foonathan::memory;

TEST_CASE("iteration_allocator", "[stack]")
{
    SECTION("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);
    }
    SECTION("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));
    }
}