Changeset 3268
- Timestamp:
- Feb 17, 2005, 11:48:49 AM (21 years ago)
- Location:
- trunk/psLib/test
- Files:
-
- 2 edited
-
dataIO/tst_psLookupTable_01.c (modified) (1 diff)
-
fileUtils/tst_psLookupTable_01.c (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/test/dataIO/tst_psLookupTable_01.c
r3264 r3268 12 12 * @author Ross Harman, MHPCC 13 13 * 14 * @version $Revision: 1. 8$ $Name: not supported by cvs2svn $15 * @date $Date: 2005-02-17 19:26:25$14 * @version $Revision: 1.9 $ $Name: not supported by cvs2svn $ 15 * @date $Date: 2005-02-17 21:48:49 $ 16 16 * 17 * Copyright 2004- 2005 Maui High Performance Computing Center, University of Hawaii17 * Copyright 2004-5 Maui High Performance Computing Center, University of Hawaii 18 18 * 19 19 */ 20 20 21 #include <string.h>22 #include "config.h"23 21 #include "pslib.h" 24 22 #include "psTest.h" 25 26 int main(int argc, char* argv[]) 23 #include <string.h> 24 25 static psS32 testLookupTableAlloc(void); 26 static psS32 testLookupTableRead(void); 27 static psS32 testLookupTableInterpolate(void); 28 static psS32 testLookupTableInterpolateAll(void); 29 30 const psF64 errorTol_psF64 = 1.0e-4; 31 const psF64 tableU8_validFrom = 0; 32 const psF64 tableU8_validTo = 3; 33 const psS32 tableU8_size = 4; 34 const psS32 tableU8_index[] = 35 { 36 0, 1, 2, 3 37 }; 38 const psU16 tableU8_val1[] = 39 { 40 2, 4, 6, 8 41 }; 42 const psU32 tableU8_val2[] = 43 { 44 4, 8, 12, 16 45 }; 46 const psU64 tableU8_val3[] = 47 { 48 8, 16, 24, 32 49 }; 50 const psS8 tableU8_val4[] = 51 { 52 0, -1, -2, -3 53 }; 54 const psS16 tableU8_val5[] = 55 { 56 -2, -4, -6, -8 57 }; 58 const psS32 tableU8_val6[] = 59 { 60 -4, -8, -12, -16 61 }; 62 const psS64 tableU8_val7[] = 63 { 64 -8, -16, -24, -32 65 }; 66 const psF32 tableU8_val8[] = 67 { 68 -0.5, 0.0, 0.5,0.75 69 }; 70 const psF64 tableU8_val9[] = 71 { 72 -1.5,-1.0,-0.25,1.75 73 }; 74 75 const psF64 tableS32_validFrom = -1; 76 const psF64 tableS32_validTo = 33; 77 const psS32 tableS32_size = 4; 78 const psS32 tableS32_index[] = 79 { 80 -1,11,22,33 81 }; 82 83 const psF64 tableF32_validFrom = -10.05; 84 const psF64 tableF32_validTo = 3500.67; 85 const psF64 tableF32_size = 4; 86 const psF32 tableF32_index[] = 87 { 88 -10.05,1.009,23.45,3500.67 89 }; 90 91 const psF64 table10_validFrom = 1; 92 const psF64 table10_validTo = 10; 93 const psS32 table10_size = 10; 94 const psU8 table10_index[] = 95 { 96 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 97 }; 98 const psU16 table10_val1[] = 99 { 100 4, 6, 8, 10, 12, 14, 16, 18, 20, 22 101 }; 102 const psU32 table10_val2[] = 103 { 104 8, 12, 16, 20, 24, 28, 32, 36, 40, 44 105 }; 106 const psU64 table10_val3[] = 107 { 108 16, 24, 32, 64, 128, 256, 512, 1024, 2048, 4096 109 }; 110 const psS8 table10_val4[] = 111 { 112 -1, -2, -3, -4, -5, -6, -7, -8, -9, -10 113 }; 114 const psS16 table10_val5[] = 115 { 116 -4, -6, -8, -10, -12, -14, -16, -18, -20, -22 117 }; 118 const psS32 table10_val6[] = 119 { 120 -8, -12, -16, -20, -24, -28, -32, -36, -40, -44 121 }; 122 const psS64 table10_val7[] = 123 { 124 -16, -24, -32, -64, -128, -256, -512,-1024,-2048,-4096 125 }; 126 const psF32 table10_val8[] = 127 { 128 -2.25,-1.5,-0.75, 0.0, 0.75, 1.50, 2.25, 3.00, 3.75, 4.50 129 }; 130 const psF64 table10_val9[] = 131 { 132 -2.67,0.66, 3.99,7.32,10.65,13.98,17.31,20.64,23.97,27.30 133 }; 134 const psF64 interpolVal1[] = 135 { 136 12.5, 25, 160, -5.25, -12.5, -25, -160, 0.9375, 11.4825 137 }; 138 const psF64 interpolVal2[] = 139 { 140 12, 24, 128, -5, -12, -24, -128, 0.75, 10.65 141 }; 142 const psF64 interpolVal3[] = 143 { 144 4, 8, 16, -1, -4, -8, -16, -2.25, -2.67 145 }; 146 147 testDescription tests[] = { 148 {testLookupTableAlloc,999,"psLookupTableAlloc",0,false}, 149 {testLookupTableRead,998,"psLookupTableRead",0,false}, 150 {testLookupTableInterpolate,997,"psLookupTableInterpolate",0,false}, 151 {testLookupTableInterpolateAll,996,"psLookupTableInterpolateAll",0,false}, 152 {NULL} 153 }; 154 155 psS32 main(psS32 argc, char* argv[]) 27 156 { 28 29 30 // Test A - Read table and interpolate value 31 printPositiveTestHeader(stdout, "psMetadata", "Test A - Read table and interpolate value"); 32 psLookupStatusType status1 = 0; 33 psF64 index1 = 2450905.0; 34 psF64 out1 = 0.0; 35 psLookupTable* table1 = NULL; 36 37 table1 = psLookupTableAlloc(UTC_DAT_FILE, 0, 2451179.6); 38 table1 = psLookupTableRead(table1); 39 out1 = psLookupTableInterpolate(table1, index1, 0, &status1); 40 if(fabs(out1-31.5)>FLT_EPSILON) { 41 printf("ERROR: Incorrect value, %lf. Expected 31.5\n", out1); 42 } else if(status1 != PS_LOOKUP_SUCCESS) { 43 printf("ERROR: Bad return status, %d\n", status1); 44 } 45 printFooter(stdout, "psMetadata", "Test A - Read table and interpolate value", true); 46 47 48 // Test B - Read table and interpolate all values 49 printPositiveTestHeader(stdout, "psMetadata", "Test B - Read table and interpolate all values"); 50 psU64 i = 0; 51 psF64 index2 = 2450905.0; 52 psVector *out2 = NULL; 53 psLookupTable* table2 = NULL; 54 psVector *stats2 = NULL; 55 psF64 truthData2[] = {31.5, 41317, 0}; 56 57 table2 = psLookupTableAlloc(UTC_DAT_FILE, 0, 2451179.6); 58 table2 = psLookupTableRead(table2); 59 stats2 = psVectorAlloc(table2->numCols, PS_TYPE_U32); 60 out2 = psLookupTableInterpolateAll(table2, index2, stats2); 61 62 for(i=0; i<table2->numCols; i++) { 63 if(fabs(out2->data.F64[i]-truthData2[i])>FLT_EPSILON) { 64 printf("ERROR: Incorrect value, %lf. Expected %lf\n", out2->data.F64[i], truthData2[i]); 65 } else if(status1 != PS_LOOKUP_SUCCESS) { 66 printf("ERROR: Bad return status, %d for column %lld\n", stats2->data.U32[i], (long long int)i); 67 } 68 } 69 printFooter(stdout, "psMetadata", "Test B - Read table and interpolate all values", true); 70 71 72 // Test C - Read beyond lower table bound 73 printPositiveTestHeader(stdout, "psMetadata", "Test C - Read beyond lower table bound"); 74 psLookupStatusType status3 = 0; 75 psF64 index3 = 2451179.7; 76 psF64 out3 = 0.0; 77 psLookupTable* table3 = NULL; 78 79 table3 = psLookupTableAlloc(UTC_DAT_FILE, 0, 2451179.6); 80 table3 = psLookupTableRead(table3); 81 out3 = psLookupTableInterpolate(table3, index3, 0, &status3); 82 if(status3 != PS_LOOKUP_PAST_BOTTOM) { 83 printf("ERROR: Bad return status, %d\n", status3); 84 } 85 printFooter(stdout, "psMetadata", "Test C - Read beyond lower table bound", true); 86 87 // Test D - Read beyond upper table bound 88 printPositiveTestHeader(stdout, "psMetadata", "Test D - Read beyond upper table bound"); 89 psLookupStatusType status4 = 0; 90 psF64 index4 = 0.0; 91 psF64 out4 = 0.0; 92 psLookupTable* table4 = NULL; 93 94 table4 = psLookupTableAlloc(UTC_DAT_FILE, 0, 2451179.6); 95 table4 = psLookupTableRead(table4); 96 out4 = psLookupTableInterpolate(table4, index4, 0, &status4); 97 if(status4 != PS_LOOKUP_PAST_TOP) { 98 printf("ERROR: Bad return status, %d\n", status4); 99 } 100 printFooter(stdout, "psMetadata", "Test D - Read beyond upper table bound", true); 101 102 103 // Test E - Free data 104 printPositiveTestHeader(stdout, "psMetadata", "Test E - Free data"); 105 psFree(table1); 106 psFree(table2); 107 psFree(table3); 108 psFree(table4); 109 psFree(out2); 110 psFree(stats2); 111 if (psMemCheckLeaks(0, NULL, stdout, false)) { 112 psError(PS_ERR_UNKNOWN,true,"Memory leaks detected"); 113 return 10; 114 } 115 psMemCheckCorruption(0); 116 int nBad = psMemCheckCorruption(0); 117 if(nBad) { 118 printf("ERROR: Found %d bad memory blocks\n", nBad); 119 } 120 printFooter(stdout, "psMetadata", "Test E - Free data", true); 121 122 157 psLogSetLevel(PS_LOG_INFO); 158 159 if ( ! runTestSuite(stderr,"psLookupTable",tests,argc,argv)) { 160 return 1; 161 } 123 162 return 0; 124 163 } 164 165 psS32 testLookupTableAlloc(void) 166 { 167 psLookupTable* table1 = NULL; 168 169 // Allocate lookup table with valid parameters 170 table1 = psLookupTableAlloc("tableF32.dat", tableF32_validFrom, tableF32_validTo); 171 if(table1 == NULL) { 172 psError(PS_ERR_UNKNOWN,true,"Null lookup table generated from valid parameters"); 173 return 1; 174 } 175 if(strcmp(table1->fileName,"tableF32.dat") != 0) { 176 psError(PS_ERR_UNKNOWN,true,"File name not properly stored in psLookupTable structure."); 177 return 2; 178 } 179 180 if( (fabs(table1->validFrom - tableF32_validFrom) > FLT_EPSILON) || 181 (fabs(table1->validTo-tableF32_validTo) > FLT_EPSILON) ) { 182 psError(PS_ERR_UNKNOWN,true,"Members validTo or validFrom not set properly"); 183 return 3; 184 } 185 psFree(table1); 186 187 // Allocate lookup table with invalid filename 188 psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message for invalid file name"); 189 table1 = psLookupTableAlloc(NULL,0,3); 190 if(table1 != NULL) { 191 psError(PS_ERR_UNKNOWN,true,"Null file name accepted by psLookupTableAlloc"); 192 return 4; 193 } 194 195 return 0; 196 } 197 198 psS32 testLookupTableRead(void) 199 { 200 psLookupTable* table1 = NULL; 201 202 // Allocate table using table with psU8 index and valid types and index-values 203 table1 = psLookupTableAlloc("tableU8.dat", 0, 100.5); 204 if(table1 == NULL) { 205 psError(PS_ERR_UNKNOWN,true,"Unable to allocate table with tableU8.dat"); 206 return 1; 207 } 208 // Read table with index U8 209 table1 = psLookupTableRead(table1); 210 if(table1 == NULL) { 211 psError(PS_ERR_UNKNOWN,true,"Unable to read table from file tableU8.dat"); 212 return 2; 213 } 214 // Verify the validFrom and validTo are updated properly 215 if( (fabs(table1->validFrom - tableU8_validFrom) > FLT_EPSILON) || 216 (fabs(table1->validTo - tableU8_validTo) > FLT_EPSILON) ) { 217 psError(PS_ERR_UNKNOWN,true,"ValidFrom or validTo not set properly"); 218 return 3; 219 } 220 // Verify index and all values in table read properly 221 for(psS32 i=0; i < tableU8_size; i++) { 222 if(table1->index->data.U8[i] != tableU8_index[i]) { 223 psError(PS_ERR_UNKNOWN,true,"Index values do not match: index[%d]=%d expected %d",i, 224 table1->index->data.U8[i], tableU8_index[i]); 225 return 3*i; 226 } 227 if(((psVector*)table1->values->data[0])->data.U16[i] != tableU8_val1[i]) { 228 psError(PS_ERR_UNKNOWN,true,"Value in column 1 do not match: value[%d]=%d expected %d",i, 229 ((psVector*)table1->values->data[0])->data.U16[i], tableU8_val1[i]); 230 return 3*i+1; 231 } 232 if(((psVector*)table1->values->data[1])->data.U32[i] != tableU8_val2[i]) { 233 psError(PS_ERR_UNKNOWN,true,"Value in column 2 do not match: value[%d]=%d expected %d",i, 234 ((psVector*)table1->values->data[1])->data.U32[i], tableU8_val2[i]); 235 return 3*i+2; 236 } 237 if(((psVector*)table1->values->data[2])->data.U64[i] != tableU8_val3[i]) { 238 psError(PS_ERR_UNKNOWN,true,"Value in column 3 do not match: value[%d]=%lld expected %lld",i, 239 ((psVector*)table1->values->data[2])->data.U64[i], tableU8_val3[i]); 240 return 3*i+3; 241 } 242 if(((psVector*)table1->values->data[3])->data.S8[i] != tableU8_val4[i]) { 243 psError(PS_ERR_UNKNOWN,true,"Value in column 4 do not match: value[%d]=%d expected %d",i, 244 ((psVector*)table1->values->data[3])->data.S8[i], tableU8_val4[i]); 245 return 3*i+4; 246 } 247 if(((psVector*)table1->values->data[4])->data.S16[i] != tableU8_val5[i]) { 248 psError(PS_ERR_UNKNOWN,true,"Value in column 5 do not match: value[%d]=%d expected %d",i, 249 ((psVector*)table1->values->data[4])->data.S16[i], tableU8_val5[i]); 250 return 3*i+5; 251 } 252 if(((psVector*)table1->values->data[5])->data.S32[i] != tableU8_val6[i]) { 253 psError(PS_ERR_UNKNOWN,true,"Value in column 6 do not match: value[%d]=%d expected %d",i, 254 ((psVector*)table1->values->data[5])->data.S32[i], tableU8_val6[i]); 255 return 3*i+6; 256 } 257 if(((psVector*)table1->values->data[6])->data.S64[i] != tableU8_val7[i]) { 258 psError(PS_ERR_UNKNOWN,true,"Value in column 7 do not match: value[%d]=%lld expected %lld",i, 259 ((psVector*)table1->values->data[6])->data.S64[i], tableU8_val7[i]); 260 return 3*i+7; 261 } 262 if(fabs(((psVector*)table1->values->data[7])->data.F32[i] - tableU8_val8[i]) > FLT_EPSILON ) { 263 psError(PS_ERR_UNKNOWN,true,"Value in column 8 do not match: value[%d]=%g expected %g",i, 264 ((psVector*)table1->values->data[7])->data.F32[i], tableU8_val8[i]); 265 return 3*i+8; 266 } 267 if(fabs(((psVector*)table1->values->data[8])->data.F64[i] - tableU8_val9[i]) > FLT_EPSILON ) { 268 psError(PS_ERR_UNKNOWN,true,"Value in column 9 do not match: value[%d]=%g expected %g",i, 269 ((psVector*)table1->values->data[8])->data.F32[i], tableU8_val9[i]); 270 return 3*i+9; 271 } 272 } 273 psFree(table1); 274 275 // Allocate table using table with psS32 index and valid types and index-values 276 table1 = psLookupTableAlloc("tableS32.dat", -110, 1000.5); 277 if(table1 == NULL) { 278 psError(PS_ERR_UNKNOWN,true,"Unable to allocate table with tableS32.dat"); 279 return 1; 280 } 281 table1 = psLookupTableRead(table1); 282 if(table1 == NULL) { 283 psError(PS_ERR_UNKNOWN,true,"Unable to read table from file tableS32.dat"); 284 return 2; 285 } 286 if( (fabs(table1->validFrom - tableS32_validFrom) > FLT_EPSILON) || 287 (fabs(table1->validTo - tableS32_validTo) > FLT_EPSILON) ) { 288 psError(PS_ERR_UNKNOWN,true,"ValidFrom or validTo not set properly"); 289 return 3; 290 } 291 for(psS32 i=0; i < tableS32_size; i++) { 292 if(table1->index->data.S32[i] != tableS32_index[i]) { 293 psError(PS_ERR_UNKNOWN,true,"Index values do not match: index[%d]=%d expected %d",i, 294 table1->index->data.S32[i], tableS32_index[i]); 295 return 4*i; 296 } 297 } 298 psFree(table1); 299 300 // Allocate table using table with psF32 index and valid types and index-values 301 table1 = psLookupTableAlloc("tableF32.dat", -1100, 5500.5); 302 if(table1 == NULL) { 303 psError(PS_ERR_UNKNOWN,true,"Unable to allocate table with tableF32.dat"); 304 return 1; 305 } 306 table1 = psLookupTableRead(table1); 307 if(table1 == NULL) { 308 psError(PS_ERR_UNKNOWN,true,"Unable to read table from file tableF32.dat"); 309 return 2; 310 } 311 if( (fabs(table1->validFrom - tableF32_validFrom) > errorTol_psF64) || 312 (fabs(table1->validTo - tableF32_validTo) > errorTol_psF64) ) { 313 psError(PS_ERR_UNKNOWN,true,"ValidFrom or validTo not set properly"); 314 return 3; 315 } 316 for(psS32 i=0; i < tableS32_size; i++) { 317 if(fabsf(table1->index->data.F32[i] - tableF32_index[i]) > FLT_EPSILON ) { 318 psError(PS_ERR_UNKNOWN,true,"Index values do not match: index[%d]=%d expected %d",i, 319 table1->index->data.F32[i], tableF32_index[i]); 320 return 4*i; 321 } 322 } 323 psFree(table1); 324 325 // Allocate table using psU8 index but with unsorted rows and verify the list is 326 // sorted properly after being read 327 table1 = psLookupTableAlloc("table10.dat",1,2); 328 if(table1 == NULL) { 329 psError(PS_ERR_UNKNOWN,true,"Unable to allocate table with table10.dat"); 330 return 1; 331 } 332 table1 = psLookupTableRead(table1); 333 if(table1 == NULL) { 334 psError(PS_ERR_UNKNOWN,true,"Unable to read table with table10.dat"); 335 return 2; 336 } 337 // Verify the validFrom and validTo are updated properly 338 if( (fabs(table1->validFrom - table10_validFrom) > FLT_EPSILON) || 339 (fabs(table1->validTo - table10_validTo) > FLT_EPSILON) ) { 340 psError(PS_ERR_UNKNOWN,true,"ValidFrom or validTo not set properly"); 341 return 3; 342 } 343 // Verify index and all values in table read properly 344 for(psS32 i=0; i < table10_size; i++) { 345 if(table1->index->data.U8[i] != table10_index[i]) { 346 psError(PS_ERR_UNKNOWN,true,"Index values do not match: index[%d]=%d expected %d",i, 347 table1->index->data.U8[i], table10_index[i]); 348 return 3*i; 349 } 350 if(((psVector*)table1->values->data[0])->data.U16[i] != table10_val1[i]) { 351 psError(PS_ERR_UNKNOWN,true,"Value in column 1 do not match: value[%d]=%d expected %d",i, 352 ((psVector*)table1->values->data[0])->data.U16[i], table10_val1[i]); 353 return 3*i+1; 354 } 355 if(((psVector*)table1->values->data[1])->data.U32[i] != table10_val2[i]) { 356 psError(PS_ERR_UNKNOWN,true,"Value in column 2 do not match: value[%d]=%d expected %d",i, 357 ((psVector*)table1->values->data[1])->data.U32[i], table10_val2[i]); 358 return 3*i+2; 359 } 360 if(((psVector*)table1->values->data[2])->data.U64[i] != table10_val3[i]) { 361 psError(PS_ERR_UNKNOWN,true,"Value in column 3 do not match: value[%d]=%lld expected %lld",i, 362 ((psVector*)table1->values->data[2])->data.U64[i], table10_val3[i]); 363 return 3*i+3; 364 } 365 if(((psVector*)table1->values->data[3])->data.S8[i] != table10_val4[i]) { 366 psError(PS_ERR_UNKNOWN,true,"Value in column 4 do not match: value[%d]=%d expected %d",i, 367 ((psVector*)table1->values->data[3])->data.S8[i], table10_val4[i]); 368 return 3*i+4; 369 } 370 if(((psVector*)table1->values->data[4])->data.S16[i] != table10_val5[i]) { 371 psError(PS_ERR_UNKNOWN,true,"Value in column 5 do not match: value[%d]=%d expected %d",i, 372 ((psVector*)table1->values->data[4])->data.S16[i], table10_val5[i]); 373 return 3*i+5; 374 } 375 if(((psVector*)table1->values->data[5])->data.S32[i] != table10_val6[i]) { 376 psError(PS_ERR_UNKNOWN,true,"Value in column 6 do not match: value[%d]=%d expected %d",i, 377 ((psVector*)table1->values->data[5])->data.S32[i], table10_val6[i]); 378 return 3*i+6; 379 } 380 if(((psVector*)table1->values->data[6])->data.S64[i] != table10_val7[i]) { 381 psError(PS_ERR_UNKNOWN,true,"Value in column 7 do not match: value[%d]=%lld expected %lld",i, 382 ((psVector*)table1->values->data[6])->data.S64[i], table10_val7[i]); 383 return 3*i+7; 384 } 385 if(fabs(((psVector*)table1->values->data[7])->data.F32[i] - table10_val8[i]) > FLT_EPSILON ) { 386 psError(PS_ERR_UNKNOWN,true,"Value in column 8 do not match: value[%d]=%g expected %g",i, 387 ((psVector*)table1->values->data[7])->data.F32[i], table10_val8[i]); 388 return 3*i+8; 389 } 390 if(fabs(((psVector*)table1->values->data[8])->data.F64[i] - table10_val9[i]) > FLT_EPSILON ) { 391 psError(PS_ERR_UNKNOWN,true,"Value in column 9 do not match: value[%d]=%g expected %g",i, 392 ((psVector*)table1->values->data[8])->data.F32[i], table10_val9[i]); 393 return 3*i+9; 394 } 395 } 396 psFree(table1); 397 398 // Allocate table using table with invalid type in type row but valid index-values 399 table1 = psLookupTableAlloc("table2.dat",0,99.99); 400 if(table1 == NULL) { 401 psError(PS_ERR_UNKNOWN,true,"Unable to allocate table with table2.dat"); 402 return 3; 403 } 404 psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message about invalid type"); 405 table1 = psLookupTableRead(table1); 406 if(table1 == NULL) { 407 psError(PS_ERR_UNKNOWN,true,"Unable to read table from file table2.dat"); 408 return 4; 409 } 410 psFree(table1); 411 412 // Allocate table using table with invalid value 413 table1 = psLookupTableAlloc("table3.dat",0,75.0); 414 if(table1 == NULL) { 415 psError(PS_ERR_UNKNOWN,true,"Unable to allocate table with table3.dat"); 416 return 5; 417 } 418 psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message about invalid value"); 419 table1 = psLookupTableRead(table1); 420 if(table1 == NULL) { 421 psError(PS_ERR_UNKNOWN,true,"Unable to read table from file table3.dat"); 422 return 6; 423 } 424 psFree(table1); 425 426 // Attempt to read for non-existant file 427 table1 = psLookupTableAlloc("nonexistantFile.dat",0,100); 428 if(table1 == NULL) { 429 psError(PS_ERR_UNKNOWN,true,"Unable to allocate table with non-existant file"); 430 return 3; 431 } 432 psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg about opening file"); 433 table1 = psLookupTableRead(table1); 434 if(table1 == NULL) { 435 psError(PS_ERR_UNKNOWN,true,"Returned NULL when table should have been returned"); 436 return 4; 437 } 438 439 // Attempt to read table with NULL specified file 440 psFree((char*)table1->fileName); 441 table1->fileName = NULL; 442 psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg about NULL file name"); 443 if(psLookupTableRead(table1) != NULL) { 444 psError(PS_ERR_UNKNOWN,true,"Did not return NULL with table specifying NULL file name"); 445 return 5; 446 } 447 psFree(table1); 448 449 // Attempt to read table with NULL table 450 psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg about NULL table"); 451 if(psLookupTableRead(NULL) != NULL) { 452 psError(PS_ERR_UNKNOWN,true,"Did not return NULL with NULL table."); 453 return 6; 454 } 455 456 return 0; 457 } 458 459 psS32 testLookupTableInterpolate(void) 460 { 461 psLookupStatusType status = 0; 462 psLookupTable* table1 = NULL; 463 psF64 out1 = 0; 464 465 // Interpolate values within the list and verify return values 466 table1 = psLookupTableAlloc("table10.dat",0,20); 467 table1 = psLookupTableRead(table1); 468 for(psS32 i = 0; i < table1->numRows-2; i++ ) { 469 out1 = psLookupTableInterpolate(table1, 5.25, i, &status); 470 if( fabs(out1-interpolVal1[i]) > errorTol_psF64) { 471 psError(PS_ERR_UNKNOWN,true,"Did not return expected value. %lg expected %lg",out1,interpolVal1[i]); 472 return 3*i; 473 } 474 } 475 for(psS32 i = 0; i < table1->numRows-2; i++ ) { 476 out1 = psLookupTableInterpolate(table1, 5.0, i, &status); 477 if( fabs(out1-interpolVal2[i]) > errorTol_psF64) { 478 psError(PS_ERR_UNKNOWN,true,"Did not return expected value. %lg expected %lg",out1,interpolVal2[i]); 479 return 4*i; 480 } 481 } 482 483 // Interpolate value just below the lowest index value 484 out1 = psLookupTableInterpolate(table1,0,0, &status); 485 if ( fabs(out1- NAN) > FLT_EPSILON ) { 486 psError(PS_ERR_UNKNOWN,true,"Did not return NAN returned %lf",out1); 487 return 5; 488 } 489 if ( status != PS_LOOKUP_PAST_TOP) { 490 psError(PS_ERR_UNKNOWN,true,"Did not return status PAST TOP"); 491 return 6; 492 } 493 494 // Interpolate value just above the highest index value 495 out1 = psLookupTableInterpolate(table1,11,0, &status); 496 if ( fabs(out1-NAN) > FLT_EPSILON ) { 497 psError(PS_ERR_UNKNOWN,true,"Did not return NAN returned %lf",out1); 498 return 7; 499 } 500 if ( status != PS_LOOKUP_PAST_BOTTOM) { 501 psError(PS_ERR_UNKNOWN,true,"Did not return status PAST BOTTOM"); 502 return 8; 503 } 504 505 // Interpolate value with a column number greater than the table has 506 psLogMsg(__func__,PS_LOG_INFO,"Following should generate error out of range."); 507 out1 = psLookupTableInterpolate(table1, 5, 100, &status); 508 if ( fabs(out1-NAN) > FLT_EPSILON ) { 509 psError(PS_ERR_UNKNOWN,true,"Did not return NAN returned %lf",out1); 510 return 9; 511 } 512 if ( status != PS_LOOKUP_ERROR ) { 513 psError(PS_ERR_UNKNOWN,true,"Did not return status LOOKUP ERROR"); 514 return 10; 515 } 516 517 // Interpolate with NULL table 518 psLogMsg(__func__,PS_LOG_INFO,"Following should generate error NULL table"); 519 out1 = psLookupTableInterpolate(NULL,5,0,&status); 520 if( fabs(out1-NAN) > FLT_EPSILON ) { 521 psError(PS_ERR_UNKNOWN,true,"Did not return NAN returned %lf",out1); 522 return 11; 523 } 524 if ( status != PS_LOOKUP_ERROR ) { 525 psError(PS_ERR_UNKNOWN,true,"Did not return status LOOKUP ERROR"); 526 return 12; 527 } 528 529 // Interpolate with status NULL 530 psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error NULL status"); 531 out1 = psLookupTableInterpolate(table1,5,0,NULL); 532 if ( fabs(out1-NAN) > FLT_EPSILON ) { 533 psError(PS_ERR_UNKNOWN,true,"Did not return NAN returned %lf",out1); 534 return 13; 535 } 536 537 psFree(table1); 538 539 return 0; 540 } 541 542 psS32 testLookupTableInterpolateAll(void) 543 { 544 psLookupTable* table1 = NULL; 545 psVector* interpValues; 546 psVector* statusVector; 547 548 // Interpolate values within the list and verify return values 549 table1 = psLookupTableAlloc("table10.dat",0,20); 550 table1 = psLookupTableRead(table1); 551 statusVector = psVectorAlloc(table1->numCols,PS_TYPE_U32); 552 interpValues = psLookupTableInterpolateAll(table1,5.25, statusVector); 553 if(interpValues == NULL) { 554 psError(PS_ERR_UNKNOWN,true,"Returned vector is NULL."); 555 return 1; 556 } 557 for(psS32 i = 0; i < table1->numRows-1; i++ ) { 558 if( fabs(interpValues->data.F64[i]-interpolVal1[i]) > errorTol_psF64) { 559 psError(PS_ERR_UNKNOWN,true,"Did not return expected value. %lg expected %lg", 560 interpValues->data.F64[i],interpolVal1[i]); 561 return 2*i; 562 } 563 } 564 for(psS32 j = 0; j < table1->numCols; j++) { 565 if(statusVector->data.U32[j] != PS_LOOKUP_SUCCESS) { 566 psError(PS_ERR_UNKNOWN,true,"Return status not success for col=%d status=%d", 567 j,statusVector->data.U32[j]); 568 return 3*j; 569 } 570 } 571 psFree(statusVector); 572 psFree(interpValues); 573 574 // Interpolate values with index outside table 575 statusVector = psVectorAlloc(table1->numCols,PS_TYPE_U32); 576 interpValues = psLookupTableInterpolateAll(table1,0,statusVector); 577 if(interpValues == NULL) { 578 psError(PS_ERR_UNKNOWN,true,"Did return NULL"); 579 return 4; 580 } 581 582 for(psS32 i = 0; i < table1->numRows-1; i++ ) { 583 if( fabs(interpValues->data.F64[i]-interpolVal3[i]) > errorTol_psF64) { 584 psError(PS_ERR_UNKNOWN,true,"Did not return expected value. %lg expected %lg", 585 interpValues->data.F64[i],interpolVal3[i]); 586 return 2*i; 587 } 588 } 589 590 for(psS32 j = 0; j < table1->numCols; j++) { 591 if(statusVector->data.S32[j] != PS_LOOKUP_PAST_TOP) { 592 psError(PS_ERR_UNKNOWN,true,"Return status did not indicate passed top for col=%d status=%d", 593 j,statusVector->data.U32[j]); 594 return 3*j; 595 } 596 } 597 psFree(interpValues); 598 psFree(statusVector); 599 600 // Interpolate values with NULL table 601 statusVector = psVectorAlloc(table1->numCols,PS_TYPE_S32); 602 psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message for NULL table"); 603 interpValues = psLookupTableInterpolateAll(NULL,5,statusVector); 604 if(interpValues != NULL ) { 605 psError(PS_ERR_UNKNOWN,true,"Did not return NULL for NULL table"); 606 return 5; 607 } 608 psFree(statusVector); 609 610 // Interpolate values with NULL status vector 611 psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message for NULL status vector"); 612 interpValues = psLookupTableInterpolateAll(table1,5,NULL); 613 if(interpValues != NULL ) { 614 psError(PS_ERR_UNKNOWN,true,"Did not return NULL for NULL status vector"); 615 return 6; 616 } 617 psFree(table1); 618 619 return 0; 620 } 621 -
trunk/psLib/test/fileUtils/tst_psLookupTable_01.c
r3264 r3268 12 12 * @author Ross Harman, MHPCC 13 13 * 14 * @version $Revision: 1. 8$ $Name: not supported by cvs2svn $15 * @date $Date: 2005-02-17 19:26:25$14 * @version $Revision: 1.9 $ $Name: not supported by cvs2svn $ 15 * @date $Date: 2005-02-17 21:48:49 $ 16 16 * 17 * Copyright 2004- 2005 Maui High Performance Computing Center, University of Hawaii17 * Copyright 2004-5 Maui High Performance Computing Center, University of Hawaii 18 18 * 19 19 */ 20 20 21 #include <string.h>22 #include "config.h"23 21 #include "pslib.h" 24 22 #include "psTest.h" 25 26 int main(int argc, char* argv[]) 23 #include <string.h> 24 25 static psS32 testLookupTableAlloc(void); 26 static psS32 testLookupTableRead(void); 27 static psS32 testLookupTableInterpolate(void); 28 static psS32 testLookupTableInterpolateAll(void); 29 30 const psF64 errorTol_psF64 = 1.0e-4; 31 const psF64 tableU8_validFrom = 0; 32 const psF64 tableU8_validTo = 3; 33 const psS32 tableU8_size = 4; 34 const psS32 tableU8_index[] = 35 { 36 0, 1, 2, 3 37 }; 38 const psU16 tableU8_val1[] = 39 { 40 2, 4, 6, 8 41 }; 42 const psU32 tableU8_val2[] = 43 { 44 4, 8, 12, 16 45 }; 46 const psU64 tableU8_val3[] = 47 { 48 8, 16, 24, 32 49 }; 50 const psS8 tableU8_val4[] = 51 { 52 0, -1, -2, -3 53 }; 54 const psS16 tableU8_val5[] = 55 { 56 -2, -4, -6, -8 57 }; 58 const psS32 tableU8_val6[] = 59 { 60 -4, -8, -12, -16 61 }; 62 const psS64 tableU8_val7[] = 63 { 64 -8, -16, -24, -32 65 }; 66 const psF32 tableU8_val8[] = 67 { 68 -0.5, 0.0, 0.5,0.75 69 }; 70 const psF64 tableU8_val9[] = 71 { 72 -1.5,-1.0,-0.25,1.75 73 }; 74 75 const psF64 tableS32_validFrom = -1; 76 const psF64 tableS32_validTo = 33; 77 const psS32 tableS32_size = 4; 78 const psS32 tableS32_index[] = 79 { 80 -1,11,22,33 81 }; 82 83 const psF64 tableF32_validFrom = -10.05; 84 const psF64 tableF32_validTo = 3500.67; 85 const psF64 tableF32_size = 4; 86 const psF32 tableF32_index[] = 87 { 88 -10.05,1.009,23.45,3500.67 89 }; 90 91 const psF64 table10_validFrom = 1; 92 const psF64 table10_validTo = 10; 93 const psS32 table10_size = 10; 94 const psU8 table10_index[] = 95 { 96 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 97 }; 98 const psU16 table10_val1[] = 99 { 100 4, 6, 8, 10, 12, 14, 16, 18, 20, 22 101 }; 102 const psU32 table10_val2[] = 103 { 104 8, 12, 16, 20, 24, 28, 32, 36, 40, 44 105 }; 106 const psU64 table10_val3[] = 107 { 108 16, 24, 32, 64, 128, 256, 512, 1024, 2048, 4096 109 }; 110 const psS8 table10_val4[] = 111 { 112 -1, -2, -3, -4, -5, -6, -7, -8, -9, -10 113 }; 114 const psS16 table10_val5[] = 115 { 116 -4, -6, -8, -10, -12, -14, -16, -18, -20, -22 117 }; 118 const psS32 table10_val6[] = 119 { 120 -8, -12, -16, -20, -24, -28, -32, -36, -40, -44 121 }; 122 const psS64 table10_val7[] = 123 { 124 -16, -24, -32, -64, -128, -256, -512,-1024,-2048,-4096 125 }; 126 const psF32 table10_val8[] = 127 { 128 -2.25,-1.5,-0.75, 0.0, 0.75, 1.50, 2.25, 3.00, 3.75, 4.50 129 }; 130 const psF64 table10_val9[] = 131 { 132 -2.67,0.66, 3.99,7.32,10.65,13.98,17.31,20.64,23.97,27.30 133 }; 134 const psF64 interpolVal1[] = 135 { 136 12.5, 25, 160, -5.25, -12.5, -25, -160, 0.9375, 11.4825 137 }; 138 const psF64 interpolVal2[] = 139 { 140 12, 24, 128, -5, -12, -24, -128, 0.75, 10.65 141 }; 142 const psF64 interpolVal3[] = 143 { 144 4, 8, 16, -1, -4, -8, -16, -2.25, -2.67 145 }; 146 147 testDescription tests[] = { 148 {testLookupTableAlloc,999,"psLookupTableAlloc",0,false}, 149 {testLookupTableRead,998,"psLookupTableRead",0,false}, 150 {testLookupTableInterpolate,997,"psLookupTableInterpolate",0,false}, 151 {testLookupTableInterpolateAll,996,"psLookupTableInterpolateAll",0,false}, 152 {NULL} 153 }; 154 155 psS32 main(psS32 argc, char* argv[]) 27 156 { 28 29 30 // Test A - Read table and interpolate value 31 printPositiveTestHeader(stdout, "psMetadata", "Test A - Read table and interpolate value"); 32 psLookupStatusType status1 = 0; 33 psF64 index1 = 2450905.0; 34 psF64 out1 = 0.0; 35 psLookupTable* table1 = NULL; 36 37 table1 = psLookupTableAlloc(UTC_DAT_FILE, 0, 2451179.6); 38 table1 = psLookupTableRead(table1); 39 out1 = psLookupTableInterpolate(table1, index1, 0, &status1); 40 if(fabs(out1-31.5)>FLT_EPSILON) { 41 printf("ERROR: Incorrect value, %lf. Expected 31.5\n", out1); 42 } else if(status1 != PS_LOOKUP_SUCCESS) { 43 printf("ERROR: Bad return status, %d\n", status1); 44 } 45 printFooter(stdout, "psMetadata", "Test A - Read table and interpolate value", true); 46 47 48 // Test B - Read table and interpolate all values 49 printPositiveTestHeader(stdout, "psMetadata", "Test B - Read table and interpolate all values"); 50 psU64 i = 0; 51 psF64 index2 = 2450905.0; 52 psVector *out2 = NULL; 53 psLookupTable* table2 = NULL; 54 psVector *stats2 = NULL; 55 psF64 truthData2[] = {31.5, 41317, 0}; 56 57 table2 = psLookupTableAlloc(UTC_DAT_FILE, 0, 2451179.6); 58 table2 = psLookupTableRead(table2); 59 stats2 = psVectorAlloc(table2->numCols, PS_TYPE_U32); 60 out2 = psLookupTableInterpolateAll(table2, index2, stats2); 61 62 for(i=0; i<table2->numCols; i++) { 63 if(fabs(out2->data.F64[i]-truthData2[i])>FLT_EPSILON) { 64 printf("ERROR: Incorrect value, %lf. Expected %lf\n", out2->data.F64[i], truthData2[i]); 65 } else if(status1 != PS_LOOKUP_SUCCESS) { 66 printf("ERROR: Bad return status, %d for column %lld\n", stats2->data.U32[i], (long long int)i); 67 } 68 } 69 printFooter(stdout, "psMetadata", "Test B - Read table and interpolate all values", true); 70 71 72 // Test C - Read beyond lower table bound 73 printPositiveTestHeader(stdout, "psMetadata", "Test C - Read beyond lower table bound"); 74 psLookupStatusType status3 = 0; 75 psF64 index3 = 2451179.7; 76 psF64 out3 = 0.0; 77 psLookupTable* table3 = NULL; 78 79 table3 = psLookupTableAlloc(UTC_DAT_FILE, 0, 2451179.6); 80 table3 = psLookupTableRead(table3); 81 out3 = psLookupTableInterpolate(table3, index3, 0, &status3); 82 if(status3 != PS_LOOKUP_PAST_BOTTOM) { 83 printf("ERROR: Bad return status, %d\n", status3); 84 } 85 printFooter(stdout, "psMetadata", "Test C - Read beyond lower table bound", true); 86 87 // Test D - Read beyond upper table bound 88 printPositiveTestHeader(stdout, "psMetadata", "Test D - Read beyond upper table bound"); 89 psLookupStatusType status4 = 0; 90 psF64 index4 = 0.0; 91 psF64 out4 = 0.0; 92 psLookupTable* table4 = NULL; 93 94 table4 = psLookupTableAlloc(UTC_DAT_FILE, 0, 2451179.6); 95 table4 = psLookupTableRead(table4); 96 out4 = psLookupTableInterpolate(table4, index4, 0, &status4); 97 if(status4 != PS_LOOKUP_PAST_TOP) { 98 printf("ERROR: Bad return status, %d\n", status4); 99 } 100 printFooter(stdout, "psMetadata", "Test D - Read beyond upper table bound", true); 101 102 103 // Test E - Free data 104 printPositiveTestHeader(stdout, "psMetadata", "Test E - Free data"); 105 psFree(table1); 106 psFree(table2); 107 psFree(table3); 108 psFree(table4); 109 psFree(out2); 110 psFree(stats2); 111 if (psMemCheckLeaks(0, NULL, stdout, false)) { 112 psError(PS_ERR_UNKNOWN,true,"Memory leaks detected"); 113 return 10; 114 } 115 psMemCheckCorruption(0); 116 int nBad = psMemCheckCorruption(0); 117 if(nBad) { 118 printf("ERROR: Found %d bad memory blocks\n", nBad); 119 } 120 printFooter(stdout, "psMetadata", "Test E - Free data", true); 121 122 157 psLogSetLevel(PS_LOG_INFO); 158 159 if ( ! runTestSuite(stderr,"psLookupTable",tests,argc,argv)) { 160 return 1; 161 } 123 162 return 0; 124 163 } 164 165 psS32 testLookupTableAlloc(void) 166 { 167 psLookupTable* table1 = NULL; 168 169 // Allocate lookup table with valid parameters 170 table1 = psLookupTableAlloc("tableF32.dat", tableF32_validFrom, tableF32_validTo); 171 if(table1 == NULL) { 172 psError(PS_ERR_UNKNOWN,true,"Null lookup table generated from valid parameters"); 173 return 1; 174 } 175 if(strcmp(table1->fileName,"tableF32.dat") != 0) { 176 psError(PS_ERR_UNKNOWN,true,"File name not properly stored in psLookupTable structure."); 177 return 2; 178 } 179 180 if( (fabs(table1->validFrom - tableF32_validFrom) > FLT_EPSILON) || 181 (fabs(table1->validTo-tableF32_validTo) > FLT_EPSILON) ) { 182 psError(PS_ERR_UNKNOWN,true,"Members validTo or validFrom not set properly"); 183 return 3; 184 } 185 psFree(table1); 186 187 // Allocate lookup table with invalid filename 188 psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message for invalid file name"); 189 table1 = psLookupTableAlloc(NULL,0,3); 190 if(table1 != NULL) { 191 psError(PS_ERR_UNKNOWN,true,"Null file name accepted by psLookupTableAlloc"); 192 return 4; 193 } 194 195 return 0; 196 } 197 198 psS32 testLookupTableRead(void) 199 { 200 psLookupTable* table1 = NULL; 201 202 // Allocate table using table with psU8 index and valid types and index-values 203 table1 = psLookupTableAlloc("tableU8.dat", 0, 100.5); 204 if(table1 == NULL) { 205 psError(PS_ERR_UNKNOWN,true,"Unable to allocate table with tableU8.dat"); 206 return 1; 207 } 208 // Read table with index U8 209 table1 = psLookupTableRead(table1); 210 if(table1 == NULL) { 211 psError(PS_ERR_UNKNOWN,true,"Unable to read table from file tableU8.dat"); 212 return 2; 213 } 214 // Verify the validFrom and validTo are updated properly 215 if( (fabs(table1->validFrom - tableU8_validFrom) > FLT_EPSILON) || 216 (fabs(table1->validTo - tableU8_validTo) > FLT_EPSILON) ) { 217 psError(PS_ERR_UNKNOWN,true,"ValidFrom or validTo not set properly"); 218 return 3; 219 } 220 // Verify index and all values in table read properly 221 for(psS32 i=0; i < tableU8_size; i++) { 222 if(table1->index->data.U8[i] != tableU8_index[i]) { 223 psError(PS_ERR_UNKNOWN,true,"Index values do not match: index[%d]=%d expected %d",i, 224 table1->index->data.U8[i], tableU8_index[i]); 225 return 3*i; 226 } 227 if(((psVector*)table1->values->data[0])->data.U16[i] != tableU8_val1[i]) { 228 psError(PS_ERR_UNKNOWN,true,"Value in column 1 do not match: value[%d]=%d expected %d",i, 229 ((psVector*)table1->values->data[0])->data.U16[i], tableU8_val1[i]); 230 return 3*i+1; 231 } 232 if(((psVector*)table1->values->data[1])->data.U32[i] != tableU8_val2[i]) { 233 psError(PS_ERR_UNKNOWN,true,"Value in column 2 do not match: value[%d]=%d expected %d",i, 234 ((psVector*)table1->values->data[1])->data.U32[i], tableU8_val2[i]); 235 return 3*i+2; 236 } 237 if(((psVector*)table1->values->data[2])->data.U64[i] != tableU8_val3[i]) { 238 psError(PS_ERR_UNKNOWN,true,"Value in column 3 do not match: value[%d]=%lld expected %lld",i, 239 ((psVector*)table1->values->data[2])->data.U64[i], tableU8_val3[i]); 240 return 3*i+3; 241 } 242 if(((psVector*)table1->values->data[3])->data.S8[i] != tableU8_val4[i]) { 243 psError(PS_ERR_UNKNOWN,true,"Value in column 4 do not match: value[%d]=%d expected %d",i, 244 ((psVector*)table1->values->data[3])->data.S8[i], tableU8_val4[i]); 245 return 3*i+4; 246 } 247 if(((psVector*)table1->values->data[4])->data.S16[i] != tableU8_val5[i]) { 248 psError(PS_ERR_UNKNOWN,true,"Value in column 5 do not match: value[%d]=%d expected %d",i, 249 ((psVector*)table1->values->data[4])->data.S16[i], tableU8_val5[i]); 250 return 3*i+5; 251 } 252 if(((psVector*)table1->values->data[5])->data.S32[i] != tableU8_val6[i]) { 253 psError(PS_ERR_UNKNOWN,true,"Value in column 6 do not match: value[%d]=%d expected %d",i, 254 ((psVector*)table1->values->data[5])->data.S32[i], tableU8_val6[i]); 255 return 3*i+6; 256 } 257 if(((psVector*)table1->values->data[6])->data.S64[i] != tableU8_val7[i]) { 258 psError(PS_ERR_UNKNOWN,true,"Value in column 7 do not match: value[%d]=%lld expected %lld",i, 259 ((psVector*)table1->values->data[6])->data.S64[i], tableU8_val7[i]); 260 return 3*i+7; 261 } 262 if(fabs(((psVector*)table1->values->data[7])->data.F32[i] - tableU8_val8[i]) > FLT_EPSILON ) { 263 psError(PS_ERR_UNKNOWN,true,"Value in column 8 do not match: value[%d]=%g expected %g",i, 264 ((psVector*)table1->values->data[7])->data.F32[i], tableU8_val8[i]); 265 return 3*i+8; 266 } 267 if(fabs(((psVector*)table1->values->data[8])->data.F64[i] - tableU8_val9[i]) > FLT_EPSILON ) { 268 psError(PS_ERR_UNKNOWN,true,"Value in column 9 do not match: value[%d]=%g expected %g",i, 269 ((psVector*)table1->values->data[8])->data.F32[i], tableU8_val9[i]); 270 return 3*i+9; 271 } 272 } 273 psFree(table1); 274 275 // Allocate table using table with psS32 index and valid types and index-values 276 table1 = psLookupTableAlloc("tableS32.dat", -110, 1000.5); 277 if(table1 == NULL) { 278 psError(PS_ERR_UNKNOWN,true,"Unable to allocate table with tableS32.dat"); 279 return 1; 280 } 281 table1 = psLookupTableRead(table1); 282 if(table1 == NULL) { 283 psError(PS_ERR_UNKNOWN,true,"Unable to read table from file tableS32.dat"); 284 return 2; 285 } 286 if( (fabs(table1->validFrom - tableS32_validFrom) > FLT_EPSILON) || 287 (fabs(table1->validTo - tableS32_validTo) > FLT_EPSILON) ) { 288 psError(PS_ERR_UNKNOWN,true,"ValidFrom or validTo not set properly"); 289 return 3; 290 } 291 for(psS32 i=0; i < tableS32_size; i++) { 292 if(table1->index->data.S32[i] != tableS32_index[i]) { 293 psError(PS_ERR_UNKNOWN,true,"Index values do not match: index[%d]=%d expected %d",i, 294 table1->index->data.S32[i], tableS32_index[i]); 295 return 4*i; 296 } 297 } 298 psFree(table1); 299 300 // Allocate table using table with psF32 index and valid types and index-values 301 table1 = psLookupTableAlloc("tableF32.dat", -1100, 5500.5); 302 if(table1 == NULL) { 303 psError(PS_ERR_UNKNOWN,true,"Unable to allocate table with tableF32.dat"); 304 return 1; 305 } 306 table1 = psLookupTableRead(table1); 307 if(table1 == NULL) { 308 psError(PS_ERR_UNKNOWN,true,"Unable to read table from file tableF32.dat"); 309 return 2; 310 } 311 if( (fabs(table1->validFrom - tableF32_validFrom) > errorTol_psF64) || 312 (fabs(table1->validTo - tableF32_validTo) > errorTol_psF64) ) { 313 psError(PS_ERR_UNKNOWN,true,"ValidFrom or validTo not set properly"); 314 return 3; 315 } 316 for(psS32 i=0; i < tableS32_size; i++) { 317 if(fabsf(table1->index->data.F32[i] - tableF32_index[i]) > FLT_EPSILON ) { 318 psError(PS_ERR_UNKNOWN,true,"Index values do not match: index[%d]=%d expected %d",i, 319 table1->index->data.F32[i], tableF32_index[i]); 320 return 4*i; 321 } 322 } 323 psFree(table1); 324 325 // Allocate table using psU8 index but with unsorted rows and verify the list is 326 // sorted properly after being read 327 table1 = psLookupTableAlloc("table10.dat",1,2); 328 if(table1 == NULL) { 329 psError(PS_ERR_UNKNOWN,true,"Unable to allocate table with table10.dat"); 330 return 1; 331 } 332 table1 = psLookupTableRead(table1); 333 if(table1 == NULL) { 334 psError(PS_ERR_UNKNOWN,true,"Unable to read table with table10.dat"); 335 return 2; 336 } 337 // Verify the validFrom and validTo are updated properly 338 if( (fabs(table1->validFrom - table10_validFrom) > FLT_EPSILON) || 339 (fabs(table1->validTo - table10_validTo) > FLT_EPSILON) ) { 340 psError(PS_ERR_UNKNOWN,true,"ValidFrom or validTo not set properly"); 341 return 3; 342 } 343 // Verify index and all values in table read properly 344 for(psS32 i=0; i < table10_size; i++) { 345 if(table1->index->data.U8[i] != table10_index[i]) { 346 psError(PS_ERR_UNKNOWN,true,"Index values do not match: index[%d]=%d expected %d",i, 347 table1->index->data.U8[i], table10_index[i]); 348 return 3*i; 349 } 350 if(((psVector*)table1->values->data[0])->data.U16[i] != table10_val1[i]) { 351 psError(PS_ERR_UNKNOWN,true,"Value in column 1 do not match: value[%d]=%d expected %d",i, 352 ((psVector*)table1->values->data[0])->data.U16[i], table10_val1[i]); 353 return 3*i+1; 354 } 355 if(((psVector*)table1->values->data[1])->data.U32[i] != table10_val2[i]) { 356 psError(PS_ERR_UNKNOWN,true,"Value in column 2 do not match: value[%d]=%d expected %d",i, 357 ((psVector*)table1->values->data[1])->data.U32[i], table10_val2[i]); 358 return 3*i+2; 359 } 360 if(((psVector*)table1->values->data[2])->data.U64[i] != table10_val3[i]) { 361 psError(PS_ERR_UNKNOWN,true,"Value in column 3 do not match: value[%d]=%lld expected %lld",i, 362 ((psVector*)table1->values->data[2])->data.U64[i], table10_val3[i]); 363 return 3*i+3; 364 } 365 if(((psVector*)table1->values->data[3])->data.S8[i] != table10_val4[i]) { 366 psError(PS_ERR_UNKNOWN,true,"Value in column 4 do not match: value[%d]=%d expected %d",i, 367 ((psVector*)table1->values->data[3])->data.S8[i], table10_val4[i]); 368 return 3*i+4; 369 } 370 if(((psVector*)table1->values->data[4])->data.S16[i] != table10_val5[i]) { 371 psError(PS_ERR_UNKNOWN,true,"Value in column 5 do not match: value[%d]=%d expected %d",i, 372 ((psVector*)table1->values->data[4])->data.S16[i], table10_val5[i]); 373 return 3*i+5; 374 } 375 if(((psVector*)table1->values->data[5])->data.S32[i] != table10_val6[i]) { 376 psError(PS_ERR_UNKNOWN,true,"Value in column 6 do not match: value[%d]=%d expected %d",i, 377 ((psVector*)table1->values->data[5])->data.S32[i], table10_val6[i]); 378 return 3*i+6; 379 } 380 if(((psVector*)table1->values->data[6])->data.S64[i] != table10_val7[i]) { 381 psError(PS_ERR_UNKNOWN,true,"Value in column 7 do not match: value[%d]=%lld expected %lld",i, 382 ((psVector*)table1->values->data[6])->data.S64[i], table10_val7[i]); 383 return 3*i+7; 384 } 385 if(fabs(((psVector*)table1->values->data[7])->data.F32[i] - table10_val8[i]) > FLT_EPSILON ) { 386 psError(PS_ERR_UNKNOWN,true,"Value in column 8 do not match: value[%d]=%g expected %g",i, 387 ((psVector*)table1->values->data[7])->data.F32[i], table10_val8[i]); 388 return 3*i+8; 389 } 390 if(fabs(((psVector*)table1->values->data[8])->data.F64[i] - table10_val9[i]) > FLT_EPSILON ) { 391 psError(PS_ERR_UNKNOWN,true,"Value in column 9 do not match: value[%d]=%g expected %g",i, 392 ((psVector*)table1->values->data[8])->data.F32[i], table10_val9[i]); 393 return 3*i+9; 394 } 395 } 396 psFree(table1); 397 398 // Allocate table using table with invalid type in type row but valid index-values 399 table1 = psLookupTableAlloc("table2.dat",0,99.99); 400 if(table1 == NULL) { 401 psError(PS_ERR_UNKNOWN,true,"Unable to allocate table with table2.dat"); 402 return 3; 403 } 404 psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message about invalid type"); 405 table1 = psLookupTableRead(table1); 406 if(table1 == NULL) { 407 psError(PS_ERR_UNKNOWN,true,"Unable to read table from file table2.dat"); 408 return 4; 409 } 410 psFree(table1); 411 412 // Allocate table using table with invalid value 413 table1 = psLookupTableAlloc("table3.dat",0,75.0); 414 if(table1 == NULL) { 415 psError(PS_ERR_UNKNOWN,true,"Unable to allocate table with table3.dat"); 416 return 5; 417 } 418 psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message about invalid value"); 419 table1 = psLookupTableRead(table1); 420 if(table1 == NULL) { 421 psError(PS_ERR_UNKNOWN,true,"Unable to read table from file table3.dat"); 422 return 6; 423 } 424 psFree(table1); 425 426 // Attempt to read for non-existant file 427 table1 = psLookupTableAlloc("nonexistantFile.dat",0,100); 428 if(table1 == NULL) { 429 psError(PS_ERR_UNKNOWN,true,"Unable to allocate table with non-existant file"); 430 return 3; 431 } 432 psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg about opening file"); 433 table1 = psLookupTableRead(table1); 434 if(table1 == NULL) { 435 psError(PS_ERR_UNKNOWN,true,"Returned NULL when table should have been returned"); 436 return 4; 437 } 438 439 // Attempt to read table with NULL specified file 440 psFree((char*)table1->fileName); 441 table1->fileName = NULL; 442 psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg about NULL file name"); 443 if(psLookupTableRead(table1) != NULL) { 444 psError(PS_ERR_UNKNOWN,true,"Did not return NULL with table specifying NULL file name"); 445 return 5; 446 } 447 psFree(table1); 448 449 // Attempt to read table with NULL table 450 psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg about NULL table"); 451 if(psLookupTableRead(NULL) != NULL) { 452 psError(PS_ERR_UNKNOWN,true,"Did not return NULL with NULL table."); 453 return 6; 454 } 455 456 return 0; 457 } 458 459 psS32 testLookupTableInterpolate(void) 460 { 461 psLookupStatusType status = 0; 462 psLookupTable* table1 = NULL; 463 psF64 out1 = 0; 464 465 // Interpolate values within the list and verify return values 466 table1 = psLookupTableAlloc("table10.dat",0,20); 467 table1 = psLookupTableRead(table1); 468 for(psS32 i = 0; i < table1->numRows-2; i++ ) { 469 out1 = psLookupTableInterpolate(table1, 5.25, i, &status); 470 if( fabs(out1-interpolVal1[i]) > errorTol_psF64) { 471 psError(PS_ERR_UNKNOWN,true,"Did not return expected value. %lg expected %lg",out1,interpolVal1[i]); 472 return 3*i; 473 } 474 } 475 for(psS32 i = 0; i < table1->numRows-2; i++ ) { 476 out1 = psLookupTableInterpolate(table1, 5.0, i, &status); 477 if( fabs(out1-interpolVal2[i]) > errorTol_psF64) { 478 psError(PS_ERR_UNKNOWN,true,"Did not return expected value. %lg expected %lg",out1,interpolVal2[i]); 479 return 4*i; 480 } 481 } 482 483 // Interpolate value just below the lowest index value 484 out1 = psLookupTableInterpolate(table1,0,0, &status); 485 if ( fabs(out1- NAN) > FLT_EPSILON ) { 486 psError(PS_ERR_UNKNOWN,true,"Did not return NAN returned %lf",out1); 487 return 5; 488 } 489 if ( status != PS_LOOKUP_PAST_TOP) { 490 psError(PS_ERR_UNKNOWN,true,"Did not return status PAST TOP"); 491 return 6; 492 } 493 494 // Interpolate value just above the highest index value 495 out1 = psLookupTableInterpolate(table1,11,0, &status); 496 if ( fabs(out1-NAN) > FLT_EPSILON ) { 497 psError(PS_ERR_UNKNOWN,true,"Did not return NAN returned %lf",out1); 498 return 7; 499 } 500 if ( status != PS_LOOKUP_PAST_BOTTOM) { 501 psError(PS_ERR_UNKNOWN,true,"Did not return status PAST BOTTOM"); 502 return 8; 503 } 504 505 // Interpolate value with a column number greater than the table has 506 psLogMsg(__func__,PS_LOG_INFO,"Following should generate error out of range."); 507 out1 = psLookupTableInterpolate(table1, 5, 100, &status); 508 if ( fabs(out1-NAN) > FLT_EPSILON ) { 509 psError(PS_ERR_UNKNOWN,true,"Did not return NAN returned %lf",out1); 510 return 9; 511 } 512 if ( status != PS_LOOKUP_ERROR ) { 513 psError(PS_ERR_UNKNOWN,true,"Did not return status LOOKUP ERROR"); 514 return 10; 515 } 516 517 // Interpolate with NULL table 518 psLogMsg(__func__,PS_LOG_INFO,"Following should generate error NULL table"); 519 out1 = psLookupTableInterpolate(NULL,5,0,&status); 520 if( fabs(out1-NAN) > FLT_EPSILON ) { 521 psError(PS_ERR_UNKNOWN,true,"Did not return NAN returned %lf",out1); 522 return 11; 523 } 524 if ( status != PS_LOOKUP_ERROR ) { 525 psError(PS_ERR_UNKNOWN,true,"Did not return status LOOKUP ERROR"); 526 return 12; 527 } 528 529 // Interpolate with status NULL 530 psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error NULL status"); 531 out1 = psLookupTableInterpolate(table1,5,0,NULL); 532 if ( fabs(out1-NAN) > FLT_EPSILON ) { 533 psError(PS_ERR_UNKNOWN,true,"Did not return NAN returned %lf",out1); 534 return 13; 535 } 536 537 psFree(table1); 538 539 return 0; 540 } 541 542 psS32 testLookupTableInterpolateAll(void) 543 { 544 psLookupTable* table1 = NULL; 545 psVector* interpValues; 546 psVector* statusVector; 547 548 // Interpolate values within the list and verify return values 549 table1 = psLookupTableAlloc("table10.dat",0,20); 550 table1 = psLookupTableRead(table1); 551 statusVector = psVectorAlloc(table1->numCols,PS_TYPE_U32); 552 interpValues = psLookupTableInterpolateAll(table1,5.25, statusVector); 553 if(interpValues == NULL) { 554 psError(PS_ERR_UNKNOWN,true,"Returned vector is NULL."); 555 return 1; 556 } 557 for(psS32 i = 0; i < table1->numRows-1; i++ ) { 558 if( fabs(interpValues->data.F64[i]-interpolVal1[i]) > errorTol_psF64) { 559 psError(PS_ERR_UNKNOWN,true,"Did not return expected value. %lg expected %lg", 560 interpValues->data.F64[i],interpolVal1[i]); 561 return 2*i; 562 } 563 } 564 for(psS32 j = 0; j < table1->numCols; j++) { 565 if(statusVector->data.U32[j] != PS_LOOKUP_SUCCESS) { 566 psError(PS_ERR_UNKNOWN,true,"Return status not success for col=%d status=%d", 567 j,statusVector->data.U32[j]); 568 return 3*j; 569 } 570 } 571 psFree(statusVector); 572 psFree(interpValues); 573 574 // Interpolate values with index outside table 575 statusVector = psVectorAlloc(table1->numCols,PS_TYPE_U32); 576 interpValues = psLookupTableInterpolateAll(table1,0,statusVector); 577 if(interpValues == NULL) { 578 psError(PS_ERR_UNKNOWN,true,"Did return NULL"); 579 return 4; 580 } 581 582 for(psS32 i = 0; i < table1->numRows-1; i++ ) { 583 if( fabs(interpValues->data.F64[i]-interpolVal3[i]) > errorTol_psF64) { 584 psError(PS_ERR_UNKNOWN,true,"Did not return expected value. %lg expected %lg", 585 interpValues->data.F64[i],interpolVal3[i]); 586 return 2*i; 587 } 588 } 589 590 for(psS32 j = 0; j < table1->numCols; j++) { 591 if(statusVector->data.S32[j] != PS_LOOKUP_PAST_TOP) { 592 psError(PS_ERR_UNKNOWN,true,"Return status did not indicate passed top for col=%d status=%d", 593 j,statusVector->data.U32[j]); 594 return 3*j; 595 } 596 } 597 psFree(interpValues); 598 psFree(statusVector); 599 600 // Interpolate values with NULL table 601 statusVector = psVectorAlloc(table1->numCols,PS_TYPE_S32); 602 psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message for NULL table"); 603 interpValues = psLookupTableInterpolateAll(NULL,5,statusVector); 604 if(interpValues != NULL ) { 605 psError(PS_ERR_UNKNOWN,true,"Did not return NULL for NULL table"); 606 return 5; 607 } 608 psFree(statusVector); 609 610 // Interpolate values with NULL status vector 611 psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message for NULL status vector"); 612 interpValues = psLookupTableInterpolateAll(table1,5,NULL); 613 if(interpValues != NULL ) { 614 psError(PS_ERR_UNKNOWN,true,"Did not return NULL for NULL status vector"); 615 return 6; 616 } 617 psFree(table1); 618 619 return 0; 620 } 621
Note:
See TracChangeset
for help on using the changeset viewer.
