Changeset 4082 for trunk/psLib/test/dataManip/tst_psRandom.c
- Timestamp:
- Jun 1, 2005, 1:50:50 PM (21 years ago)
- File:
-
- 1 edited
-
trunk/psLib/test/dataManip/tst_psRandom.c (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/test/dataManip/tst_psRandom.c
r3682 r4082 34 34 psS32 testStatus = true; 35 35 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()"); 36 static psS32 testRandomAlloc(void); 37 static psS32 testRandomUniform(void); 38 static psS32 testRandomGaussian(void); 39 static psS32 testRandomPoisson(void); 40 static psS32 testRandomResetUniform(void); 41 static psS32 testRandomResetGaussian(void); 42 static psS32 testRandomResetPoisson(void); 43 44 testDescription 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 55 psS32 main(psS32 argc, char* argv[]) 56 { 57 if(!runTestSuite(stderr,"psRandom",tests,argc,argv)) { 58 return 1; 59 } 60 61 return 0; 62 } 63 64 psS32 testRandomAlloc(void) 65 { 66 psRandom *myRNG = NULL; 45 67 46 68 // Valid type allocation 47 69 myRNG = psRandomAlloc(PS_RANDOM_TAUS, SEED); 48 70 if (myRNG == NULL) { 49 p rintf("ERROR: Could not allocate psRandom structure\n");50 testStatus = false;71 psError(PS_ERR_UNKNOWN,true,"Could not allocate psRandom structure"); 72 return 1; 51 73 } 52 74 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; 55 77 } 56 78 psFree(myRNG); … … 61 83 psLogSetDestination("dest:stderr"); 62 84 if (myRNG == NULL) { 63 p rintf("ERROR: Could not allocate psRandom structure\n");64 testStatus = false;85 psError(PS_ERR_UNKNOWN,true,"Could not allocate psRandom structure"); 86 return 3; 65 87 } 66 88 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; 69 91 } 70 92 psFree(myRNG); … … 75 97 if (myRNG != NULL) { 76 98 psError(PS_ERR_UNKNOWN,true,"Did not return NULL for invalid type"); 77 testStatus = false;99 return 5; 78 100 } 79 101 … … 82 104 if(myRNG == NULL) { 83 105 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 113 psS32 testRandomUniform(void) 114 { 115 psRandom *myRNG = NULL; 108 116 psVector *rans = psVectorAlloc(NUM_DATA, PS_TYPE_F64); 109 117 psStats *stats = psStatsAlloc(PS_STAT_SAMPLE_MEAN); 110 118 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 121 126 for (psS32 i = 0 ; i < NUM_DATA ; i++) { 122 127 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); 127 131 stats->options = PS_STAT_SAMPLE_STDEV; 128 132 stats = psVectorStats(stats, rans, NULL, NULL, 0); 129 printf("Standard deviation is %.2f\n", stats->sampleStdev);133 // Verify mean and stdev 130 134 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; 133 138 } 134 139 if ((fabs(stats->sampleStdev - UNIFORM_STDEV) / UNIFORM_STDEV) > ERROR_TOLERANCE) { 135 p rintf("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 140 145 psFree(myRNG); 141 146 psFree(rans); … … 145 150 if(psRandomUniform(NULL) != 0) { 146 151 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 158 psS32 testRandomGaussian(void) 159 { 160 psRandom *myRNG = NULL; 169 161 psVector *rans = psVectorAlloc(NUM_DATA, PS_TYPE_F64); 170 162 psStats *stats = psStatsAlloc(PS_STAT_SAMPLE_MEAN); 171 163 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 182 171 for (psS32 i = 0 ; i < NUM_DATA ; i++) { 183 172 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); 188 177 stats->options = PS_STAT_SAMPLE_STDEV; 189 178 stats = psVectorStats(stats, rans, NULL, NULL, 0); 190 printf("Standard deviation is %.2f\n", stats->sampleStdev); 179 180 // Verify statistics 191 181 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; 194 185 } 195 186 if ((fabs(stats->sampleStdev - GAUSSIAN_STDEV) / GAUSSIAN_STDEV) > ERROR_TOLERANCE) { 196 p rintf("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 201 192 psFree(myRNG); 202 193 psFree(rans); … … 206 197 if(psRandomGaussian(NULL) != 0) { 207 198 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 205 psS32 testRandomPoisson(void) 206 { 207 psRandom *myRNG = NULL; 230 208 psVector *rans = psVectorAlloc(NUM_DATA, PS_TYPE_F64); 231 209 psStats *stats = psStatsAlloc(PS_STAT_SAMPLE_MEAN); 232 210 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 243 218 for (psS32 i = 0 ; i < NUM_DATA ; i++) { 244 219 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); 249 224 stats->options = PS_STAT_SAMPLE_STDEV; 250 225 stats = psVectorStats(stats, rans, NULL, NULL, 0); 251 printf("Standard deviation is %.2f\n", stats->sampleStdev);252 226 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; 255 230 } 256 231 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 263 237 psFree(myRNG); 264 238 psFree(rans); … … 268 242 if(psRandomPoisson(NULL, POISSON_MEAN) != 0) { 269 243 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 250 psS32 testRandomResetUniform(void) 251 { 252 psRandom *myRNG = NULL; 292 253 psVector *rans00 = psVectorAlloc(NUM_DATA, PS_TYPE_F64); 293 254 psVector *rans01 = psVectorAlloc(NUM_DATA, PS_TYPE_F64); 294 255 psVector *rans02 = psVectorAlloc(NUM_DATA, PS_TYPE_F64); 295 256 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 304 264 psRandomReset(myRNG, SEED); 305 265 for (psS32 i = 0 ; i < NUM_DATA ; i++) { … … 315 275 } 316 276 277 // Verify reset to original seed produces same results 317 278 for (psS32 i = 0 ; i < NUM_DATA ; i++) { 318 279 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; 320 282 } 321 283 } 322 284 323 psMemCheckCorruption(1);324 285 psFree(myRNG); 325 286 psFree(rans00); … … 337 298 psRandomReset(NULL,SEED); 338 299 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 303 psS32 testRandomResetGaussian(void) 304 { 305 psRandom *myRNG = NULL; 358 306 psVector *rans00 = psVectorAlloc(NUM_DATA, PS_TYPE_F64); 359 307 psVector *rans01 = psVectorAlloc(NUM_DATA, PS_TYPE_F64); 360 308 psVector *rans02 = psVectorAlloc(NUM_DATA, PS_TYPE_F64); 361 309 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 370 317 psRandomReset(myRNG, SEED); 371 318 for (psS32 i = 0 ; i < NUM_DATA ; i++) { … … 381 328 } 382 329 330 // Verify data from original seed produces same data after reset 383 331 for (psS32 i = 0 ; i < NUM_DATA ; i++) { 384 332 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; 386 335 } 387 336 } 388 337 389 psMemCheckCorruption(1);390 338 psFree(myRNG); 391 339 psFree(rans00); 392 340 psFree(rans01); 393 341 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 346 psS32 testRandomResetPoisson(void) 347 { 348 psRandom *myRNG = NULL; 413 349 psVector *rans00 = psVectorAlloc(NUM_DATA, PS_TYPE_F64); 414 350 psVector *rans01 = psVectorAlloc(NUM_DATA, PS_TYPE_F64); 415 351 psVector *rans02 = psVectorAlloc(NUM_DATA, PS_TYPE_F64); 416 352 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 425 360 psRandomReset(myRNG, SEED); 426 361 for (psS32 i = 0 ; i < NUM_DATA ; i++) { … … 436 371 } 437 372 373 // Verify the original seed produces same data after reset 438 374 for (psS32 i = 0 ; i < NUM_DATA ; i++) { 439 375 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; 441 378 } 442 379 } 443 380 444 psMemCheckCorruption(1);445 381 psFree(myRNG); 446 382 psFree(rans00); 447 383 psFree(rans01); 448 384 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.
