source: tests/malloc.cfa@ d653faf

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since d653faf was 10d609a, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

allow free( 0p )

  • Property mode set to 100644
File size: 13.2 KB
Line 
1#include <assert.h>
2#include <malloc.h> // malloc_usable_size
3#include <stdint.h> // uintptr_t
4#include <stdlib.h> // posix_memalign
5#include <fstream.hfa>
6#include <stdlib.hfa> // access C malloc, realloc
7#include <unistd.h> // getpagesize
8
9int tests_total;
10int tests_failed;
11size_t tAlign = 32;
12struct S1 { int d1; } __attribute__((aligned(32)));
13typedef struct S1 T1;
14
15void test_base( void * ip, size_t size, size_t align) {
16 tests_total += 1;
17 bool passed = (malloc_size(ip) == size) && (malloc_usable_size(ip) >= size) && (malloc_alignment(ip) == align) && ((uintptr_t)ip % align == 0);
18 if (!passed) {
19 printf("failed test %2d: %4lu %4lu but got %4lu ( %3lu ) %4lu\n", tests_total, size, align, malloc_size(ip), malloc_usable_size(ip), malloc_alignment(ip));
20 tests_failed += 1;
21 }
22}
23
24void test_fill( void * ip_, size_t start, size_t end, char fill) {
25 tests_total += 1;
26 bool passed = true;
27 char * ip = (char *) ip_;
28 for (i; start ~ end) passed = passed && (ip[i] == fill);
29 if (!passed) {
30 printf("failed test %2d: fill\n", tests_total);
31 tests_failed += 1;
32 }
33}
34
35void test_use( void * ip_) {
36 tests_total += 1;
37 bool passed = true;
38 int * ip = (int *) ip_;
39 size_t size = malloc_size(ip);
40 for (i; 0 ~ size ~ sizeof(int)) ip[i/sizeof(int)] = 0xdeadbeef;
41 for (i; 0 ~ size ~ sizeof(int)) passed = passed && (ip[i/sizeof(int)] == 0xdeadbeef);
42 size_t usize = malloc_usable_size(ip);
43 for (i; size ~ usize ~ sizeof(int)) ip[i/sizeof(int)] = -1;
44 for (i; size ~ usize ~ sizeof(int)) passed = passed && (ip[i/sizeof(int)] == -1);
45 if (!passed) {
46 printf("failed test %2d: use\n", tests_total);
47 tests_failed += 1;
48 }
49}
50
51int main( void ) {
52 size_t elemSize = sizeof(int);
53 size_t dim = 8;
54 size_t size = dim * elemSize;
55 size_t align = 64;
56 const size_t libAlign = libAlign();
57 char fill = '\xde';
58 int * ip;
59 T1 * tp;
60
61 // testing C malloc
62
63 tests_total = 0;
64 tests_failed = 0;
65
66 ip = (int *) (void *) malloc( size );
67 test_base(ip, size, libAlign);
68 test_use(ip);
69 free(ip);
70
71 ip = (int *) (void *) malloc( 0 );
72 test_base(ip, 0, libAlign);
73 test_use(ip);
74 free(ip);
75
76 ip = (int *) (void *) aalloc( dim, elemSize );
77 test_base(ip, size, libAlign);
78 test_use(ip);
79 free(ip);
80
81 ip = (int *) (void *) aalloc( 0, elemSize );
82 test_base(ip, 0, libAlign);
83 test_use(ip);
84 free(ip);
85
86 ip = (int *) (void *) aalloc( dim, 0 );
87 test_base(ip, 0, libAlign);
88 test_use(ip);
89 free(ip);
90
91 ip = (int *) (void *) aalloc( 0, 0 );
92 test_base(ip, 0, libAlign);
93 test_use(ip);
94 free(ip);
95
96 ip = (int *) (void *) calloc( dim, elemSize );
97 test_base(ip, size, libAlign);
98 test_fill(ip, 0, size, '\0');
99 test_use(ip);
100 free(ip);
101
102 ip = (int *) (void *) calloc( 0, elemSize );
103 test_base(ip, 0, libAlign);
104 test_fill(ip, 0, 0, '\0');
105 test_use(ip);
106 free(ip);
107
108 ip = (int *) (void *) calloc( dim, 0 );
109 test_base(ip, 0, libAlign);
110 test_fill(ip, 0, 0, '\0');
111 test_use(ip);
112 free(ip);
113
114 ip = (int *) (void *) malloc( size );
115 ip = (int *) (void *) resize( (void *) ip, size / 4 );
116 test_base(ip, size / 4, libAlign);
117 test_use(ip);
118 free(ip);
119
120 ip = (int *) (void *) malloc( size );
121 ip = (int *) (void *) resize( (void *) ip, size * 4 );
122 test_base(ip, size * 4, libAlign);
123 test_use(ip);
124 free(ip);
125
126 ip = (int *) (void *) malloc( size );
127 ip = (int *) (void *) resize( (void *) ip, 0 );
128 test_base(ip, 0, libAlign);
129 test_use(ip);
130 free(ip);
131
132 ip = (int *) (void *) resize( NULL, size );
133 test_base(ip, size, libAlign);
134 test_use(ip);
135 free(ip);
136
137 ip = (int *) (void *) resize( 0p, size );
138 test_base(ip, size, libAlign);
139 test_use(ip);
140 free(ip);
141
142 ip = (int *) (void *) calloc( dim, elemSize );
143 ip = (int *) (void *) realloc( (void *) ip, size / 4 );
144 test_base(ip, size / 4, libAlign);
145 test_fill(ip, 0, size / 4, '\0');
146 test_use(ip);
147 free(ip);
148
149 ip = (int *) (void *) calloc( dim, elemSize );
150 ip = (int *) (void *) realloc( (void *) ip, size * 4 );
151 test_base(ip, size * 4, libAlign);
152 test_fill(ip, 0, size * 4, '\0');
153 test_use(ip);
154 free(ip);
155
156 ip = (int *) (void *) calloc( dim, elemSize );
157 ip = (int *) (void *) realloc( (void *) ip, 0 );
158 test_base(ip, 0, libAlign);
159 test_use(ip);
160 free(ip);
161
162 ip = (int *) (void *) realloc( NULL, size );
163 test_base(ip, size , libAlign);
164 test_use(ip);
165 free(ip);
166
167 ip = (int *) (void *) realloc( 0p, size );
168 test_base(ip, size, libAlign);
169 test_use(ip);
170 free(ip);
171
172 ip = (int *) (void *) memalign( align, size );
173 test_base(ip, size, align);
174 test_use(ip);
175 free(ip);
176
177 ip = (int *) (void *) memalign( align, 0 );
178 test_base(ip, 0, libAlign);
179 test_use(ip);
180 free(ip);
181
182 ip = (int *) (void *) amemalign( align, dim, elemSize );
183 test_base(ip, size, align);
184 test_use(ip);
185 free(ip);
186
187 ip = (int *) (void *) amemalign( align, 0, elemSize );
188 test_base(ip, 0, libAlign);
189 test_use(ip);
190 free(ip);
191
192 ip = (int *) (void *) amemalign( align, dim, 0 );
193 test_base(ip, 0, libAlign);
194 test_use(ip);
195 free(ip);
196
197 ip = (int *) (void *) cmemalign( align, dim, elemSize );
198 test_base(ip, size, align);
199 test_fill(ip, 0, size, '\0');
200 test_use(ip);
201 free(ip);
202
203 ip = (int *) (void *) cmemalign( align, 0, elemSize );
204 test_base(ip, 0, libAlign);
205 test_use(ip);
206 free(ip);
207
208 ip = (int *) (void *) cmemalign( align, dim, 0 );
209 test_base(ip, 0, libAlign);
210 test_use(ip);
211 free(ip);
212
213 ip = (int *) (void *) aligned_alloc( align, size );
214 test_base(ip, size, align);
215 test_use(ip);
216 free(ip);
217
218 ip = (int *) (void *) aligned_alloc( align, 0 );
219 test_base(ip, 0, libAlign);
220 test_use(ip);
221 free(ip);
222
223 (int) posix_memalign( (void **) &ip, align, size );
224 test_base(ip, size, align);
225 test_use(ip);
226 free(ip);
227
228 (int) posix_memalign( (void **) &ip, align, 0 );
229 test_base(ip, 0, libAlign);
230 test_use(ip);
231 free(ip);
232
233 ip = (int *) (void *) valloc( size );
234 test_base(ip, size, getpagesize());
235 test_use(ip);
236 free(ip);
237
238 ip = (int *) (void *) valloc( 0 );
239 test_base(ip, 0, libAlign);
240 test_use(ip);
241 free(ip);
242
243 ip = (int *) (void *) pvalloc( getpagesize() * 3 / 2 );
244 test_base(ip, getpagesize() * 2, getpagesize());
245 test_use(ip);
246 free(ip);
247
248 ip = (int *) (void *) pvalloc( 0 );
249 test_base(ip, 0, libAlign);
250 test_use(ip);
251 free(ip);
252
253 ip = (int *) (void *) malloc( size );
254 ip = (int *) (void *) resize( (void *) ip, libAlign, size / 2 );
255 test_base(ip, size / 2, libAlign);
256 test_use(ip);
257 free(ip);
258
259 ip = (int *) (void *) aligned_alloc( align, size );
260 ip = (int *) (void *) resize( (void *) ip, align, size / 2 );
261 test_base(ip, size / 2, align);
262 test_use(ip);
263 free(ip);
264
265 ip = (int *) (void *) malloc( size );
266 ip = (int *) (void *) resize( (void *) ip, align, size / 4 );
267 test_base(ip, size / 4, align);
268 test_use(ip);
269 free(ip);
270
271 ip = (int *) (void *) malloc( size );
272 ip = (int *) (void *) resize( (void *) ip, align, 0 );
273 test_base(ip, 0, libAlign);
274 test_use(ip);
275 free(ip);
276
277 ip = (int *) (void *) resize( NULL, align, size );
278 test_base(ip, size, align);
279 test_use(ip);
280 free(ip);
281
282 ip = (int *) (void *) resize( 0p, align, size );
283 test_base(ip, size, align);
284 test_use(ip);
285 free(ip);
286
287 ip = (int *) (void *) calloc( dim, elemSize );
288 ip = (int *) (void *) realloc( (void *) ip, libAlign, size / 2 );
289 test_base(ip, size / 2, libAlign);
290 test_fill(ip, 0, size / 2, '\0');
291 test_use(ip);
292 free(ip);
293
294 ip = (int *) (void *) cmemalign( align, dim, elemSize );
295 ip = (int *) (void *) realloc( (void *) ip, align, size / 2 );
296 test_base(ip, size / 2, align);
297 test_fill(ip, 0, size / 2, '\0');
298 test_use(ip);
299 free(ip);
300
301 ip = (int *) (void *) calloc( dim, elemSize );
302 ip = (int *) (void *) realloc( (void *) ip, align, size / 4 );
303 test_base(ip, size / 4, align);
304 test_fill(ip, 0, size / 4, '\0');
305 test_use(ip);
306 free(ip);
307
308 ip = (int *) (void *) calloc( dim, elemSize );
309 ip = (int *) (void *) realloc( (void *) ip, 0, size * 4 );
310 test_base(ip, size * 4, libAlign);
311 test_fill(ip, 0, size * 4, '\0');
312 test_use(ip);
313 free(ip);
314
315 ip = (int *) (void *) calloc( dim, elemSize );
316 ip = (int *) (void *) realloc( (void *) ip, align, 0 );
317 test_base(ip, 0, libAlign);
318 test_use(ip);
319 free(ip);
320
321 free( 0p ); // sanity check
322 free( NULL ); // sanity check
323
324 if (tests_failed == 0) printf("PASSED C malloc tests\n\n");
325 else printf("failed C malloc tests : %d/%d\n\n", tests_failed, tests_total);
326
327 // testing CFA malloc
328
329 tests_total = 0;
330 tests_failed = 0;
331
332 ip = malloc();
333 test_base(ip, elemSize, libAlign);
334 test_use(ip);
335 free(ip);
336
337 ip = aalloc( dim );
338 test_base(ip, size, libAlign);
339 test_use(ip);
340 free(ip);
341
342 ip = aalloc( 0 );
343 test_base(ip, 0, libAlign);
344 test_use(ip);
345 free(ip);
346
347 ip = calloc( dim );
348 test_base(ip, size, libAlign);
349 test_fill(ip, 0, size, '\0');
350 test_use(ip);
351 free(ip);
352
353 ip = calloc( 0 );
354 test_base(ip, 0, libAlign);
355 test_use(ip);
356 free(ip);
357
358 ip = aalloc( dim );
359 ip = resize( ip, size / 4 );
360 test_base(ip, size / 4, libAlign);
361 test_use(ip);
362 free(ip);
363
364 ip = aalloc( dim );
365 ip = resize( ip, size * 4 );
366 test_base(ip, size * 4, libAlign);
367 test_use(ip);
368 free(ip);
369
370 ip = aalloc( dim );
371 ip = resize( ip, 0 );
372 test_base(ip, 0, libAlign);
373 test_use(ip);
374 free(ip);
375
376 ip = resize( (int*)0p, size );
377 test_base(ip, size, libAlign);
378 test_use(ip);
379 free(ip);
380
381 ip = resize( (int*)0p, size );
382 test_base(ip, size, libAlign);
383 test_use(ip);
384 free(ip);
385
386 ip = calloc( dim );
387 ip = realloc( ip, size / 4 );
388 test_base(ip, size / 4, libAlign);
389 test_fill(ip, 0, size / 4, '\0');
390 test_use(ip);
391 free(ip);
392
393 ip = calloc( dim );
394 ip = realloc( ip, size * 4 );
395 test_base(ip, size * 4, libAlign);
396 test_fill(ip, 0, size, '\0');
397 test_use(ip);
398 free(ip);
399
400 ip = calloc( dim );
401 ip = realloc( ip, 0 );
402 test_base(ip, 0, libAlign);
403 test_use(ip);
404 free(ip);
405
406 ip = realloc( (int*)0p, size );
407 test_base(ip, size , libAlign);
408 test_use(ip);
409 free(ip);
410
411 ip = realloc( (int*)0p, size );
412 test_base(ip, size, libAlign);
413 test_use(ip);
414 free(ip);
415
416 ip = memalign( align );
417 test_base(ip, elemSize, align);
418 test_use(ip);
419 free(ip);
420
421 ip = amemalign( align, dim );
422 test_base(ip, size, align);
423 test_use(ip);
424 free(ip);
425
426 ip = amemalign( align, 0 );
427 test_base(ip, 0, libAlign);
428 test_use(ip);
429 free(ip);
430
431 ip = cmemalign( align, dim );
432 test_base(ip, size, align);
433 test_fill(ip, 0, size, '\0');
434 test_use(ip);
435 free(ip);
436
437 ip = cmemalign( align, 0 );
438 test_base(ip, 0, libAlign);
439 test_use(ip);
440 free(ip);
441
442 ip = aligned_alloc( align );
443 test_base(ip, elemSize, align);
444 test_use(ip);
445 free(ip);
446
447 (int) posix_memalign( (int **) &ip, align );
448 test_base(ip, elemSize, align);
449 test_use(ip);
450 free(ip);
451
452 ip = valloc();
453 test_base(ip, elemSize, getpagesize());
454 test_use(ip);
455 free(ip);
456
457 ip = pvalloc();
458 test_base(ip, getpagesize(), getpagesize());
459 test_use(ip);
460 free(ip);
461
462 if (tests_failed == 0) printf("PASSED CFA malloc tests\n\n");
463 else printf("failed CFA malloc tests : %d/%d\n\n", tests_failed, tests_total);
464
465 // testing CFA malloc with aligned struct
466
467 elemSize = sizeof(T1);
468 size = dim * elemSize;
469 tests_total = 0;
470 tests_failed = 0;
471
472 tp = malloc();
473 test_base(tp, elemSize, tAlign );
474 test_use(tp);
475 free(tp);
476
477 tp = aalloc( dim );
478 test_base(tp, size, tAlign );
479 test_use(tp);
480 free(tp);
481
482 tp = aalloc( 0 );
483 test_base(tp, 0, libAlign);
484 test_use(tp);
485 free(tp);
486
487 tp = calloc( dim );
488 test_base(tp, size, tAlign );
489 test_fill(tp, 0, size, '\0');
490 test_use(tp);
491 free(tp);
492
493 tp = calloc( 0 );
494 test_base(tp, 0, libAlign);
495 test_use(tp);
496 free(tp);
497
498 tp = aalloc( dim );
499 tp = resize( tp, size / 4 );
500 test_base(tp, size / 4, tAlign );
501 test_use(tp);
502 free(tp);
503
504 tp = malloc();
505 tp = resize( tp, size * 4 );
506 test_base(tp, size * 4, tAlign );
507 test_use(tp);
508 free(tp);
509
510 tp = aalloc( dim );
511 tp = resize( tp, 0 );
512 test_base(tp, 0, libAlign);
513 test_use(tp);
514 free(tp);
515
516 tp = resize( (T1*)0p, size );
517 test_base(tp, size, tAlign );
518 test_use(tp);
519 free(tp);
520
521 tp = resize( (T1*)0p, size );
522 test_base(tp, size, tAlign );
523 test_use(tp);
524 free(tp);
525
526 tp = calloc( dim );
527 tp = realloc( tp, size / 4 );
528 test_base(tp, size / 4, tAlign );
529 test_fill(tp, 0, size / 4, '\0');
530 test_use(tp);
531 free(tp);
532
533 tp = calloc( dim );
534 tp = realloc( tp, size * 4 );
535 test_base(tp, size * 4, tAlign );
536 test_fill(tp, 0, size, '\0');
537 test_use(tp);
538 free(tp);
539
540 tp = calloc( dim );
541 tp = realloc( tp, 0 );
542 test_base(tp, 0, libAlign);
543 test_use(tp);
544 free(tp);
545
546 tp = realloc( (T1*)0p, size );
547 test_base(tp, size , tAlign );
548 test_use(tp);
549 free(tp);
550
551 tp = realloc( (T1*)0p, size );
552 test_base(tp, size, tAlign );
553 test_use(tp);
554 free(tp);
555
556 tp = memalign( align );
557 test_base(tp, elemSize, align);
558 test_use(tp);
559 free(tp);
560
561 tp = amemalign( align, dim );
562 test_base(tp, size, align);
563 test_use(tp);
564 free(tp);
565
566 tp = amemalign( align, 0 );
567 test_base(tp, 0, libAlign);
568 test_use(tp);
569 free(tp);
570
571 tp = cmemalign( align, dim );
572 test_base(tp, size, align);
573 test_fill(tp, 0, size, '\0');
574 test_use(tp);
575 free(tp);
576
577 tp = cmemalign( align, 0 );
578 test_base(tp, 0, libAlign);
579 test_use(tp);
580 free(tp);
581
582 tp = aligned_alloc( align );
583 test_base(tp, elemSize, align);
584 test_use(tp);
585 free(tp);
586
587 (int) posix_memalign( (T1 **) &tp, align );
588 test_base(tp, elemSize, align);
589 test_use(tp);
590 free(tp);
591
592 tp = valloc();
593 test_base(tp, elemSize, getpagesize());
594 test_use(tp);
595 free(tp);
596
597 tp = pvalloc();
598 test_base(tp, getpagesize(), getpagesize());
599 test_use(tp);
600 free(tp);
601
602 if (tests_failed == 0) printf("PASSED CFA malloc tests (aligned struct)\n\n");
603 else printf("failed CFA malloc tests (aligned struct) : %d/%d\n\n", tests_failed, tests_total);
604
605 return 0;
606}
607
608// Local Variables: //
609// tab-width: 4 //
610// compile-command: "cfa malloc.cfa" //
611// End: //
Note: See TracBrowser for help on using the repository browser.