]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/tsputil.cpp
1 /********************************************************************
3 * Copyright (c) 1997-2010, 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
)
25 // CASE(, testIEEEremainder)
27 default: name
= ""; break; //needed to end loop
33 PUtilTest::testIEEEremainder()
35 double pinf
= uprv_getInfinity();
36 double ninf
= -uprv_getInfinity();
37 double nan
= uprv_getNaN();
43 // simple remainder checks
44 remainderTest(7.0, 2.5, -0.5);
45 remainderTest(7.0, -2.5, -0.5);
48 // The following tests fails on S/390 with IEEE support in release builds;
50 // The functioning of ChoiceFormat is not affected by this bug.
51 remainderTest(-7.0, 2.5, 0.5);
52 remainderTest(-7.0, -2.5, 0.5);
54 remainderTest(5.0, 3.0, -1.0);
57 //remainderTest(43.7, 2.5, 1.25);
62 remainderTest(pinf, 1.0, 1.25);
63 remainderTest(1.0, pinf, 1.0);
64 remainderTest(ninf, 1.0, 1.25);
65 remainderTest(1.0, ninf, 1.0);
67 // test infinity and nan
68 remainderTest(ninf, pinf, 1.25);
69 remainderTest(ninf, nan, 1.25);
70 remainderTest(pinf, nan, 1.25);
72 // test infinity and zero
73 remainderTest(pinf, pzero, 1.25);
74 remainderTest(pinf, nzero, 1.25);
75 remainderTest(ninf, pzero, 1.25);
76 remainderTest(ninf, nzero, 1.25);
81 PUtilTest::remainderTest(double x
, double y
, double exp
)
83 double result
= uprv_IEEEremainder(x
,y
);
85 if( uprv_isNaN(result
) &&
86 ! ( uprv_isNaN(x
) || uprv_isNaN(y
))) {
87 errln(UnicodeString("FAIL: got NaN as result without NaN as argument"));
88 errln(UnicodeString(" IEEEremainder(") + x
+ ", " + y
+ ") is " + result
+ ", expected " + exp
);
90 else if(result
!= exp
)
91 errln(UnicodeString("FAIL: IEEEremainder(") + x
+ ", " + y
+ ") is " + result
+ ", expected " + exp
);
93 logln(UnicodeString("OK: IEEEremainder(") + x
+ ", " + y
+ ") is " + result
);
99 PUtilTest::testMaxMin()
101 double pinf
= uprv_getInfinity();
102 double ninf
= -uprv_getInfinity();
103 double nan
= uprv_getNaN();
110 maxMinTest(pinf
, ninf
, pinf
, TRUE
);
111 maxMinTest(pinf
, ninf
, ninf
, FALSE
);
113 // +Inf with +0 and -0
114 maxMinTest(pinf
, pzero
, pinf
, TRUE
);
115 maxMinTest(pinf
, pzero
, pzero
, FALSE
);
116 maxMinTest(pinf
, nzero
, pinf
, TRUE
);
117 maxMinTest(pinf
, nzero
, nzero
, FALSE
);
119 // -Inf with +0 and -0
120 maxMinTest(ninf
, pzero
, pzero
, TRUE
);
121 maxMinTest(ninf
, pzero
, ninf
, FALSE
);
122 maxMinTest(ninf
, nzero
, nzero
, TRUE
);
123 maxMinTest(ninf
, nzero
, ninf
, FALSE
);
125 // NaN with +Inf and -Inf
126 maxMinTest(pinf
, nan
, nan
, TRUE
);
127 maxMinTest(pinf
, nan
, nan
, FALSE
);
128 maxMinTest(ninf
, nan
, nan
, TRUE
);
129 maxMinTest(ninf
, nan
, nan
, FALSE
);
132 maxMinTest(nan
, nan
, nan
, TRUE
);
133 maxMinTest(nan
, nan
, nan
, FALSE
);
135 // NaN with +0 and -0
136 maxMinTest(nan
, pzero
, nan
, TRUE
);
137 maxMinTest(nan
, pzero
, nan
, FALSE
);
138 maxMinTest(nan
, nzero
, nan
, TRUE
);
139 maxMinTest(nan
, nzero
, nan
, FALSE
);
141 // +Inf with DBL_MAX and DBL_MIN
142 maxMinTest(pinf
, DBL_MAX
, pinf
, TRUE
);
143 maxMinTest(pinf
, -DBL_MAX
, pinf
, TRUE
);
144 maxMinTest(pinf
, DBL_MIN
, pinf
, TRUE
);
145 maxMinTest(pinf
, -DBL_MIN
, pinf
, TRUE
);
146 maxMinTest(pinf
, DBL_MIN
, DBL_MIN
, FALSE
);
147 maxMinTest(pinf
, -DBL_MIN
, -DBL_MIN
, FALSE
);
148 maxMinTest(pinf
, DBL_MAX
, DBL_MAX
, FALSE
);
149 maxMinTest(pinf
, -DBL_MAX
, -DBL_MAX
, FALSE
);
151 // -Inf with DBL_MAX and DBL_MIN
152 maxMinTest(ninf
, DBL_MAX
, DBL_MAX
, TRUE
);
153 maxMinTest(ninf
, -DBL_MAX
, -DBL_MAX
, TRUE
);
154 maxMinTest(ninf
, DBL_MIN
, DBL_MIN
, TRUE
);
155 maxMinTest(ninf
, -DBL_MIN
, -DBL_MIN
, TRUE
);
156 maxMinTest(ninf
, DBL_MIN
, ninf
, FALSE
);
157 maxMinTest(ninf
, -DBL_MIN
, ninf
, FALSE
);
158 maxMinTest(ninf
, DBL_MAX
, ninf
, FALSE
);
159 maxMinTest(ninf
, -DBL_MAX
, ninf
, FALSE
);
161 // +0 with DBL_MAX and DBL_MIN
162 maxMinTest(pzero
, DBL_MAX
, DBL_MAX
, TRUE
);
163 maxMinTest(pzero
, -DBL_MAX
, pzero
, TRUE
);
164 maxMinTest(pzero
, DBL_MIN
, DBL_MIN
, TRUE
);
165 maxMinTest(pzero
, -DBL_MIN
, pzero
, TRUE
);
166 maxMinTest(pzero
, DBL_MIN
, pzero
, FALSE
);
167 maxMinTest(pzero
, -DBL_MIN
, -DBL_MIN
, FALSE
);
168 maxMinTest(pzero
, DBL_MAX
, pzero
, FALSE
);
169 maxMinTest(pzero
, -DBL_MAX
, -DBL_MAX
, FALSE
);
171 // -0 with DBL_MAX and DBL_MIN
172 maxMinTest(nzero
, DBL_MAX
, DBL_MAX
, TRUE
);
173 maxMinTest(nzero
, -DBL_MAX
, nzero
, TRUE
);
174 maxMinTest(nzero
, DBL_MIN
, DBL_MIN
, TRUE
);
175 maxMinTest(nzero
, -DBL_MIN
, nzero
, TRUE
);
176 maxMinTest(nzero
, DBL_MIN
, nzero
, FALSE
);
177 maxMinTest(nzero
, -DBL_MIN
, -DBL_MIN
, FALSE
);
178 maxMinTest(nzero
, DBL_MAX
, nzero
, FALSE
);
179 maxMinTest(nzero
, -DBL_MAX
, -DBL_MAX
, FALSE
);
183 PUtilTest::maxMinTest(double a
, double b
, double exp
, UBool max
)
188 result
= uprv_fmax(a
, b
);
190 result
= uprv_fmin(a
, b
);
192 UBool nanResultOK
= (uprv_isNaN(a
) || uprv_isNaN(b
));
194 if(uprv_isNaN(result
) && ! nanResultOK
) {
195 errln(UnicodeString("FAIL: got NaN as result without NaN as argument"));
197 errln(UnicodeString(" max(") + a
+ ", " + b
+ ") is " + result
+ ", expected " + exp
);
199 errln(UnicodeString(" min(") + a
+ ", " + b
+ ") is " + result
+ ", expected " + exp
);
201 else if(result
!= exp
&& ! (uprv_isNaN(result
) || uprv_isNaN(exp
)))
203 errln(UnicodeString("FAIL: max(") + a
+ ", " + b
+ ") is " + result
+ ", expected " + exp
);
205 errln(UnicodeString("FAIL: min(") + a
+ ", " + b
+ ") is " + result
+ ", expected " + exp
);
209 logln(UnicodeString("OK: max(") + a
+ ", " + b
+ ") is " + result
);
211 logln(UnicodeString("OK: min(") + a
+ ", " + b
+ ") is " + result
);
215 //==============================
217 // NaN is weird- comparisons with NaN _always_ return false, with the
218 // exception of !=, which _always_ returns true
220 PUtilTest::testNaN(void)
222 logln("NaN tests may show that the expected NaN!=NaN etc. is not true on some");
223 logln("platforms; however, ICU does not rely on them because it defines");
224 logln("and uses uprv_isNaN(). Therefore, most failing NaN tests only report warnings.");
226 PUtilTest::testIsNaN();
234 logln("End of NaN tests.");
237 //==============================
240 PUtilTest::testPositiveInfinity(void)
242 double pinf
= uprv_getInfinity();
243 double ninf
= -uprv_getInfinity();
246 if(uprv_isInfinite(pinf
) != TRUE
) {
247 errln("FAIL: isInfinite(+Infinity) returned FALSE, should be TRUE.");
250 if(uprv_isPositiveInfinity(pinf
) != TRUE
) {
251 errln("FAIL: isPositiveInfinity(+Infinity) returned FALSE, should be TRUE.");
254 if(uprv_isNegativeInfinity(pinf
) != FALSE
) {
255 errln("FAIL: isNegativeInfinity(+Infinity) returned TRUE, should be FALSE.");
258 if((pinf
> DBL_MAX
) != TRUE
) {
259 errln("FAIL: +Infinity > DBL_MAX returned FALSE, should be TRUE.");
262 if((pinf
> DBL_MIN
) != TRUE
) {
263 errln("FAIL: +Infinity > DBL_MIN returned FALSE, should be TRUE.");
266 if((pinf
> ninf
) != TRUE
) {
267 errln("FAIL: +Infinity > -Infinity returned FALSE, should be TRUE.");
270 if((pinf
> ten
) != TRUE
) {
271 errln("FAIL: +Infinity > 10.0 returned FALSE, should be TRUE.");
275 //==============================
278 PUtilTest::testNegativeInfinity(void)
280 double pinf
= uprv_getInfinity();
281 double ninf
= -uprv_getInfinity();
284 if(uprv_isInfinite(ninf
) != TRUE
) {
285 errln("FAIL: isInfinite(-Infinity) returned FALSE, should be TRUE.");
288 if(uprv_isNegativeInfinity(ninf
) != TRUE
) {
289 errln("FAIL: isNegativeInfinity(-Infinity) returned FALSE, should be TRUE.");
292 if(uprv_isPositiveInfinity(ninf
) != FALSE
) {
293 errln("FAIL: isPositiveInfinity(-Infinity) returned TRUE, should be FALSE.");
296 if((ninf
< DBL_MAX
) != TRUE
) {
297 errln("FAIL: -Infinity < DBL_MAX returned FALSE, should be TRUE.");
300 if((ninf
< DBL_MIN
) != TRUE
) {
301 errln("FAIL: -Infinity < DBL_MIN returned FALSE, should be TRUE.");
304 if((ninf
< pinf
) != TRUE
) {
305 errln("FAIL: -Infinity < +Infinity returned FALSE, should be TRUE.");
308 if((ninf
< ten
) != TRUE
) {
309 errln("FAIL: -Infinity < 10.0 returned FALSE, should be TRUE.");
313 //==============================
316 // -0.0 == 0.0 == TRUE
317 // -0.0 < 0.0 == FALSE
318 // generating -0.0 must be done at runtime. compiler apparently ignores sign?
320 PUtilTest::testZero(void)
322 // volatile is used to fake out the compiler optimizer. We really want to divide by 0.
323 volatile double pzero
= 0.0;
324 volatile double nzero
= 0.0;
328 if((pzero
== nzero
) != TRUE
) {
329 errln("FAIL: 0.0 == -0.0 returned FALSE, should be TRUE.");
332 if((pzero
> nzero
) != FALSE
) {
333 errln("FAIL: 0.0 > -0.0 returned TRUE, should be FALSE.");
336 if((pzero
>= nzero
) != TRUE
) {
337 errln("FAIL: 0.0 >= -0.0 returned FALSE, should be TRUE.");
340 if((pzero
< nzero
) != FALSE
) {
341 errln("FAIL: 0.0 < -0.0 returned TRUE, should be FALSE.");
344 if((pzero
<= nzero
) != TRUE
) {
345 errln("FAIL: 0.0 <= -0.0 returned FALSE, should be TRUE.");
347 #ifndef OS400 /* OS/400 will generate divide by zero exception MCH1214 */
348 if(uprv_isInfinite(1/pzero
) != TRUE
) {
349 errln("FAIL: isInfinite(1/0.0) returned FALSE, should be TRUE.");
352 if(uprv_isInfinite(1/nzero
) != TRUE
) {
353 errln("FAIL: isInfinite(1/-0.0) returned FALSE, should be TRUE.");
356 if(uprv_isPositiveInfinity(1/pzero
) != TRUE
) {
357 errln("FAIL: isPositiveInfinity(1/0.0) returned FALSE, should be TRUE.");
360 if(uprv_isNegativeInfinity(1/nzero
) != TRUE
) {
361 errln("FAIL: isNegativeInfinity(1/-0.0) returned FALSE, should be TRUE.");
366 //==============================
369 PUtilTest::testIsNaN(void)
371 double pinf
= uprv_getInfinity();
372 double ninf
= -uprv_getInfinity();
373 double nan
= uprv_getNaN();
376 if(uprv_isNaN(nan
) == FALSE
) {
377 errln("FAIL: isNaN() returned FALSE for NaN.");
380 if(uprv_isNaN(pinf
) == TRUE
) {
381 errln("FAIL: isNaN() returned TRUE for +Infinity.");
384 if(uprv_isNaN(ninf
) == TRUE
) {
385 errln("FAIL: isNaN() returned TRUE for -Infinity.");
388 if(uprv_isNaN(ten
) == TRUE
) {
389 errln("FAIL: isNaN() returned TRUE for 10.0.");
393 //==============================
396 PUtilTest::NaNGT(void)
398 double pinf
= uprv_getInfinity();
399 double ninf
= -uprv_getInfinity();
400 double nan
= uprv_getNaN();
403 if((nan
> nan
) != FALSE
) {
404 logln("WARNING: NaN > NaN returned TRUE, should be FALSE");
407 if((nan
> pinf
) != FALSE
) {
408 logln("WARNING: NaN > +Infinity returned TRUE, should be FALSE");
411 if((nan
> ninf
) != FALSE
) {
412 logln("WARNING: NaN > -Infinity returned TRUE, should be FALSE");
415 if((nan
> ten
) != FALSE
) {
416 logln("WARNING: NaN > 10.0 returned TRUE, should be FALSE");
420 //==============================
423 PUtilTest::NaNLT(void)
425 double pinf
= uprv_getInfinity();
426 double ninf
= -uprv_getInfinity();
427 double nan
= uprv_getNaN();
430 if((nan
< nan
) != FALSE
) {
431 logln("WARNING: NaN < NaN returned TRUE, should be FALSE");
434 if((nan
< pinf
) != FALSE
) {
435 logln("WARNING: NaN < +Infinity returned TRUE, should be FALSE");
438 if((nan
< ninf
) != FALSE
) {
439 logln("WARNING: NaN < -Infinity returned TRUE, should be FALSE");
442 if((nan
< ten
) != FALSE
) {
443 logln("WARNING: NaN < 10.0 returned TRUE, should be FALSE");
447 //==============================
450 PUtilTest::NaNGTE(void)
452 double pinf
= uprv_getInfinity();
453 double ninf
= -uprv_getInfinity();
454 double nan
= uprv_getNaN();
457 if((nan
>= nan
) != FALSE
) {
458 logln("WARNING: NaN >= NaN returned TRUE, should be FALSE");
461 if((nan
>= pinf
) != FALSE
) {
462 logln("WARNING: NaN >= +Infinity returned TRUE, should be FALSE");
465 if((nan
>= ninf
) != FALSE
) {
466 logln("WARNING: NaN >= -Infinity returned TRUE, should be FALSE");
469 if((nan
>= ten
) != FALSE
) {
470 logln("WARNING: NaN >= 10.0 returned TRUE, should be FALSE");
474 //==============================
477 PUtilTest::NaNLTE(void)
479 double pinf
= uprv_getInfinity();
480 double ninf
= -uprv_getInfinity();
481 double nan
= uprv_getNaN();
484 if((nan
<= nan
) != FALSE
) {
485 logln("WARNING: NaN <= NaN returned TRUE, should be FALSE");
488 if((nan
<= pinf
) != FALSE
) {
489 logln("WARNING: NaN <= +Infinity returned TRUE, should be FALSE");
492 if((nan
<= ninf
) != FALSE
) {
493 logln("WARNING: NaN <= -Infinity returned TRUE, should be FALSE");
496 if((nan
<= ten
) != FALSE
) {
497 logln("WARNING: NaN <= 10.0 returned TRUE, should be FALSE");
501 //==============================
504 PUtilTest::NaNE(void)
506 double pinf
= uprv_getInfinity();
507 double ninf
= -uprv_getInfinity();
508 double nan
= uprv_getNaN();
511 if((nan
== nan
) != FALSE
) {
512 logln("WARNING: NaN == NaN returned TRUE, should be FALSE");
515 if((nan
== pinf
) != FALSE
) {
516 logln("WARNING: NaN == +Infinity returned TRUE, should be FALSE");
519 if((nan
== ninf
) != FALSE
) {
520 logln("WARNING: NaN == -Infinity returned TRUE, should be FALSE");
523 if((nan
== ten
) != FALSE
) {
524 logln("WARNING: NaN == 10.0 returned TRUE, should be FALSE");
528 //==============================
531 PUtilTest::NaNNE(void)
533 double pinf
= uprv_getInfinity();
534 double ninf
= -uprv_getInfinity();
535 double nan
= uprv_getNaN();
538 if((nan
!= nan
) != TRUE
) {
539 logln("WARNING: NaN != NaN returned FALSE, should be TRUE");
542 if((nan
!= pinf
) != TRUE
) {
543 logln("WARNING: NaN != +Infinity returned FALSE, should be TRUE");
546 if((nan
!= ninf
) != TRUE
) {
547 logln("WARNING: NaN != -Infinity returned FALSE, should be TRUE");
550 if((nan
!= ten
) != TRUE
) {
551 logln("WARNING: NaN != 10.0 returned FALSE, should be TRUE");
555 U_INLINE
int32_t inlineTriple(int32_t x
) {
559 // "code" coverage test for Jitterbug 4515 RFE: in C++, use U_INLINE=inline
561 PUtilTest::testU_INLINE() {
562 if(inlineTriple(2)!=6 || inlineTriple(-55)!=-165) {
563 errln("inlineTriple() failed");