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