-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.cpp
61 lines (52 loc) · 1.45 KB
/
tests.cpp
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
#include "FreeListAllocator.h"
#include <gtest/gtest.h>
namespace
{
/**
* @brief Allocate Tests
*
*/
TEST(Allocate, AllocateOneChunk)
{
FreeListAllocator allocator(2048);
allocator.Init();
void* ptr512 = allocator.Allocate(512);
ASSERT_EQ(allocator.GetUsedBytes(), 512 + FreeListAllocator::AllocationHeaderSize);
}
TEST(Allocate, AllocateFewChunks)
{
FreeListAllocator allocator(2048);
allocator.Init();
void* ptr512 = allocator.Allocate(512);
void* ptr1024 = allocator.Allocate(1024);
ASSERT_EQ(ptr512 + 512 + FreeListAllocator::AllocationHeaderSize, ptr1024);
}
/**
* @brief Free Tests
*
*/
TEST(Free, FreeOneChunk)
{
FreeListAllocator allocator(2048);
allocator.Init();
void* ptr2048 = allocator.Allocate(2048);
allocator.Free(ptr2048);
ASSERT_EQ(allocator.GetUsedBytes(), 0);
}
TEST(Free, MergeAfterFree)
{
FreeListAllocator allocator(2048);
allocator.Init();
void* ptr1024 = allocator.Allocate(1024);
void* ptr512 = allocator.Allocate(512);
allocator.Free(ptr512);
auto freeList = allocator.GetFreeList();
ASSERT_EQ(freeList.size(), 1);
ASSERT_EQ(freeList.begin()->blockSize, 1024);
}
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}