]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/tsputil.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1997-2011, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
11 #include <float.h> // DBL_MAX, DBL_MIN
14 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
17 PUtilTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
19 //if (exec) logln("TestSuite PUtilTest: ");
23 CASE(2, testPositiveInfinity
)
24 CASE(3, testNegativeInfinity
)
26 // CASE(, testIEEEremainder)
28 default: name
= ""; break; //needed to end loop
34 PUtilTest::testIEEEremainder()
36 double pinf
= uprv_getInfinity();
37 double ninf
= -uprv_getInfinity();
38 double nan
= uprv_getNaN();
44 // simple remainder checks
45 remainderTest(7.0, 2.5, -0.5);
46 remainderTest(7.0, -2.5, -0.5);
47 #if U_PLATFORM != U_PF_OS390
49 // The following tests fails on S/390 with IEEE support in release builds;
51 // The functioning of ChoiceFormat is not affected by this bug.
52 remainderTest(-7.0, 2.5, 0.5);
53 remainderTest(-7.0, -2.5, 0.5);
55 remainderTest(5.0, 3.0, -1.0);
58 //remainderTest(43.7, 2.5, 1.25);
63 remainderTest(pinf, 1.0, 1.25);
64 remainderTest(1.0, pinf, 1.0);
65 remainderTest(ninf, 1.0, 1.25);
66 remainderTest(1.0, ninf, 1.0);
68 // test infinity and nan
69 remainderTest(ninf, pinf, 1.25);
70 remainderTest(ninf, nan, 1.25);
71 remainderTest(pinf, nan, 1.25);
73 // test infinity and zero
74 remainderTest(pinf, pzero, 1.25);
75 remainderTest(pinf, nzero, 1.25);
76 remainderTest(ninf, pzero, 1.25);
77 remainderTest(ninf, nzero, 1.25);
82 PUtilTest::remainderTest(double x
, double y
, double exp
)
84 double result
= uprv_IEEEremainder(x
,y
);
86 if( uprv_isNaN(result
) &&
87 ! ( uprv_isNaN(x
) || uprv_isNaN(y
))) {
88 errln(UnicodeString("FAIL: got NaN as result without NaN as argument"));
89 errln(UnicodeString(" IEEEremainder(") + x
+ ", " + y
+ ") is " + result
+ ", expected " + exp
);
91 else if(result
!= exp
)
92 errln(UnicodeString("FAIL: IEEEremainder(") + x
+ ", " + y
+ ") is " + result
+ ", expected " + exp
);
94 logln(UnicodeString("OK: IEEEremainder(") + x
+ ", " + y
+ ") is " + result
);
100 PUtilTest::testMaxMin()
102 double pinf
= uprv_getInfinity();
103 double ninf
= -uprv_getInfinity();
104 double nan
= uprv_getNaN();
111 maxMinTest(pinf
, ninf
, pinf
, TRUE
);
112 maxMinTest(pinf
, ninf
, ninf
, FALSE
);
114 // +Inf with +0 and -0
115 maxMinTest(pinf
, pzero
, pinf
, TRUE
);
116 maxMinTest(pinf
, pzero
, pzero
, FALSE
);
117 maxMinTest(pinf
, nzero
, pinf
, TRUE
);
118 maxMinTest(pinf
, nzero
, nzero
, FALSE
);
120 // -Inf with +0 and -0
121 maxMinTest(ninf
, pzero
, pzero
, TRUE
);
122 maxMinTest(ninf
, pzero
, ninf
, FALSE
);
123 maxMinTest(ninf
, nzero
, nzero
, TRUE
);
124 maxMinTest(ninf
, nzero
, ninf
, FALSE
);
126 // NaN with +Inf and -Inf
127 maxMinTest(pinf
, nan
, nan
, TRUE
);
128 maxMinTest(pinf
, nan
, nan
, FALSE
);
129 maxMinTest(ninf
, nan
, nan
, TRUE
);
130 maxMinTest(ninf
, nan
, nan
, FALSE
);
133 maxMinTest(nan
, nan
, nan
, TRUE
);
134 maxMinTest(nan
, nan
, nan
, FALSE
);
136 // NaN with +0 and -0
137 maxMinTest(nan
, pzero
, nan
, TRUE
);
138 maxMinTest(nan
, pzero
, nan
, FALSE
);
139 maxMinTest(nan
, nzero
, nan
, TRUE
);
140 maxMinTest(nan
, nzero
, nan
, FALSE
);
142 // +Inf with DBL_MAX and DBL_MIN
143 maxMinTest(pinf
, DBL_MAX
, pinf
, TRUE
);
144 maxMinTest(pinf
, -DBL_MAX
, pinf
, TRUE
);
145 maxMinTest(pinf
, DBL_MIN
, pinf
, TRUE
);
146 maxMinTest(pinf
, -DBL_MIN
, pinf
, TRUE
);
147 maxMinTest(pinf
, DBL_MIN
, DBL_MIN
, FALSE
);
148 maxMinTest(pinf
, -DBL_MIN
, -DBL_MIN
, FALSE
);
149 maxMinTest(pinf
, DBL_MAX
, DBL_MAX
, FALSE
);
150 maxMinTest(pinf
, -DBL_MAX
, -DBL_MAX
, FALSE
);
152 // -Inf with DBL_MAX and DBL_MIN
153 maxMinTest(ninf
, DBL_MAX
, DBL_MAX
, TRUE
);
154 maxMinTest(ninf
, -DBL_MAX
, -DBL_MAX
, TRUE
);
155 maxMinTest(ninf
, DBL_MIN
, DBL_MIN
, TRUE
);
156 maxMinTest(ninf
, -DBL_MIN
, -DBL_MIN
, TRUE
);
157 maxMinTest(ninf
, DBL_MIN
, ninf
, FALSE
);
158 maxMinTest(ninf
, -DBL_MIN
, ninf
, FALSE
);
159 maxMinTest(ninf
, DBL_MAX
, ninf
, FALSE
);
160 maxMinTest(ninf
, -DBL_MAX
, ninf
, FALSE
);
162 // +0 with DBL_MAX and DBL_MIN
163 maxMinTest(pzero
, DBL_MAX
, DBL_MAX
, TRUE
);
164 maxMinTest(pzero
, -DBL_MAX
, pzero
, TRUE
);
165 maxMinTest(pzero
, DBL_MIN
, DBL_MIN
, TRUE
);
166 maxMinTest(pzero
, -DBL_MIN
, pzero
, TRUE
);
167 maxMinTest(pzero
, DBL_MIN
, pzero
, FALSE
);
168 maxMinTest(pzero
, -DBL_MIN
, -DBL_MIN
, FALSE
);
169 maxMinTest(pzero
, DBL_MAX
, pzero
, FALSE
);
170 maxMinTest(pzero
, -DBL_MAX
, -DBL_MAX
, FALSE
);
172 // -0 with DBL_MAX and DBL_MIN
173 maxMinTest(nzero
, DBL_MAX
, DBL_MAX
, TRUE
);
174 maxMinTest(nzero
, -DBL_MAX
, nzero
, TRUE
);
175 maxMinTest(nzero
, DBL_MIN
, DBL_MIN
, TRUE
);
176 maxMinTest(nzero
, -DBL_MIN
, nzero
, TRUE
);
177 maxMinTest(nzero
, DBL_MIN
, nzero
, FALSE
);
178 maxMinTest(nzero
, -DBL_MIN
, -DBL_MIN
, FALSE
);
179 maxMinTest(nzero
, DBL_MAX
, nzero
, FALSE
);
180 maxMinTest(nzero
, -DBL_MAX
, -DBL_MAX
, FALSE
);
184 PUtilTest::maxMinTest(double a
, double b
, double exp
, UBool max
)
189 result
= uprv_fmax(a
, b
);
191 result
= uprv_fmin(a
, b
);
193 UBool nanResultOK
= (uprv_isNaN(a
) || uprv_isNaN(b
));
195 if(uprv_isNaN(result
) && ! nanResultOK
) {
196 errln(UnicodeString("FAIL: got NaN as result without NaN as argument"));
198 errln(UnicodeString(" max(") + a
+ ", " + b
+ ") is " + result
+ ", expected " + exp
);
200 errln(UnicodeString(" min(") + a
+ ", " + b
+ ") is " + result
+ ", expected " + exp
);
202 else if(result
!= exp
&& ! (uprv_isNaN(result
) || uprv_isNaN(exp
)))
204 errln(UnicodeString("FAIL: max(") + a
+ ", " + b
+ ") is " + result
+ ", expected " + exp
);
206 errln(UnicodeString("FAIL: min(") + a
+ ", " + b
+ ") is " + result
+ ", expected " + exp
);
210 logln(UnicodeString("OK: max(") + a
+ ", " + b
+ ") is " + result
);
212 logln(UnicodeString("OK: min(") + a
+ ", " + b
+ ") is " + result
);
216 //==============================
218 // NaN is weird- comparisons with NaN _always_ return false, with the
219 // exception of !=, which _always_ returns true
221 PUtilTest::testNaN(void)
223 logln("NaN tests may show that the expected NaN!=NaN etc. is not true on some");
224 logln("platforms; however, ICU does not rely on them because it defines");
225 logln("and uses uprv_isNaN(). Therefore, most failing NaN tests only report warnings.");
227 PUtilTest::testIsNaN();
235 logln("End of NaN tests.");
238 //==============================
241 PUtilTest::testPositiveInfinity(void)
243 double pinf
= uprv_getInfinity();
244 double ninf
= -uprv_getInfinity();
247 if(uprv_isInfinite(pinf
) != TRUE
) {
248 errln("FAIL: isInfinite(+Infinity) returned FALSE, should be TRUE.");
251 if(uprv_isPositiveInfinity(pinf
) != TRUE
) {
252 errln("FAIL: isPositiveInfinity(+Infinity) returned FALSE, should be TRUE.");
255 if(uprv_isNegativeInfinity(pinf
) != FALSE
) {
256 errln("FAIL: isNegativeInfinity(+Infinity) returned TRUE, should be FALSE.");
259 if((pinf
> DBL_MAX
) != TRUE
) {
260 errln("FAIL: +Infinity > DBL_MAX returned FALSE, should be TRUE.");
263 if((pinf
> DBL_MIN
) != TRUE
) {
264 errln("FAIL: +Infinity > DBL_MIN returned FALSE, should be TRUE.");
267 if((pinf
> ninf
) != TRUE
) {
268 errln("FAIL: +Infinity > -Infinity returned FALSE, should be TRUE.");
271 if((pinf
> ten
) != TRUE
) {
272 errln("FAIL: +Infinity > 10.0 returned FALSE, should be TRUE.");
276 //==============================
279 PUtilTest::testNegativeInfinity(void)
281 double pinf
= uprv_getInfinity();
282 double ninf
= -uprv_getInfinity();
285 if(uprv_isInfinite(ninf
) != TRUE
) {
286 errln("FAIL: isInfinite(-Infinity) returned FALSE, should be TRUE.");
289 if(uprv_isNegativeInfinity(ninf
) != TRUE
) {
290 errln("FAIL: isNegativeInfinity(-Infinity) returned FALSE, should be TRUE.");
293 if(uprv_isPositiveInfinity(ninf
) != FALSE
) {
294 errln("FAIL: isPositiveInfinity(-Infinity) returned TRUE, should be FALSE.");
297 if((ninf
< DBL_MAX
) != TRUE
) {
298 errln("FAIL: -Infinity < DBL_MAX returned FALSE, should be TRUE.");
301 if((ninf
< DBL_MIN
) != TRUE
) {
302 errln("FAIL: -Infinity < DBL_MIN returned FALSE, should be TRUE.");
305 if((ninf
< pinf
) != TRUE
) {
306 errln("FAIL: -Infinity < +Infinity returned FALSE, should be TRUE.");
309 if((ninf
< ten
) != TRUE
) {
310 errln("FAIL: -Infinity < 10.0 returned FALSE, should be TRUE.");
314 //==============================
317 // -0.0 == 0.0 == TRUE
318 // -0.0 < 0.0 == FALSE
319 // generating -0.0 must be done at runtime. compiler apparently ignores sign?
321 PUtilTest::testZero(void)
323 // volatile is used to fake out the compiler optimizer. We really want to divide by 0.
324 volatile double pzero
= 0.0;
325 volatile double nzero
= 0.0;
329 if((pzero
== nzero
) != TRUE
) {
330 errln("FAIL: 0.0 == -0.0 returned FALSE, should be TRUE.");
333 if((pzero
> nzero
) != FALSE
) {
334 errln("FAIL: 0.0 > -0.0 returned TRUE, should be FALSE.");
337 if((pzero
>= nzero
) != TRUE
) {
338 errln("FAIL: 0.0 >= -0.0 returned FALSE, should be TRUE.");
341 if((pzero
< nzero
) != FALSE
) {
342 errln("FAIL: 0.0 < -0.0 returned TRUE, should be FALSE.");
345 if((pzero
<= nzero
) != TRUE
) {
346 errln("FAIL: 0.0 <= -0.0 returned FALSE, should be TRUE.");
348 #if U_PLATFORM != U_PF_OS400 /* OS/400 will generate divide by zero exception MCH1214 */
349 if(uprv_isInfinite(1/pzero
) != TRUE
) {
350 errln("FAIL: isInfinite(1/0.0) returned FALSE, should be TRUE.");
353 if(uprv_isInfinite(1/nzero
) != TRUE
) {
354 errln("FAIL: isInfinite(1/-0.0) returned FALSE, should be TRUE.");
357 if(uprv_isPositiveInfinity(1/pzero
) != TRUE
) {
358 errln("FAIL: isPositiveInfinity(1/0.0) returned FALSE, should be TRUE.");
361 if(uprv_isNegativeInfinity(1/nzero
) != TRUE
) {
362 errln("FAIL: isNegativeInfinity(1/-0.0) returned FALSE, should be TRUE.");
367 //==============================
370 PUtilTest::testIsNaN(void)
372 double pinf
= uprv_getInfinity();
373 double ninf
= -uprv_getInfinity();
374 double nan
= uprv_getNaN();
377 if(uprv_isNaN(nan
) == FALSE
) {
378 errln("FAIL: isNaN() returned FALSE for NaN.");
381 if(uprv_isNaN(pinf
) == TRUE
) {
382 errln("FAIL: isNaN() returned TRUE for +Infinity.");
385 if(uprv_isNaN(ninf
) == TRUE
) {
386 errln("FAIL: isNaN() returned TRUE for -Infinity.");
389 if(uprv_isNaN(ten
) == TRUE
) {
390 errln("FAIL: isNaN() returned TRUE for 10.0.");
394 //==============================
397 PUtilTest::NaNGT(void)
399 double pinf
= uprv_getInfinity();
400 double ninf
= -uprv_getInfinity();
401 double nan
= uprv_getNaN();
404 if((nan
> nan
) != FALSE
) {
405 logln("WARNING: NaN > NaN returned TRUE, should be FALSE");
408 if((nan
> pinf
) != FALSE
) {
409 logln("WARNING: NaN > +Infinity returned TRUE, should be FALSE");
412 if((nan
> ninf
) != FALSE
) {
413 logln("WARNING: NaN > -Infinity returned TRUE, should be FALSE");
416 if((nan
> ten
) != FALSE
) {
417 logln("WARNING: NaN > 10.0 returned TRUE, should be FALSE");
421 //==============================
424 PUtilTest::NaNLT(void)
426 double pinf
= uprv_getInfinity();
427 double ninf
= -uprv_getInfinity();
428 double nan
= uprv_getNaN();
431 if((nan
< nan
) != FALSE
) {
432 logln("WARNING: NaN < NaN returned TRUE, should be FALSE");
435 if((nan
< pinf
) != FALSE
) {
436 logln("WARNING: NaN < +Infinity returned TRUE, should be FALSE");
439 if((nan
< ninf
) != FALSE
) {
440 logln("WARNING: NaN < -Infinity returned TRUE, should be FALSE");
443 if((nan
< ten
) != FALSE
) {
444 logln("WARNING: NaN < 10.0 returned TRUE, should be FALSE");
448 //==============================
451 PUtilTest::NaNGTE(void)
453 double pinf
= uprv_getInfinity();
454 double ninf
= -uprv_getInfinity();
455 double nan
= uprv_getNaN();
458 if((nan
>= nan
) != FALSE
) {
459 logln("WARNING: NaN >= NaN returned TRUE, should be FALSE");
462 if((nan
>= pinf
) != FALSE
) {
463 logln("WARNING: NaN >= +Infinity returned TRUE, should be FALSE");
466 if((nan
>= ninf
) != FALSE
) {
467 logln("WARNING: NaN >= -Infinity returned TRUE, should be FALSE");
470 if((nan
>= ten
) != FALSE
) {
471 logln("WARNING: NaN >= 10.0 returned TRUE, should be FALSE");
475 //==============================
478 PUtilTest::NaNLTE(void)
480 double pinf
= uprv_getInfinity();
481 double ninf
= -uprv_getInfinity();
482 double nan
= uprv_getNaN();
485 if((nan
<= nan
) != FALSE
) {
486 logln("WARNING: NaN <= NaN returned TRUE, should be FALSE");
489 if((nan
<= pinf
) != FALSE
) {
490 logln("WARNING: NaN <= +Infinity returned TRUE, should be FALSE");
493 if((nan
<= ninf
) != FALSE
) {
494 logln("WARNING: NaN <= -Infinity returned TRUE, should be FALSE");
497 if((nan
<= ten
) != FALSE
) {
498 logln("WARNING: NaN <= 10.0 returned TRUE, should be FALSE");
502 //==============================
505 PUtilTest::NaNE(void)
507 double pinf
= uprv_getInfinity();
508 double ninf
= -uprv_getInfinity();
509 double nan
= uprv_getNaN();
512 if((nan
== nan
) != FALSE
) {
513 logln("WARNING: NaN == NaN returned TRUE, should be FALSE");
516 if((nan
== pinf
) != FALSE
) {
517 logln("WARNING: NaN == +Infinity returned TRUE, should be FALSE");
520 if((nan
== ninf
) != FALSE
) {
521 logln("WARNING: NaN == -Infinity returned TRUE, should be FALSE");
524 if((nan
== ten
) != FALSE
) {
525 logln("WARNING: NaN == 10.0 returned TRUE, should be FALSE");
529 //==============================
532 PUtilTest::NaNNE(void)
534 double pinf
= uprv_getInfinity();
535 double ninf
= -uprv_getInfinity();
536 double nan
= uprv_getNaN();
539 if((nan
!= nan
) != TRUE
) {
540 logln("WARNING: NaN != NaN returned FALSE, should be TRUE");
543 if((nan
!= pinf
) != TRUE
) {
544 logln("WARNING: NaN != +Infinity returned FALSE, should be TRUE");
547 if((nan
!= ninf
) != TRUE
) {
548 logln("WARNING: NaN != -Infinity returned FALSE, should be TRUE");
551 if((nan
!= ten
) != TRUE
) {
552 logln("WARNING: NaN != 10.0 returned FALSE, should be TRUE");