]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/tsputil.cpp
1 /********************************************************************
3 * Copyright (c) 1997-2011, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
9 #include <float.h> // DBL_MAX, DBL_MIN
12 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
15 PUtilTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
17 //if (exec) logln("TestSuite PUtilTest: ");
21 CASE(2, testPositiveInfinity
)
22 CASE(3, testNegativeInfinity
)
24 // CASE(, testIEEEremainder)
26 default: name
= ""; break; //needed to end loop
32 PUtilTest::testIEEEremainder()
34 double pinf
= uprv_getInfinity();
35 double ninf
= -uprv_getInfinity();
36 double nan
= uprv_getNaN();
42 // simple remainder checks
43 remainderTest(7.0, 2.5, -0.5);
44 remainderTest(7.0, -2.5, -0.5);
45 #if U_PLATFORM != U_PF_OS390
47 // The following tests fails on S/390 with IEEE support in release builds;
49 // The functioning of ChoiceFormat is not affected by this bug.
50 remainderTest(-7.0, 2.5, 0.5);
51 remainderTest(-7.0, -2.5, 0.5);
53 remainderTest(5.0, 3.0, -1.0);
56 //remainderTest(43.7, 2.5, 1.25);
61 remainderTest(pinf, 1.0, 1.25);
62 remainderTest(1.0, pinf, 1.0);
63 remainderTest(ninf, 1.0, 1.25);
64 remainderTest(1.0, ninf, 1.0);
66 // test infinity and nan
67 remainderTest(ninf, pinf, 1.25);
68 remainderTest(ninf, nan, 1.25);
69 remainderTest(pinf, nan, 1.25);
71 // test infinity and zero
72 remainderTest(pinf, pzero, 1.25);
73 remainderTest(pinf, nzero, 1.25);
74 remainderTest(ninf, pzero, 1.25);
75 remainderTest(ninf, nzero, 1.25);
80 PUtilTest::remainderTest(double x
, double y
, double exp
)
82 double result
= uprv_IEEEremainder(x
,y
);
84 if( uprv_isNaN(result
) &&
85 ! ( uprv_isNaN(x
) || uprv_isNaN(y
))) {
86 errln(UnicodeString("FAIL: got NaN as result without NaN as argument"));
87 errln(UnicodeString(" IEEEremainder(") + x
+ ", " + y
+ ") is " + result
+ ", expected " + exp
);
89 else if(result
!= exp
)
90 errln(UnicodeString("FAIL: IEEEremainder(") + x
+ ", " + y
+ ") is " + result
+ ", expected " + exp
);
92 logln(UnicodeString("OK: IEEEremainder(") + x
+ ", " + y
+ ") is " + result
);
98 PUtilTest::testMaxMin()
100 double pinf
= uprv_getInfinity();
101 double ninf
= -uprv_getInfinity();
102 double nan
= uprv_getNaN();
109 maxMinTest(pinf
, ninf
, pinf
, TRUE
);
110 maxMinTest(pinf
, ninf
, ninf
, FALSE
);
112 // +Inf with +0 and -0
113 maxMinTest(pinf
, pzero
, pinf
, TRUE
);
114 maxMinTest(pinf
, pzero
, pzero
, FALSE
);
115 maxMinTest(pinf
, nzero
, pinf
, TRUE
);
116 maxMinTest(pinf
, nzero
, nzero
, FALSE
);
118 // -Inf with +0 and -0
119 maxMinTest(ninf
, pzero
, pzero
, TRUE
);
120 maxMinTest(ninf
, pzero
, ninf
, FALSE
);
121 maxMinTest(ninf
, nzero
, nzero
, TRUE
);
122 maxMinTest(ninf
, nzero
, ninf
, FALSE
);
124 // NaN with +Inf and -Inf
125 maxMinTest(pinf
, nan
, nan
, TRUE
);
126 maxMinTest(pinf
, nan
, nan
, FALSE
);
127 maxMinTest(ninf
, nan
, nan
, TRUE
);
128 maxMinTest(ninf
, nan
, nan
, FALSE
);
131 maxMinTest(nan
, nan
, nan
, TRUE
);
132 maxMinTest(nan
, nan
, nan
, FALSE
);
134 // NaN with +0 and -0
135 maxMinTest(nan
, pzero
, nan
, TRUE
);
136 maxMinTest(nan
, pzero
, nan
, FALSE
);
137 maxMinTest(nan
, nzero
, nan
, TRUE
);
138 maxMinTest(nan
, nzero
, nan
, FALSE
);
140 // +Inf with DBL_MAX and DBL_MIN
141 maxMinTest(pinf
, DBL_MAX
, pinf
, TRUE
);
142 maxMinTest(pinf
, -DBL_MAX
, pinf
, TRUE
);
143 maxMinTest(pinf
, DBL_MIN
, pinf
, TRUE
);
144 maxMinTest(pinf
, -DBL_MIN
, pinf
, TRUE
);
145 maxMinTest(pinf
, DBL_MIN
, DBL_MIN
, FALSE
);
146 maxMinTest(pinf
, -DBL_MIN
, -DBL_MIN
, FALSE
);
147 maxMinTest(pinf
, DBL_MAX
, DBL_MAX
, FALSE
);
148 maxMinTest(pinf
, -DBL_MAX
, -DBL_MAX
, FALSE
);
150 // -Inf with DBL_MAX and DBL_MIN
151 maxMinTest(ninf
, DBL_MAX
, DBL_MAX
, TRUE
);
152 maxMinTest(ninf
, -DBL_MAX
, -DBL_MAX
, TRUE
);
153 maxMinTest(ninf
, DBL_MIN
, DBL_MIN
, TRUE
);
154 maxMinTest(ninf
, -DBL_MIN
, -DBL_MIN
, TRUE
);
155 maxMinTest(ninf
, DBL_MIN
, ninf
, FALSE
);
156 maxMinTest(ninf
, -DBL_MIN
, ninf
, FALSE
);
157 maxMinTest(ninf
, DBL_MAX
, ninf
, FALSE
);
158 maxMinTest(ninf
, -DBL_MAX
, ninf
, FALSE
);
160 // +0 with DBL_MAX and DBL_MIN
161 maxMinTest(pzero
, DBL_MAX
, DBL_MAX
, TRUE
);
162 maxMinTest(pzero
, -DBL_MAX
, pzero
, TRUE
);
163 maxMinTest(pzero
, DBL_MIN
, DBL_MIN
, TRUE
);
164 maxMinTest(pzero
, -DBL_MIN
, pzero
, TRUE
);
165 maxMinTest(pzero
, DBL_MIN
, pzero
, FALSE
);
166 maxMinTest(pzero
, -DBL_MIN
, -DBL_MIN
, FALSE
);
167 maxMinTest(pzero
, DBL_MAX
, pzero
, FALSE
);
168 maxMinTest(pzero
, -DBL_MAX
, -DBL_MAX
, FALSE
);
170 // -0 with DBL_MAX and DBL_MIN
171 maxMinTest(nzero
, DBL_MAX
, DBL_MAX
, TRUE
);
172 maxMinTest(nzero
, -DBL_MAX
, nzero
, TRUE
);
173 maxMinTest(nzero
, DBL_MIN
, DBL_MIN
, TRUE
);
174 maxMinTest(nzero
, -DBL_MIN
, nzero
, TRUE
);
175 maxMinTest(nzero
, DBL_MIN
, nzero
, FALSE
);
176 maxMinTest(nzero
, -DBL_MIN
, -DBL_MIN
, FALSE
);
177 maxMinTest(nzero
, DBL_MAX
, nzero
, FALSE
);
178 maxMinTest(nzero
, -DBL_MAX
, -DBL_MAX
, FALSE
);
182 PUtilTest::maxMinTest(double a
, double b
, double exp
, UBool max
)
187 result
= uprv_fmax(a
, b
);
189 result
= uprv_fmin(a
, b
);
191 UBool nanResultOK
= (uprv_isNaN(a
) || uprv_isNaN(b
));
193 if(uprv_isNaN(result
) && ! nanResultOK
) {
194 errln(UnicodeString("FAIL: got NaN as result without NaN as argument"));
196 errln(UnicodeString(" max(") + a
+ ", " + b
+ ") is " + result
+ ", expected " + exp
);
198 errln(UnicodeString(" min(") + a
+ ", " + b
+ ") is " + result
+ ", expected " + exp
);
200 else if(result
!= exp
&& ! (uprv_isNaN(result
) || uprv_isNaN(exp
)))
202 errln(UnicodeString("FAIL: max(") + a
+ ", " + b
+ ") is " + result
+ ", expected " + exp
);
204 errln(UnicodeString("FAIL: min(") + a
+ ", " + b
+ ") is " + result
+ ", expected " + exp
);
208 logln(UnicodeString("OK: max(") + a
+ ", " + b
+ ") is " + result
);
210 logln(UnicodeString("OK: min(") + a
+ ", " + b
+ ") is " + result
);
214 //==============================
216 // NaN is weird- comparisons with NaN _always_ return false, with the
217 // exception of !=, which _always_ returns true
219 PUtilTest::testNaN(void)
221 logln("NaN tests may show that the expected NaN!=NaN etc. is not true on some");
222 logln("platforms; however, ICU does not rely on them because it defines");
223 logln("and uses uprv_isNaN(). Therefore, most failing NaN tests only report warnings.");
225 PUtilTest::testIsNaN();
233 logln("End of NaN tests.");
236 //==============================
239 PUtilTest::testPositiveInfinity(void)
241 double pinf
= uprv_getInfinity();
242 double ninf
= -uprv_getInfinity();
245 if(uprv_isInfinite(pinf
) != TRUE
) {
246 errln("FAIL: isInfinite(+Infinity) returned FALSE, should be TRUE.");
249 if(uprv_isPositiveInfinity(pinf
) != TRUE
) {
250 errln("FAIL: isPositiveInfinity(+Infinity) returned FALSE, should be TRUE.");
253 if(uprv_isNegativeInfinity(pinf
) != FALSE
) {
254 errln("FAIL: isNegativeInfinity(+Infinity) returned TRUE, should be FALSE.");
257 if((pinf
> DBL_MAX
) != TRUE
) {
258 errln("FAIL: +Infinity > DBL_MAX returned FALSE, should be TRUE.");
261 if((pinf
> DBL_MIN
) != TRUE
) {
262 errln("FAIL: +Infinity > DBL_MIN returned FALSE, should be TRUE.");
265 if((pinf
> ninf
) != TRUE
) {
266 errln("FAIL: +Infinity > -Infinity returned FALSE, should be TRUE.");
269 if((pinf
> ten
) != TRUE
) {
270 errln("FAIL: +Infinity > 10.0 returned FALSE, should be TRUE.");
274 //==============================
277 PUtilTest::testNegativeInfinity(void)
279 double pinf
= uprv_getInfinity();
280 double ninf
= -uprv_getInfinity();
283 if(uprv_isInfinite(ninf
) != TRUE
) {
284 errln("FAIL: isInfinite(-Infinity) returned FALSE, should be TRUE.");
287 if(uprv_isNegativeInfinity(ninf
) != TRUE
) {
288 errln("FAIL: isNegativeInfinity(-Infinity) returned FALSE, should be TRUE.");
291 if(uprv_isPositiveInfinity(ninf
) != FALSE
) {
292 errln("FAIL: isPositiveInfinity(-Infinity) returned TRUE, should be FALSE.");
295 if((ninf
< DBL_MAX
) != TRUE
) {
296 errln("FAIL: -Infinity < DBL_MAX returned FALSE, should be TRUE.");
299 if((ninf
< DBL_MIN
) != TRUE
) {
300 errln("FAIL: -Infinity < DBL_MIN returned FALSE, should be TRUE.");
303 if((ninf
< pinf
) != TRUE
) {
304 errln("FAIL: -Infinity < +Infinity returned FALSE, should be TRUE.");
307 if((ninf
< ten
) != TRUE
) {
308 errln("FAIL: -Infinity < 10.0 returned FALSE, should be TRUE.");
312 //==============================
315 // -0.0 == 0.0 == TRUE
316 // -0.0 < 0.0 == FALSE
317 // generating -0.0 must be done at runtime. compiler apparently ignores sign?
319 PUtilTest::testZero(void)
321 // volatile is used to fake out the compiler optimizer. We really want to divide by 0.
322 volatile double pzero
= 0.0;
323 volatile double nzero
= 0.0;
327 if((pzero
== nzero
) != TRUE
) {
328 errln("FAIL: 0.0 == -0.0 returned FALSE, should be TRUE.");
331 if((pzero
> nzero
) != FALSE
) {
332 errln("FAIL: 0.0 > -0.0 returned TRUE, should be FALSE.");
335 if((pzero
>= nzero
) != TRUE
) {
336 errln("FAIL: 0.0 >= -0.0 returned FALSE, should be TRUE.");
339 if((pzero
< nzero
) != FALSE
) {
340 errln("FAIL: 0.0 < -0.0 returned TRUE, should be FALSE.");
343 if((pzero
<= nzero
) != TRUE
) {
344 errln("FAIL: 0.0 <= -0.0 returned FALSE, should be TRUE.");
346 #if U_PLATFORM != U_PF_OS400 /* OS/400 will generate divide by zero exception MCH1214 */
347 if(uprv_isInfinite(1/pzero
) != TRUE
) {
348 errln("FAIL: isInfinite(1/0.0) returned FALSE, should be TRUE.");
351 if(uprv_isInfinite(1/nzero
) != TRUE
) {
352 errln("FAIL: isInfinite(1/-0.0) returned FALSE, should be TRUE.");
355 if(uprv_isPositiveInfinity(1/pzero
) != TRUE
) {
356 errln("FAIL: isPositiveInfinity(1/0.0) returned FALSE, should be TRUE.");
359 if(uprv_isNegativeInfinity(1/nzero
) != TRUE
) {
360 errln("FAIL: isNegativeInfinity(1/-0.0) returned FALSE, should be TRUE.");
365 //==============================
368 PUtilTest::testIsNaN(void)
370 double pinf
= uprv_getInfinity();
371 double ninf
= -uprv_getInfinity();
372 double nan
= uprv_getNaN();
375 if(uprv_isNaN(nan
) == FALSE
) {
376 errln("FAIL: isNaN() returned FALSE for NaN.");
379 if(uprv_isNaN(pinf
) == TRUE
) {
380 errln("FAIL: isNaN() returned TRUE for +Infinity.");
383 if(uprv_isNaN(ninf
) == TRUE
) {
384 errln("FAIL: isNaN() returned TRUE for -Infinity.");
387 if(uprv_isNaN(ten
) == TRUE
) {
388 errln("FAIL: isNaN() returned TRUE for 10.0.");
392 //==============================
395 PUtilTest::NaNGT(void)
397 double pinf
= uprv_getInfinity();
398 double ninf
= -uprv_getInfinity();
399 double nan
= uprv_getNaN();
402 if((nan
> nan
) != FALSE
) {
403 logln("WARNING: NaN > NaN returned TRUE, should be FALSE");
406 if((nan
> pinf
) != FALSE
) {
407 logln("WARNING: NaN > +Infinity returned TRUE, should be FALSE");
410 if((nan
> ninf
) != FALSE
) {
411 logln("WARNING: NaN > -Infinity returned TRUE, should be FALSE");
414 if((nan
> ten
) != FALSE
) {
415 logln("WARNING: NaN > 10.0 returned TRUE, should be FALSE");
419 //==============================
422 PUtilTest::NaNLT(void)
424 double pinf
= uprv_getInfinity();
425 double ninf
= -uprv_getInfinity();
426 double nan
= uprv_getNaN();
429 if((nan
< nan
) != FALSE
) {
430 logln("WARNING: NaN < NaN returned TRUE, should be FALSE");
433 if((nan
< pinf
) != FALSE
) {
434 logln("WARNING: NaN < +Infinity returned TRUE, should be FALSE");
437 if((nan
< ninf
) != FALSE
) {
438 logln("WARNING: NaN < -Infinity returned TRUE, should be FALSE");
441 if((nan
< ten
) != FALSE
) {
442 logln("WARNING: NaN < 10.0 returned TRUE, should be FALSE");
446 //==============================
449 PUtilTest::NaNGTE(void)
451 double pinf
= uprv_getInfinity();
452 double ninf
= -uprv_getInfinity();
453 double nan
= uprv_getNaN();
456 if((nan
>= nan
) != FALSE
) {
457 logln("WARNING: NaN >= NaN returned TRUE, should be FALSE");
460 if((nan
>= pinf
) != FALSE
) {
461 logln("WARNING: NaN >= +Infinity returned TRUE, should be FALSE");
464 if((nan
>= ninf
) != FALSE
) {
465 logln("WARNING: NaN >= -Infinity returned TRUE, should be FALSE");
468 if((nan
>= ten
) != FALSE
) {
469 logln("WARNING: NaN >= 10.0 returned TRUE, should be FALSE");
473 //==============================
476 PUtilTest::NaNLTE(void)
478 double pinf
= uprv_getInfinity();
479 double ninf
= -uprv_getInfinity();
480 double nan
= uprv_getNaN();
483 if((nan
<= nan
) != FALSE
) {
484 logln("WARNING: NaN <= NaN returned TRUE, should be FALSE");
487 if((nan
<= pinf
) != FALSE
) {
488 logln("WARNING: NaN <= +Infinity returned TRUE, should be FALSE");
491 if((nan
<= ninf
) != FALSE
) {
492 logln("WARNING: NaN <= -Infinity returned TRUE, should be FALSE");
495 if((nan
<= ten
) != FALSE
) {
496 logln("WARNING: NaN <= 10.0 returned TRUE, should be FALSE");
500 //==============================
503 PUtilTest::NaNE(void)
505 double pinf
= uprv_getInfinity();
506 double ninf
= -uprv_getInfinity();
507 double nan
= uprv_getNaN();
510 if((nan
== nan
) != FALSE
) {
511 logln("WARNING: NaN == NaN returned TRUE, should be FALSE");
514 if((nan
== pinf
) != FALSE
) {
515 logln("WARNING: NaN == +Infinity returned TRUE, should be FALSE");
518 if((nan
== ninf
) != FALSE
) {
519 logln("WARNING: NaN == -Infinity returned TRUE, should be FALSE");
522 if((nan
== ten
) != FALSE
) {
523 logln("WARNING: NaN == 10.0 returned TRUE, should be FALSE");
527 //==============================
530 PUtilTest::NaNNE(void)
532 double pinf
= uprv_getInfinity();
533 double ninf
= -uprv_getInfinity();
534 double nan
= uprv_getNaN();
537 if((nan
!= nan
) != TRUE
) {
538 logln("WARNING: NaN != NaN returned FALSE, should be TRUE");
541 if((nan
!= pinf
) != TRUE
) {
542 logln("WARNING: NaN != +Infinity returned FALSE, should be TRUE");
545 if((nan
!= ninf
) != TRUE
) {
546 logln("WARNING: NaN != -Infinity returned FALSE, should be TRUE");
549 if((nan
!= ten
) != TRUE
) {
550 logln("WARNING: NaN != 10.0 returned FALSE, should be TRUE");