/
arena_test.h
66 lines (51 loc) · 1.38 KB
/
arena_test.h
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
64
65
66
#import "arena.h"
TEST(arena) {
struct foo_t {
char string[4] = "foo";
};
struct bar_t {
char string[7] = "barbar";
};
struct hello_t {
hello_t() { n = alive()++; }
~hello_t() { alive()--; n = -1; }
int n;
static int& alive() { static int x = 0; return x; }
};
arena_t arena(rmr.mb());
int count = 10;
auto foos = arena.alloc<foo_t>(count);
arena.alloc<double>(1000);
ASSERT(arena.allocated() >= sizeof(double) * 1000 + sizeof(foo_t) * count);
for (int i = 0; i < count; i++) ASSERT(string(foos[i].string) == "foo");
arena.clear();
ASSERT(arena.allocated() == 0);
auto bars = arena.alloc<bar_t>(count);
arena.alloc<char>(2000);
ASSERT(arena.allocated() >= sizeof(char) * 2000 + sizeof(bar_t) * count);
for (int i = 0; i < 10; i++) ASSERT(string(bars[i].string) == "barbar");
arena.clear();
ASSERT(arena.allocated() == 0);
hello_t* begin;
hello_t* end;
{
auto alloc = arena.scoped_alloc<hello_t>(10);
begin = alloc.begin();
end = alloc.end();
ASSERT(hello_t::alive() == 10);
}
ASSERT(hello_t::alive() == 0);
ASSERT(arena.allocated() == 0);
while(begin != end) {
hello_t& h = *begin;
ASSERT(h.n == -1);
begin++;
}
{
auto alloc = arena.scoped_alloc<string>(5, "string");
for (string& s : alloc) {
ASSERT(s == "string");
}
}
ASSERT(arena.allocated() == 0);
}