source: tests/malloc.cfa@ 124400b

ast-experimental
Last change on this file since 124400b was 116a2ea, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

new heap and associated tests updated

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