IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 6, 2004, 12:34:06 PM (22 years ago)
Author:
desonia
Message:

astyle fixed?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/test/sysUtils/tst_psMemory.c

    r1365 r1406  
    66*  @author Robert DeSonia, MHPCC
    77*
    8 *  @version $Revision: 1.21 $ $Name: not supported by cvs2svn $
    9 *  @date $Date: 2004-08-02 19:43:23 $
     8*  @version $Revision: 1.22 $ $Name: not supported by cvs2svn $
     9*  @date $Date: 2004-08-06 22:34:06 $
    1010*
    1111*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    8080                              }
    8181                          };
    82                          
     82
    8383int main( int argc, char* argv[] )
    8484{
    8585    psLogSetLevel( PS_LOG_INFO );
    86    
     86
    8787    return ( ! runTestSuite( stderr, "psMemory", tests, argc, argv ) );
    8888}
     
    9494    const int size = 100;
    9595    int failed = 0;
    96    
     96
    9797    psLogMsg( __func__, PS_LOG_INFO, "psAlloc shall allocate memory blocks writeable by caller.\n" );
    98    
     98
    9999    mem = ( int* ) psAlloc( size * sizeof( int ) );
    100100    if ( mem == NULL ) {
    101             psError( __FILE__, "psAlloc returned a NULL value in %s!", __func__ );
    102             return 1;
     101        psError( __FILE__, "psAlloc returned a NULL value in %s!", __func__ );
     102        return 1;
     103    }
     104
     105    for ( int index = 0;index < size;index++ ) {
     106        mem[ index ] = index;
     107    }
     108
     109    for ( int index = 0;index < size;index++ ) {
     110        if ( mem[ index ] != index ) {
     111            failed++;
    103112        }
    104        
    105     for ( int index = 0;index < size;index++ ) {
    106             mem[ index ] = index;
    107         }
    108        
    109     for ( int index = 0;index < size;index++ ) {
    110             if ( mem[ index ] != index ) {
    111                     failed++;
    112                 }
    113         }
    114        
     113    }
     114
    115115    psFree( mem );
    116    
     116
    117117    return failed;
    118118}
     
    123123    int * mem;
    124124    int ref = 0;
    125    
     125
    126126    psLogMsg( __func__, PS_LOG_INFO, "memory reference count shall be incrementable/decrementable" );
    127    
     127
    128128    mem = ( int* ) psAlloc( 100 * sizeof( int ) );
    129    
     129
    130130    ref = psMemGetRefCounter( mem );
    131131    if ( ref != 1 ) {
    132             psError( __func__, "Expected to buffer reference count to be initially 1, but it was %d.", ref );
    133             return 1;
    134         }
    135        
     132        psError( __func__, "Expected to buffer reference count to be initially 1, but it was %d.", ref );
     133        return 1;
     134    }
     135
    136136    psMemIncrRefCounter( mem );
    137137    psMemIncrRefCounter( mem );
    138138    psMemIncrRefCounter( mem );
    139    
     139
    140140    ref = psMemGetRefCounter( mem );
    141141    if ( ref != 4 ) {
    142             psError( __func__, "Expected to find buffer reference count to be 4, but it was %d.", ref );
    143             return 1;
    144         }
    145        
     142        psError( __func__, "Expected to find buffer reference count to be 4, but it was %d.", ref );
     143        return 1;
     144    }
     145
    146146    psMemDecrRefCounter( mem );
    147147    psMemDecrRefCounter( mem );
    148    
     148
    149149    ref = psMemGetRefCounter( mem );
    150150    if ( ref != 2 ) {
    151             psError( __func__, "Expected to find buffer reference count to be 2, but it was %d.", ref );
    152             return 1;
    153         }
    154        
     151        psError( __func__, "Expected to find buffer reference count to be 2, but it was %d.", ref );
     152        return 1;
     153    }
     154
    155155    psLogMsg( __func__, PS_LOG_INFO, "psFree shall be just decrement a multiple refererenced pointer." );
    156    
     156
    157157    psFree( mem );
    158    
     158
    159159    ref = psMemGetRefCounter( mem );
    160160    if ( ref != 1 ) {
    161             psError( __func__, "Expected to find buffer reference count to be 1, but it was %d.", ref );
    162             return 1;
    163         }
    164        
     161        psError( __func__, "Expected to find buffer reference count to be 1, but it was %d.", ref );
     162        return 1;
     163    }
     164
    165165    psFree( mem );
    166    
     166
    167167    return 0;
    168168}
     
    174174    int * mem[ 100 ];
    175175    psMemExhaustedCallback cb;
    176    
    177     for ( int lcv = 0; lcv < 100; lcv++ ) {
    178             mem[ lcv ] = NULL;
    179         }
    180        
     176
     177    for ( int lcv = 0; lcv < 100; lcv++ ) {
     178        mem[ lcv ] = NULL;
     179    }
     180
    181181    psLogMsg( __func__, PS_LOG_INFO, "Upon requesting more memory than is available, psRealloc shall call "
    182182              "the psMemExhaustedCallback.\n" );
    183              
     183
    184184    exhaustedCallbackCalled = 0;
    185    
     185
    186186    cb = psMemExhaustedCallbackSet( TPOutOfMemoryExhaustedCallback );
    187    
    188     for ( int lcv = 0; lcv < 100; lcv++ ) {
    189             mem[ lcv ] = ( int* ) psAlloc( 10 );
    190         }
    191        
    192     for ( int lcv = 0; lcv < 100; lcv++ ) {
    193             mem[ lcv ] = ( int* ) psRealloc( mem[ lcv ], SIZE_MAX - 1000 );
    194         }
    195        
     187
     188    for ( int lcv = 0; lcv < 100; lcv++ ) {
     189        mem[ lcv ] = ( int* ) psAlloc( 10 );
     190    }
     191
     192    for ( int lcv = 0; lcv < 100; lcv++ ) {
     193        mem[ lcv ] = ( int* ) psRealloc( mem[ lcv ], SIZE_MAX - 1000 );
     194    }
     195
    196196    psMemExhaustedCallbackSet( cb );
    197    
     197
    198198    if ( exhaustedCallbackCalled == 0 ) {
    199             psError( __FILE__, "Called psRealloc with HUGE memory requirement and survived in %s!", __func__ );
    200             return 1;
    201         }
    202        
    203     for ( int lcv = 0; lcv < 100; lcv++ ) {
    204             psFree( mem[ lcv ] );
    205         }
    206        
     199        psError( __FILE__, "Called psRealloc with HUGE memory requirement and survived in %s!", __func__ );
     200        return 1;
     201    }
     202
     203    for ( int lcv = 0; lcv < 100; lcv++ ) {
     204        psFree( mem[ lcv ] );
     205    }
     206
    207207    return 0;
    208208}
     
    213213    int * mem[ 100 ];
    214214    psMemExhaustedCallback cb;
    215    
    216     for ( int lcv = 0; lcv < 100; lcv++ ) {
    217             mem[ lcv ] = NULL;
    218         }
    219        
     215
     216    for ( int lcv = 0; lcv < 100; lcv++ ) {
     217        mem[ lcv ] = NULL;
     218    }
     219
    220220    psLogMsg( __func__, PS_LOG_INFO, "Upon requesting more memory than is available, psalloc shall call "
    221221              "the psMemExhaustedCallback.\n" );
    222              
     222
    223223    exhaustedCallbackCalled = 0;
    224    
     224
    225225    cb = psMemExhaustedCallbackSet( TPOutOfMemoryExhaustedCallback );
    226    
    227     for ( int lcv = 0; lcv < 100; lcv++ ) {
    228             mem[ lcv ] = ( int* ) psAlloc( SIZE_MAX - 1000 );
    229         }
    230        
     226
     227    for ( int lcv = 0; lcv < 100; lcv++ ) {
     228        mem[ lcv ] = ( int* ) psAlloc( SIZE_MAX - 1000 );
     229    }
     230
    231231    psMemExhaustedCallbackSet( cb );
    232    
     232
    233233    if ( exhaustedCallbackCalled == 0 ) {
    234             psError( __FILE__, "Called psAlloc with HUGE memory requirement and survived in %s!", __func__ );
    235             return 1;
    236         }
    237        
    238     for ( int lcv = 0; lcv < 100; lcv++ ) {
    239             psFree( mem[ lcv ] );
    240         }
    241        
     234        psError( __FILE__, "Called psAlloc with HUGE memory requirement and survived in %s!", __func__ );
     235        return 1;
     236    }
     237
     238    for ( int lcv = 0; lcv < 100; lcv++ ) {
     239        psFree( mem[ lcv ] );
     240    }
     241
    242242    return 0;
    243243}
     
    250250    int* mem3;
    251251    const int initialSize = 100;
    252    
     252
    253253    psLogMsg( __func__, PS_LOG_INFO, "psRealloc shall increase/decrease memory buffer while "
    254254              "preserving contents" );
    255              
     255
    256256    // allocate buffer with known values.
    257257    mem1 = ( int* ) psAlloc( initialSize * sizeof( int ) );
     
    259259    mem3 = ( int* ) psAlloc( initialSize * sizeof( int ) );
    260260    for ( int lcv = 0;lcv < initialSize;lcv++ ) {
    261             mem1[ lcv ] = mem2[ lcv ] = mem3[ lcv ] = lcv;
    262         }
    263        
     261        mem1[ lcv ] = mem2[ lcv ] = mem3[ lcv ] = lcv;
     262    }
     263
    264264    psMemCheckCorruption( 1 );
    265265    psLogMsg( __func__, PS_LOG_INFO, "Expanding memory buffer." );
    266    
     266
    267267    // realloc to 2x
    268268    mem1 = ( int* ) psRealloc( mem1, 2 * initialSize * sizeof( int ) );
    269269    mem2 = ( int* ) psRealloc( mem2, 2 * initialSize * sizeof( int ) );
    270270    mem3 = ( int* ) psRealloc( mem3, 2 * initialSize * sizeof( int ) );
    271    
     271
    272272    // check values of initial block
    273273    for ( int i = 0;i < initialSize;i++ ) {
    274             if ( mem1[ i ] != i || mem2[ i ] != i || mem3[ i ] != i ) {
    275                     psError( __FILE__, "Realloc didn't preserve the contents with expanding buffer in %s.",
    276                              __func__ );
    277                     break;
    278                 }
     274        if ( mem1[ i ] != i || mem2[ i ] != i || mem3[ i ] != i ) {
     275            psError( __FILE__, "Realloc didn't preserve the contents with expanding buffer in %s.",
     276                     __func__ );
     277            break;
    279278        }
    280        
     279    }
     280
    281281    psMemCheckCorruption( 1 );
    282282    psLogMsg( __func__, PS_LOG_INFO, "Shrinking memory buffer." );
    283    
     283
    284284    // realloc to 1/2 initial value.
    285285    mem1 = ( int* ) psRealloc( mem1, ( initialSize / 2 ) * sizeof( int ) );
    286286    mem2 = ( int* ) psRealloc( mem2, ( initialSize / 2 ) * sizeof( int ) );
    287287    mem3 = ( int* ) psRealloc( mem3, ( initialSize / 2 ) * sizeof( int ) );
    288    
     288
    289289    // check values of initial block
    290290    for ( int i = 0;i < initialSize / 2;i++ ) {
    291             if ( mem1[ i ] != i || mem2[ i ] != i || mem3[ i ] != i ) {
    292                     psError( __FILE__, "Realloc didn't preserve the contents with shrinking buffer in %s.",
    293                              __func__ );
    294                     break;
    295                 }
     291        if ( mem1[ i ] != i || mem2[ i ] != i || mem3[ i ] != i ) {
     292            psError( __FILE__, "Realloc didn't preserve the contents with shrinking buffer in %s.",
     293                     __func__ );
     294            break;
    296295        }
    297        
     296    }
     297
    298298    psFree( mem1 );
    299299    psFree( mem2 );
    300300    psFree( mem3 );
    301    
     301
    302302    return 0;
    303303}
     
    311311    const int initialSize = 100;
    312312    int mark;
    313    
     313
    314314    allocCallbackCalled = 0;
    315315    freeCallbackCalled = 0;
    316316    psMemAllocateCallbackSet( memAllocateCallback );
    317317    psMemFreeCallbackSet( memFreeCallback );
    318    
     318
    319319    psMemAllocateCallbackSetID( currentId + 1 );
    320320    psMemFreeCallbackSetID( currentId + 1 );
    321    
     321
    322322    psLogMsg( __func__, PS_LOG_INFO, "call to psAlloc/psRealloc shall generate a callback if specified "
    323323              "memory ID is allocated." );
    324              
     324
    325325    // allocate buffer with known values.
    326326    mem1 = ( int* ) psAlloc( initialSize * sizeof( int ) );
    327327    mem2 = ( int* ) psAlloc( initialSize * sizeof( int ) );
    328328    mem3 = ( int* ) psAlloc( initialSize * sizeof( int ) );
    329    
     329
    330330    psFree( mem1 );
    331331    psFree( mem2 );
    332332    psFree( mem3 );
    333    
     333
    334334    if ( allocCallbackCalled != 2 || freeCallbackCalled != 2 ) {
    335             psError( __FILE__, "alloc/free callbacks were not called the proper number of times in %s",
    336                      __func__ );
    337             return 1;
    338         }
    339        
     335        psError( __FILE__, "alloc/free callbacks were not called the proper number of times in %s",
     336                 __func__ );
     337        return 1;
     338    }
     339
    340340    allocCallbackCalled = 0;
    341341    freeCallbackCalled = 0;
    342    
     342
    343343    mark = psMemGetId();
    344    
     344
    345345    mem1 = ( int* ) psAlloc( initialSize * sizeof( int ) );
    346    
     346
    347347    psMemAllocateCallbackSetID( mark );
    348    
     348
    349349    mem1 = ( int* ) psRealloc( mem1, initialSize * 2 * sizeof( int ) );
    350    
     350
    351351    psFree( mem1 );
    352    
     352
    353353    if ( allocCallbackCalled != 2 ) {
    354             psError( __FILE__, "realloc callbacks were not called the proper number of times in %s",
    355                      __func__ );
    356             return 1;
    357         }
    358        
    359     return 0;
    360    
     354        psError( __FILE__, "realloc callbacks were not called the proper number of times in %s",
     355                 __func__ );
     356        return 1;
     357    }
     358
     359    return 0;
     360
    361361}
    362362
     
    370370    int nLeaks = 0;
    371371    int lineMark = 0;
    372    
     372
    373373    psLogMsg( __func__, PS_LOG_INFO, "psMemCheckLeaks shall return the number of blocks above an ID "
    374374              "that are still allocated" );
    375              
     375
    376376    for ( lcv = 0;lcv < numBuffers;lcv++ ) {
    377             lineMark = __LINE__ + 1;
    378             buffers[ lcv ] = psAlloc( sizeof( int ) );
    379         }
    380        
     377        lineMark = __LINE__ + 1;
     378        buffers[ lcv ] = psAlloc( sizeof( int ) );
     379    }
     380
    381381    for ( lcv = 1;lcv < numBuffers;lcv++ ) {
     382        psFree( buffers[ lcv ] );
     383    }
     384
     385    psLogMsg( __func__, PS_LOG_INFO, "following psMemCheckLeaks call should produce one instance." );
     386
     387    nLeaks = psMemCheckLeaks( currentId, &blks, stderr );
     388
     389    if ( nLeaks != 1 ) {
     390        psError( __FILE__, "psMemCheckLeaks should have found 1 leak, but found %d in %s.", nLeaks, __func__ );
     391        return 1;
     392    }
     393
     394    if ( blks[ 0 ] ->lineno != lineMark ) {
     395        psError( __FILE__, "psMemCheckLeaks found a leak other than the expected one (line %d vs %d) in %s.",
     396                 lineMark, blks[ 0 ] ->lineno, __func__ );
     397        return 1;
     398    }
     399
     400    psFree( buffers[ 0 ] );
     401    psFree( blks );
     402
     403    psLogMsg( __func__, PS_LOG_INFO, "Testing psMemCheckLeaks again with a different leak location" );
     404    psMemCheckLeaks( currentId, NULL, stderr );
     405
     406    for ( lcv = 0;lcv < numBuffers;lcv++ ) {
     407        lineMark = __LINE__ + 1;
     408        buffers[ lcv ] = psAlloc( sizeof( int ) );
     409    }
     410
     411    for ( lcv = 0;lcv < numBuffers - 1;lcv++ ) {
     412        psFree( buffers[ lcv ] );
     413    }
     414
     415    psLogMsg( __func__, PS_LOG_INFO, "following psMemCheckLeaks call should produce one error." );
     416
     417    nLeaks = psMemCheckLeaks( currentId, &blks, stderr );
     418
     419    if ( nLeaks != 1 ) {
     420        psError( __FILE__, "psMemCheckLeaks should have found 1 leak, but found %d in %s.", nLeaks, __func__ );
     421        return 1;
     422    }
     423
     424    if ( blks[ 0 ] ->lineno != lineMark ) {
     425        psError( __FILE__, "psMemCheckLeaks found a leak other than the expected one in %s.", __func__ );
     426        return 1;
     427    }
     428
     429    psFree( buffers[ 4 ] );
     430    psFree( blks );
     431
     432    psLogMsg( __func__, PS_LOG_INFO, "Testing psMemCheckLeaks again with multiple leak locations." );
     433
     434    for ( lcv = 0;lcv < numBuffers;lcv++ ) {
     435        lineMark = __LINE__ + 1;
     436        buffers[ lcv ] = psAlloc( sizeof( int ) );
     437    }
     438
     439    for ( lcv = 0;lcv < numBuffers;lcv++ ) {
     440        if ( lcv % 2 == 0 ) {
    382441            psFree( buffers[ lcv ] );
    383442        }
    384        
    385     psLogMsg( __func__, PS_LOG_INFO, "following psMemCheckLeaks call should produce one instance." );
    386    
     443    }
     444
     445    psLogMsg( __func__, PS_LOG_INFO, "following psMemCheckLeaks call should produce two errors." );
     446
    387447    nLeaks = psMemCheckLeaks( currentId, &blks, stderr );
    388    
    389     if ( nLeaks != 1 ) {
    390             psError( __FILE__, "psMemCheckLeaks should have found 1 leak, but found %d in %s.", nLeaks, __func__ );
    391             return 1;
    392         }
    393        
     448
     449    if ( nLeaks != 2 ) {
     450        psError( __FILE__, "psMemCheckLeaks should have found 1 leak, but found %d in %s.", nLeaks, __func__ );
     451        return 1;
     452    }
     453
    394454    if ( blks[ 0 ] ->lineno != lineMark ) {
    395             psError( __FILE__, "psMemCheckLeaks found a leak other than the expected one (line %d vs %d) in %s.",
    396                      lineMark, blks[ 0 ] ->lineno, __func__ );
    397             return 1;
    398         }
    399        
    400     psFree( buffers[ 0 ] );
    401     psFree( blks );
    402    
    403     psLogMsg( __func__, PS_LOG_INFO, "Testing psMemCheckLeaks again with a different leak location" );
    404     psMemCheckLeaks( currentId, NULL, stderr );
    405    
    406     for ( lcv = 0;lcv < numBuffers;lcv++ ) {
    407             lineMark = __LINE__ + 1;
    408             buffers[ lcv ] = psAlloc( sizeof( int ) );
    409         }
    410        
    411     for ( lcv = 0;lcv < numBuffers - 1;lcv++ ) {
    412             psFree( buffers[ lcv ] );
    413         }
    414        
    415     psLogMsg( __func__, PS_LOG_INFO, "following psMemCheckLeaks call should produce one error." );
    416    
    417     nLeaks = psMemCheckLeaks( currentId, &blks, stderr );
    418    
    419     if ( nLeaks != 1 ) {
    420             psError( __FILE__, "psMemCheckLeaks should have found 1 leak, but found %d in %s.", nLeaks, __func__ );
    421             return 1;
    422         }
    423        
    424     if ( blks[ 0 ] ->lineno != lineMark ) {
    425             psError( __FILE__, "psMemCheckLeaks found a leak other than the expected one in %s.", __func__ );
    426             return 1;
    427         }
    428        
    429     psFree( buffers[ 4 ] );
    430     psFree( blks );
    431    
    432     psLogMsg( __func__, PS_LOG_INFO, "Testing psMemCheckLeaks again with multiple leak locations." );
    433    
    434     for ( lcv = 0;lcv < numBuffers;lcv++ ) {
    435             lineMark = __LINE__ + 1;
    436             buffers[ lcv ] = psAlloc( sizeof( int ) );
    437         }
    438        
    439     for ( lcv = 0;lcv < numBuffers;lcv++ ) {
    440             if ( lcv % 2 == 0 ) {
    441                     psFree( buffers[ lcv ] );
    442                 }
    443         }
    444        
    445     psLogMsg( __func__, PS_LOG_INFO, "following psMemCheckLeaks call should produce two errors." );
    446    
    447     nLeaks = psMemCheckLeaks( currentId, &blks, stderr );
    448    
    449     if ( nLeaks != 2 ) {
    450             psError( __FILE__, "psMemCheckLeaks should have found 1 leak, but found %d in %s.", nLeaks, __func__ );
    451             return 1;
    452         }
    453        
    454     if ( blks[ 0 ] ->lineno != lineMark ) {
    455             psError( __FILE__, "psMemCheckLeaks found a leak other than the expected one in %s.", __func__ );
    456             return 1;
    457         }
    458        
     455        psError( __FILE__, "psMemCheckLeaks found a leak other than the expected one in %s.", __func__ );
     456        return 1;
     457    }
     458
    459459    psFree( blks );
    460460    psFree( buffers[ 1 ] );
    461461    psFree( buffers[ 3 ] );
    462    
     462
    463463    return 0;
    464464}
     
    470470    int corruptions = 0;
    471471    psMemProblemCallback cb;
    472    
     472
    473473    psLogMsg( __func__, PS_LOG_INFO, "psMemCheckCorruption shall detect memory corruptions" );
    474    
     474
    475475    buffer = psAlloc( sizeof( int ) );
    476    
     476
    477477    // cause memory corruption via buffer underflow
    478478    *buffer = 1;
     
    480480    oldValue = *buffer;
    481481    *buffer = 2;
    482    
     482
    483483    problemCallbackCalled = 0;
    484484    cb = psMemProblemCallbackSet( memProblemCallback );
    485    
     485
    486486    psLogMsg( __func__, PS_LOG_INFO, "psMemCheckCorruption should output an error message and "
    487487              "memProblemCallback callback should be called." );
    488              
     488
    489489    corruptions = psMemCheckCorruption( 0 );
    490    
     490
    491491    // restore the memory problem callback
    492492    psMemProblemCallbackSet( cb );
    493    
     493
    494494    // restore the value, 'uncorrupting' the buffer
    495495    *buffer = oldValue;
    496496    buffer++;
    497    
     497
    498498    psFree( buffer );
    499    
     499
    500500    if ( corruptions != 1 ) {
    501             psError( __FILE__, "Expected one memory corruption but found %d in %s.",
    502                      corruptions, __func__ );
    503             return 1;
    504         }
    505        
     501        psError( __FILE__, "Expected one memory corruption but found %d in %s.",
     502                 corruptions, __func__ );
     503        return 1;
     504    }
     505
    506506    if ( problemCallbackCalled != 1 ) {
    507             psError( __FILE__, "The memProblemCallback was not invoked but should have been in %s",
    508                      __func__ );
    509             return 1;
    510         }
    511        
    512     return 0;
    513    
     507        psError( __FILE__, "The memProblemCallback was not invoked but should have been in %s",
     508                 __func__ );
     509        return 1;
     510    }
     511
     512    return 0;
     513
    514514}
    515515
     
    547547
    548548    void * buffer = psAlloc( 1024 );
    549    
     549
    550550    psFree( buffer );
    551    
     551
    552552    psLogMsg( __func__, PS_LOG_INFO, "Next should be an error about multiple freeing." );
    553553    psFree( buffer );
    554    
    555     return 0;
    556 }
     554
     555    return 0;
     556}
Note: See TracChangeset for help on using the changeset viewer.