/
main.c
162 lines (129 loc) · 4.22 KB
/
main.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* @file main.c
*
* @section License
* Copyright (C) 2014-2016, Erik Moqvist
*
* This library 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 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* This file is part of the Simba project.
*/
#include "simba.h"
static char buffer[2048];
static int test_alloc_free(struct harness_t *harness)
{
int i;
struct heap_t heap;
void *buffers[16];
size_t sizes[8] = { 16, 32, 64, 128, 256, 512, 512, 512 };
BTASSERT(heap_init(&heap, buffer, sizeof(buffer), sizes) == 0);
/* Allocate a few buffers... */
for (i = 0; i < 16; i++) {
buffers[i] = heap_alloc(&heap, 1 + (4 * i));
BTASSERT(buffers[i] != NULL);
}
/* ...and free them. */
for (i = 0; i < 16; i++) {
BTASSERT(heap_free(&heap, buffers[i]) == 0);
}
/* Allocate from the free list... */
for (i = 0; i < 16; i++) {
buffers[i] = heap_alloc(&heap, 1 + (4 * i));
BTASSERT(buffers[i] != NULL);
}
/* ...and free them. */
for (i = 0; i < 16; i++) {
BTASSERT(heap_free(&heap, buffers[i]) == 0);
}
return (0);
}
static int test_double_free(struct harness_t *harness)
{
struct heap_t heap;
void *buf_p;
size_t sizes[8] = { 16, 32, 64, 128, 256, 512, 512, 512 };
BTASSERT(heap_init(&heap, buffer, sizeof(buffer), sizes) == 0);
buf_p = heap_alloc(&heap, 1);
BTASSERT(buf_p != NULL);
BTASSERT(heap_free(&heap, buf_p) == 0);
BTASSERT(heap_free(&heap, buf_p) == -1);
return (0);
}
static int test_share(struct harness_t *harness)
{
struct heap_t heap;
void *buf_p;
size_t sizes[8] = { 16, 32, 64, 128, 256, 512, 512, 512 };
BTASSERT(heap_init(&heap, buffer, sizeof(buffer), sizes) == 0);
buf_p = heap_alloc(&heap, 1);
BTASSERT(buf_p != NULL);
BTASSERT(heap_share(&heap, buf_p, 1) == 0);
BTASSERT(heap_free(&heap, buf_p) == 1);
BTASSERT(heap_free(&heap, buf_p) == 0);
return (0);
}
static int test_big_buffer(struct harness_t *harness)
{
struct heap_t heap;
void *buffers[4];
size_t sizes[8] = { 16, 32, 64, 128, 256, 512, 512, 512 };
BTASSERT(heap_init(&heap, buffer, sizeof(buffer), sizes) == 0);
/* Allocate new block. */
buffers[0] = heap_alloc(&heap, 513);
BTASSERT(buffers[0] != NULL);
BTASSERT(heap_free(&heap, buffers[0]) == 0);
/* Allocate from the free list. */
buffers[0] = heap_alloc(&heap, 513);
BTASSERT(buffers[0] != NULL);
BTASSERT(heap_free(&heap, buffers[0]) == 0);
/* Allocate from the end of the free list*/
buffers[0] = heap_alloc(&heap, 513);
BTASSERT(buffers[0] != NULL);
buffers[1] = heap_alloc(&heap, 514);
BTASSERT(buffers[0] != NULL);
BTASSERT(heap_free(&heap, buffers[1]) == 0);
BTASSERT(heap_free(&heap, buffers[0]) == 0);
buffers[0] = heap_alloc(&heap, 514);
BTASSERT(buffers[0] != NULL);
BTASSERT(heap_free(&heap, buffers[0]) == 0);
return (0);
}
static int test_out_of_memory(struct harness_t *harness)
{
struct heap_t heap;
void *buf_p;
size_t sizes[8] = { 16, 32, 64, 128, 256, 512, 512, 512 };
BTASSERT(heap_init(&heap, buffer, 32, sizes) == 0);
buf_p = heap_alloc(&heap, 1);
BTASSERT(buf_p != NULL);
buf_p = heap_alloc(&heap, 26);
BTASSERT(buf_p == NULL);
/* Big buffer. */
buf_p = heap_alloc(&heap, 3000);
BTASSERT(buf_p == NULL);
return (0);
}
int main()
{
struct harness_t harness;
struct harness_testcase_t harness_testcases[] = {
{ test_alloc_free, "test_alloc_free" },
{ test_double_free, "test_double_free" },
{ test_share, "test_share" },
{ test_big_buffer, "test_big_buffer" },
{ test_out_of_memory, "test_out_of_memory" },
{ NULL, NULL }
};
sys_start();
harness_init(&harness);
harness_run(&harness, harness_testcases);
return (0);
}