-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.h
458 lines (343 loc) · 12.5 KB
/
tests.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// TESTS - header
#ifndef TESTS
#define TESTS
#include "../headers/memory.h"
#include "helpers.h"
#include <stdlib.h>
#include <time.h>
/*
* Testing memory_allocation - test 1 - equal blocks
*/
void test(c_size_t *memory_size, c_size_t memory_idx, c_size_t block_size)
{
char region[memory_size[memory_idx]];
memory_init(region, memory_size[memory_idx]);
char *ptr;
printf("\n-----------------------------------------------------------------------\n");
printf(RED BOLD "\nREQUIRED BLOCK SIZE: %dB / %dB\n" RESET, block_size, memory_size[memory_idx]);
int32_t ideal = (memory_size[memory_idx] / block_size) * block_size;
int32_t success = 0;
while ((ptr = (char *)memory_alloc(block_size)) != NULL)
{
success += block_size;
memset(ptr, FULL_BYTE, block_size);
display_block((void *)ptr - FULL_HEADER_SIZE, "FULL BLOCK");
}
print_memory(region, memory_size[memory_idx]);
print_free_list();
printf(RED ITALIC "\nSUCCESSFULY ALLOCATED: %dB (%.2lf%%) of %dB\n" RESET, success, GET_PERCENT(success, ideal), ideal);
printf(" - %d of %d blocks\n", success / block_size, ideal / block_size);
FREEZE
}
void test_equal_blocks_allocation(c_size_t *memory_size, c_size_t n, c_size_t from, c_size_t to)
{
printf(YELLOW BOLD "#################### Test equal blocks allocation #####################\n" RESET);
for (int32_t memory_idx = 0; memory_idx < n; ++memory_idx)
for (int32_t block_size = from; block_size <= to; ++block_size)
test(memory_size, memory_idx, block_size);
printf(YELLOW BOLD "\n######################################################################\n" RESET);
}
/*
* Testing memory_allocation - test 2, 3, 4 - random blocks
*/
void test_random_blocks_allocation(c_size_t *memory_size, c_size_t n, c_size_t from, c_size_t to)
{
srand(time(0));
printf(YELLOW BOLD "#################### Test random blocks allocation ####################\n" RESET);
for (int32_t memory_idx = 0; memory_idx < n; ++memory_idx)
{
char region[memory_size[memory_idx]];
memory_init(region, memory_size[memory_idx]);
char *ptr;
printf("\n-----------------------------------------------------------------------\n");
printf(RED BOLD "MEMORY SIZE: %dB\n" RESET, memory_size[memory_idx]);
int32_t block_size = 0;
while ((ptr = (char *)memory_alloc(block_size = GET_RANDOM(from, to))) != NULL)
{
memset(ptr, FULL_BYTE, block_size);
display_block((void *)ptr - FULL_HEADER_SIZE, "FULL BLOCK");
}
print_memory(region, memory_size[memory_idx]);
print_free_list();
FREEZE
}
printf(YELLOW BOLD "\n######################################################################\n" RESET);
}
/*
* Interactive allocation testing
*/
void test_custom_blocks_allocation(c_size_t memory_size)
{
printf(YELLOW BOLD "#################### Test custom blocks allocation ####################\n" RESET);
char region[memory_size];
memory_init(region, memory_size);
char *ptr;
int32_t block_size = 0;
while (scanf("%d", &block_size) == 1)
{
if ((ptr = (char *)memory_alloc(block_size)) == NULL)
continue;
printf("\n-----------------------------------------------------------------------\n");
printf(RED BOLD "\nBLOCK SIZE: %dB, MEMORY SIZE: %dB\n" RESET, block_size, memory_size);
memset(ptr, FULL_BYTE, block_size);
print_memory(region, memory_size);
print_free_list();
}
printf(YELLOW BOLD "\n######################################################################\n" RESET);
}
/*
* Testing memory_init function
*/
void test_memory_init()
{
printf(YELLOW BOLD "########################### Test memory_init ##########################\n" RESET);
c_size_t memory_size = 256;
char region[memory_size];
printf("\n* Small region *\n");
memory_init(region, 25);
PRINT_TEST_RESULT(!heap_g);
printf("\n* Zero region size *\n");
memory_init(region, 0);
PRINT_TEST_RESULT(!heap_g);
printf("\n* Negative region size *\n");
memory_init(region, -256);
PRINT_TEST_RESULT(!heap_g);
printf("\n* Wrong pointer to region *\n");
memory_init(NULL, memory_size);
PRINT_TEST_RESULT(!heap_g);
printf("\n* Correct arguments *\n");
memory_init(region, memory_size);
PRINT_TEST_RESULT(heap_g);
printf("\n* Rewriting existing memory *\n");
memory_init(region, memory_size);
PRINT_TEST_RESULT(heap_g);
printf("\n* Smallest possible size of region *\n");
memory_init(region, MIN_MEMORY_SIZE);
PRINT_TEST_RESULT(heap_g);
printf(YELLOW BOLD "\n######################################################################\n" RESET);
FREEZE
}
/*
* Test memory_check function
*/
void test_memory_check()
{
printf(YELLOW BOLD "########################## Test memory_check ##########################\n" RESET);
printf("\n* Checking before memory_init *\n");
PRINT_TEST_RESULT(!memory_check(0xff));
c_size_t memory_size = 256;
char region[memory_size];
memory_init(region, memory_size);
char *ptr1 = (char *)memory_alloc(16);
char *ptr2 = (char *)memory_alloc(32);
char *ptr3 = (char *)memory_alloc(64);
printf("\n* Bigger block than available memory *\n");
char *ptr4 = (char *)memory_alloc(128);
PRINT_TEST_RESULT(!ptr4);
printf("\n* Freeing middle block *\n");
PRINT_TEST_RESULT(!memory_free(ptr2));
printf("\n* Checking null pointer *\n");
PRINT_TEST_RESULT(!memory_check(NULL));
printf("\n* Checking invalid pointer *\n");
PRINT_TEST_RESULT(!memory_check(heap_g));
printf("\n* Memory before tests *\n");
print_memory(region, memory_size);
printf("\n* Checking all memory *\n");
for (int32_t offset = -20; offset < memory_size + 20; ++offset)
{
int32_t result = memory_check(heap_g + offset);
printf("%p", heap_g + offset);
if (heap_g + offset == ptr1 || heap_g + offset == ptr3)
PRINT_TEST_RESULT(result);
else
PRINT_TEST_RESULT(!result);
}
printf("\n* Checking free pointer *\n");
PRINT_TEST_RESULT(!memory_check(ptr2));
printf("\n* Freeing of already free block *\n");
PRINT_TEST_RESULT(memory_free(ptr2));
printf("\n* Checking block in full memory *\n");
ptr2 = (char *)memory_alloc(32);
ptr4 = (char *)memory_alloc(100);
PRINT_TEST_RESULT(memory_check(ptr1));
printf(YELLOW BOLD "\n######################################################################\n" RESET);
FREEZE
}
/*
* Testing valid pointers from memory
*/
void test_valid_pointers(c_size_t memory_size)
{
printf(YELLOW BOLD "######################### Test valid pointers #########################\n" RESET);
char region[memory_size];
memory_init(region, memory_size);
putchar('\n');
for (int32_t offset = -20; offset < memory_size + 20; ++offset)
{
if (heap_g + offset == heap_g)
printf(YELLOW "MEMORY START\n" RESET);
if (heap_g + offset == heap_g + HEADER_SIZE)
printf(YELLOW "AVAILABLE MEMORY\n" RESET);
if (heap_g + offset == GET_END_OF_MEMORY())
printf(YELLOW "MEMORY END\n" RESET);
printf("%p ", heap_g + offset);
printf(IS_VALID_POINTER(heap_g + offset) ? (GREEN "- VALID\n" RESET) : (RED "- INVALID\n" RESET));
}
printf(YELLOW BOLD "\n######################################################################\n" RESET);
FREEZE
}
/*
* Testing memory_free function and memory free cases
*/
void memory_free_case(c_size_t free_1, c_size_t free_2, c_size_t free_3)
{
c_size_t memory_size = 256;
char region[memory_size];
memory_init(region, memory_size);
char *ptr1 = (char *)memory_alloc(8);
char *ptr2 = (char *)memory_alloc(16);
char *ptr3 = (char *)memory_alloc(32);
if (free_1)
memory_free(ptr1);
if (free_2)
memory_free(ptr2);
if (free_3)
memory_free(ptr3);
printf(YELLOW BOLD "\n* Memory before test *\n" RESET);
print_memory(region, memory_size);
printf(YELLOW BOLD "\n* Testing blocks: %s |", free_1 ? "free" : "full");
printf(" %s ", free_2 ? "FREE" : "FULL");
printf("| %s *\n" RESET, free_3 ? "free" : "full");
printf("\n* Freeing middle block *\n");
if (!free_2)
PRINT_TEST_RESULT(!memory_free(ptr2));
else
PRINT_TEST_RESULT(memory_free(ptr2));
print_memory(region, memory_size);
print_free_list(region, memory_size);
printf("\n* Test result *\n");
if ((free_1 && free_3) || free_2)
PRINT_TEST_RESULT(!memory_check(ptr1) && !memory_check(ptr2) && !memory_check(ptr3));
else if (free_1)
PRINT_TEST_RESULT(!memory_check(ptr1) && !memory_check(ptr2) && memory_check(ptr3));
else if (free_3)
PRINT_TEST_RESULT(memory_check(ptr1) && !memory_check(ptr2) && !memory_check(ptr3));
else
PRINT_TEST_RESULT(memory_check(ptr1) && !memory_check(ptr2) && memory_check(ptr3));
}
void test_memory_free_cases()
{
printf(YELLOW BOLD "####################### Test memory_free cases ########################\n" RESET);
// full, FULL, full
memory_free_case(0, 0, 0);
FREEZE
// free, FULL, full
memory_free_case(1, 0, 0);
FREEZE
// full, FULL, free
memory_free_case(0, 0, 1);
FREEZE
// free, FULL, free
memory_free_case(1, 0, 1);
FREEZE
// free, FREE, free
memory_free_case(1, 1, 1);
printf(YELLOW BOLD "\n######################################################################\n" RESET);
FREEZE
}
/*
* Testing memory_free function
*/
void test_memory_free()
{
srand(time(0));
printf(YELLOW BOLD "########################## Test memory_free ###########################\n" RESET);
printf("\n* Memory free before memory_init *\n");
PRINT_TEST_RESULT(memory_free(0xff));
c_size_t memory_size = 512;
char region[memory_size];
memory_init(region, memory_size);
printf("\n* Freeing null pointer *\n");
PRINT_TEST_RESULT(memory_free(NULL));
void *ptr[20];
for (int32_t idx = 0; idx < 20; ++idx)
ptr[idx] = memory_alloc(GET_RANDOM(8, 16));
print_memory(region, memory_size);
FREEZE
printf("\n* Freeing all blocks *\n");
for (int32_t idx = 0; idx < 20; ++idx)
{
printf("%p", ptr[idx]);
PRINT_TEST_RESULT(!memory_free(ptr[idx]));
}
print_memory(region, memory_size);
FREEZE
for (int32_t idx = 0; idx < 20; ++idx)
ptr[idx] = memory_alloc(GET_RANDOM(8, 16));
print_memory(region, memory_size);
FREEZE
printf("\n* Freeing even blocks *\n");
for (int32_t idx = 1; idx < 20; idx += 2)
{
printf("%p", ptr[idx]);
PRINT_TEST_RESULT(!memory_free(ptr[idx]));
}
print_memory(region, memory_size);
print_free_list();
FREEZE
memory_init(region, memory_size);
char *ptr_free = (char *)memory_alloc(350);
printf("\n* Freeing block multiple times *\n");
display_block(ptr_free - FULL_HEADER_SIZE, "FULL BLOCK");
PRINT_TEST_RESULT(!memory_free(ptr_free));
PRINT_TEST_RESULT(memory_free(ptr_free));
printf(YELLOW BOLD "\n######################################################################\n" RESET);
FREEZE
}
/*
* Testing memory_alloc function
*/
void test_memory_alloc()
{
printf(YELLOW BOLD "########################## Test memory_alloc ##########################\n" RESET);
memory_init(NULL, 0);
printf("\n* Allocating zero size *\n");
PRINT_TEST_RESULT(!memory_alloc(0));
printf("\n* Allocating before memory_init *\n");
PRINT_TEST_RESULT(!memory_alloc(50));
c_size_t memory_size = 1024;
char region[memory_size];
memory_init(region, memory_size);
char *ptr;
printf("\n* Allocating bigger size than memory size *\n");
PRINT_TEST_RESULT(!memory_alloc(2 * memory_size));
FREEZE
printf("\n* Allocating from size 1 until memory is full *\n");
int32_t success = 0;
for (int32_t block_size = 1; (ptr = (char *)memory_alloc(block_size)); success += block_size++)
display_block(ptr - FULL_HEADER_SIZE, "FULL BLOCK");
print_memory(region, memory_size);
printf(RED ITALIC "\nSUCCESSFULY ALLOCATED: %dB (%.2lf%%) of %dB\n" RESET, success, GET_PERCENT(success, memory_size), memory_size);
FREEZE
memory_init(region, memory_size);
printf("\n* Allocating the biggest block *\n");
for (int32_t block_size = memory_size; !ptr; --block_size)
ptr = (char *)memory_alloc(block_size);
print_memory(region, memory_size);
printf("Memory size: %d\n", memory_size);
display_block(ptr - FULL_HEADER_SIZE, "BIGGEST");
printf("\n* Allocating block when memory full *\n");
PRINT_TEST_RESULT(!memory_alloc(50));
FREEZE
memory_init(region, memory_size);
printf("\n* Allocating block of integer type *\n");
PRINT_TEST_RESULT(memory_alloc(sizeof(int)));
printf("\n* Allocating array of integer type *\n");
PRINT_TEST_RESULT(memory_alloc(8 * sizeof(int)));
printf("\n* Allocating array of double type *\n");
PRINT_TEST_RESULT(memory_alloc(15 * sizeof(double)));
print_memory(region, memory_size);
printf(YELLOW BOLD "\n######################################################################\n" RESET);
FREEZE
}
#endif