1 /********************************************************************
3 * Copyright (c) 1998-2006, 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"
24 static UBool
compareWithNAN(double x
, double y
);
25 static void doAssert(double expect
, double got
, const char *message
);
27 static void TestPUtilAPI(void){
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 */
36 log_verbose("Testing the API uprv_modf()\n");
37 y1
= uprv_modf(value1
, &n1
);
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
);
45 log_verbose("[float] x = %f n = %f y = %f\n", value1
, n1
, y1
);
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.");
51 log_verbose("Testing the API uprv_ceil()\n");
52 expn1
=uprv_ceil(value1
);
53 doAssert(expn1
, 1, "uprv_ceil(0.021) failed.");
55 log_verbose("Testing the API uprv_floor()\n");
56 expn1
=uprv_floor(value1
);
57 doAssert(expn1
, 0, "uprv_floor(0.021) failed.");
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.");
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.");
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");
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.");
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");
77 log_verbose("Testing the API uprv_max()\n");
78 doAssert(uprv_max(4, 2), 4, "uprv_max(4, 2) failed.");
80 log_verbose("Testing the API uprv_min()\n");
81 doAssert(uprv_min(-4, 2), -4, "uprv_min(-4, 2) failed.");
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");
90 log_verbose("Testing the API uprv_pow10()\n");
91 doAssert(uprv_pow10(4), 10000, "uprv_pow10(4) failed.");
93 log_verbose("Testing the API uprv_isNegativeInfinity()\n");
94 isTrue
=uprv_isNegativeInfinity(uprv_getInfinity() * -1);
96 log_err("ERROR: uprv_isNegativeInfinity failed.\n");
98 log_verbose("Testing the API uprv_isPositiveInfinity()\n");
99 isTrue
=uprv_isPositiveInfinity(uprv_getInfinity());
101 log_err("ERROR: uprv_isPositiveInfinity failed.\n");
103 log_verbose("Testing the API uprv_isInfinite()\n");
104 isTrue
=uprv_isInfinite(uprv_getInfinity());
106 log_err("ERROR: uprv_isInfinite failed.\n");
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.");
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
);
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");
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");
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");
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
203 const char* dataDirectory
;
204 int32_t dataDirectoryLen
;
208 log_verbose("Testing chars to UChars\n");
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();*/
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
);
220 if(u_strcmp(temp
, udataDir
) != 0){
221 log_err("ERROR: u_charsToUChars failed. Expected %s, Got %s\n", austrdup(temp
), austrdup(udataDir
));
223 log_verbose("Testing UChars to chars\n");
224 charvalue
=(char*)malloc(sizeof(char) * (u_strlen(udataDir
) + 1));
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
);
234 log_verbose("Testing uprv_timezone()....\n");
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! *****");
241 if ((tzoffset
% 1800 != 0)) {
242 log_err("FAIL: t_timezone may be incorrect. It is not a multiple of 30min.");
244 /*tzoffset=uprv_getUTCtime();*/
250 static void testIEEEremainder()
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;
259 /* simple remainder checks*/
260 remainderTest(7.0, 2.5, -0.5);
261 remainderTest(7.0, -2.5, -0.5);
263 remainderTest(43.7, 2.5, 1.2);
266 /* infinity and real*/
267 remainderTest(1.0, pinf
, 1.0);
268 remainderTest(1.0, ninf
, 1.0);
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
);
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); */
284 static void remainderTest(double x
, double y
, double exp
)
286 double result
= uprv_IEEEremainder(x
,y
);
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
);
293 else if(!compareWithNAN(result
, exp
)) {
294 log_err("FAIL: IEEEremainder(%f, %f) is %f, expected %f\n", x
, y
, result
, exp
);
296 log_verbose("OK: IEEEremainder(%f, %f) is %f\n", x
, y
, result
);
302 static UBool
compareWithNAN(double x
, double y
)
304 if( uprv_isNaN(x
) || uprv_isNaN(y
) ) {
305 if(!uprv_isNaN(x
) || !uprv_isNaN(y
) ) {
309 else if (y
!= x
) { /* no NaN's involved */
316 static void doAssert(double got
, double expect
, const char *message
)
318 if(! compareWithNAN(expect
, got
) ) {
319 log_err("ERROR : %s. Expected : %lf, Got: %lf\n", message
, expect
, got
);
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
,
333 U_UNSUPPORTED_ATTRIBUTE
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",
343 "U_UNEXPECTED_TOKEN",
344 "U_UNSUPPORTED_ATTRIBUTE"
347 static void TestErrorName(void){
349 const char* errorName
;
350 for(;code
<U_ERROR_LIMIT
;code
++){
351 errorName
= u_errorName((UErrorCode
)code
);
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
);
362 void addPUtilTest(TestNode
** root
);
365 addPUtilTest(TestNode
** root
)
367 addTest(root
, &TestPUtilAPI
, "putiltst/TestPUtilAPI");
368 /* addTest(root, &testIEEEremainder, "putiltst/testIEEEremainder"); */
369 addTest(root
, &TestErrorName
, "putiltst/TestErrorName");