Changeset 05d499ac for tests/alloc.cfa


Ignore:
Timestamp:
Oct 14, 2022, 9:39:05 AM (19 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, master
Children:
59c7e3e, d0fa494
Parents:
80fbdc9
Message:

change printf to sout

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/alloc.cfa

    r80fbdc9 r05d499ac  
    1010// Created On       : Wed Feb  3 07:56:22 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Oct 13 21:39:34 2022
    13 // Update Count     : 449
     12// Last Modified On : Fri Oct 14 09:31:39 2022
     13// Update Count     : 491
    1414//
    1515
     
    3434        ip = (int *)malloc( sizeof(*ip) );                                      // C malloc, type unsafe
    3535        *ip = 0xdeadbeef;
    36         printf( "C   malloc %#x\n", *ip );
     36        sout | "C   malloc" | hex(*ip);
    3737        free( ip );
    3838
    3939        ip = malloc();                                                                          // CFA malloc, type safe
    4040        *ip = 0xdeadbeef;
    41         printf( "CFA malloc %#x\n", *ip );
     41        sout | "CFA malloc" | hex(*ip);
    4242        free( ip );
    4343
    4444        ip = alloc();                                                                           // CFA alloc, type safe
    4545        *ip = 0xdeadbeef;
    46         printf( "CFA alloc %#x\n", *ip );
     46        sout | "CFA alloc" | hex(*ip);
    4747        free( ip );
    4848
    4949        ip = alloc( fill`fill );                                                        // CFA alloc, fill
    50         printf( "CFA alloc, fill %08x\n", *ip );
     50        sout | "CFA alloc, fill" | wd(8, nobase(hex(*ip)));
    5151        free( ip );
    5252
    5353        ip = alloc( 3`fill );                                                           // CFA alloc, fill
    54         printf( "CFA alloc, fill %d\n", *ip );
     54        sout | "CFA alloc, fill" | *ip;
    5555        free( ip );
    5656
    5757
    5858        // allocation, array types
    59         printf( "\n" );
     59        sout | nl;
    6060
    6161        ip = (int *)calloc( dim, sizeof( *ip ) );                       // C array calloc, type unsafe
    62         printf( "C   array calloc, fill 0\n" );
    63         for ( i; dim ) { printf( "%#x ", ip[i] ); }
    64         printf( "\n" );
     62        sout | "C   array calloc, fill 0";
     63        for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; }
     64        sout | nl;
    6565        free( ip );
    6666
    6767        ip = calloc( dim );                                                                     // CFA array calloc, type safe
    68         printf( "CFA array calloc, fill 0\n" );
    69         for ( i; dim ) { printf( "%#x ", ip[i] ); }
    70         printf( "\n" );
     68        sout | "CFA array calloc, fill 0";
     69        for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; }
     70        sout | nl;
    7171        free( ip );
    7272
    7373        ip = alloc( dim );                                                                      // CFA array alloc, type safe
    7474        for ( i; dim ) { ip[i] = 0xdeadbeef; }
    75         printf( "CFA array alloc, no fill\n" );
    76         for ( i; dim ) { printf( "%#x ", ip[i] ); }
    77         printf( "\n" );
     75        sout | "CFA array alloc, no fill";
     76        for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; }
     77        sout | nl;
    7878        free( ip );
    7979
    8080        ip = alloc( 2 * dim, fill`fill );                                       // CFA array alloc, fill
    81         printf( "CFA array alloc, fill %#hhx\n", fill );
    82         for ( i; 2 * dim ) { printf( "%#x ", ip[i] ); }
    83         printf( "\n" );
     81        sout | "CFA array alloc, fill" | hex(fill);
     82        for ( i; 2 * dim ) { sout | hex(ip[i]) | ' ' | nonl; }
     83        sout | nl;
    8484        free( ip );
    8585
    8686        ip = alloc( 2 * dim, ((int)0xdeadbeef)`fill );          // CFA array alloc, fill
    87         printf( "CFA array alloc, fill %#hhx\n", 0xdeadbeef );
    88         for ( i; 2 * dim ) { printf( "%#x ", ip[i] ); }
    89         printf( "\n" );
     87        sout | "CFA array alloc, fill" | hex((char)0xdeadbeef);
     88        for ( i; 2 * dim ) { sout | hex(ip[i]) | ' ' | nonl; }
     89        sout | nl;
    9090        // do not free
    9191
    9292        ip1 = alloc( 2 * dim, [ip, 2 * dim]`fill );                     // CFA array alloc, fill
    93         printf( "CFA array alloc, fill from array\n" );
    94         for ( i; 2 * dim ) { printf( "%#x %#x, ", ip[i], ip1[i] ); }
     93        sout | "CFA array alloc, fill from array";
     94        for ( i; 2 * dim ) { sout | hex(ip[i]) | hex(ip1[i]) | ", " | nonl; }
    9595        free( ip1 );
    96         printf( "\n" );
     96        sout | nl;
    9797
    9898
    9999        // realloc, non-array types
    100         printf( "\n" );
     100        sout | nl;
    101101
    102102        ip = (int *)realloc( ip, dim * sizeof(*ip) );           // C realloc
    103         printf( "C realloc\n" );
    104         for ( i; dim ) { printf( "%#x ", ip[i] ); }
    105         printf( "\n" );
     103        sout | "C realloc";
     104        for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; }
     105        sout | nl;
    106106        // do not free
    107107
    108108        ip = realloc( ip, 2 * dim * sizeof(*ip) );                      // CFA realloc
    109109        for ( i; dim ~ 2 * dim ) { ip[i] = 0x1010101; }
    110         printf( "CFA realloc\n" );
    111         for ( i; 2 * dim ) { printf( "%#x ", ip[i] ); }
    112         printf( "\n" );
     110        sout | "CFA realloc";
     111        for ( i; 2 * dim ) { sout | hex(ip[i]) | ' ' | nonl; }
     112        sout | nl;
    113113        // do not free
    114114
    115115
    116116        // realloc, array types
    117         printf( "\n" );
     117        sout | nl;
    118118
    119119        ip = alloc( dim, ip`realloc );                                          // CFA realloc array alloc
    120120        for ( i; dim ) { ip[i] = 0xdeadbeef; }
    121         printf( "CFA realloc array alloc\n" );
    122         for ( i; dim ) { printf( "%#x ", ip[i] ); }
    123         printf( "\n" );
     121        sout | "CFA realloc array alloc";
     122        for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; }
     123        sout | nl;
    124124        // do not free
    125125
    126126        ip = alloc( 2 * dim, ip`realloc );                                      // CFA realloc array alloc
    127127        for ( i; dim ~ 2 * dim ) { ip[i] = 0x1010101; }         // fill upper part
    128         printf( "CFA realloc array alloc\n" );
    129         for ( i; 2 * dim ) { printf( "%#x ", ip[i] ); }
    130         printf( "\n" );
     128        sout | "CFA realloc array alloc";
     129        for ( i; 2 * dim ) { sout | hex(ip[i]) | ' ' | nonl; }
     130        sout | nl;
    131131        // do not free
    132132
    133133        ip = alloc( dim, ip`realloc );                                          // CFA realloc array alloc
    134         printf( "CFA realloc array alloc\n" );
    135         for ( i; dim ) { printf( "%#x ", ip[i] ); }
    136         printf( "\n" );
     134        sout | "CFA realloc array alloc";
     135        for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; }
     136        sout | nl;
    137137        // do not free
    138138
    139139        ip = alloc( 3 * dim, ip`realloc, fill`fill );           // CFA realloc array alloc, fill
    140         printf( "CFA realloc array alloc, fill\n" );
    141         for ( i; 3 * dim ) { printf( "%#x ", ip[i] ); }
    142         printf( "\n" );
     140        sout | "CFA realloc array alloc, fill";
     141        for ( i; 3 * dim ) { sout | hex(ip[i]) | ' ' | nonl; }
     142        sout | nl;
    143143        // do not free
    144144
    145145        ip = alloc( dim, ip`realloc, fill`fill );                       // CFA realloc array alloc, fill
    146         printf( "CFA realloc array alloc, fill\n" );
    147         for ( i; dim ) { printf( "%#x ", ip[i] ); }
    148         printf( "\n" );
     146        sout | "CFA realloc array alloc, fill";
     147        for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; }
     148        sout | nl;
    149149        // do not free
    150150
    151151        ip = alloc( 3 * dim, ip`realloc, fill`fill );           // CFA realloc array alloc, fill
    152         printf( "CFA realloc array alloc, fill\n" );
    153         for ( i; 3 * dim ) { printf( "%#x ", ip[i] ); }
    154         printf( "\n" );
     152        sout | "CFA realloc array alloc, fill";
     153        for ( i; 3 * dim ) { sout | hex(ip[i]) | ' ' | nonl; }
     154        sout | nl;
    155155        // do not free
    156156
    157157        ip = alloc( 5 * dim, ip`realloc, 5`fill );                      // CFA realloc array alloc, 5
    158         printf( "CFA realloc array alloc, 5\n" );
    159         for ( i; 5 * dim ) { printf( "%#x ", ip[i] ); }
    160         printf( "\n" );
     158        sout | "CFA realloc array alloc, 5";
     159        for ( i; 5 * dim ) { sout | hex(ip[i]) | ' ' | nonl; }
     160        sout | nl;
    161161        // do not free
    162162
    163163        ip = alloc( dim, ip`realloc, 5`fill );                          // CFA realloc array alloc, 5
    164         printf( "CFA realloc array alloc, 5\n" );
    165         for ( i; dim ) { printf( "%#x ", ip[i] ); }
    166         printf( "\n" );
     164        sout | "CFA realloc array alloc, 5";
     165        for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; }
     166        sout | nl;
    167167        // do not free
    168168
    169169        ip = alloc( 5 * dim, ip`realloc, 5`fill );                      // CFA realloc array alloc, 5
    170         printf( "CFA realloc array alloc, 5\n" );
    171         for ( i; 5 * dim ) { printf( "%#x ", ip[i] ); }
    172         printf( "\n" );
     170        sout | "CFA realloc array alloc, 5";
     171        for ( i; 5 * dim ) { sout | hex(ip[i]) | ' ' | nonl; }
     172        sout | nl;
    173173
    174174        free( ip );
     
    221221
    222222        // alignment, non-array types
    223         printf( "\n" );
     223        sout | nl;
    224224        enum { Alignment = 128 };
    225225
    226226        stp = &(*(Struct*)memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign
    227227        assert( (uintptr_t)stp % Alignment == 0 );
    228         printf( "C   memalign %d %g\n", stp->x, stp->y );
     228        sout | "C   memalign " | stp->x | stp->y;
    229229        free( stp );
    230230
    231231        stp = &(*memalign( Alignment )){ 42, 42.5 };            // CFA memalign
    232232        assert( (uintptr_t)stp % Alignment == 0 );
    233         printf( "CFA memalign %d %g\n", stp->x, stp->y );
     233        sout | "CFA memalign" | stp->x | stp->y;
    234234        free( stp );
    235235
     
    237237        *stp = (Struct){ 42, 42.5 };
    238238        assert( (uintptr_t)stp % Alignment == 0 );
    239         printf( "CFA posix_memalign %d %g\n", stp->x, stp->y );
     239        sout | "CFA posix_memalign" | stp->x | stp->y;
    240240        free( stp );
    241241
     
    243243        *stp = (Struct){ 42, 42.5 };
    244244        assert( (uintptr_t)stp % Alignment == 0 );
    245         printf( "CFA posix_memalign %d %g\n", stp->x, stp->y );
     245        sout | "CFA posix_memalign" | stp->x | stp->y;
    246246        free( stp );
    247247
    248248        stp = &(*alloc( Alignment`align)){ 42, 42.5 };          // CFA alloc_align
    249249        assert( (uintptr_t)stp % Alignment == 0 );
    250         printf( "CFA alloc_align %d %g\n", stp->x, stp->y );
     250        sout | "CFA alloc_align" | stp->x | stp->y;
    251251        free( stp );
    252252
    253253        stp = &(*alloc( Alignment`align )){ 42, 42.5 };         // CFA alloc_align
    254254        assert( (uintptr_t)stp % Alignment == 0 );
    255         printf( "CFA alloc_align %d %g\n", stp->x, stp->y );
     255        sout | "CFA alloc_align" | stp->x | stp->y;
    256256        free( stp );
    257257
    258258        stp = alloc( Alignment`align, fill`fill );                      // CFA memalign, fill
    259259        assert( (uintptr_t)stp % Alignment == 0 );
    260         printf( "CFA alloc_align fill %#x %a\n", stp->x, stp->y );
     260        sout | "CFA alloc_align fill" | hex(stp->x) | hex(stp->y);
    261261        free( stp );
    262262
    263263        stp = alloc( Alignment`align, (Struct){ 42, 42.5 }`fill ); // CFA memalign, fill
    264264        assert( (uintptr_t)stp % Alignment == 0 );
    265         printf( "CFA alloc_align fill %d %g\n", stp->x, stp->y );
     265        sout | "CFA alloc_align fill" | stp->x | stp->y;
    266266        // do not free
    267267
    268268        stp = &(*alloc( stp`realloc, 4096`align )){ 42, 42.5 }; // CFA realign
    269269        assert( (uintptr_t)stp % 4096 == 0 );
    270         printf( "CFA alloc_align %d %g\n", stp->x, stp->y );
     270        sout | "CFA alloc_align" | stp->x | stp->y;
    271271        free( stp );
    272272
    273273
    274274        // alignment, array types
    275         printf( "\n" );
     275        sout | nl;
    276276
    277277        stp = alloc( dim, Alignment`align );                // CFA array memalign
    278278        assert( (uintptr_t)stp % Alignment == 0 );
    279279        for ( i; dim ) { stp[i] = (Struct){ 42, 42.5 }; }
    280         printf( "CFA array alloc_align\n" );
    281         for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
    282         printf( "\n" );
     280        sout | "CFA array alloc_align";
     281        for ( i; dim ) { sout | stp[i].x | stp[i].y | ", " | nonl; }
     282        sout | nl;
    283283        free( stp );
    284284
    285285        stp = alloc( dim, Alignment`align, fill`fill );         // CFA array memalign, fill
    286286        assert( (uintptr_t)stp % Alignment == 0 );
    287         printf( "CFA array alloc_align, fill\n" );
    288         for ( i; dim ) { printf( "%#x %a, ", stp[i].x, stp[i].y ); }
    289         printf( "\n" );
     287        sout | "CFA array alloc_align, fill";
     288        for ( i; dim ) { sout | hex(stp[i].x) | hex(stp[i].y) | ", " | nonl; }
     289        sout | nl;
    290290        free( stp );
    291291
    292292        stp = alloc( dim, Alignment`align, ((Struct){ 42, 42.5 })`fill ); // CFA array memalign, fill
    293293        assert( (uintptr_t)stp % Alignment == 0 );
    294         printf( "CFA array alloc_align, fill\n" );
    295         for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
    296         printf( "\n" );
     294        sout | "CFA array alloc_align, fill";
     295        for ( i; dim ) { sout | stp[i].x | stp[i].y | ", " | nonl; }
     296        sout | nl;
    297297        // do not free
    298298
    299299        stp1 = alloc( dim, Alignment`align, [stp, dim]`fill );  // CFA array memalign, fill
    300300        assert( (uintptr_t)stp % Alignment == 0 );
    301         printf( "CFA array alloc_align, fill array\n" );
    302         for ( i; dim ) { printf( "%d %g, ", stp1[i].x, stp1[i].y ); }
    303         printf( "\n" );
     301        sout | "CFA array alloc_align, fill array";
     302        for ( i; dim ) { sout | stp1[i].x | stp1[i].y | ", " | nonl; }
     303        sout | nl;
    304304        free( stp1 );
    305305
     
    307307        assert( (uintptr_t)stp % 4096 == 0 );
    308308        for ( i; dim ) { stp[i] = (Struct){ 42, 42.5 }; }
    309         printf( "CFA realloc array alloc_align\n" );
    310         for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
    311         printf( "\n" );
     309        sout | "CFA realloc array alloc_align";
     310        for ( i; dim ) { sout | stp[i].x | stp[i].y | ", " | nonl; }
     311        sout | nl;
    312312        free( stp );
    313313
    314314
    315315        // data, non-array types
    316         printf( "\n" );
     316        sout | nl;
    317317
    318318        memset( &st, fill );                                // CFA memset, type safe
    319         printf( "CFA memset %#x %a\n", st.x, st.y );
     319        sout | "CFA memset" | hex(st.x) | hex(st.y);
    320320        memcpy( &st1, &st );                                // CFA memcpy, type safe
    321         printf( "CFA memcpy %#x %a\n", st1.x, st1.y );
     321        sout | "CFA memcpy" | hex(st1.x) | hex(st1.y);
    322322
    323323
    324324        // data, array types
    325         printf( "\n" );
     325        sout | nl;
    326326
    327327        amemset( sta, fill, dim );                                                      // CFA array memset, type safe
    328         printf( "CFA array memset\n" );
    329         for ( i; dim ) { printf( "%#x %a, ", sta[i].x, sta[i].y ); }
    330         printf( "\n" );
     328        sout | "CFA array memset";
     329        for ( i; dim ) { sout | hex(sta[i].x) | hex(sta[i].y) | ", " | nonl; }
     330        sout | nl;
    331331
    332332        amemcpy( sta1, sta, dim );                                                      // CFA array memcpy, type safe
    333         printf( "CFA array memcpy\n" );
    334         for ( i; dim ) { printf( "%#x %a, ", sta1[i].x, sta1[i].y ); }
    335         printf( "\n" );
     333        sout | "CFA array memcpy";
     334        for ( i; dim ) { sout | hex(sta1[i].x) | hex(sta1[i].y) | ", " | nonl; }
     335        sout | nl;
    336336
    337337        // new, non-array types
    338         printf( "\n" );
     338        sout | nl;
    339339
    340340        const_count = dest_count = 0;
     
    344344        assert( const_count == 2 && dest_count == 0 );          // assertion for testing
    345345
    346         printf( "CFA new initialize\n%d %g %d %g\n", stp->x, stp->y, stp1->x, stp1->y );
     346        sout | "CFA new initialize" | nl | stp->x | stp->y | stp1->x | stp1->y;
    347347        delete( stp, stp1 );
    348348        assert( const_count == 2 && dest_count == 2 );          // assertion for testing
     
    351351        stp = anew( dim, 42, 42.5 );
    352352        assert( const_count == 2 + dim && dest_count == 2 ); // assertion for testing
    353         printf( "CFA array new initialize\n" );
    354         for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
    355         printf( "\n" );
     353        sout | "CFA array new initialize";
     354        for ( i; dim ) { sout | stp[i].x | stp[i].y | ", " | nonl; }
     355        sout | nl;
    356356
    357357        stp1 = anew( dim, 42, 42.5 );
    358358        assert( const_count == 2 + 2 * dim && dest_count == 2 ); // assertion for testing
    359         for ( i; dim ) { printf( "%d %g, ", stp1[i].x, stp1[i].y ); }
    360         printf( "\n" );
     359        for ( i; dim ) { sout | stp1[i].x | stp1[i].y | ", " | nonl; }
     360        sout | nl;
    361361        adelete( stp, stp1 );
    362362        assert( const_count == 2 + 2 * dim && dest_count == 2 + 2 * dim); // assertion for testing
    363363
    364364        // extras
    365         printf( "\n" );
     365        sout | nl;
    366366
    367367        float * fp = malloc() + 1;
    368         printf( "pointer arithmetic %d\n", fp == fp - 1 );
     368        sout | "pointer arithmetic" | fp == fp - 1;
    369369        free( fp - 1 );
    370370
    371371        ip = foo( bar( baz( malloc(), 0 ), 0 ), 0 );
    372372        *ip = 0xdeadbeef;
    373         printf( "CFA deep malloc %#x\n", *ip );
     373        sout | "CFA deep malloc" | hex(*ip);
    374374
    375375        dp = alloc(5.0`fill);                                                           // just for testing multiple free
     
    379379#ifdef ERR1
    380380        stp = malloc();
    381         printf( "\nSHOULD FAIL\n" );
     381        sout | "\nSHOULD FAIL";
    382382        ip = realloc( stp, dim * sizeof( *stp ) );
    383383        ip = memset( stp, 10 );
Note: See TracChangeset for help on using the changeset viewer.