Changeset 3281
- Timestamp:
- Feb 17, 2005, 2:01:39 PM (21 years ago)
- Location:
- trunk/psLib/test/collections
- Files:
-
- 2 deleted
- 2 edited
-
Makefile.am (modified) (2 diffs)
-
tst_psArray.c (modified) (3 diffs)
-
tst_psArray01.c (deleted)
-
tst_psArray02.c (deleted)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/test/collections/Makefile.am
r3275 r3281 18 18 tst_psVector \ 19 19 tst_psArray \ 20 tst_psArray01 \21 tst_psArray02 \22 20 tst_psBitSet \ 23 21 tst_psVectorSort_01 \ … … 42 40 tst_psVector_SOURCES = tst_psVector.c 43 41 tst_psArray_SOURCES = tst_psArray.c 44 tst_psArray01_SOURCES = tst_psArray01.c45 tst_psArray02_SOURCES = tst_psArray02.c46 42 tst_psBitSet_SOURCES = tst_psBitSet.c 47 43 tst_psVectorSort_01_SOURCES = tst_psVectorSort_01.c -
trunk/psLib/test/collections/tst_psArray.c
r3264 r3281 17 17 * @author Ross Harman, MHPCC 18 18 * 19 * @version $Revision: 1.1 0$ $Name: not supported by cvs2svn $20 * @date $Date: 2005-02-1 7 19:26:24$19 * @version $Revision: 1.11 $ $Name: not supported by cvs2svn $ 20 * @date $Date: 2005-02-18 00:01:39 $ 21 21 * 22 22 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 36 36 testStruct; 37 37 38 39 psS32 main(psS32 argc, 40 char* argv[]) 38 static int testStructCompare(const void **a, const void **b) 39 { 40 testStruct* first = (testStruct*)*a; 41 testStruct* second = (testStruct*)*b; 42 43 if(first->x < second->x) 44 return -1; 45 else if (first->x > second->x) 46 return 1; 47 else 48 return 0; 49 } 50 51 static psS32 testArray( void ); 52 static psS32 testArray01( void ); 53 static psS32 testArrayAdd( void ); 54 55 testDescription tests[] = { 56 {testArray, -1, "psArray", 0, false}, 57 {testArray01, -2, "psArray", 0, false}, 58 {testArrayAdd, 788, "psArrayAdd", 0, false}, 59 {NULL} 60 }; 61 62 psS32 main( psS32 argc, char* argv[] ) 63 { 64 psLogSetLevel( PS_LOG_INFO ); 65 66 return ( ! runTestSuite( stderr, "psArray", tests, argc, argv ) ); 67 } 68 69 70 psS32 testArray(void) 41 71 { 42 72 // Create array of pointers … … 218 248 return 0; 219 249 } 250 251 psS32 testArray01(void) 252 { 253 // Create array of pointers 254 testStruct *mySt[10]; 255 256 // Test A - Create void pointer array 257 printPositiveTestHeader(stdout,"psArray", "Create void pointer array"); 258 psArray *psArr = psArrayAlloc(10); 259 if (psArr->nalloc != 10) { 260 psError(PS_ERR_UNKNOWN, true,"psArray didn't have proper number of elements."); 261 return 1; 262 } 263 printFooter(stdout, "psArray", "Create void pointer array", true); 264 265 // Test B - Add data to void pointer array 266 printPositiveTestHeader(stdout, "psArray", "Add data to void pointer array"); 267 for(psS32 i = 0; i < 10; i++) { 268 testStruct *ts = psAlloc(sizeof(testStruct)); 269 ts->x = 10*(10-i); 270 ts->y = 10.1*(10-i); 271 mySt[i] = ts; 272 psArr->data[i] = ts; 273 psMemIncrRefCounter(ts); 274 } 275 276 for(psS32 i = 0; i < 10; i++) { 277 testStruct *ts = (testStruct*)psArr->data[i]; 278 printf("ts[%d].x = %d ts[%d].y = %.2f\n", i, ts->x, i, ts->y); 279 if (fabsf(ts->x - 10*(10-i)) > 0.01f || fabsf(ts->y - 10.1*(10-i)) > 0.01f) { 280 psError(PS_ERR_UNKNOWN, true,"Couldn't properly get elements from array."); 281 return 2; 282 } 283 } 284 printf("array size = %d\n", psArr->nalloc); 285 if (psArr->nalloc != 10) { 286 psError(PS_ERR_UNKNOWN, true,"Array Size wrong"); 287 return 3; 288 } 289 printf("array population = %d\n", psArr->n); 290 if (psArr->n != 10) { 291 psError(PS_ERR_UNKNOWN, true,"Array population wrong"); 292 return 4; 293 } 294 printFooter(stdout, "psArray", "Add data to void pointer array", true); 295 296 // Test C - Sort data in array 297 printPositiveTestHeader(stdout,"psArray","Sort data in array"); 298 psArr = psArraySort(psArr,testStructCompare); 299 for(psS32 i = 0; i < 10; i++) { 300 testStruct *ts = (testStruct*)psArr->data[i]; 301 printf("ts[%d].x = %d ts[%d].y = %.2f\n", i, ts->x, i, ts->y); 302 if (fabsf(ts->x - 10*(i+1)) > 0.01f || fabsf(ts->y - 10.1*(i+1)) > 0.01f) { 303 psError(PS_ERR_UNKNOWN, true,"Couldn't properly get elements from array."); 304 return 5; 305 } 306 } 307 printFooter(stdout,"psArray","Sort data in array",true); 308 309 // Test D - Attempt to sort null array 310 printPositiveTestHeader(stdout,"psArray","Attempt to sort array"); 311 psArray* tempArr = psArraySort(NULL,testStructCompare); 312 if(tempArr != NULL) { 313 psError(PS_ERR_UNKNOWN,true,"Array sort did not return null when sorting null array"); 314 return 6; 315 } 316 printFooter(stdout,"psArray","Attempt to sort array",true); 317 318 // Test E - Free void pointer array 319 printPositiveTestHeader(stdout, "psArray", "Free void pointer array"); 320 psFree(psArr); 321 for(psS32 i = 0; i < 10; i++) { 322 psFree(mySt[i]); 323 } 324 if( psMemCheckLeaks(0, NULL, stdout, false) != 0) { 325 psError(PS_ERR_UNKNOWN,true,"Memory leaks detected."); 326 return 110; 327 } 328 psS32 nBad = psMemCheckCorruption(0); 329 if(nBad) { 330 printf("ERROR: Found %d bad memory blocks\n", nBad); 331 return 111; 332 } 333 printFooter(stdout, "psArray" ,"Free void pointer array", true); 334 335 return 0; 336 } 337 338 psS32 testArrayAdd( void ) 339 { 340 int subtest = 0; 341 float* data; 342 int nalloc = 5; 343 int n = 0; 344 int delta = 5; 345 346 // allocate the array. 347 psArray* arr = psArrayAlloc(nalloc); 348 arr->n = n; 349 350 // test arrayAdd until n == nalloc 351 while (n < nalloc) { 352 data = psAlloc(sizeof(float)); 353 arr = psArrayAdd(arr, delta*2, data); // make delta unique versus next delta used. 354 355 subtest++; 356 if (arr->nalloc != nalloc) { 357 psError(PS_ERR_UNKNOWN,true, 358 "psArrayAdd expanded the psArray unnecessarily. n=%d", 359 n); 360 return subtest; 361 } 362 363 subtest++; 364 if (arr->n != ++n) { 365 psError(PS_ERR_UNKNOWN,true, 366 "psArrayAdd did not increment the size of the psArray. n=%d", 367 n); 368 return subtest; 369 } 370 371 subtest++; 372 if (arr->data[n-1] != data) { 373 psError(PS_ERR_UNKNOWN,true, 374 "psArrayAdd didn't set the element to data. n=%d", 375 n); 376 return subtest; 377 } 378 } 379 380 // now try to add an element when the array is full. 381 data = psAlloc(sizeof(float)); 382 arr = psArrayAdd(arr, delta, data); 383 384 // make sure the array was expanded 385 subtest++; 386 if (arr->nalloc != nalloc+delta) { 387 psError(PS_ERR_UNKNOWN,true, 388 "psArrayAdd did not expand the psArray when it was already full." 389 " old nalloc=%d, nalloc=%d, delta=%d", 390 nalloc, arr->nalloc, delta); 391 return subtest; 392 } 393 nalloc = arr->nalloc; 394 395 subtest++; 396 if (arr->n != ++n) { 397 psError(PS_ERR_UNKNOWN,true, 398 "psArrayAdd did not increment psArray.n by 1 after expanding it."); 399 return subtest; 400 } 401 402 subtest++; 403 if (arr->data[n-1] != data) { 404 psError(PS_ERR_UNKNOWN,true, 405 "psArrayAdd didn't set the second element to data."); 406 return subtest; 407 } 408 409 // make the array full again (operation tested already) 410 while (arr->n < arr->nalloc) { 411 data = psAlloc(sizeof(float)); 412 arr = psArrayAdd(arr, 0, data); 413 } 414 nalloc = arr->nalloc; 415 n = arr->n; 416 417 // now add to full array with delta = 0; verify that the array is 418 // expanded by 10 419 data = psAlloc(sizeof(float)); 420 arr = psArrayAdd(arr, 0, data); 421 422 subtest++; 423 if (arr->nalloc != nalloc+10) { 424 psError(PS_ERR_UNKNOWN,true, 425 "psArrayAdd did not expand the psArray by 10 when delta < 1." 426 " old nalloc=%d, nalloc=%d", 427 nalloc, arr->nalloc); 428 return subtest; 429 } 430 431 psFree(arr); 432 433 return 0; // the value that indicates success is part of the testDescription 434 } 435
Note:
See TracChangeset
for help on using the changeset viewer.
