source: tests/malloc.cfa@ 37c3db8

Last change on this file since 37c3db8 was 10b5970, checked in by Michael Brooks <mlbrooks@…>, 9 months ago

Fix many test-suite- and libcfa-caused unused variable warnings.

In scope are easy fixes among tests whose sole warnings were unused variable. Reduces the wflags lax list by 40%.

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