-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
126 lines (74 loc) · 2.58 KB
/
test.c
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "garena.h"
#include "test.h"
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
void test_arena_create()
{
constexpr size_t arena_size = 1024;
Arena *arena = arena_create(arena_size);
TEST_EQUAL(arena->end - arena->begin, arena_size);
arena_destroy(arena);
}
void test_arena_alloc()
{
constexpr size_t arena_size = 1024;
constexpr size_t num_bytes = 256;
constexpr size_t num_more_bytes = 64;
constexpr ptrdiff_t expected_offset = (arena_size - (num_more_bytes * sizeof(int) + num_bytes));
Arena *arena = arena_create(arena_size);
char *bytes = arena_alloc(arena, num_bytes, 1);
TEST_EQUAL(
(ptrdiff_t)(arena->end - arena->begin),
(ptrdiff_t)(arena_size - num_bytes));
int *more_data = arena_alloc(arena, num_more_bytes, sizeof(int));
TEST_EQUAL(
(ptrdiff_t)(arena->end - arena->begin),
expected_offset);
TEST_EQUAL(
bytes - (char *)more_data,
num_bytes - num_bytes % sizeof(int));
arena_destroy(arena);
}
void test_arena_alloc_aligned()
{
constexpr size_t arena_size = 1024;
constexpr size_t allocation1 = 6;
constexpr size_t allocation2 = 1;
constexpr unsigned int alignment = 4;
constexpr ptrdiff_t expected_offset = alignof(size_t) % allocation1 + allocation2;
Arena *arena = arena_create(arena_size);
char *alloc1 = arena_alloc_aligned(arena, allocation1, 1, alignment);
char *alloc2 = arena_alloc_aligned(arena, allocation2, 1, alignment);
TEST_EQUAL(alloc2, alloc1 - expected_offset);
arena_destroy(arena);
}
void test_failures()
{
TEST_FALSE(garena_set_default_alignment(3));
TEST_NULL(arena_create(0));
TEST_NULL(arena_alloc(NULL, 0, 0));
TEST_NULL(arena_alloc_aligned(NULL, 0, 0, 0));
constexpr size_t arena_size = 10;
Arena *arena = arena_create(arena_size);
char *end = arena->end;
TEST_NULL(arena_alloc_aligned(arena, 0, 0, 3));
arena_alloc(arena, arena_size, 1);
TEST_NULL(arena_alloc(arena, 1, 1));
arena->end = end;
TEST_NULL(arena_alloc(arena, (size_t)PTRDIFF_MAX + 1, 1));
TEST_NULL(arena_alloc(arena, arena_size + 1, 1));
TEST_NULL(arena_alloc_aligned(arena, 0, 0, 3));
TEST_NULL(arena_alloc_aligned(arena, (size_t)PTRDIFF_MAX + 1, 1, 0));
TEST_NULL(arena_alloc_aligned(arena, arena_size + 1, 1, 0));
arena_destroy(arena);
}
int main()
{
SUITE(test_arena_create);
SUITE(test_arena_alloc);
SUITE(test_arena_alloc_aligned);
SUITE(test_failures);
WRAP_UP();
return 0;
}