-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_allocator_test.cpp
63 lines (56 loc) · 1.6 KB
/
memory_allocator_test.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
62
63
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "memory_allocator.hpp"
#include <gtest/gtest.h>
TEST(MemoryAllocatorTest, allocate) {
char buf[256];
MemoryAllocator ma(buf, sizeof buf);
ASSERT_EQ(ma.allocate(101), buf + 16);
ASSERT_EQ(ma.allocate(51), buf + 16 + 104 + 16);
ASSERT_TRUE(ma.allocate(100) == NULL);
ASSERT_EQ(ma.allocate(51), buf + 16 + 104 + 16 + 52 + 16);
ASSERT_TRUE(ma.allocate(1) == NULL);
}
TEST(MemoryAllocatorTest, allocate2) {
char buf[16 + 4];
MemoryAllocator ma(buf, sizeof buf);
ASSERT_EQ(ma.allocate(1), buf + 16);
}
TEST(MemoryAllocatorTest, free) {
char buf[256];
MemoryAllocator ma(buf, sizeof buf);
ASSERT_EQ(240U, ma.max_free_size());
void *p = ma.allocate(100);
ASSERT_EQ(124U, ma.max_free_size());
ma.free(p);
ASSERT_EQ(240U, ma.max_free_size());
void *p1 = ma.allocate(100);
ASSERT_EQ(124U, ma.max_free_size());
void *p2 = ma.allocate(100);
ASSERT_EQ(8U, ma.max_free_size());
ma.free(p2);
ASSERT_EQ(124U, ma.max_free_size());
ma.free(p1);
ASSERT_EQ(240U, ma.max_free_size());
p1 = ma.allocate(100);
ASSERT_EQ(124U, ma.max_free_size());
p2 = ma.allocate(100);
ASSERT_EQ(8U, ma.max_free_size());
ma.free(p1);
ASSERT_EQ(100U, ma.max_free_size());
ma.free(p2);
ASSERT_EQ(240U, ma.max_free_size());
}
TEST(MemoryAllocatorTest, max_free_size) {
char buf[256];
MemoryAllocator ma(buf, sizeof buf);
ma.allocate(101);
ASSERT_EQ(120U, ma.max_free_size());
ma.allocate(51);
ASSERT_EQ(52U, ma.max_free_size());
ma.allocate(51);
ASSERT_EQ(0U, ma.max_free_size());
ma.allocate(19);
ASSERT_EQ(0U, ma.max_free_size());
}