source: tests/malloc.cfa@ 4be0117

Last change on this file since 4be0117 was 829821c, checked in by Michael Brooks <mlbrooks@…>, 8 months ago

Fix test suite to run with no warnings on gcc 12, 13, 14

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