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