-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy patharena_allocator_test.cpp
More file actions
59 lines (46 loc) · 2.08 KB
/
arena_allocator_test.cpp
File metadata and controls
59 lines (46 loc) · 2.08 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
54
55
56
57
58
// Copyright (c) 2013, 2014, Huang-Ming Huang, Object Computing, Inc.
// All rights reserved.
//
// This file is part of mFAST.
//
// mFAST is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// mFAST is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with mFast. If not, see <http://www.gnu.org/licenses/>.
//
#include <catch2/catch_test_macros.hpp>
#include <mfast/arena_allocator.h>
#include <cstring>
#include <algorithm>
using namespace mfast;
TEST_CASE("test arena_allocatore", "[arena_allocatore_test]")
{
arena_allocator alloc;
void* block1 = alloc.allocate(arena_allocator::default_chunk_size/2);
alloc.reset();
void* block2 = alloc.allocate(arena_allocator::default_chunk_size/2);
REQUIRE(block1 == block2);
// use all the remaing space of current chunk
//void* block3 =
alloc.allocate(arena_allocator::chunk_user_size - arena_allocator::default_chunk_size/2);
// now we will get memory from another chunk
void* block4 = alloc.allocate(arena_allocator::default_chunk_size/2);
alloc.reset();
void* block5 = alloc.allocate(arena_allocator::chunk_user_size);
REQUIRE(block4 == block5);
// this should get the memory from the first chunk
void* block6 = alloc.allocate(arena_allocator::chunk_user_size);
REQUIRE(block6 == block1);
// make sure we can allocate memory far larger than the default chunk size
char* block7 = static_cast<char*>(alloc.allocate(3*arena_allocator::default_chunk_size));
// if the returned memroy block is smaller than need, we should get a memory access error at this point.
memset(block7, 0, 3*arena_allocator::default_chunk_size);
}