1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1998-2006, 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 "cstring.h"
22 #include "putilimp.h"
23
24 static UBool compareWithNAN(double x, double y);
25 static void doAssert(double expect, double got, const char *message);
26
TestPUtilAPI(void)27 static void TestPUtilAPI(void){
28
29 double n1=0.0, y1=0.0, expn1, expy1;
30 double value1 = 0.021;
31 UVersionInfo versionArray = {0x01, 0x00, 0x02, 0x02};
32 char versionString[17]; /* xxx.xxx.xxx.xxx\0 */
33 char *str=0;
34 UBool isTrue=FALSE;
35
36 log_verbose("Testing the API uprv_modf()\n");
37 y1 = uprv_modf(value1, &n1);
38 expn1=0;
39 expy1=0.021;
40 if(y1 != expy1 || n1 != expn1){
41 log_err("Error in uprv_modf. Expected IntegralValue=%f, Got=%f, \n Expected FractionalValue=%f, Got=%f\n",
42 expn1, n1, expy1, y1);
43 }
44 if(VERBOSITY){
45 log_verbose("[float] x = %f n = %f y = %f\n", value1, n1, y1);
46 }
47 log_verbose("Testing the API uprv_fmod()\n");
48 expn1=uprv_fmod(30.50, 15.00);
49 doAssert(expn1, 0.5, "uprv_fmod(30.50, 15.00) failed.");
50
51 log_verbose("Testing the API uprv_ceil()\n");
52 expn1=uprv_ceil(value1);
53 doAssert(expn1, 1, "uprv_ceil(0.021) failed.");
54
55 log_verbose("Testing the API uprv_floor()\n");
56 expn1=uprv_floor(value1);
57 doAssert(expn1, 0, "uprv_floor(0.021) failed.");
58
59 log_verbose("Testing the API uprv_fabs()\n");
60 expn1=uprv_fabs((2.02-1.345));
61 doAssert(expn1, 0.675, "uprv_fabs(2.02-1.345) failed.");
62
63 log_verbose("Testing the API uprv_fmax()\n");
64 doAssert(uprv_fmax(2.4, 1.2), 2.4, "uprv_fmax(2.4, 1.2) failed.");
65
66 log_verbose("Testing the API uprv_fmax() with x value= NaN\n");
67 expn1=uprv_fmax(uprv_getNaN(), 1.2);
68 doAssert(expn1, uprv_getNaN(), "uprv_fmax(uprv_getNaN(), 1.2) failed. when one parameter is NaN");
69
70 log_verbose("Testing the API uprv_fmin()\n");
71 doAssert(uprv_fmin(2.4, 1.2), 1.2, "uprv_fmin(2.4, 1.2) failed.");
72
73 log_verbose("Testing the API uprv_fmin() with x value= NaN\n");
74 expn1=uprv_fmin(uprv_getNaN(), 1.2);
75 doAssert(expn1, uprv_getNaN(), "uprv_fmin(uprv_getNaN(), 1.2) failed. when one parameter is NaN");
76
77 log_verbose("Testing the API uprv_max()\n");
78 doAssert(uprv_max(4, 2), 4, "uprv_max(4, 2) failed.");
79
80 log_verbose("Testing the API uprv_min()\n");
81 doAssert(uprv_min(-4, 2), -4, "uprv_min(-4, 2) failed.");
82
83 log_verbose("Testing the API uprv_trunc()\n");
84 doAssert(uprv_trunc(12.3456), 12, "uprv_trunc(12.3456) failed.");
85 doAssert(uprv_trunc(12.234E2), 1223, "uprv_trunc(12.234E2) failed.");
86 doAssert(uprv_trunc(uprv_getNaN()), uprv_getNaN(), "uprv_trunc(uprv_getNaN()) failed. with parameter=NaN");
87 doAssert(uprv_trunc(uprv_getInfinity()), uprv_getInfinity(), "uprv_trunc(uprv_getInfinity()) failed. with parameter=Infinity");
88
89
90 log_verbose("Testing the API uprv_pow10()\n");
91 doAssert(uprv_pow10(4), 10000, "uprv_pow10(4) failed.");
92
93 log_verbose("Testing the API uprv_isNegativeInfinity()\n");
94 isTrue=uprv_isNegativeInfinity(uprv_getInfinity() * -1);
95 if(isTrue != TRUE){
96 log_err("ERROR: uprv_isNegativeInfinity failed.\n");
97 }
98 log_verbose("Testing the API uprv_isPositiveInfinity()\n");
99 isTrue=uprv_isPositiveInfinity(uprv_getInfinity());
100 if(isTrue != TRUE){
101 log_err("ERROR: uprv_isPositiveInfinity failed.\n");
102 }
103 log_verbose("Testing the API uprv_isInfinite()\n");
104 isTrue=uprv_isInfinite(uprv_getInfinity());
105 if(isTrue != TRUE){
106 log_err("ERROR: uprv_isInfinite failed.\n");
107 }
108
109 #if 0
110 log_verbose("Testing the API uprv_digitsAfterDecimal()....\n");
111 doAssert(uprv_digitsAfterDecimal(value1), 3, "uprv_digitsAfterDecimal() failed.");
112 doAssert(uprv_digitsAfterDecimal(1.2345E2), 2, "uprv_digitsAfterDecimal(1.2345E2) failed.");
113 doAssert(uprv_digitsAfterDecimal(1.2345E-2), 6, "uprv_digitsAfterDecimal(1.2345E-2) failed.");
114 doAssert(uprv_digitsAfterDecimal(1.2345E2), 2, "uprv_digitsAfterDecimal(1.2345E2) failed.");
115 doAssert(uprv_digitsAfterDecimal(-1.2345E-20), 24, "uprv_digitsAfterDecimal(1.2345E-20) failed.");
116 doAssert(uprv_digitsAfterDecimal(1.2345E20), 0, "uprv_digitsAfterDecimal(1.2345E20) failed.");
117 doAssert(uprv_digitsAfterDecimal(-0.021), 3, "uprv_digitsAfterDecimal(-0.021) failed.");
118 doAssert(uprv_digitsAfterDecimal(23.0), 0, "uprv_digitsAfterDecimal(23.0) failed.");
119 doAssert(uprv_digitsAfterDecimal(0.022223333321), 9, "uprv_digitsAfterDecimal(0.022223333321) failed.");
120 #endif
121
122
123 log_verbose("Testing the API u_versionToString().....\n");
124 u_versionToString(versionArray, versionString);
125 if(strcmp(versionString, "1.0.2.2") != 0){
126 log_err("ERROR: u_versionToString() failed. Expected: 1.0.2.2, Got=%s\n", versionString);
127 }
128 log_verbose("Testing the API u_versionToString().....with versionArray=NULL\n");
129 u_versionToString(NULL, versionString);
130 if(strcmp(versionString, "") != 0){
131 log_err("ERROR: u_versionToString() failed. with versionArray=NULL. It should just return\n");
132 }
133 log_verbose("Testing the API u_versionToString().....with versionArray=NULL\n");
134 u_versionToString(NULL, versionString);
135 if(strcmp(versionString, "") != 0){
136 log_err("ERROR: u_versionToString() failed . It should just return\n");
137 }
138 log_verbose("Testing the API u_versionToString().....with versionString=NULL\n");
139 u_versionToString(versionArray, NULL);
140 if(strcmp(versionString, "") != 0){
141 log_err("ERROR: u_versionToString() failed. with versionArray=NULL It should just return\n");
142 }
143 versionArray[0] = 0x0a;
144 log_verbose("Testing the API u_versionToString().....\n");
145 u_versionToString(versionArray, versionString);
146 if(strcmp(versionString, "10.0.2.2") != 0){
147 log_err("ERROR: u_versionToString() failed. Expected: 10.0.2.2, Got=%s\n", versionString);
148 }
149 versionArray[0] = 0xa0;
150 u_versionToString(versionArray, versionString);
151 if(strcmp(versionString, "160.0.2.2") != 0){
152 log_err("ERROR: u_versionToString() failed. Expected: 160.0.2.2, Got=%s\n", versionString);
153 }
154 versionArray[0] = 0xa0;
155 versionArray[1] = 0xa0;
156 u_versionToString(versionArray, versionString);
157 if(strcmp(versionString, "160.160.2.2") != 0){
158 log_err("ERROR: u_versionToString() failed. Expected: 160.160.2.2, Got=%s\n", versionString);
159 }
160 versionArray[0] = 0x01;
161 versionArray[1] = 0x0a;
162 u_versionToString(versionArray, versionString);
163 if(strcmp(versionString, "1.10.2.2") != 0){
164 log_err("ERROR: u_versionToString() failed. Expected: 160.160.2.2, Got=%s\n", versionString);
165 }
166
167 log_verbose("Testing the API u_versionFromString() ....\n");
168 u_versionFromString(versionArray, "1.3.5.6");
169 u_versionToString(versionArray, versionString);
170 if(strcmp(versionString, "1.3.5.6") != 0){
171 log_err("ERROR: u_getVersion() failed. Expected: 1.3.5.6, Got=%s\n", versionString);
172 }
173 log_verbose("Testing the API u_versionFromString() where versionArray=NULL....\n");
174 u_versionFromString(NULL, "1.3.5.6");
175 u_versionToString(versionArray, versionString);
176 if(strcmp(versionString, "1.3.5.6") != 0){
177 log_err("ERROR: u_getVersion() failed. Expected: 1.3.5.6, Got=%s\n", versionString);
178 }
179
180 log_verbose("Testing the API u_getVersion().....\n");
181 u_getVersion(versionArray);
182 u_versionToString(versionArray, versionString);
183 if(strcmp(versionString, U_ICU_VERSION) != 0){
184 log_err("ERROR: u_getVersion() failed. Got=%s, expected %s\n", versionString, U_ICU_VERSION);
185 }
186 log_verbose("Testing the API u_errorName()...\n");
187 str=(char*)u_errorName((UErrorCode)0);
188 if(strcmp(str, "U_ZERO_ERROR") != 0){
189 log_err("ERROR: u_getVersion() failed. Expected: U_ZERO_ERROR Got=%s\n", str);
190 }
191 log_verbose("Testing the API u_errorName()...\n");
192 str=(char*)u_errorName((UErrorCode)-127);
193 if(strcmp(str, "U_USING_DEFAULT_WARNING") != 0){
194 log_err("ERROR: u_getVersion() failed. Expected: U_USING_DEFAULT_WARNING Got=%s\n", str);
195 }
196 log_verbose("Testing the API u_errorName().. with BOGUS ERRORCODE...\n");
197 str=(char*)u_errorName((UErrorCode)200);
198 if(strcmp(str, "[BOGUS UErrorCode]") != 0){
199 log_err("ERROR: u_getVersion() failed. Expected: [BOGUS UErrorCode] Got=%s\n", str);
200 }
201
202 {
203 const char* dataDirectory;
204 int32_t dataDirectoryLen;
205 UChar *udataDir=0;
206 UChar temp[100];
207 char *charvalue=0;
208 log_verbose("Testing chars to UChars\n");
209
210 /* This cannot really work on a japanese system. u_uastrcpy will have different results than */
211 /* u_charsToUChars when there is a backslash in the string! */
212 /*dataDirectory=u_getDataDirectory();*/
213
214 dataDirectory="directory1"; /*no backslashes*/
215 dataDirectoryLen=(int32_t)strlen(dataDirectory);
216 udataDir=(UChar*)malloc(sizeof(UChar) * (dataDirectoryLen + 1));
217 u_charsToUChars(dataDirectory, udataDir, (dataDirectoryLen + 1));
218 u_uastrcpy(temp, dataDirectory);
219
220 if(u_strcmp(temp, udataDir) != 0){
221 log_err("ERROR: u_charsToUChars failed. Expected %s, Got %s\n", austrdup(temp), austrdup(udataDir));
222 }
223 log_verbose("Testing UChars to chars\n");
224 charvalue=(char*)malloc(sizeof(char) * (u_strlen(udataDir) + 1));
225
226 u_UCharsToChars(udataDir, charvalue, (u_strlen(udataDir)+1));
227 if(strcmp(charvalue, dataDirectory) != 0){
228 log_err("ERROR: u_UCharsToChars failed. Expected %s, Got %s\n", charvalue, dataDirectory);
229 }
230 free(charvalue);
231 free(udataDir);
232 }
233
234 log_verbose("Testing uprv_timezone()....\n");
235 {
236 int32_t tzoffset = uprv_timezone();
237 log_verbose("Value returned from uprv_timezone = %d\n", tzoffset);
238 if (tzoffset != 28800) {
239 log_verbose("***** WARNING: If testing in the PST timezone, t_timezone should return 28800! *****");
240 }
241 if ((tzoffset % 1800 != 0)) {
242 log_err("FAIL: t_timezone may be incorrect. It is not a multiple of 30min.");
243 }
244 /*tzoffset=uprv_getUTCtime();*/
245
246 }
247 }
248
249 #if 0
250 static void testIEEEremainder()
251 {
252 double pinf = uprv_getInfinity();
253 double ninf = -uprv_getInfinity();
254 double nan = uprv_getNaN();
255 /* double pzero = 0.0;*/
256 /* double nzero = 0.0;
257 nzero *= -1;*/
258
259 /* simple remainder checks*/
260 remainderTest(7.0, 2.5, -0.5);
261 remainderTest(7.0, -2.5, -0.5);
262 /* this should work
263 remainderTest(43.7, 2.5, 1.2);
264 */
265
266 /* infinity and real*/
267 remainderTest(1.0, pinf, 1.0);
268 remainderTest(1.0, ninf, 1.0);
269
270 /*test infinity and real*/
271 remainderTest(nan, 1.0, nan);
272 remainderTest(1.0, nan, nan);
273 /*test infinity and nan*/
274 remainderTest(ninf, nan, nan);
275 remainderTest(pinf, nan, nan);
276
277 /* test infinity and zero */
278 /* remainderTest(pinf, pzero, 1.25);
279 remainderTest(pinf, nzero, 1.25);
280 remainderTest(ninf, pzero, 1.25);
281 remainderTest(ninf, nzero, 1.25); */
282 }
283
284 static void remainderTest(double x, double y, double exp)
285 {
286 double result = uprv_IEEEremainder(x,y);
287
288 if( uprv_isNaN(result) &&
289 ! ( uprv_isNaN(x) || uprv_isNaN(y))) {
290 log_err("FAIL: got NaN as result without NaN as argument");
291 log_err(" IEEEremainder(%f, %f) is %f, expected %f\n", x, y, result, exp);
292 }
293 else if(!compareWithNAN(result, exp)) {
294 log_err("FAIL: IEEEremainder(%f, %f) is %f, expected %f\n", x, y, result, exp);
295 } else{
296 log_verbose("OK: IEEEremainder(%f, %f) is %f\n", x, y, result);
297 }
298
299 }
300 #endif
301
compareWithNAN(double x,double y)302 static UBool compareWithNAN(double x, double y)
303 {
304 if( uprv_isNaN(x) || uprv_isNaN(y) ) {
305 if(!uprv_isNaN(x) || !uprv_isNaN(y) ) {
306 return FALSE;
307 }
308 }
309 else if (y != x) { /* no NaN's involved */
310 return FALSE;
311 }
312
313 return TRUE;
314 }
315
doAssert(double got,double expect,const char * message)316 static void doAssert(double got, double expect, const char *message)
317 {
318 if(! compareWithNAN(expect, got) ) {
319 log_err("ERROR : %s. Expected : %lf, Got: %lf\n", message, expect, got);
320 }
321 }
322
323
324 #define _CODE_ARR_LEN 8
325 static const UErrorCode errorCode[_CODE_ARR_LEN] = {
326 U_USING_FALLBACK_WARNING,
327 U_STRING_NOT_TERMINATED_WARNING,
328 U_ILLEGAL_ARGUMENT_ERROR,
329 U_STATE_TOO_OLD_ERROR,
330 U_BAD_VARIABLE_DEFINITION,
331 U_RULE_MASK_ERROR,
332 U_UNEXPECTED_TOKEN,
333 U_UNSUPPORTED_ATTRIBUTE
334 };
335
336 static const char* str[] = {
337 "U_USING_FALLBACK_WARNING",
338 "U_STRING_NOT_TERMINATED_WARNING",
339 "U_ILLEGAL_ARGUMENT_ERROR",
340 "U_STATE_TOO_OLD_ERROR",
341 "U_BAD_VARIABLE_DEFINITION",
342 "U_RULE_MASK_ERROR",
343 "U_UNEXPECTED_TOKEN",
344 "U_UNSUPPORTED_ATTRIBUTE"
345 };
346
TestErrorName(void)347 static void TestErrorName(void){
348 int32_t code=0;
349 const char* errorName ;
350 for(;code<U_ERROR_LIMIT;code++){
351 errorName = u_errorName((UErrorCode)code);
352 }
353
354 for(code=0;code<_CODE_ARR_LEN; code++){
355 errorName = u_errorName(errorCode[code]);
356 if(uprv_strcmp(str[code],errorName )!=0){
357 log_err("Error : u_errorName failed. Expected: %s Got: %s \n",str[code],errorName);
358 }
359 }
360 }
361
362 void addPUtilTest(TestNode** root);
363
364 void
addPUtilTest(TestNode ** root)365 addPUtilTest(TestNode** root)
366 {
367 addTest(root, &TestPUtilAPI, "putiltst/TestPUtilAPI");
368 /* addTest(root, &testIEEEremainder, "putiltst/testIEEEremainder"); */
369 addTest(root, &TestErrorName, "putiltst/TestErrorName");
370 }
371
372