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