]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/putiltst.c
ICU-6.2.22.tar.gz
[apple/icu.git] / icuSources / test / cintltst / putiltst.c
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1998-2004, 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
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_log10()\n");
94 doAssert(uprv_log10(3456), 3, "uprv_log10(3456) failed.");
95 #ifdef OS390
96 doAssert(uprv_log10(1.0e55), 55, "uprv_log10(1.0e55) failed.");
97 #else
98 doAssert(uprv_log10(1.0e300), 300, "uprv_log10(1.0e300) failed.");
99 #endif
100 log_verbose("Testing the API uprv_isNegativeInfinity()\n");
101 isTrue=uprv_isNegativeInfinity(uprv_getInfinity() * -1);
102 if(isTrue != TRUE){
103 log_err("ERROR: uprv_isNegativeInfinity failed.\n");
104 }
105 log_verbose("Testing the API uprv_isPositiveInfinity()\n");
106 isTrue=uprv_isPositiveInfinity(uprv_getInfinity());
107 if(isTrue != TRUE){
108 log_err("ERROR: uprv_isPositiveInfinity failed.\n");
109 }
110 log_verbose("Testing the API uprv_isInfinite()\n");
111 isTrue=uprv_isInfinite(uprv_getInfinity());
112 if(isTrue != TRUE){
113 log_err("ERROR: uprv_isInfinite failed.\n");
114 }
115
116 #if 0
117 log_verbose("Testing the API uprv_digitsAfterDecimal()....\n");
118 doAssert(uprv_digitsAfterDecimal(value1), 3, "uprv_digitsAfterDecimal() failed.");
119 doAssert(uprv_digitsAfterDecimal(1.2345E2), 2, "uprv_digitsAfterDecimal(1.2345E2) failed.");
120 doAssert(uprv_digitsAfterDecimal(1.2345E-2), 6, "uprv_digitsAfterDecimal(1.2345E-2) failed.");
121 doAssert(uprv_digitsAfterDecimal(1.2345E2), 2, "uprv_digitsAfterDecimal(1.2345E2) failed.");
122 doAssert(uprv_digitsAfterDecimal(-1.2345E-20), 24, "uprv_digitsAfterDecimal(1.2345E-20) failed.");
123 doAssert(uprv_digitsAfterDecimal(1.2345E20), 0, "uprv_digitsAfterDecimal(1.2345E20) failed.");
124 doAssert(uprv_digitsAfterDecimal(-0.021), 3, "uprv_digitsAfterDecimal(-0.021) failed.");
125 doAssert(uprv_digitsAfterDecimal(23.0), 0, "uprv_digitsAfterDecimal(23.0) failed.");
126 doAssert(uprv_digitsAfterDecimal(0.022223333321), 9, "uprv_digitsAfterDecimal(0.022223333321) failed.");
127 #endif
128
129
130 log_verbose("Testing the API u_versionToString().....\n");
131 u_versionToString(versionArray, versionString);
132 if(strcmp(versionString, "1.0.2.2") != 0){
133 log_err("ERROR: u_versionToString() failed. Expected: 1.0.2.2, Got=%s\n", versionString);
134 }
135 log_verbose("Testing the API u_versionToString().....with versionArray=NULL\n");
136 u_versionToString(NULL, versionString);
137 if(strcmp(versionString, "") != 0){
138 log_err("ERROR: u_versionToString() failed. with versionArray=NULL. It should just return\n");
139 }
140 log_verbose("Testing the API u_versionToString().....with versionArray=NULL\n");
141 u_versionToString(NULL, versionString);
142 if(strcmp(versionString, "") != 0){
143 log_err("ERROR: u_versionToString() failed . It should just return\n");
144 }
145 log_verbose("Testing the API u_versionToString().....with versionString=NULL\n");
146 u_versionToString(versionArray, NULL);
147 if(strcmp(versionString, "") != 0){
148 log_err("ERROR: u_versionToString() failed. with versionArray=NULL It should just return\n");
149 }
150 versionArray[0] = 0x0a;
151 log_verbose("Testing the API u_versionToString().....\n");
152 u_versionToString(versionArray, versionString);
153 if(strcmp(versionString, "10.0.2.2") != 0){
154 log_err("ERROR: u_versionToString() failed. Expected: 10.0.2.2, Got=%s\n", versionString);
155 }
156 versionArray[0] = 0xa0;
157 u_versionToString(versionArray, versionString);
158 if(strcmp(versionString, "160.0.2.2") != 0){
159 log_err("ERROR: u_versionToString() failed. Expected: 160.0.2.2, Got=%s\n", versionString);
160 }
161 versionArray[0] = 0xa0;
162 versionArray[1] = 0xa0;
163 u_versionToString(versionArray, versionString);
164 if(strcmp(versionString, "160.160.2.2") != 0){
165 log_err("ERROR: u_versionToString() failed. Expected: 160.160.2.2, Got=%s\n", versionString);
166 }
167 versionArray[0] = 0x01;
168 versionArray[1] = 0x0a;
169 u_versionToString(versionArray, versionString);
170 if(strcmp(versionString, "1.10.2.2") != 0){
171 log_err("ERROR: u_versionToString() failed. Expected: 160.160.2.2, Got=%s\n", versionString);
172 }
173
174 log_verbose("Testing the API u_versionFromString() ....\n");
175 u_versionFromString(versionArray, "1.3.5.6");
176 u_versionToString(versionArray, versionString);
177 if(strcmp(versionString, "1.3.5.6") != 0){
178 log_err("ERROR: u_getVersion() failed. Expected: 1.3.5.6, Got=%s\n", versionString);
179 }
180 log_verbose("Testing the API u_versionFromString() where versionArray=NULL....\n");
181 u_versionFromString(NULL, "1.3.5.6");
182 u_versionToString(versionArray, versionString);
183 if(strcmp(versionString, "1.3.5.6") != 0){
184 log_err("ERROR: u_getVersion() failed. Expected: 1.3.5.6, Got=%s\n", versionString);
185 }
186
187 log_verbose("Testing the API u_getVersion().....\n");
188 u_getVersion(versionArray);
189 u_versionToString(versionArray, versionString);
190 if(strcmp(versionString, U_ICU_VERSION) != 0){
191 log_err("ERROR: u_getVersion() failed. Got=%s, expected %s\n", versionString, U_ICU_VERSION);
192 }
193 log_verbose("Testing the API u_errorName()...\n");
194 str=(char*)u_errorName((UErrorCode)0);
195 if(strcmp(str, "U_ZERO_ERROR") != 0){
196 log_err("ERROR: u_getVersion() failed. Expected: U_ZERO_ERROR Got=%s\n", str);
197 }
198 log_verbose("Testing the API u_errorName()...\n");
199 str=(char*)u_errorName((UErrorCode)-127);
200 if(strcmp(str, "U_USING_DEFAULT_WARNING") != 0){
201 log_err("ERROR: u_getVersion() failed. Expected: U_USING_DEFAULT_WARNING Got=%s\n", str);
202 }
203 log_verbose("Testing the API u_errorName().. with BOGUS ERRORCODE...\n");
204 str=(char*)u_errorName((UErrorCode)200);
205 if(strcmp(str, "[BOGUS UErrorCode]") != 0){
206 log_err("ERROR: u_getVersion() failed. Expected: [BOGUS UErrorCode] Got=%s\n", str);
207 }
208
209 {
210 const char* dataDirectory;
211 int32_t dataDirectoryLen;
212 UChar *udataDir=0;
213 UChar temp[100];
214 char *charvalue=0;
215 log_verbose("Testing chars to UChars\n");
216
217 /* This cannot really work on a japanese system. u_uastrcpy will have different results than */
218 /* u_charsToUChars when there is a backslash in the string! */
219 /*dataDirectory=u_getDataDirectory();*/
220
221 dataDirectory="directory1"; /*no backslashes*/
222 dataDirectoryLen=(int32_t)strlen(dataDirectory);
223 udataDir=(UChar*)malloc(sizeof(UChar) * (dataDirectoryLen + 1));
224 u_charsToUChars(dataDirectory, udataDir, (dataDirectoryLen + 1));
225 u_uastrcpy(temp, dataDirectory);
226
227 if(u_strcmp(temp, udataDir) != 0){
228 log_err("ERROR: u_charsToUChars failed. Expected %s, Got %s\n", austrdup(temp), austrdup(udataDir));
229 }
230 log_verbose("Testing UChars to chars\n");
231 charvalue=(char*)malloc(sizeof(char) * (u_strlen(udataDir) + 1));
232
233 u_UCharsToChars(udataDir, charvalue, (u_strlen(udataDir)+1));
234 if(strcmp(charvalue, dataDirectory) != 0){
235 log_err("ERROR: u_UCharsToChars failed. Expected %s, Got %s\n", charvalue, dataDirectory);
236 }
237 free(charvalue);
238 free(udataDir);
239 }
240
241 log_verbose("Testing uprv_timezone()....\n");
242 {
243 int32_t tzoffset = uprv_timezone();
244 log_verbose("Value returned from uprv_timezone = %d\n", tzoffset);
245 if (tzoffset != 28800) {
246 log_verbose("***** WARNING: If testing in the PST timezone, t_timezone should return 28800! *****");
247 }
248 if ((tzoffset % 1800 != 0)) {
249 log_err("FAIL: t_timezone may be incorrect. It is not a multiple of 30min.");
250 }
251 /*tzoffset=uprv_getUTCtime();*/
252
253 }
254 }
255
256 #if 0
257 static void testIEEEremainder()
258 {
259 double pinf = uprv_getInfinity();
260 double ninf = -uprv_getInfinity();
261 double nan = uprv_getNaN();
262 /* double pzero = 0.0;*/
263 /* double nzero = 0.0;
264 nzero *= -1;*/
265
266 /* simple remainder checks*/
267 remainderTest(7.0, 2.5, -0.5);
268 remainderTest(7.0, -2.5, -0.5);
269 /* this should work
270 remainderTest(43.7, 2.5, 1.2);
271 */
272
273 /* infinity and real*/
274 remainderTest(1.0, pinf, 1.0);
275 remainderTest(1.0, ninf, 1.0);
276
277 /*test infinity and real*/
278 remainderTest(nan, 1.0, nan);
279 remainderTest(1.0, nan, nan);
280 /*test infinity and nan*/
281 remainderTest(ninf, nan, nan);
282 remainderTest(pinf, nan, nan);
283
284 /* test infinity and zero */
285 /* remainderTest(pinf, pzero, 1.25);
286 remainderTest(pinf, nzero, 1.25);
287 remainderTest(ninf, pzero, 1.25);
288 remainderTest(ninf, nzero, 1.25); */
289 }
290
291 static void remainderTest(double x, double y, double exp)
292 {
293 double result = uprv_IEEEremainder(x,y);
294
295 if( uprv_isNaN(result) &&
296 ! ( uprv_isNaN(x) || uprv_isNaN(y))) {
297 log_err("FAIL: got NaN as result without NaN as argument");
298 log_err(" IEEEremainder(%f, %f) is %f, expected %f\n", x, y, result, exp);
299 }
300 else if(!compareWithNAN(result, exp)) {
301 log_err("FAIL: IEEEremainder(%f, %f) is %f, expected %f\n", x, y, result, exp);
302 } else{
303 log_verbose("OK: IEEEremainder(%f, %f) is %f\n", x, y, result);
304 }
305
306 }
307 #endif
308
309 static UBool compareWithNAN(double x, double y)
310 {
311 if( uprv_isNaN(x) || uprv_isNaN(y) ) {
312 if(!uprv_isNaN(x) || !uprv_isNaN(y) ) {
313 return FALSE;
314 }
315 }
316 else if (y != x) { /* no NaN's involved */
317 return FALSE;
318 }
319
320 return TRUE;
321 }
322
323 static void doAssert(double got, double expect, const char *message)
324 {
325 if(! compareWithNAN(expect, got) ) {
326 log_err("ERROR : %s. Expected : %lf, Got: %lf\n", message, expect, got);
327 }
328 }
329
330
331 #define _CODE_ARR_LEN 8
332 static const UErrorCode errorCode[_CODE_ARR_LEN] = {
333 U_USING_FALLBACK_WARNING,
334 U_STRING_NOT_TERMINATED_WARNING,
335 U_ILLEGAL_ARGUMENT_ERROR,
336 U_STATE_TOO_OLD_ERROR,
337 U_BAD_VARIABLE_DEFINITION,
338 U_RULE_MASK_ERROR,
339 U_UNEXPECTED_TOKEN,
340 U_UNSUPPORTED_ATTRIBUTE
341 };
342
343 static const char* str[] = {
344 "U_USING_FALLBACK_WARNING",
345 "U_STRING_NOT_TERMINATED_WARNING",
346 "U_ILLEGAL_ARGUMENT_ERROR",
347 "U_STATE_TOO_OLD_ERROR",
348 "U_BAD_VARIABLE_DEFINITION",
349 "U_RULE_MASK_ERROR",
350 "U_UNEXPECTED_TOKEN",
351 "U_UNSUPPORTED_ATTRIBUTE"
352 };
353
354 static void TestErrorName(void){
355 int32_t code=0;
356 const char* errorName ;
357 for(;code<U_ERROR_LIMIT;code++){
358 errorName = u_errorName((UErrorCode)code);
359 }
360
361 for(code=0;code<_CODE_ARR_LEN; code++){
362 errorName = u_errorName(errorCode[code]);
363 if(uprv_strcmp(str[code],errorName )!=0){
364 log_err("Error : u_errorName failed. Expected: %s Got: %s \n",str[code],errorName);
365 }
366 }
367 }
368
369 void addPUtilTest(TestNode** root);
370
371 void
372 addPUtilTest(TestNode** root)
373 {
374 addTest(root, &TestPUtilAPI, "putiltst/TestPUtilAPI");
375 /* addTest(root, &testIEEEremainder, "putiltst/testIEEEremainder"); */
376 addTest(root, &TestErrorName, "putiltst/TestErrorName");
377 }
378