1 /********************************************************************
3 * Copyright (c) 1998-2004, 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_log10()\n");
94 doAssert(uprv_log10(3456), 3, "uprv_log10(3456) failed.");
96 doAssert(uprv_log10(1.0e55
), 55, "uprv_log10(1.0e55) failed.");
98 doAssert(uprv_log10(1.0e300
), 300, "uprv_log10(1.0e300) failed.");
100 log_verbose("Testing the API uprv_isNegativeInfinity()\n");
101 isTrue
=uprv_isNegativeInfinity(uprv_getInfinity() * -1);
103 log_err("ERROR: uprv_isNegativeInfinity failed.\n");
105 log_verbose("Testing the API uprv_isPositiveInfinity()\n");
106 isTrue
=uprv_isPositiveInfinity(uprv_getInfinity());
108 log_err("ERROR: uprv_isPositiveInfinity failed.\n");
110 log_verbose("Testing the API uprv_isInfinite()\n");
111 isTrue
=uprv_isInfinite(uprv_getInfinity());
113 log_err("ERROR: uprv_isInfinite failed.\n");
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.");
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
);
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");
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");
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");
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
210 const char* dataDirectory
;
211 int32_t dataDirectoryLen
;
215 log_verbose("Testing chars to UChars\n");
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();*/
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
);
227 if(u_strcmp(temp
, udataDir
) != 0){
228 log_err("ERROR: u_charsToUChars failed. Expected %s, Got %s\n", austrdup(temp
), austrdup(udataDir
));
230 log_verbose("Testing UChars to chars\n");
231 charvalue
=(char*)malloc(sizeof(char) * (u_strlen(udataDir
) + 1));
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
);
241 log_verbose("Testing uprv_timezone()....\n");
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! *****");
248 if ((tzoffset
% 1800 != 0)) {
249 log_err("FAIL: t_timezone may be incorrect. It is not a multiple of 30min.");
251 /*tzoffset=uprv_getUTCtime();*/
257 static void testIEEEremainder()
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;
266 /* simple remainder checks*/
267 remainderTest(7.0, 2.5, -0.5);
268 remainderTest(7.0, -2.5, -0.5);
270 remainderTest(43.7, 2.5, 1.2);
273 /* infinity and real*/
274 remainderTest(1.0, pinf
, 1.0);
275 remainderTest(1.0, ninf
, 1.0);
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
);
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); */
291 static void remainderTest(double x
, double y
, double exp
)
293 double result
= uprv_IEEEremainder(x
,y
);
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
);
300 else if(!compareWithNAN(result
, exp
)) {
301 log_err("FAIL: IEEEremainder(%f, %f) is %f, expected %f\n", x
, y
, result
, exp
);
303 log_verbose("OK: IEEEremainder(%f, %f) is %f\n", x
, y
, result
);
309 static UBool
compareWithNAN(double x
, double y
)
311 if( uprv_isNaN(x
) || uprv_isNaN(y
) ) {
312 if(!uprv_isNaN(x
) || !uprv_isNaN(y
) ) {
316 else if (y
!= x
) { /* no NaN's involved */
323 static void doAssert(double got
, double expect
, const char *message
)
325 if(! compareWithNAN(expect
, got
) ) {
326 log_err("ERROR : %s. Expected : %lf, Got: %lf\n", message
, expect
, got
);
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
,
340 U_UNSUPPORTED_ATTRIBUTE
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",
350 "U_UNEXPECTED_TOKEN",
351 "U_UNSUPPORTED_ATTRIBUTE"
354 static void TestErrorName(void){
356 const char* errorName
;
357 for(;code
<U_ERROR_LIMIT
;code
++){
358 errorName
= u_errorName((UErrorCode
)code
);
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
);
369 void addPUtilTest(TestNode
** root
);
372 addPUtilTest(TestNode
** root
)
374 addTest(root
, &TestPUtilAPI
, "putiltst/TestPUtilAPI");
375 /* addTest(root, &testIEEEremainder, "putiltst/testIEEEremainder"); */
376 addTest(root
, &TestErrorName
, "putiltst/TestErrorName");