IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 1, 2005, 1:50:50 PM (21 years ago)
Author:
evanalst
Message:

Update test cases to not print to STDERR or STDOUT values that change
with platforms.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/test/dataManip/tst_psRandom.c

    r3682 r4082  
    3434psS32 testStatus = true;
    3535
    36 psS32 t00()
    37 {
    38     psRandom *myRNG = NULL;
    39     psS32 currentId = psMemGetId();
    40     psS32 memLeaks = 0;
    41 
    42     printPositiveTestHeader(stdout,
    43                             "psRandom functions",
    44                             "psRandomAlloc()");
     36static psS32 testRandomAlloc(void);
     37static psS32 testRandomUniform(void);
     38static psS32 testRandomGaussian(void);
     39static psS32 testRandomPoisson(void);
     40static psS32 testRandomResetUniform(void);
     41static psS32 testRandomResetGaussian(void);
     42static psS32 testRandomResetPoisson(void);
     43
     44testDescription tests[] = {
     45                              {testRandomAlloc,000,"psRandomAlloc",0,false},
     46                              {testRandomUniform,000,"psRandomUniform",0,false},
     47                              {testRandomGaussian,000,"psRandomGaussian",0,false},
     48                              {testRandomPoisson,000,"psRandomPoisson",0,false},
     49                              {testRandomResetUniform,000,"psRandomReset",0,false},
     50                              {testRandomResetGaussian,000,"psRandomReset",0,false},
     51                              {testRandomResetPoisson,000,"psRandomReset",0,false},
     52                              {NULL}
     53                          };
     54
     55psS32 main(psS32 argc, char* argv[])
     56{
     57    if(!runTestSuite(stderr,"psRandom",tests,argc,argv)) {
     58        return 1;
     59    }
     60
     61    return 0;
     62}
     63
     64psS32 testRandomAlloc(void)
     65{
     66    psRandom *myRNG = NULL;
    4567
    4668    // Valid type allocation
    4769    myRNG = psRandomAlloc(PS_RANDOM_TAUS, SEED);
    4870    if (myRNG == NULL) {
    49         printf("ERROR: Could not allocate psRandom structure\n");
    50         testStatus = false;
     71        psError(PS_ERR_UNKNOWN,true,"Could not allocate psRandom structure");
     72        return 1;
    5173    }
    5274    if (myRNG->type != PS_RANDOM_TAUS) {
    53         psError(PS_ERR_UNKNOWN,true,"Did not have the expected type");
    54         testStatus = false;
     75        psError(PS_ERR_UNKNOWN,true,"Type %d not as expected %d",myRNG->type,PS_RANDOM_TAUS);
     76        return 2;
    5577    }
    5678    psFree(myRNG);
     
    6183    psLogSetDestination("dest:stderr");
    6284    if (myRNG == NULL) {
    63         printf("ERROR: Could not allocate psRandom structure\n");
    64         testStatus = false;
     85        psError(PS_ERR_UNKNOWN,true,"Could not allocate psRandom structure");
     86        return 3;
    6587    }
    6688    if (myRNG->type != PS_RANDOM_TAUS) {
    67         psError(PS_ERR_UNKNOWN,true,"Did not have the expected type");
    68         testStatus = false;
     89        psError(PS_ERR_UNKNOWN,true,"Type %d not as expected %d",myRNG->type,PS_RANDOM_TAUS);
     90        return 4;
    6991    }
    7092    psFree(myRNG);
     
    7597    if (myRNG != NULL) {
    7698        psError(PS_ERR_UNKNOWN,true,"Did not return NULL for invalid type");
    77         testStatus = false;
     99        return 5;
    78100    }
    79101
     
    82104    if(myRNG == NULL) {
    83105        psError(PS_ERR_UNKNOWN,true,"Did not return allocated psRandom");
    84         testStatus = false;
    85     }
    86     psFree(myRNG);
    87 
    88     psMemCheckCorruption(1);
    89     memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
    90     if (0 != memLeaks) {
    91         printf("ERROR: Memory Leaks! (%d leaks)", memLeaks);
    92         testStatus = false;
    93     }
    94 
    95     printFooter(stdout,
    96                 "psRandom functions",
    97                 "psRandomAlloc()",
    98                 testStatus);
    99 
    100     return(testStatus);
    101 }
    102 
    103 psS32 t01()
    104 {
    105     psRandom *myRNG = NULL;
    106     psS32 currentId = psMemGetId();
    107     psS32 memLeaks = 0;
     106        return 6;
     107    }
     108    psFree(myRNG);
     109
     110    return 0;
     111}
     112
     113psS32 testRandomUniform(void)
     114{
     115    psRandom *myRNG = NULL;
    108116    psVector *rans = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
    109117    psStats *stats = psStatsAlloc(PS_STAT_SAMPLE_MEAN);
    110118
    111     printPositiveTestHeader(stdout,
    112                             "psRandom functions",
    113                             "psRandomAlloc() and psRandomUniform()");
    114 
    115     myRNG = psRandomAlloc(PS_RANDOM_TAUS, SEED);
    116     if (myRNG == NULL) {
    117         printf("ERROR: Could not allocate psRandom structure\n");
    118         testStatus = false;
    119     }
    120 
     119    myRNG = psRandomAlloc(PS_RANDOM_TAUS, SEED);
     120    if (myRNG == NULL) {
     121        psError(PS_ERR_UNKNOWN,true,"Could not allocate psRandom structure");
     122        return 1;
     123    }
     124
     125    // Initialize vector data with random number
    121126    for (psS32 i = 0 ; i < NUM_DATA ; i++) {
    122127        rans->data.F64[i] = psRandomUniform(myRNG);
    123         //        printf("%.2f\n", rans->data.F64[i]);
    124     }
    125     stats = psVectorStats(stats, rans, NULL, NULL, 0);
    126     printf("Mean is %.2f\n", stats->sampleMean);
     128    }
     129    // Perform vector stats on random data (mean, stdev)
     130    stats = psVectorStats(stats, rans, NULL, NULL, 0);
    127131    stats->options = PS_STAT_SAMPLE_STDEV;
    128132    stats = psVectorStats(stats, rans, NULL, NULL, 0);
    129     printf("Standard deviation is %.2f\n", stats->sampleStdev);
     133    // Verify mean and stdev
    130134    if ((fabs(stats->sampleMean - UNIFORM_MEAN) / UNIFORM_MEAN) > ERROR_TOLERANCE) {
    131         printf("ERROR: psRandomUniform() mean is %.2f, should be %.2f\n", stats->sampleMean, UNIFORM_MEAN);
    132         testStatus = false;
     135        psError(PS_ERR_UNKNOWN,true,"psRandomUniform mean is %.2f, should be %.2f",
     136                stats->sampleMean, UNIFORM_MEAN);
     137        return 2;
    133138    }
    134139    if ((fabs(stats->sampleStdev - UNIFORM_STDEV) / UNIFORM_STDEV) > ERROR_TOLERANCE) {
    135         printf("ERROR: psRandomUniform() stdev is %.2f, should be %.2f\n", stats->sampleStdev, UNIFORM_STDEV);
    136         testStatus = false;
    137     }
    138 
    139     psMemCheckCorruption(1);
     140        psError(PS_ERR_UNKNOWN,true,"psRandomUniform stdev is %.2f, should be %.2f",
     141                stats->sampleStdev, UNIFORM_STDEV);
     142        return 3;
     143    }
     144
    140145    psFree(myRNG);
    141146    psFree(rans);
     
    145150    if(psRandomUniform(NULL) != 0) {
    146151        psError(PS_ERR_UNKNOWN,true,"Did not return zero for null psRandom");
    147         testStatus = false;
    148     }
    149 
    150     psMemCheckCorruption(1);
    151     memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
    152     if (0 != memLeaks) {
    153         psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
    154     }
    155 
    156     printFooter(stdout,
    157                 "psRandom functions",
    158                 "psRandomAlloc() and psRandomUniform()",
    159                 testStatus);
    160 
    161     return(testStatus);
    162 }
    163 
    164 psS32 t02()
    165 {
    166     psRandom *myRNG = NULL;
    167     psS32 currentId = psMemGetId();
    168     psS32 memLeaks = 0;
     152        return 4;
     153    }
     154
     155    return 0;
     156}
     157
     158psS32 testRandomGaussian(void)
     159{
     160    psRandom *myRNG = NULL;
    169161    psVector *rans = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
    170162    psStats *stats = psStatsAlloc(PS_STAT_SAMPLE_MEAN);
    171163
    172     printPositiveTestHeader(stdout,
    173                             "psRandom functions",
    174                             "psRandomAlloc() and psRandomGaussian()");
    175 
    176     myRNG = psRandomAlloc(PS_RANDOM_TAUS, SEED);
    177     if (myRNG == NULL) {
    178         printf("ERROR: Could not allocate psRandom structure\n");
    179         testStatus = false;
    180     }
    181 
     164    myRNG = psRandomAlloc(PS_RANDOM_TAUS, SEED);
     165    if (myRNG == NULL) {
     166        psError(PS_ERR_UNKNOWN,true,"Could not allocate psRandom structure");
     167        return 1;
     168    }
     169
     170    // Initialize vector with random data
    182171    for (psS32 i = 0 ; i < NUM_DATA ; i++) {
    183172        rans->data.F64[i] = psRandomGaussian(myRNG);
    184         //        printf("%.2f\n", rans->data.F64[i]);
    185     }
    186     stats = psVectorStats(stats, rans, NULL, NULL, 0);
    187     printf("Mean is %.2f\n", stats->sampleMean);
     173    }
     174
     175    // Perform vector stats on data (mean, stdev)
     176    stats = psVectorStats(stats, rans, NULL, NULL, 0);
    188177    stats->options = PS_STAT_SAMPLE_STDEV;
    189178    stats = psVectorStats(stats, rans, NULL, NULL, 0);
    190     printf("Standard deviation is %.2f\n", stats->sampleStdev);
     179
     180    // Verify statistics
    191181    if ((fabs(stats->sampleMean - GAUSSIAN_MEAN) / 1.0) > ERROR_TOLERANCE) {
    192         printf("ERROR: psRandomUniform() mean is %.2f, should be %.2f\n", stats->sampleMean, GAUSSIAN_MEAN);
    193         testStatus = false;
     182        psError(PS_ERR_UNKNOWN,true,"psRandomGaussian mean is %.2f, should be %.2f",
     183                stats->sampleMean, GAUSSIAN_MEAN);
     184        return 2;
    194185    }
    195186    if ((fabs(stats->sampleStdev - GAUSSIAN_STDEV) / GAUSSIAN_STDEV) > ERROR_TOLERANCE) {
    196         printf("ERROR: psRandomUniform() stdev is %.2f, should be %.2f\n", stats->sampleStdev, GAUSSIAN_STDEV);
    197         testStatus = false;
    198     }
    199 
    200     psMemCheckCorruption(1);
     187        psError(PS_ERR_UNKNOWN,true,"psRandomGaussian stdev is %.2f, should be %.2f",
     188                stats->sampleStdev, GAUSSIAN_STDEV);
     189        return 3;
     190    }
     191
    201192    psFree(myRNG);
    202193    psFree(rans);
     
    206197    if(psRandomGaussian(NULL) != 0) {
    207198        psError(PS_ERR_UNKNOWN,true,"Did not return zero for null psRandom");
    208         testStatus = false;
    209     }
    210 
    211     psMemCheckCorruption(1);
    212     memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
    213     if (0 != memLeaks) {
    214         psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
    215     }
    216 
    217     printFooter(stdout,
    218                 "psRandom functions",
    219                 "psRandomAlloc() and psRandomGaussian()",
    220                 testStatus);
    221 
    222     return(testStatus);
    223 }
    224 
    225 psS32 t03()
    226 {
    227     psRandom *myRNG = NULL;
    228     psS32 currentId = psMemGetId();
    229     psS32 memLeaks = 0;
     199        return 4;
     200    }
     201
     202    return 0;
     203}
     204
     205psS32 testRandomPoisson(void)
     206{
     207    psRandom *myRNG = NULL;
    230208    psVector *rans = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
    231209    psStats *stats = psStatsAlloc(PS_STAT_SAMPLE_MEAN);
    232210
    233     printPositiveTestHeader(stdout,
    234                             "psRandom functions",
    235                             "psRandomAlloc() and psRandomPoisson()");
    236 
    237     myRNG = psRandomAlloc(PS_RANDOM_TAUS, SEED);
    238     if (myRNG == NULL) {
    239         printf("ERROR: Could not allocate psRandom structure\n");
    240         testStatus = false;
    241     }
    242 
     211    myRNG = psRandomAlloc(PS_RANDOM_TAUS, SEED);
     212    if (myRNG == NULL) {
     213        psError(PS_ERR_UNKNOWN,true,"Could not allocate psRandom structure");
     214        return 1;
     215    }
     216
     217    // Initialize vector with random data
    243218    for (psS32 i = 0 ; i < NUM_DATA ; i++) {
    244219        rans->data.F64[i] = psRandomPoisson(myRNG, POISSON_MEAN);
    245         //        printf("%.2f\n", rans->data.F64[i]);
    246     }
    247     stats = psVectorStats(stats, rans, NULL, NULL, 0);
    248     printf("Mean is %.2f\n", stats->sampleMean);
     220    }
     221
     222    // Perform statistics on data
     223    stats = psVectorStats(stats, rans, NULL, NULL, 0);
    249224    stats->options = PS_STAT_SAMPLE_STDEV;
    250225    stats = psVectorStats(stats, rans, NULL, NULL, 0);
    251     printf("Standard deviation is %.2f\n", stats->sampleStdev);
    252226    if ((fabs(stats->sampleMean - POISSON_MEAN) / POISSON_MEAN) > ERROR_TOLERANCE) {
    253         printf("ERROR: psRandomUniform() mean is %.2f, should be %.2f\n", stats->sampleMean, POISSON_MEAN);
    254         testStatus = false;
     227        psError(PS_ERR_UNKNOWN,true,"psRandomPoisson mean is %.2f, should be %.2f",
     228                stats->sampleMean, POISSON_MEAN);
     229        return 2;
    255230    }
    256231    if ((fabs(stats->sampleStdev - POISSON_STDEV) / POISSON_STDEV) > ERROR_TOLERANCE) {
    257         printf("ERROR: psRandomUniform() stdev is %.2f, should be %.2f\n", stats->sampleStdev, POISSON_STDEV);
    258         testStatus = false;
    259     }
    260 
    261 
    262     psMemCheckCorruption(1);
     232        psError(PS_ERR_UNKNOWN,true,"psRandomPoisson stdev is %.2f, should be %.2f",
     233                stats->sampleStdev, POISSON_STDEV);
     234        return 3;
     235    }
     236
    263237    psFree(myRNG);
    264238    psFree(rans);
     
    268242    if(psRandomPoisson(NULL, POISSON_MEAN) != 0) {
    269243        psError(PS_ERR_UNKNOWN,true,"Did not return zero for null psRandom");
    270         testStatus = false;
    271     }
    272 
    273     psMemCheckCorruption(1);
    274     memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
    275     if (0 != memLeaks) {
    276         psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
    277     }
    278 
    279     printFooter(stdout,
    280                 "psRandom functions",
    281                 "psRandomAlloc() and psRandomPoisson()",
    282                 testStatus);
    283 
    284     return(testStatus);
    285 }
    286 
    287 psS32 t04()
    288 {
    289     psRandom *myRNG = NULL;
    290     psS32 currentId = psMemGetId();
    291     psS32 memLeaks = 0;
     244        return 4;
     245    }
     246
     247    return 0;
     248}
     249
     250psS32 testRandomResetUniform(void)
     251{
     252    psRandom *myRNG = NULL;
    292253    psVector *rans00 = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
    293254    psVector *rans01 = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
    294255    psVector *rans02 = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
    295256
    296     printPositiveTestHeader(stdout,
    297                             "psRandom functions",
    298                             "psRandomAlloc(), psRandomReset(), and psRandomUniform()");
    299     myRNG = psRandomAlloc(PS_RANDOM_TAUS, SEED);
    300     if (myRNG == NULL) {
    301         printf("ERROR: Could not allocate psRandom structure\n");
    302         testStatus = false;
    303     }
     257    myRNG = psRandomAlloc(PS_RANDOM_TAUS, SEED);
     258    if (myRNG == NULL) {
     259        psError(PS_ERR_UNKNOWN,true,"Could not allocate psRandom structure");
     260        return 1;
     261    }
     262
     263    // Random reset
    304264    psRandomReset(myRNG, SEED);
    305265    for (psS32 i = 0 ; i < NUM_DATA ; i++) {
     
    315275    }
    316276
     277    // Verify reset to original seed produces same results
    317278    for (psS32 i = 0 ; i < NUM_DATA ; i++) {
    318279        if (rans00->data.F64[i] != rans02->data.F64[i]) {
    319             printf("ERROR: psRandomUniform() did not produce the same results with the same seed\n");
     280            psError(PS_ERR_UNKNOWN,true,"psRandomUniform did not produce the same results with the same seed");
     281            return i+1;
    320282        }
    321283    }
    322284
    323     psMemCheckCorruption(1);
    324285    psFree(myRNG);
    325286    psFree(rans00);
     
    337298    psRandomReset(NULL,SEED);
    338299
    339     psMemCheckCorruption(1);
    340     memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
    341     if (0 != memLeaks) {
    342         psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
    343     }
    344 
    345     printFooter(stdout,
    346                 "psRandom functions",
    347                 "psRandomAlloc(), psRandomReset(), and psRandomUniform()",
    348                 testStatus);
    349 
    350     return(testStatus);
    351 }
    352 
    353 psS32 t05()
    354 {
    355     psRandom *myRNG = NULL;
    356     psS32 currentId = psMemGetId();
    357     psS32 memLeaks = 0;
     300    return 0;
     301}
     302
     303psS32 testRandomResetGaussian(void)
     304{
     305    psRandom *myRNG = NULL;
    358306    psVector *rans00 = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
    359307    psVector *rans01 = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
    360308    psVector *rans02 = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
    361309
    362     printPositiveTestHeader(stdout,
    363                             "psRandom functions",
    364                             "psRandomAlloc(), psRandomReset(), and psRandomGaussian()");
    365     myRNG = psRandomAlloc(PS_RANDOM_TAUS, SEED);
    366     if (myRNG == NULL) {
    367         printf("ERROR: Could not allocate psRandom structure\n");
    368         testStatus = false;
    369     }
     310    myRNG = psRandomAlloc(PS_RANDOM_TAUS, SEED);
     311    if (myRNG == NULL) {
     312        psError(PS_ERR_UNKNOWN,true,"Could not allocate psRandom structure");
     313        return 1;
     314    }
     315
     316    // Initialize random data in vectors
    370317    psRandomReset(myRNG, SEED);
    371318    for (psS32 i = 0 ; i < NUM_DATA ; i++) {
     
    381328    }
    382329
     330    // Verify data from original seed produces same data after reset
    383331    for (psS32 i = 0 ; i < NUM_DATA ; i++) {
    384332        if (rans00->data.F64[i] != rans02->data.F64[i]) {
    385             printf("ERROR: psRandomGaussian() did not produce the same results with the same seed\n");
     333            psError(PS_ERR_UNKNOWN,true,"psRandomGaussian did not produce the same results with the same seed");
     334            return 2;
    386335        }
    387336    }
    388337
    389     psMemCheckCorruption(1);
    390338    psFree(myRNG);
    391339    psFree(rans00);
    392340    psFree(rans01);
    393341    psFree(rans02);
    394     psMemCheckCorruption(1);
    395     memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
    396     if (0 != memLeaks) {
    397         psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
    398     }
    399 
    400     printFooter(stdout,
    401                 "psRandom functions",
    402                 "psRandomAlloc(), psRandomReset(), and psRandomGaussian()",
    403                 testStatus);
    404 
    405     return(testStatus);
    406 }
    407 
    408 psS32 t06()
    409 {
    410     psRandom *myRNG = NULL;
    411     psS32 currentId = psMemGetId();
    412     psS32 memLeaks = 0;
     342
     343    return 0;
     344}
     345
     346psS32 testRandomResetPoisson(void)
     347{
     348    psRandom *myRNG = NULL;
    413349    psVector *rans00 = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
    414350    psVector *rans01 = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
    415351    psVector *rans02 = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
    416352
    417     printPositiveTestHeader(stdout,
    418                             "psRandom functions",
    419                             "psRandomAlloc(), psRandomReset(), and psRandomPoisson()");
    420     myRNG = psRandomAlloc(PS_RANDOM_TAUS, SEED);
    421     if (myRNG == NULL) {
    422         printf("ERROR: Could not allocate psRandom structure\n");
    423         testStatus = false;
    424     }
     353    myRNG = psRandomAlloc(PS_RANDOM_TAUS, SEED);
     354    if (myRNG == NULL) {
     355        psError(PS_ERR_UNKNOWN,true,"Could not allocate psRandom structure");
     356        return 1;
     357    }
     358
     359    // Initialize vectors with random data
    425360    psRandomReset(myRNG, SEED);
    426361    for (psS32 i = 0 ; i < NUM_DATA ; i++) {
     
    436371    }
    437372
     373    // Verify the original seed produces same data after reset
    438374    for (psS32 i = 0 ; i < NUM_DATA ; i++) {
    439375        if (rans00->data.F64[i] != rans02->data.F64[i]) {
    440             printf("ERROR: psRandomPoisson() did not produce the same results with the same seed\n");
     376            psError(PS_ERR_UNKNOWN,true,"psRandomPoisson did not produce the same results with the same seed");
     377            return 2;
    441378        }
    442379    }
    443380
    444     psMemCheckCorruption(1);
    445381    psFree(myRNG);
    446382    psFree(rans00);
    447383    psFree(rans01);
    448384    psFree(rans02);
    449     psMemCheckCorruption(1);
    450     memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
    451     if (0 != memLeaks) {
    452         psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
    453     }
    454 
    455     printFooter(stdout,
    456                 "psRandom functions",
    457                 "psRandomAlloc(), psRandomReset(), and psRandomPoisson()",
    458                 testStatus);
    459 
    460     return(testStatus);
    461 }
    462 
    463 psS32 main()
    464 {
    465     testStatus = true;
    466 
    467     t00();
    468     t01();
    469     t02();
    470     t03();
    471     t04();
    472     t05();
    473     t06();
    474 
    475     return(!testStatus);
    476 }
     385
     386    return 0;
     387}
     388
Note: See TracChangeset for help on using the changeset viewer.