1 /******************************************************************** 2 * COPYRIGHT: 3 * Copyright (c) 1998-2010, International Business Machines Corporation and 4 * others. All Rights Reserved. 5 ********************************************************************/ 6 /* 7 * File putiltst.c (Tests the API in putil) 8 * 9 * Modification History: 10 * 11 * Date Name Description 12 * 07/12/2000 Madhu Creation 13 ******************************************************************************* 14 */ 15 16 #include "unicode/utypes.h" 17 #include "cintltst.h" 18 #include "cmemory.h" 19 #include "unicode/putil.h" 20 #include "unicode/ustring.h" 21 #include "unicode/icudataver.h" 22 #include "cstring.h" 23 #include "putilimp.h" 24 #include "toolutil.h" 25 26 static UBool compareWithNAN(double x, double y); 27 static void doAssert(double expect, double got, const char *message); 28 29 static void TestPUtilAPI(void){ 30 31 double n1=0.0, y1=0.0, expn1, expy1; 32 double value1 = 0.021; 33 char *str=0; 34 UBool isTrue=FALSE; 35 36 log_verbose("Testing the API uprv_modf()\n"); 37 y1 = uprv_modf(value1, &n1); 38 expn1=0; 39 expy1=0.021; 40 if(y1 != expy1 || n1 != expn1){ 41 log_err("Error in uprv_modf. Expected IntegralValue=%f, Got=%f, \n Expected FractionalValue=%f, Got=%f\n", 42 expn1, n1, expy1, y1); 43 } 44 if(VERBOSITY){ 45 log_verbose("[float] x = %f n = %f y = %f\n", value1, n1, y1); 46 } 47 log_verbose("Testing the API uprv_fmod()\n"); 48 expn1=uprv_fmod(30.50, 15.00); 49 doAssert(expn1, 0.5, "uprv_fmod(30.50, 15.00) failed."); 50 51 log_verbose("Testing the API uprv_ceil()\n"); 52 expn1=uprv_ceil(value1); 53 doAssert(expn1, 1, "uprv_ceil(0.021) failed."); 54 55 log_verbose("Testing the API uprv_floor()\n"); 56 expn1=uprv_floor(value1); 57 doAssert(expn1, 0, "uprv_floor(0.021) failed."); 58 59 log_verbose("Testing the API uprv_fabs()\n"); 60 expn1=uprv_fabs((2.02-1.345)); 61 doAssert(expn1, 0.675, "uprv_fabs(2.02-1.345) failed."); 62 63 log_verbose("Testing the API uprv_fmax()\n"); 64 doAssert(uprv_fmax(2.4, 1.2), 2.4, "uprv_fmax(2.4, 1.2) failed."); 65 66 log_verbose("Testing the API uprv_fmax() with x value= NaN\n"); 67 expn1=uprv_fmax(uprv_getNaN(), 1.2); 68 doAssert(expn1, uprv_getNaN(), "uprv_fmax(uprv_getNaN(), 1.2) failed. when one parameter is NaN"); 69 70 log_verbose("Testing the API uprv_fmin()\n"); 71 doAssert(uprv_fmin(2.4, 1.2), 1.2, "uprv_fmin(2.4, 1.2) failed."); 72 73 log_verbose("Testing the API uprv_fmin() with x value= NaN\n"); 74 expn1=uprv_fmin(uprv_getNaN(), 1.2); 75 doAssert(expn1, uprv_getNaN(), "uprv_fmin(uprv_getNaN(), 1.2) failed. when one parameter is NaN"); 76 77 log_verbose("Testing the API uprv_max()\n"); 78 doAssert(uprv_max(4, 2), 4, "uprv_max(4, 2) failed."); 79 80 log_verbose("Testing the API uprv_min()\n"); 81 doAssert(uprv_min(-4, 2), -4, "uprv_min(-4, 2) failed."); 82 83 log_verbose("Testing the API uprv_trunc()\n"); 84 doAssert(uprv_trunc(12.3456), 12, "uprv_trunc(12.3456) failed."); 85 doAssert(uprv_trunc(12.234E2), 1223, "uprv_trunc(12.234E2) failed."); 86 doAssert(uprv_trunc(uprv_getNaN()), uprv_getNaN(), "uprv_trunc(uprv_getNaN()) failed. with parameter=NaN"); 87 doAssert(uprv_trunc(uprv_getInfinity()), uprv_getInfinity(), "uprv_trunc(uprv_getInfinity()) failed. with parameter=Infinity"); 88 89 90 log_verbose("Testing the API uprv_pow10()\n"); 91 doAssert(uprv_pow10(4), 10000, "uprv_pow10(4) failed."); 92 93 log_verbose("Testing the API uprv_isNegativeInfinity()\n"); 94 isTrue=uprv_isNegativeInfinity(uprv_getInfinity() * -1); 95 if(isTrue != TRUE){ 96 log_err("ERROR: uprv_isNegativeInfinity failed.\n"); 97 } 98 log_verbose("Testing the API uprv_isPositiveInfinity()\n"); 99 isTrue=uprv_isPositiveInfinity(uprv_getInfinity()); 100 if(isTrue != TRUE){ 101 log_err("ERROR: uprv_isPositiveInfinity failed.\n"); 102 } 103 log_verbose("Testing the API uprv_isInfinite()\n"); 104 isTrue=uprv_isInfinite(uprv_getInfinity()); 105 if(isTrue != TRUE){ 106 log_err("ERROR: uprv_isInfinite failed.\n"); 107 } 108 109 #if 0 110 log_verbose("Testing the API uprv_digitsAfterDecimal()....\n"); 111 doAssert(uprv_digitsAfterDecimal(value1), 3, "uprv_digitsAfterDecimal() failed."); 112 doAssert(uprv_digitsAfterDecimal(1.2345E2), 2, "uprv_digitsAfterDecimal(1.2345E2) failed."); 113 doAssert(uprv_digitsAfterDecimal(1.2345E-2), 6, "uprv_digitsAfterDecimal(1.2345E-2) failed."); 114 doAssert(uprv_digitsAfterDecimal(1.2345E2), 2, "uprv_digitsAfterDecimal(1.2345E2) failed."); 115 doAssert(uprv_digitsAfterDecimal(-1.2345E-20), 24, "uprv_digitsAfterDecimal(1.2345E-20) failed."); 116 doAssert(uprv_digitsAfterDecimal(1.2345E20), 0, "uprv_digitsAfterDecimal(1.2345E20) failed."); 117 doAssert(uprv_digitsAfterDecimal(-0.021), 3, "uprv_digitsAfterDecimal(-0.021) failed."); 118 doAssert(uprv_digitsAfterDecimal(23.0), 0, "uprv_digitsAfterDecimal(23.0) failed."); 119 doAssert(uprv_digitsAfterDecimal(0.022223333321), 9, "uprv_digitsAfterDecimal(0.022223333321) failed."); 120 #endif 121 122 log_verbose("Testing the API u_errorName()...\n"); 123 str=(char*)u_errorName((UErrorCode)0); 124 if(strcmp(str, "U_ZERO_ERROR") != 0){ 125 log_err("ERROR: u_getVersion() failed. Expected: U_ZERO_ERROR Got=%s\n", str); 126 } 127 log_verbose("Testing the API u_errorName()...\n"); 128 str=(char*)u_errorName((UErrorCode)-127); 129 if(strcmp(str, "U_USING_DEFAULT_WARNING") != 0){ 130 log_err("ERROR: u_getVersion() failed. Expected: U_USING_DEFAULT_WARNING Got=%s\n", str); 131 } 132 log_verbose("Testing the API u_errorName().. with BOGUS ERRORCODE...\n"); 133 str=(char*)u_errorName((UErrorCode)200); 134 if(strcmp(str, "[BOGUS UErrorCode]") != 0){ 135 log_err("ERROR: u_getVersion() failed. Expected: [BOGUS UErrorCode] Got=%s\n", str); 136 } 137 138 { 139 const char* dataDirectory; 140 int32_t dataDirectoryLen; 141 UChar *udataDir=0; 142 UChar temp[100]; 143 char *charvalue=0; 144 log_verbose("Testing chars to UChars\n"); 145 146 /* This cannot really work on a japanese system. u_uastrcpy will have different results than */ 147 /* u_charsToUChars when there is a backslash in the string! */ 148 /*dataDirectory=u_getDataDirectory();*/ 149 150 dataDirectory="directory1"; /*no backslashes*/ 151 dataDirectoryLen=(int32_t)strlen(dataDirectory); 152 udataDir=(UChar*)malloc(sizeof(UChar) * (dataDirectoryLen + 1)); 153 u_charsToUChars(dataDirectory, udataDir, (dataDirectoryLen + 1)); 154 u_uastrcpy(temp, dataDirectory); 155 156 if(u_strcmp(temp, udataDir) != 0){ 157 log_err("ERROR: u_charsToUChars failed. Expected %s, Got %s\n", austrdup(temp), austrdup(udataDir)); 158 } 159 log_verbose("Testing UChars to chars\n"); 160 charvalue=(char*)malloc(sizeof(char) * (u_strlen(udataDir) + 1)); 161 162 u_UCharsToChars(udataDir, charvalue, (u_strlen(udataDir)+1)); 163 if(strcmp(charvalue, dataDirectory) != 0){ 164 log_err("ERROR: u_UCharsToChars failed. Expected %s, Got %s\n", charvalue, dataDirectory); 165 } 166 free(charvalue); 167 free(udataDir); 168 } 169 170 log_verbose("Testing uprv_timezone()....\n"); 171 { 172 int32_t tzoffset = uprv_timezone(); 173 log_verbose("Value returned from uprv_timezone = %d\n", tzoffset); 174 if (tzoffset != 28800) { 175 log_verbose("***** WARNING: If testing in the PST timezone, t_timezone should return 28800! *****"); 176 } 177 if ((tzoffset % 1800 != 0)) { 178 log_err("FAIL: t_timezone may be incorrect. It is not a multiple of 30min."); 179 } 180 /*tzoffset=uprv_getUTCtime();*/ 181 182 } 183 } 184 185 static void TestVersion() 186 { 187 UVersionInfo versionArray = {0x01, 0x00, 0x02, 0x02}; 188 UVersionInfo versionArray2 = {0x01, 0x00, 0x02, 0x02}; 189 char versionString[17]; /* xxx.xxx.xxx.xxx\0 */ 190 UChar versionUString[] = { 0x0031, 0x002E, 0x0030, 0x002E, 191 0x0032, 0x002E, 0x0038, 0x0000 }; /* 1.0.2.8 */ 192 UBool isModified = FALSE; 193 UVersionInfo version; 194 UErrorCode status = U_ZERO_ERROR; 195 196 log_verbose("Testing the API u_versionToString().....\n"); 197 u_versionToString(versionArray, versionString); 198 if(strcmp(versionString, "1.0.2.2") != 0){ 199 log_err("ERROR: u_versionToString() failed. Expected: 1.0.2.2, Got=%s\n", versionString); 200 } 201 log_verbose("Testing the API u_versionToString().....with versionArray=NULL\n"); 202 u_versionToString(NULL, versionString); 203 if(strcmp(versionString, "") != 0){ 204 log_err("ERROR: u_versionToString() failed. with versionArray=NULL. It should just return\n"); 205 } 206 log_verbose("Testing the API u_versionToString().....with versionArray=NULL\n"); 207 u_versionToString(NULL, versionString); 208 if(strcmp(versionString, "") != 0){ 209 log_err("ERROR: u_versionToString() failed . It should just return\n"); 210 } 211 log_verbose("Testing the API u_versionToString().....with versionString=NULL\n"); 212 u_versionToString(versionArray, NULL); 213 if(strcmp(versionString, "") != 0){ 214 log_err("ERROR: u_versionToString() failed. with versionArray=NULL It should just return\n"); 215 } 216 versionArray[0] = 0x0a; 217 log_verbose("Testing the API u_versionToString().....\n"); 218 u_versionToString(versionArray, versionString); 219 if(strcmp(versionString, "10.0.2.2") != 0){ 220 log_err("ERROR: u_versionToString() failed. Expected: 10.0.2.2, Got=%s\n", versionString); 221 } 222 versionArray[0] = 0xa0; 223 u_versionToString(versionArray, versionString); 224 if(strcmp(versionString, "160.0.2.2") != 0){ 225 log_err("ERROR: u_versionToString() failed. Expected: 160.0.2.2, Got=%s\n", versionString); 226 } 227 versionArray[0] = 0xa0; 228 versionArray[1] = 0xa0; 229 u_versionToString(versionArray, versionString); 230 if(strcmp(versionString, "160.160.2.2") != 0){ 231 log_err("ERROR: u_versionToString() failed. Expected: 160.160.2.2, Got=%s\n", versionString); 232 } 233 versionArray[0] = 0x01; 234 versionArray[1] = 0x0a; 235 u_versionToString(versionArray, versionString); 236 if(strcmp(versionString, "1.10.2.2") != 0){ 237 log_err("ERROR: u_versionToString() failed. Expected: 160.160.2.2, Got=%s\n", versionString); 238 } 239 240 log_verbose("Testing the API u_versionFromString() ....\n"); 241 u_versionFromString(versionArray, "1.3.5.6"); 242 u_versionToString(versionArray, versionString); 243 if(strcmp(versionString, "1.3.5.6") != 0){ 244 log_err("ERROR: u_getVersion() failed. Expected: 1.3.5.6, Got=%s\n", versionString); 245 } 246 log_verbose("Testing the API u_versionFromString() where versionArray=NULL....\n"); 247 u_versionFromString(NULL, "1.3.5.6"); 248 u_versionToString(versionArray, versionString); 249 if(strcmp(versionString, "1.3.5.6") != 0){ 250 log_err("ERROR: u_getVersion() failed. Expected: 1.3.5.6, Got=%s\n", versionString); 251 } 252 253 log_verbose("Testing the API u_getVersion().....\n"); 254 u_getVersion(versionArray); 255 u_versionToString(versionArray, versionString); 256 if(strcmp(versionString, U_ICU_VERSION) != 0){ 257 log_err("ERROR: u_getVersion() failed. Got=%s, expected %s\n", versionString, U_ICU_VERSION); 258 } 259 /* test unicode */ 260 log_verbose("Testing u_versionFromUString...\n"); 261 u_versionFromString(versionArray,"1.0.2.8"); 262 u_versionFromUString(versionArray2, versionUString); 263 u_versionToString(versionArray2, versionString); 264 if(memcmp(versionArray, versionArray2, sizeof(UVersionInfo))) { 265 log_err("FAIL: u_versionFromUString produced a different result - not 1.0.2.8 but %s [%x.%x.%x.%x]\n", 266 versionString, 267 (int)versionArray2[0], 268 (int)versionArray2[1], 269 (int)versionArray2[2], 270 (int)versionArray2[3]); 271 } 272 else { 273 log_verbose(" from UString: %s\n", versionString); 274 } 275 276 /* Test the data version API for better code coverage */ 277 u_getDataVersion(version, &status); 278 if (U_FAILURE(status)) { 279 log_data_err("ERROR: Unable to get data version. %s\n", u_errorName(status)); 280 } else { 281 u_isDataOlder(version, &isModified, &status); 282 if (U_FAILURE(status)) { 283 log_err("ERROR: Unable to compare data version. %s\n", u_errorName(status)); 284 } 285 } 286 } 287 288 static void TestCompareVersions() 289 { 290 /* use a 1d array to be palatable to java */ 291 const char *testCases[] = { 292 /* v1 <|=|> v2 */ 293 "0.0.0.0", "=", "0.0.0.0", 294 "3.1.2.0", ">", "3.0.9.0", 295 "3.2.8.6", "<", "3.4", 296 "4.0", ">", "3.2", 297 NULL, NULL, NULL 298 }; 299 const char *v1str; 300 const char *opstr; 301 const char *v2str; 302 int32_t op, invop, got, invgot; 303 UVersionInfo v1, v2; 304 int32_t j; 305 log_verbose("Testing memcmp()\n"); 306 for(j=0;testCases[j]!=NULL;j+=3) { 307 v1str = testCases[j+0]; 308 opstr = testCases[j+1]; 309 v2str = testCases[j+2]; 310 switch(opstr[0]) { 311 case '-': 312 case '<': op = -1; break; 313 case '0': 314 case '=': op = 0; break; 315 case '+': 316 case '>': op = 1; break; 317 default: log_err("Bad operator at j/3=%d\n", (j/3)); return; 318 } 319 invop = 0-op; /* inverse operation: with v1 and v2 switched */ 320 u_versionFromString(v1, v1str); 321 u_versionFromString(v2, v2str); 322 got = memcmp(v1, v2, sizeof(UVersionInfo)); 323 invgot = memcmp(v2, v1, sizeof(UVersionInfo)); /* Opposite */ 324 if((got <= 0 && op <= 0) || (got >= 0 && op >= 0)) { 325 log_verbose("%d: %s %s %s, OK\n", (j/3), v1str, opstr, v2str); 326 } else { 327 log_err("%d: %s %s %s: wanted values of the same sign, %d got %d\n", (j/3), v1str, opstr, v2str, op, got); 328 } 329 if((invgot >= 0 && invop >= 0) || (invgot <= 0 && invop <= 0)) { 330 log_verbose("%d: %s (%d) %s, OK (inverse)\n", (j/3), v2str, invop, v1str); 331 } else { 332 log_err("%d: %s (%d) %s: wanted values of the same sign, %d got %d\n", (j/3), v2str, invop, v1str, invop, invgot); 333 } 334 } 335 } 336 337 338 339 #if 0 340 static void testIEEEremainder() 341 { 342 double pinf = uprv_getInfinity(); 343 double ninf = -uprv_getInfinity(); 344 double nan = uprv_getNaN(); 345 /* double pzero = 0.0;*/ 346 /* double nzero = 0.0; 347 nzero *= -1;*/ 348 349 /* simple remainder checks*/ 350 remainderTest(7.0, 2.5, -0.5); 351 remainderTest(7.0, -2.5, -0.5); 352 /* this should work 353 remainderTest(43.7, 2.5, 1.2); 354 */ 355 356 /* infinity and real*/ 357 remainderTest(1.0, pinf, 1.0); 358 remainderTest(1.0, ninf, 1.0); 359 360 /*test infinity and real*/ 361 remainderTest(nan, 1.0, nan); 362 remainderTest(1.0, nan, nan); 363 /*test infinity and nan*/ 364 remainderTest(ninf, nan, nan); 365 remainderTest(pinf, nan, nan); 366 367 /* test infinity and zero */ 368 /* remainderTest(pinf, pzero, 1.25); 369 remainderTest(pinf, nzero, 1.25); 370 remainderTest(ninf, pzero, 1.25); 371 remainderTest(ninf, nzero, 1.25); */ 372 } 373 374 static void remainderTest(double x, double y, double exp) 375 { 376 double result = uprv_IEEEremainder(x,y); 377 378 if( uprv_isNaN(result) && 379 ! ( uprv_isNaN(x) || uprv_isNaN(y))) { 380 log_err("FAIL: got NaN as result without NaN as argument"); 381 log_err(" IEEEremainder(%f, %f) is %f, expected %f\n", x, y, result, exp); 382 } 383 else if(!compareWithNAN(result, exp)) { 384 log_err("FAIL: IEEEremainder(%f, %f) is %f, expected %f\n", x, y, result, exp); 385 } else{ 386 log_verbose("OK: IEEEremainder(%f, %f) is %f\n", x, y, result); 387 } 388 389 } 390 #endif 391 392 static UBool compareWithNAN(double x, double y) 393 { 394 if( uprv_isNaN(x) || uprv_isNaN(y) ) { 395 if(!uprv_isNaN(x) || !uprv_isNaN(y) ) { 396 return FALSE; 397 } 398 } 399 else if (y != x) { /* no NaN's involved */ 400 return FALSE; 401 } 402 403 return TRUE; 404 } 405 406 static void doAssert(double got, double expect, const char *message) 407 { 408 if(! compareWithNAN(expect, got) ) { 409 log_err("ERROR : %s. Expected : %lf, Got: %lf\n", message, expect, got); 410 } 411 } 412 413 414 #define _CODE_ARR_LEN 8 415 static const UErrorCode errorCode[_CODE_ARR_LEN] = { 416 U_USING_FALLBACK_WARNING, 417 U_STRING_NOT_TERMINATED_WARNING, 418 U_ILLEGAL_ARGUMENT_ERROR, 419 U_STATE_TOO_OLD_ERROR, 420 U_BAD_VARIABLE_DEFINITION, 421 U_RULE_MASK_ERROR, 422 U_UNEXPECTED_TOKEN, 423 U_UNSUPPORTED_ATTRIBUTE 424 }; 425 426 static const char* str[] = { 427 "U_USING_FALLBACK_WARNING", 428 "U_STRING_NOT_TERMINATED_WARNING", 429 "U_ILLEGAL_ARGUMENT_ERROR", 430 "U_STATE_TOO_OLD_ERROR", 431 "U_BAD_VARIABLE_DEFINITION", 432 "U_RULE_MASK_ERROR", 433 "U_UNEXPECTED_TOKEN", 434 "U_UNSUPPORTED_ATTRIBUTE" 435 }; 436 437 static void TestErrorName(void){ 438 int32_t code=0; 439 const char* errorName ; 440 for(;code<U_ERROR_LIMIT;code++){ 441 errorName = u_errorName((UErrorCode)code); 442 if(!errorName || errorName[0] == 0) { 443 log_err("Error: u_errorName(0x%X) failed.\n",code); 444 } 445 } 446 447 for(code=0;code<_CODE_ARR_LEN; code++){ 448 errorName = u_errorName(errorCode[code]); 449 if(uprv_strcmp(str[code],errorName )!=0){ 450 log_err("Error : u_errorName failed. Expected: %s Got: %s \n",str[code],errorName); 451 } 452 } 453 } 454 455 void addPUtilTest(TestNode** root); 456 457 static void addToolUtilTests(TestNode** root); 458 459 void 460 addPUtilTest(TestNode** root) 461 { 462 addTest(root, &TestVersion, "putiltst/TestVersion"); 463 addTest(root, &TestCompareVersions, "putiltst/TestCompareVersions"); 464 /* addTest(root, &testIEEEremainder, "putiltst/testIEEEremainder"); */ 465 addTest(root, &TestErrorName, "putiltst/TestErrorName"); 466 addTest(root, &TestPUtilAPI, "putiltst/TestPUtilAPI"); 467 468 addToolUtilTests(root); 469 } 470 471 /* Tool Util Tests ================ */ 472 #define TOOLUTIL_TESTBUF_SIZE 2048 473 static char toolutil_testBuf[TOOLUTIL_TESTBUF_SIZE]; 474 static const char *NULLSTR="NULL"; 475 476 /** 477 * Normalize NULL to 'NULL' for testing 478 */ 479 #define STRNULL(x) ((x)?(x):NULLSTR) 480 481 static void toolutil_findBasename(void) 482 { 483 struct { 484 const char *inBuf; 485 const char *expectResult; 486 } testCases[] = { 487 { 488 U_FILE_SEP_STRING "usr" U_FILE_SEP_STRING "bin" U_FILE_SEP_STRING "pkgdata", 489 "pkgdata" 490 }, 491 { 492 U_FILE_SEP_STRING "usr" U_FILE_SEP_STRING "bin" U_FILE_SEP_STRING, 493 "" 494 }, 495 { 496 U_FILE_ALT_SEP_STRING "usr" U_FILE_ALT_SEP_STRING "bin" U_FILE_ALT_SEP_STRING "pkgdata", 497 "pkgdata" 498 }, 499 { 500 U_FILE_ALT_SEP_STRING "usr" U_FILE_ALT_SEP_STRING "bin" U_FILE_ALT_SEP_STRING, 501 "" 502 }, 503 }; 504 int32_t count=(sizeof(testCases)/sizeof(testCases[0])); 505 int32_t i; 506 507 508 log_verbose("Testing findBaseName()\n"); 509 for(i=0;i<count;i++) { 510 const char *result; 511 const char *input = STRNULL(testCases[i].inBuf); 512 const char *expect = STRNULL(testCases[i].expectResult); 513 log_verbose("Test case [%d/%d]: %s\n", i, count-1, input); 514 result = STRNULL(findBasename(testCases[i].inBuf)); 515 if(result==expect||!strcmp(result,expect)) { 516 log_verbose(" -> %s PASS\n", result); 517 } else { 518 log_err("FAIL: Test case [%d/%d]: %s -> %s but expected %s\n", i, count-1, input, result, expect); 519 } 520 } 521 } 522 523 524 static void toolutil_findDirname(void) 525 { 526 int i; 527 struct { 528 const char *inBuf; 529 int32_t outBufLen; 530 UErrorCode expectStatus; 531 const char *expectResult; 532 } testCases[] = { 533 { 534 U_FILE_SEP_STRING "usr" U_FILE_SEP_STRING "bin" U_FILE_SEP_STRING "pkgdata", 535 200, 536 U_ZERO_ERROR, 537 U_FILE_SEP_STRING "usr" U_FILE_SEP_STRING "bin", 538 }, 539 { 540 U_FILE_SEP_STRING "usr" U_FILE_SEP_STRING "bin" U_FILE_SEP_STRING "pkgdata", 541 2, 542 U_BUFFER_OVERFLOW_ERROR, 543 NULL 544 }, 545 { 546 U_FILE_ALT_SEP_STRING "usr" U_FILE_ALT_SEP_STRING "bin" U_FILE_ALT_SEP_STRING "pkgdata", 547 200, 548 U_ZERO_ERROR, 549 U_FILE_ALT_SEP_STRING "usr" U_FILE_ALT_SEP_STRING "bin" 550 }, 551 { 552 U_FILE_ALT_SEP_STRING "usr" U_FILE_ALT_SEP_STRING "bin" U_FILE_ALT_SEP_STRING "pkgdata", 553 2, 554 U_BUFFER_OVERFLOW_ERROR, 555 NULL 556 }, 557 { 558 U_FILE_ALT_SEP_STRING "usr" U_FILE_ALT_SEP_STRING "bin" U_FILE_SEP_STRING "pkgdata", 559 200, 560 U_ZERO_ERROR, 561 U_FILE_ALT_SEP_STRING "usr" U_FILE_ALT_SEP_STRING "bin" 562 }, 563 { 564 U_FILE_ALT_SEP_STRING "usr" U_FILE_SEP_STRING "bin" U_FILE_ALT_SEP_STRING "pkgdata", 565 200, 566 U_ZERO_ERROR, 567 U_FILE_ALT_SEP_STRING "usr" U_FILE_SEP_STRING "bin" 568 }, 569 { 570 U_FILE_ALT_SEP_STRING "usr" U_FILE_ALT_SEP_STRING "bin" U_FILE_ALT_SEP_STRING "pkgdata", 571 2, 572 U_BUFFER_OVERFLOW_ERROR, 573 NULL 574 }, 575 { 576 U_FILE_ALT_SEP_STRING "vmlinuz", 577 200, 578 U_ZERO_ERROR, 579 U_FILE_ALT_SEP_STRING 580 }, 581 { 582 U_FILE_SEP_STRING "vmlinux", 583 200, 584 U_ZERO_ERROR, 585 U_FILE_SEP_STRING 586 }, 587 { 588 "pkgdata", 589 0, 590 U_BUFFER_OVERFLOW_ERROR, 591 NULL 592 }, 593 { 594 "pkgdata", 595 1, 596 U_BUFFER_OVERFLOW_ERROR, 597 NULL 598 }, 599 { 600 "pkgdata", 601 2, 602 U_ZERO_ERROR, 603 "." 604 }, 605 { 606 "pkgdata", 607 20, 608 U_ZERO_ERROR, 609 "." 610 } 611 }; 612 int32_t count=(sizeof(testCases)/sizeof(testCases[0])); 613 614 log_verbose("Testing findDirname()\n"); 615 for(i=0;i<count;i++) { 616 const char *result; 617 const char *input = STRNULL(testCases[i].inBuf); 618 const char *expect = STRNULL(testCases[i].expectResult); 619 UErrorCode status = U_ZERO_ERROR; 620 uprv_memset(toolutil_testBuf, 0x55, TOOLUTIL_TESTBUF_SIZE); 621 622 log_verbose("Test case [%d/%d]: %s\n", i, count-1, input); 623 result = STRNULL(findDirname(testCases[i].inBuf, toolutil_testBuf, testCases[i].outBufLen, &status)); 624 log_verbose(" -> %s, \n", u_errorName(status)); 625 if(status != testCases[i].expectStatus) { 626 log_verbose("FAIL: Test case [%d/%d]: %s got error code %s but expected %s\n", i, count-1, input, u_errorName(status), u_errorName(testCases[i].expectStatus)); 627 } 628 if(result==expect||!strcmp(result,expect)) { 629 log_verbose(" = -> %s \n", result); 630 } else { 631 log_err("FAIL: Test case [%d/%d]: %s -> %s but expected %s\n", i, count-1, input, result, expect); 632 } 633 } 634 } 635 636 637 638 static void addToolUtilTests(TestNode** root) { 639 addTest(root, &toolutil_findBasename, "putiltst/toolutil/findBasename"); 640 addTest(root, &toolutil_findDirname, "putiltst/toolutil/findDirname"); 641 /* 642 Not yet tested: 643 644 addTest(root, &toolutil_getLongPathname, "putiltst/toolutil/getLongPathname"); 645 addTest(root, &toolutil_getCurrentYear, "putiltst/toolutil/getCurrentYear"); 646 addTest(root, &toolutil_UToolMemory, "putiltst/toolutil/UToolMemory"); 647 */ 648 } 649