-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.c
More file actions
296 lines (241 loc) · 8.23 KB
/
Copy pathtests.c
File metadata and controls
296 lines (241 loc) · 8.23 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "tests.h"
#include "mem_wrapper.h"
/*
* Tests malloc by allocating a high number of 1 byte blocks of memory
* A small wait time is added to sanity check time buckets
* Half the memory is freed and checked again
*/
bool one_byte_malloc_test(void) {
// create a bunch of pointers to allocate
uint8_t *test[TEST_PTR_CNT];
// allocate memory
for(int i = 0; i < TEST_PTR_CNT; i++) {
test[i] = (uint8_t *)malloc(sizeof(uint8_t));
}
// check stats are as expected
mem_stats_t results = get_mem_stats();
// Correct number of allocations are made
if(results.num_allocations != TEST_PTR_CNT) {
return false;
}
// Allocated size is as expected
if(results.current_allocated_size != sizeof(uint8_t)*TEST_PTR_CNT) {
return false;
}
// Memory bucket count is as expected
if(results.allocation_buckets[0] != TEST_PTR_CNT) {
return false;
}
// Time bucket count is as expected
if(results.time_buckets[0] != TEST_PTR_CNT) {
return false;
}
// Move all memory to the next time bucket
sleep(1);
results = get_mem_stats();
// Correct number of allocations are made
if(results.num_allocations != TEST_PTR_CNT) {
return false;
}
// Allocated size is as expected
if(results.current_allocated_size != sizeof(uint8_t)*TEST_PTR_CNT) {
return false;
}
// Memory bucket count is as expected
if(results.allocation_buckets[0] != TEST_PTR_CNT) {
return false;
}
// Time bucket count is as expected
if(results.time_buckets[1] != TEST_PTR_CNT) {
return false;
}
// Free half the memory
for(int i = 0; i < TEST_PTR_CNT/2; i++) {
free(test[i]);
}
// check stats are as expected
results = get_mem_stats();
// Correct number of allocations are made
if(results.num_allocations != TEST_PTR_CNT/2) {
return false;
}
// Allocated size is as expected
if(results.current_allocated_size != sizeof(uint8_t)*TEST_PTR_CNT/2) {
return false;
}
// Memory bucket count is as expected
if(results.allocation_buckets[0] != TEST_PTR_CNT/2) {
return false;
}
// Time bucket count is as expected
if(results.time_buckets[1] != TEST_PTR_CNT/2) {
return false;
}
// Free the rest of the memory
for(int i = TEST_PTR_CNT/2; i < TEST_PTR_CNT; i++) {
free(test[i]);
}
// check stats are as expected
results = get_mem_stats();
// Correct number of allocations are made
if(results.num_allocations != 0) {
return false;
}
// Allocated size is as expected
if(results.current_allocated_size != 0) {
return false;
}
// Memory bucket count is as expected
if(results.allocation_buckets[0] != 0) {
return false;
}
// Time bucket count is as expected
if(results.time_buckets[1] != 0) {
return false;
}
return true;
}
/*
* Deliberately allocates blocks of memory that populate all the different buckets
* The memory is freed as well to ensure that it also works as expected.
*/
bool multisize_malloc_test(void) {
// create a bunch of pointers to allocate
uint8_t *test[ALLOCATION_BUCKET_COUNT+ALLOCATION_BUCKET_OFFSET-1][TEST_PTR_CNT];
mem_stats_t results;
uint64_t expected_size = 0;
// sweep through multiple times allocating a block that belongs in each size bucket
for(int j = 0; j < TEST_PTR_CNT; j++) {
// allocate memory starting from 2 bytes to 4096
for(int i = 1; i < ALLOCATION_BUCKET_COUNT+ALLOCATION_BUCKET_OFFSET-1; i++) {
test[i][j] = (uint8_t *)malloc(sizeof(uint8_t)*(1 << (i)));
expected_size += sizeof(uint8_t)*(1 << (i));
}
// check stats are as expected each time
results = get_mem_stats();
// Correct number of allocations are made
if(results.num_allocations != (ALLOCATION_BUCKET_COUNT+ALLOCATION_BUCKET_OFFSET-2)*(j+1)) {
return false;
}
// Allocated size is as expected
if(results.current_allocated_size != expected_size) {
return false;
}
// Memory bucket count is as expected
for(int i = 0; i < ALLOCATION_BUCKET_COUNT; i++) {
if(results.allocation_buckets[i] != j+1) {
return false;
}
}
}
// sweep through again, this time freeing memory
for(int j = 0; j < TEST_PTR_CNT; j++) {
for(int i = 1; i < ALLOCATION_BUCKET_COUNT+ALLOCATION_BUCKET_OFFSET-1; i++) {
free(test[i][j]);
expected_size -= sizeof(uint8_t)*(1 << (i));
}
// check stats are as expected each time
results = get_mem_stats();
// Correct number of allocations are made
if(results.num_allocations != (TEST_PTR_CNT*(ALLOCATION_BUCKET_COUNT+ALLOCATION_BUCKET_OFFSET-2))-(ALLOCATION_BUCKET_COUNT+ALLOCATION_BUCKET_OFFSET-2)*(j+1)) {
return false;
}
// Allocated size is as expected
if(results.current_allocated_size != expected_size) {
return false;
}
// Memory bucket count is as expected
for(int i = 0; i < ALLOCATION_BUCKET_COUNT; i++) {
if(results.allocation_buckets[i] != TEST_PTR_CNT-(j+1)) {
return false;
}
}
}
return true;
}
/*
* Tests the time buckets by allocating memory and waiting
*/
bool calloc_realloc_time_bucket_test(void) {
// create a bunch of pointers to allocate
uint8_t *test[TEST_PTR_CNT];
mem_stats_t results;
uint64_t expected_size = 0;
// allocate odd amount of memory with calloc
for(int i = 0; i < TEST_PTR_CNT; i++) {
test[i] = (uint8_t *)calloc(3, sizeof(uint8_t));
expected_size += 3*sizeof(uint8_t);
}
// check stats are as expected each time
results = get_mem_stats();
// Correct number of allocations are made
if(results.num_allocations != TEST_PTR_CNT) {
return false;
}
// Allocated size is as expected
if(results.current_allocated_size != expected_size) {
return false;
}
// Memory bucket count is as expected
if(results.allocation_buckets[0] != TEST_PTR_CNT) {
return false;
}
for(int i = 1; i < ALLOCATION_BUCKET_COUNT; i++) {
if(results.allocation_buckets[i] != 0) {
return false;
}
}
// reallocate the previously calloced memory with realloc (make it 1 byte bigger to observe bucket change)
for(int i = 0; i < TEST_PTR_CNT; i++) {
test[i] = (uint8_t *)realloc(test[i], 4*sizeof(uint8_t));
expected_size += sizeof(uint8_t);
}
for(int j = 0; j < TIME_BUCKET_COUNT; j++) {
// check stats are as expected each time
results = get_mem_stats();
// Correct number of allocations are made
if(results.num_allocations != TEST_PTR_CNT) {
return false;
}
// Allocated size is as expected
if(results.current_allocated_size != expected_size) {
return false;
}
// Memory bucket count is as expected
if(results.allocation_buckets[1] != TEST_PTR_CNT) {
return false;
}
for(int i = 0; i < ALLOCATION_BUCKET_COUNT; i++) {
if(i != 1) {
if(results.allocation_buckets[i] != 0) {
return false;
}
}
}
// Time bucket count is as expected
if(results.time_buckets[j] != TEST_PTR_CNT) {
return false;
}
sleep(pow(10,j));
}
// sweep through again, this time freeing memory
for(int i = 0; i < TEST_PTR_CNT; i++) {
free(test[i]);
expected_size -= 4*sizeof(uint8_t);
// check stats are as expected each time
results = get_mem_stats();
// Correct number of allocations are made
if(results.num_allocations != TEST_PTR_CNT-(i+1)) {
return false;
}
// Allocated size is as expected
if(results.current_allocated_size != expected_size) {
return false;
}
// Memory bucket count is as expected
if(results.allocation_buckets[1] != TEST_PTR_CNT-(i+1)) {
return false;
}
}
return true;
}