1 /********************************************************************
3 * Copyright (c) 1998-2016, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 * File putiltst.c (Tests the API in putil)
9 * Modification History:
11 * Date Name Description
12 * 07/12/2000 Madhu Creation
13 *******************************************************************************
16 #include "unicode/utypes.h"
19 #include "unicode/putil.h"
20 #include "unicode/ustring.h"
21 #include "unicode/icudataver.h"
28 /* See the comments on U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC. */
29 static void TestSignedRightShiftIsArithmetic(void) {
34 UBool signedRightShiftIsArithmetic
= x4
==0xffff5fff && m1
==-1;
35 if(signedRightShiftIsArithmetic
==U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC
) {
36 log_info("signed right shift is Arithmetic Shift Right: %d\n",
37 signedRightShiftIsArithmetic
);
39 log_err("error: unexpected signed right shift is Arithmetic Shift Right: %d\n"
40 " You need to change the value of U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC "
41 "for your platform.\n",
42 signedRightShiftIsArithmetic
);
46 static UBool
compareWithNAN(double x
, double y
);
47 static void doAssert(double expect
, double got
, const char *message
);
49 static void TestPUtilAPI(void){
51 double n1
=0.0, y1
=0.0, expn1
, expy1
;
52 double value1
= 0.021;
56 log_verbose("Testing the API uprv_modf()\n");
57 y1
= uprv_modf(value1
, &n1
);
60 if(y1
!= expy1
|| n1
!= expn1
){
61 log_err("Error in uprv_modf. Expected IntegralValue=%f, Got=%f, \n Expected FractionalValue=%f, Got=%f\n",
62 expn1
, n1
, expy1
, y1
);
64 if(getTestOption(VERBOSITY_OPTION
)){
65 log_verbose("[float] x = %f n = %f y = %f\n", value1
, n1
, y1
);
67 log_verbose("Testing the API uprv_fmod()\n");
68 expn1
=uprv_fmod(30.50, 15.00);
69 doAssert(expn1
, 0.5, "uprv_fmod(30.50, 15.00) failed.");
71 log_verbose("Testing the API uprv_ceil()\n");
72 expn1
=uprv_ceil(value1
);
73 doAssert(expn1
, 1, "uprv_ceil(0.021) failed.");
75 log_verbose("Testing the API uprv_floor()\n");
76 expn1
=uprv_floor(value1
);
77 doAssert(expn1
, 0, "uprv_floor(0.021) failed.");
79 log_verbose("Testing the API uprv_fabs()\n");
80 expn1
=uprv_fabs((2.02-1.345));
81 doAssert(expn1
, 0.675, "uprv_fabs(2.02-1.345) failed.");
83 log_verbose("Testing the API uprv_fmax()\n");
84 doAssert(uprv_fmax(2.4, 1.2), 2.4, "uprv_fmax(2.4, 1.2) failed.");
86 log_verbose("Testing the API uprv_fmax() with x value= NaN\n");
87 expn1
=uprv_fmax(uprv_getNaN(), 1.2);
88 doAssert(expn1
, uprv_getNaN(), "uprv_fmax(uprv_getNaN(), 1.2) failed. when one parameter is NaN");
90 log_verbose("Testing the API uprv_fmin()\n");
91 doAssert(uprv_fmin(2.4, 1.2), 1.2, "uprv_fmin(2.4, 1.2) failed.");
93 log_verbose("Testing the API uprv_fmin() with x value= NaN\n");
94 expn1
=uprv_fmin(uprv_getNaN(), 1.2);
95 doAssert(expn1
, uprv_getNaN(), "uprv_fmin(uprv_getNaN(), 1.2) failed. when one parameter is NaN");
97 log_verbose("Testing the API uprv_max()\n");
98 doAssert(uprv_max(4, 2), 4, "uprv_max(4, 2) failed.");
100 log_verbose("Testing the API uprv_min()\n");
101 doAssert(uprv_min(-4, 2), -4, "uprv_min(-4, 2) failed.");
103 log_verbose("Testing the API uprv_trunc()\n");
104 doAssert(uprv_trunc(12.3456), 12, "uprv_trunc(12.3456) failed.");
105 doAssert(uprv_trunc(12.234E2
), 1223, "uprv_trunc(12.234E2) failed.");
106 doAssert(uprv_trunc(uprv_getNaN()), uprv_getNaN(), "uprv_trunc(uprv_getNaN()) failed. with parameter=NaN");
107 doAssert(uprv_trunc(uprv_getInfinity()), uprv_getInfinity(), "uprv_trunc(uprv_getInfinity()) failed. with parameter=Infinity");
110 log_verbose("Testing the API uprv_pow10()\n");
111 doAssert(uprv_pow10(4), 10000, "uprv_pow10(4) failed.");
113 log_verbose("Testing the API uprv_isNegativeInfinity()\n");
114 isTrue
=uprv_isNegativeInfinity(uprv_getInfinity() * -1);
116 log_err("ERROR: uprv_isNegativeInfinity failed.\n");
118 log_verbose("Testing the API uprv_isPositiveInfinity()\n");
119 isTrue
=uprv_isPositiveInfinity(uprv_getInfinity());
121 log_err("ERROR: uprv_isPositiveInfinity failed.\n");
123 log_verbose("Testing the API uprv_isInfinite()\n");
124 isTrue
=uprv_isInfinite(uprv_getInfinity());
126 log_err("ERROR: uprv_isInfinite failed.\n");
130 log_verbose("Testing the API uprv_digitsAfterDecimal()....\n");
131 doAssert(uprv_digitsAfterDecimal(value1
), 3, "uprv_digitsAfterDecimal() failed.");
132 doAssert(uprv_digitsAfterDecimal(1.2345E2
), 2, "uprv_digitsAfterDecimal(1.2345E2) failed.");
133 doAssert(uprv_digitsAfterDecimal(1.2345E-2), 6, "uprv_digitsAfterDecimal(1.2345E-2) failed.");
134 doAssert(uprv_digitsAfterDecimal(1.2345E2
), 2, "uprv_digitsAfterDecimal(1.2345E2) failed.");
135 doAssert(uprv_digitsAfterDecimal(-1.2345E-20), 24, "uprv_digitsAfterDecimal(1.2345E-20) failed.");
136 doAssert(uprv_digitsAfterDecimal(1.2345E20
), 0, "uprv_digitsAfterDecimal(1.2345E20) failed.");
137 doAssert(uprv_digitsAfterDecimal(-0.021), 3, "uprv_digitsAfterDecimal(-0.021) failed.");
138 doAssert(uprv_digitsAfterDecimal(23.0), 0, "uprv_digitsAfterDecimal(23.0) failed.");
139 doAssert(uprv_digitsAfterDecimal(0.022223333321), 9, "uprv_digitsAfterDecimal(0.022223333321) failed.");
142 log_verbose("Testing the API u_errorName()...\n");
143 str
=(char*)u_errorName((UErrorCode
)0);
144 if(strcmp(str
, "U_ZERO_ERROR") != 0){
145 log_err("ERROR: u_getVersion() failed. Expected: U_ZERO_ERROR Got=%s\n", str
);
147 log_verbose("Testing the API u_errorName()...\n");
148 str
=(char*)u_errorName((UErrorCode
)-127);
149 if(strcmp(str
, "U_USING_DEFAULT_WARNING") != 0){
150 log_err("ERROR: u_getVersion() failed. Expected: U_USING_DEFAULT_WARNING Got=%s\n", str
);
152 log_verbose("Testing the API u_errorName().. with BOGUS ERRORCODE...\n");
153 str
=(char*)u_errorName((UErrorCode
)200);
154 if(strcmp(str
, "[BOGUS UErrorCode]") != 0){
155 log_err("ERROR: u_getVersion() failed. Expected: [BOGUS UErrorCode] Got=%s\n", str
);
159 const char* dataDirectory
;
160 int32_t dataDirectoryLen
;
164 log_verbose("Testing chars to UChars\n");
166 /* This cannot really work on a japanese system. u_uastrcpy will have different results than */
167 /* u_charsToUChars when there is a backslash in the string! */
168 /*dataDirectory=u_getDataDirectory();*/
170 dataDirectory
="directory1"; /*no backslashes*/
171 dataDirectoryLen
=(int32_t)strlen(dataDirectory
);
172 udataDir
=(UChar
*)malloc(sizeof(UChar
) * (dataDirectoryLen
+ 1));
173 u_charsToUChars(dataDirectory
, udataDir
, (dataDirectoryLen
+ 1));
174 u_uastrcpy(temp
, dataDirectory
);
176 if(u_strcmp(temp
, udataDir
) != 0){
177 log_err("ERROR: u_charsToUChars failed. Expected %s, Got %s\n", austrdup(temp
), austrdup(udataDir
));
179 log_verbose("Testing UChars to chars\n");
180 charvalue
=(char*)malloc(sizeof(char) * (u_strlen(udataDir
) + 1));
182 u_UCharsToChars(udataDir
, charvalue
, (u_strlen(udataDir
)+1));
183 if(strcmp(charvalue
, dataDirectory
) != 0){
184 log_err("ERROR: u_UCharsToChars failed. Expected %s, Got %s\n", charvalue
, dataDirectory
);
190 log_verbose("Testing uprv_timezone()....\n");
192 int32_t tzoffset
= uprv_timezone();
193 log_verbose("Value returned from uprv_timezone = %d\n", tzoffset
);
194 if (tzoffset
!= 28800) {
195 log_verbose("***** WARNING: If testing in the PST timezone, t_timezone should return 28800! *****");
197 if ((tzoffset
% 1800 != 0)) {
198 log_info("Note: t_timezone offset of %ld (for %s : %s) is not a multiple of 30min.", tzoffset
, uprv_tzname(0), uprv_tzname(1));
200 /*tzoffset=uprv_getUTCtime();*/
205 static void TestVersion(void)
207 UVersionInfo versionArray
= {0x01, 0x00, 0x02, 0x02};
208 UVersionInfo versionArray2
= {0x01, 0x00, 0x02, 0x02};
209 char versionString
[17]; /* xxx.xxx.xxx.xxx\0 */
210 UChar versionUString
[] = { 0x0031, 0x002E, 0x0030, 0x002E,
211 0x0032, 0x002E, 0x0038, 0x0000 }; /* 1.0.2.8 */
212 UVersionInfo version
;
213 UErrorCode status
= U_ZERO_ERROR
;
215 log_verbose("Testing the API u_versionToString().....\n");
216 u_versionToString(versionArray
, versionString
);
217 if(strcmp(versionString
, "1.0.2.2") != 0){
218 log_err("ERROR: u_versionToString() failed. Expected: 1.0.2.2, Got=%s\n", versionString
);
220 log_verbose("Testing the API u_versionToString().....with versionArray=NULL\n");
221 u_versionToString(NULL
, versionString
);
222 if(strcmp(versionString
, "") != 0){
223 log_err("ERROR: u_versionToString() failed. with versionArray=NULL. It should just return\n");
225 log_verbose("Testing the API u_versionToString().....with versionArray=NULL\n");
226 u_versionToString(NULL
, versionString
);
227 if(strcmp(versionString
, "") != 0){
228 log_err("ERROR: u_versionToString() failed . It should just return\n");
230 log_verbose("Testing the API u_versionToString().....with versionString=NULL\n");
231 u_versionToString(versionArray
, NULL
);
232 if(strcmp(versionString
, "") != 0){
233 log_err("ERROR: u_versionToString() failed. with versionArray=NULL It should just return\n");
235 versionArray
[0] = 0x0a;
236 log_verbose("Testing the API u_versionToString().....\n");
237 u_versionToString(versionArray
, versionString
);
238 if(strcmp(versionString
, "10.0.2.2") != 0){
239 log_err("ERROR: u_versionToString() failed. Expected: 10.0.2.2, Got=%s\n", versionString
);
241 versionArray
[0] = 0xa0;
242 u_versionToString(versionArray
, versionString
);
243 if(strcmp(versionString
, "160.0.2.2") != 0){
244 log_err("ERROR: u_versionToString() failed. Expected: 160.0.2.2, Got=%s\n", versionString
);
246 versionArray
[0] = 0xa0;
247 versionArray
[1] = 0xa0;
248 u_versionToString(versionArray
, versionString
);
249 if(strcmp(versionString
, "160.160.2.2") != 0){
250 log_err("ERROR: u_versionToString() failed. Expected: 160.160.2.2, Got=%s\n", versionString
);
252 versionArray
[0] = 0x01;
253 versionArray
[1] = 0x0a;
254 u_versionToString(versionArray
, versionString
);
255 if(strcmp(versionString
, "1.10.2.2") != 0){
256 log_err("ERROR: u_versionToString() failed. Expected: 160.160.2.2, Got=%s\n", versionString
);
259 log_verbose("Testing the API u_versionFromString() ....\n");
260 u_versionFromString(versionArray
, "1.3.5.6");
261 u_versionToString(versionArray
, versionString
);
262 if(strcmp(versionString
, "1.3.5.6") != 0){
263 log_err("ERROR: u_getVersion() failed. Expected: 1.3.5.6, Got=%s\n", versionString
);
265 log_verbose("Testing the API u_versionFromString() where versionArray=NULL....\n");
266 u_versionFromString(NULL
, "1.3.5.6");
267 u_versionToString(versionArray
, versionString
);
268 if(strcmp(versionString
, "1.3.5.6") != 0){
269 log_err("ERROR: u_getVersion() failed. Expected: 1.3.5.6, Got=%s\n", versionString
);
272 log_verbose("Testing the API u_getVersion().....\n");
273 u_getVersion(versionArray
);
274 u_versionToString(versionArray
, versionString
);
275 if(strcmp(versionString
, U_ICU_VERSION
) != 0){
276 log_err("ERROR: u_getVersion() failed. Got=%s, expected %s\n", versionString
, U_ICU_VERSION
);
279 log_verbose("Testing u_versionFromUString...\n");
280 u_versionFromString(versionArray
,"1.0.2.8");
281 u_versionFromUString(versionArray2
, versionUString
);
282 u_versionToString(versionArray2
, versionString
);
283 if(memcmp(versionArray
, versionArray2
, sizeof(UVersionInfo
))) {
284 log_err("FAIL: u_versionFromUString produced a different result - not 1.0.2.8 but %s [%x.%x.%x.%x]\n",
286 (int)versionArray2
[0],
287 (int)versionArray2
[1],
288 (int)versionArray2
[2],
289 (int)versionArray2
[3]);
292 log_verbose(" from UString: %s\n", versionString
);
295 /* Test the data version API for better code coverage */
296 u_getDataVersion(version
, &status
);
297 if (U_FAILURE(status
)) {
298 log_data_err("ERROR: Unable to get data version. %s\n", u_errorName(status
));
302 static void TestCompareVersions(void)
304 /* use a 1d array to be palatable to java */
305 const char *testCases
[] = {
307 "0.0.0.0", "=", "0.0.0.0",
308 "3.1.2.0", ">", "3.0.9.0",
309 "3.2.8.6", "<", "3.4",
316 int32_t op
, invop
, got
, invgot
;
319 log_verbose("Testing memcmp()\n");
320 for(j
=0;testCases
[j
]!=NULL
;j
+=3) {
321 v1str
= testCases
[j
+0];
322 opstr
= testCases
[j
+1];
323 v2str
= testCases
[j
+2];
326 case '<': op
= -1; break;
328 case '=': op
= 0; break;
330 case '>': op
= 1; break;
331 default: log_err("Bad operator at j/3=%d\n", (j
/3)); return;
333 invop
= 0-op
; /* inverse operation: with v1 and v2 switched */
334 u_versionFromString(v1
, v1str
);
335 u_versionFromString(v2
, v2str
);
336 got
= memcmp(v1
, v2
, sizeof(UVersionInfo
));
337 invgot
= memcmp(v2
, v1
, sizeof(UVersionInfo
)); /* Opposite */
338 if((got
<= 0 && op
<= 0) || (got
>= 0 && op
>= 0)) {
339 log_verbose("%d: %s %s %s, OK\n", (j
/3), v1str
, opstr
, v2str
);
341 log_err("%d: %s %s %s: wanted values of the same sign, %d got %d\n", (j
/3), v1str
, opstr
, v2str
, op
, got
);
343 if((invgot
>= 0 && invop
>= 0) || (invgot
<= 0 && invop
<= 0)) {
344 log_verbose("%d: %s (%d) %s, OK (inverse)\n", (j
/3), v2str
, invop
, v1str
);
346 log_err("%d: %s (%d) %s: wanted values of the same sign, %d got %d\n", (j
/3), v2str
, invop
, v1str
, invop
, invgot
);
354 static void testIEEEremainder()
356 double pinf
= uprv_getInfinity();
357 double ninf
= -uprv_getInfinity();
358 double nan
= uprv_getNaN();
359 /* double pzero = 0.0;*/
360 /* double nzero = 0.0;
363 /* simple remainder checks*/
364 remainderTest(7.0, 2.5, -0.5);
365 remainderTest(7.0, -2.5, -0.5);
367 remainderTest(43.7, 2.5, 1.2);
370 /* infinity and real*/
371 remainderTest(1.0, pinf
, 1.0);
372 remainderTest(1.0, ninf
, 1.0);
374 /*test infinity and real*/
375 remainderTest(nan
, 1.0, nan
);
376 remainderTest(1.0, nan
, nan
);
377 /*test infinity and nan*/
378 remainderTest(ninf
, nan
, nan
);
379 remainderTest(pinf
, nan
, nan
);
381 /* test infinity and zero */
382 /* remainderTest(pinf, pzero, 1.25);
383 remainderTest(pinf, nzero, 1.25);
384 remainderTest(ninf, pzero, 1.25);
385 remainderTest(ninf, nzero, 1.25); */
388 static void remainderTest(double x
, double y
, double exp
)
390 double result
= uprv_IEEEremainder(x
,y
);
392 if( uprv_isNaN(result
) &&
393 ! ( uprv_isNaN(x
) || uprv_isNaN(y
))) {
394 log_err("FAIL: got NaN as result without NaN as argument");
395 log_err(" IEEEremainder(%f, %f) is %f, expected %f\n", x
, y
, result
, exp
);
397 else if(!compareWithNAN(result
, exp
)) {
398 log_err("FAIL: IEEEremainder(%f, %f) is %f, expected %f\n", x
, y
, result
, exp
);
400 log_verbose("OK: IEEEremainder(%f, %f) is %f\n", x
, y
, result
);
406 static UBool
compareWithNAN(double x
, double y
)
408 if( uprv_isNaN(x
) || uprv_isNaN(y
) ) {
409 if(!uprv_isNaN(x
) || !uprv_isNaN(y
) ) {
413 else if (y
!= x
) { /* no NaN's involved */
420 static void doAssert(double got
, double expect
, const char *message
)
422 if(! compareWithNAN(expect
, got
) ) {
423 log_err("ERROR : %s. Expected : %lf, Got: %lf\n", message
, expect
, got
);
428 #define _CODE_ARR_LEN 8
429 static const UErrorCode errorCode
[_CODE_ARR_LEN
] = {
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
,
437 U_UNSUPPORTED_ATTRIBUTE
440 static const char* str
[] = {
441 "U_USING_FALLBACK_WARNING",
442 "U_STRING_NOT_TERMINATED_WARNING",
443 "U_ILLEGAL_ARGUMENT_ERROR",
444 "U_STATE_TOO_OLD_ERROR",
445 "U_BAD_VARIABLE_DEFINITION",
447 "U_UNEXPECTED_TOKEN",
448 "U_UNSUPPORTED_ATTRIBUTE"
451 static void TestErrorName(void){
453 const char* errorName
;
454 for(;code
<U_ERROR_LIMIT
;code
++){
455 errorName
= u_errorName((UErrorCode
)code
);
456 if(!errorName
|| errorName
[0] == 0) {
457 log_err("Error: u_errorName(0x%X) failed.\n",code
);
461 for(code
=0;code
<_CODE_ARR_LEN
; code
++){
462 errorName
= u_errorName(errorCode
[code
]);
463 if(uprv_strcmp(str
[code
],errorName
)!=0){
464 log_err("Error : u_errorName failed. Expected: %s Got: %s \n",str
[code
],errorName
);
469 #define AESTRNCPY_SIZE 13
471 static const char * dump_binline(uint8_t *bytes
) {
472 static char buf
[512];
475 sprintf(buf
+(i
*3), "%02x ", bytes
[i
]);
480 static void Test_aestrncpy(int32_t line
, const uint8_t *expect
, const uint8_t *src
, int32_t len
)
482 uint8_t str_buf
[AESTRNCPY_SIZE
] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
485 log_verbose("\n%s:%d: Beginning test of uprv_aestrncpy(dst, src, %d)\n", __FILE__
, line
, len
);
486 ret
= uprv_aestrncpy(str_buf
, src
, len
);
488 log_err("\n%s:%d: FAIL: uprv_aestrncpy returned %p expected %p\n", __FILE__
, line
, (void*)ret
, (void*)str_buf
);
490 if(!uprv_memcmp(str_buf
, expect
, AESTRNCPY_SIZE
)) {
491 log_verbose("\n%s:%d: OK - compared OK.", __FILE__
, line
);
492 log_verbose("\n%s:%d: expected: %s", __FILE__
, line
, dump_binline((uint8_t *)expect
));
493 log_verbose("\n%s:%d: got : %s\n", __FILE__
, line
, dump_binline(str_buf
));
495 log_err ("\n%s:%d: FAIL: uprv_aestrncpy output differs", __FILE__
, line
);
496 log_err ("\n%s:%d: expected: %s", __FILE__
, line
, dump_binline((uint8_t *)expect
));
497 log_err ("\n%s:%d: got : %s\n", __FILE__
, line
, dump_binline(str_buf
));
501 static void TestString(void)
504 uint8_t str_tst
[AESTRNCPY_SIZE
] = { 0x81, 0x4b, 0x5c, 0x82, 0x25, 0x00, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f };
506 uint8_t str_exp1
[AESTRNCPY_SIZE
] = { 0x61, 0x2e, 0x2a, 0x62, 0x0a, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
507 uint8_t str_exp2
[AESTRNCPY_SIZE
] = { 0x61, 0x2e, 0x2a, 0x62, 0x0a, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
508 uint8_t str_exp3
[AESTRNCPY_SIZE
] = { 0x61, 0x2e, 0x2a, 0x62, 0x0a, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff };
512 /* test #1- copy with -1 length */
513 Test_aestrncpy(__LINE__
, str_exp1
, str_tst
, -1);
514 Test_aestrncpy(__LINE__
, str_exp1
, str_tst
, 6);
515 Test_aestrncpy(__LINE__
, str_exp2
, str_tst
, 5);
516 Test_aestrncpy(__LINE__
, str_exp3
, str_tst
, 8);
519 void addPUtilTest(TestNode
** root
);
521 static void addToolUtilTests(TestNode
** root
);
524 addPUtilTest(TestNode
** root
)
526 addTest(root
, &TestVersion
, "putiltst/TestVersion");
527 addTest(root
, &TestCompareVersions
, "putiltst/TestCompareVersions");
528 /* addTest(root, &testIEEEremainder, "putiltst/testIEEEremainder"); */
529 addTest(root
, &TestErrorName
, "putiltst/TestErrorName");
530 addTest(root
, &TestPUtilAPI
, "putiltst/TestPUtilAPI");
531 addTest(root
, &TestString
, "putiltst/TestString");
532 addToolUtilTests(root
);
535 /* Tool Util Tests ================ */
536 #define TOOLUTIL_TESTBUF_SIZE 2048
537 static char toolutil_testBuf
[TOOLUTIL_TESTBUF_SIZE
];
538 static const char *NULLSTR
="NULL";
541 * Normalize NULL to 'NULL' for testing
543 #define STRNULL(x) ((x)?(x):NULLSTR)
545 static void toolutil_findBasename(void)
549 const char *expectResult
;
552 U_FILE_SEP_STRING
"usr" U_FILE_SEP_STRING
"bin" U_FILE_SEP_STRING
"pkgdata",
556 U_FILE_SEP_STRING
"usr" U_FILE_SEP_STRING
"bin" U_FILE_SEP_STRING
,
560 U_FILE_ALT_SEP_STRING
"usr" U_FILE_ALT_SEP_STRING
"bin" U_FILE_ALT_SEP_STRING
"pkgdata",
564 U_FILE_ALT_SEP_STRING
"usr" U_FILE_ALT_SEP_STRING
"bin" U_FILE_ALT_SEP_STRING
,
568 int32_t count
=UPRV_LENGTHOF(testCases
);
572 log_verbose("Testing findBaseName()\n");
573 for(i
=0;i
<count
;i
++) {
575 const char *input
= STRNULL(testCases
[i
].inBuf
);
576 const char *expect
= STRNULL(testCases
[i
].expectResult
);
577 log_verbose("Test case [%d/%d]: %s\n", i
, count
-1, input
);
578 result
= STRNULL(findBasename(testCases
[i
].inBuf
));
579 if(result
==expect
||!strcmp(result
,expect
)) {
580 log_verbose(" -> %s PASS\n", result
);
582 log_err("FAIL: Test case [%d/%d]: %s -> %s but expected %s\n", i
, count
-1, input
, result
, expect
);
588 static void toolutil_findDirname(void)
594 UErrorCode expectStatus
;
595 const char *expectResult
;
598 U_FILE_SEP_STRING
"usr" U_FILE_SEP_STRING
"bin" U_FILE_SEP_STRING
"pkgdata",
601 U_FILE_SEP_STRING
"usr" U_FILE_SEP_STRING
"bin",
604 U_FILE_SEP_STRING
"usr" U_FILE_SEP_STRING
"bin" U_FILE_SEP_STRING
"pkgdata",
606 U_BUFFER_OVERFLOW_ERROR
,
610 U_FILE_ALT_SEP_STRING
"usr" U_FILE_ALT_SEP_STRING
"bin" U_FILE_ALT_SEP_STRING
"pkgdata",
613 U_FILE_ALT_SEP_STRING
"usr" U_FILE_ALT_SEP_STRING
"bin"
616 U_FILE_ALT_SEP_STRING
"usr" U_FILE_ALT_SEP_STRING
"bin" U_FILE_ALT_SEP_STRING
"pkgdata",
618 U_BUFFER_OVERFLOW_ERROR
,
622 U_FILE_ALT_SEP_STRING
"usr" U_FILE_ALT_SEP_STRING
"bin" U_FILE_SEP_STRING
"pkgdata",
625 U_FILE_ALT_SEP_STRING
"usr" U_FILE_ALT_SEP_STRING
"bin"
628 U_FILE_ALT_SEP_STRING
"usr" U_FILE_SEP_STRING
"bin" U_FILE_ALT_SEP_STRING
"pkgdata",
631 U_FILE_ALT_SEP_STRING
"usr" U_FILE_SEP_STRING
"bin"
634 U_FILE_ALT_SEP_STRING
"usr" U_FILE_ALT_SEP_STRING
"bin" U_FILE_ALT_SEP_STRING
"pkgdata",
636 U_BUFFER_OVERFLOW_ERROR
,
640 U_FILE_ALT_SEP_STRING
"vmlinuz",
643 U_FILE_ALT_SEP_STRING
646 U_FILE_SEP_STRING
"vmlinux",
654 U_BUFFER_OVERFLOW_ERROR
,
664 int32_t count
=UPRV_LENGTHOF(testCases
);
666 log_verbose("Testing findDirname()\n");
667 for(i
=0;i
<count
;i
++) {
669 const char *input
= STRNULL(testCases
[i
].inBuf
);
670 const char *expect
= STRNULL(testCases
[i
].expectResult
);
671 UErrorCode status
= U_ZERO_ERROR
;
672 uprv_memset(toolutil_testBuf
, 0x55, TOOLUTIL_TESTBUF_SIZE
);
674 log_verbose("Test case [%d/%d]: %s\n", i
, count
-1, input
);
675 result
= STRNULL(findDirname(testCases
[i
].inBuf
, toolutil_testBuf
, testCases
[i
].outBufLen
, &status
));
676 log_verbose(" -> %s, \n", u_errorName(status
));
677 if(status
!= testCases
[i
].expectStatus
) {
678 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 if(result
==expect
||!strcmp(result
,expect
)) {
681 log_verbose(" = -> %s \n", result
);
683 log_err("FAIL: Test case [%d/%d]: %s -> %s but expected %s\n", i
, count
-1, input
, result
, expect
);
690 static void addToolUtilTests(TestNode
** root
) {
691 addTest(root
, &toolutil_findBasename
, "putiltst/toolutil/findBasename");
692 addTest(root
, &toolutil_findDirname
, "putiltst/toolutil/findDirname");
693 addTest(root
, &TestSignedRightShiftIsArithmetic
, "putiltst/toolutil/TestSignedRightShiftIsArithmetic");
697 addTest(root, &toolutil_getLongPathname, "putiltst/toolutil/getLongPathname");
698 addTest(root, &toolutil_getCurrentYear, "putiltst/toolutil/getCurrentYear");
699 addTest(root, &toolutil_UToolMemory, "putiltst/toolutil/UToolMemory");