-
Notifications
You must be signed in to change notification settings - Fork 738
/
Copy pathtest-alloc-testing.c
252 lines (180 loc) · 5.06 KB
/
test-alloc-testing.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
Copyright (c) 2005-2008, Simon Howard
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* Tests for the allocation testing framework. */
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "alloc-testing.h"
#include "framework.h"
static void test_malloc_free(void)
{
void *block, *block2, *block3, *block4;
unsigned char *ptr;
int i;
/* Allocate a block and check that the counters increase */
assert(alloc_test_get_allocated() == 0);
block = malloc(1024);
assert(block != NULL);
assert(alloc_test_get_allocated() == 1024);
/* Check that the block is initialised with garbage */
ptr = block;
for (i = 0; i < 1024; ++i) {
assert(ptr[i] != 0);
}
/* Free the block back and check the counters decrease */
free(block);
assert(alloc_test_get_allocated() == 0);
/* Try setting a limit */
alloc_test_set_limit(3);
block = malloc(1024);
assert(block != NULL);
block2 = malloc(1024);
assert(block2 != NULL);
block3 = malloc(1024);
assert(block3 != NULL);
block4 = malloc(1024);
assert(block4 == NULL);
free(block);
free(block2);
free(block3);
free(block4);
}
static void test_realloc(void)
{
void *block;
void *block2;
/* This block will be allocated while the other tests are run */
block2 = malloc(1024);
/* Allocate a block */
block = malloc(1024);
assert(block != NULL);
assert(alloc_test_get_allocated() == 1024 + 1024);
/* Reallocate the block larger */
block = realloc(block, 2048);
assert(block != NULL);
assert(alloc_test_get_allocated() == 2048 + 1024);
/* Reallocate the block smaller */
block = realloc(block, 1500);
assert(block != NULL);
assert(alloc_test_get_allocated() == 1500 + 1024);
free(block);
assert(alloc_test_get_allocated() == 0 + 1024);
/* Test passing a NULL pointer to make realloc behave as malloc() */
block = realloc(NULL, 1024);
assert(block != NULL);
assert(alloc_test_get_allocated() == 1024 + 1024);
free(block);
free(block2);
assert(alloc_test_get_allocated() == 0);
/* Test realloc with a limit set */
block = malloc(512);
assert(block != NULL);
assert(alloc_test_get_allocated() == 512);
alloc_test_set_limit(1);
block = realloc(block, 1024);
assert(block != NULL);
assert(alloc_test_get_allocated() == 1024);
assert(realloc(block, 2048) == NULL);
assert(alloc_test_get_allocated() == 1024);
free(block);
assert(alloc_test_get_allocated() == 0);
/* Test NULL realloc with limit */
alloc_test_set_limit(1);
block = realloc(NULL, 1024);
assert(block != NULL);
assert(alloc_test_get_allocated() == 1024);
assert(realloc(NULL, 1024) == NULL);
assert(alloc_test_get_allocated() == 1024);
free(block);
}
static void test_calloc(void)
{
unsigned char *block;
int i;
assert(alloc_test_get_allocated() == 0);
/* Allocate a block */
block = calloc(16, 64);
assert(alloc_test_get_allocated() == 1024);
assert(block != NULL);
/* Check the block contents are initialised to zero */
for (i = 0; i < 1024; ++i) {
assert(block[i] == 0);
}
free(block);
assert(alloc_test_get_allocated() == 0);
/* Test calloc with limit */
alloc_test_set_limit(1);
block = calloc(1024, 1);
assert(block != NULL);
assert(alloc_test_get_allocated() == 1024);
assert(calloc(1024, 1) == NULL);
assert(alloc_test_get_allocated() == 1024);
free(block);
}
static void test_strdup(void)
{
char *str;
assert(alloc_test_get_allocated() == 0);
/* Test strdup */
str = strdup("hello world");
assert(str != NULL);
assert(strcmp(str, "hello world") == 0);
assert(alloc_test_get_allocated() == 12);
free(str);
assert(alloc_test_get_allocated() == 0);
/* Test strdup with limit */
alloc_test_set_limit(1);
str = strdup("hello world");
assert(str != NULL);
assert(alloc_test_get_allocated() == 12);
assert(strdup("hello world") == NULL);
assert(alloc_test_get_allocated() == 12);
free(str);
}
static void test_limits(void)
{
void *block;
/* Test normal malloc */
block = malloc(2048);
assert(block != NULL);
free(block);
/* Test malloc with limit */
alloc_test_set_limit(1);
block = malloc(1024);
assert(block != NULL);
assert(malloc(1024) == NULL);
free(block);
/* Check that it is possible to remove the limit */
alloc_test_set_limit(-1);
block = malloc(1024);
assert(block != NULL);
free(block);
}
/* clang-format off */
static UnitTestFunction tests[] = {
test_malloc_free,
test_realloc,
test_calloc,
test_strdup,
test_limits,
NULL
};
/* clang-format on */
int main(int argc, char *argv[])
{
run_tests(tests);
return 0;
}