]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/tsputil.cpp
ICU-6.2.8.tar.gz
[apple/icu.git] / icuSources / test / intltest / tsputil.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2004, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7 #include "tsputil.h"
8
9 #include <float.h> // DBL_MAX, DBL_MIN
10 #include "putilimp.h"
11
12 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
13
14 void
15 PUtilTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
16 {
17 //if (exec) logln("TestSuite PUtilTest: ");
18 switch (index) {
19 CASE(0, testMaxMin)
20 CASE(1, testNaN)
21 CASE(2, testPositiveInfinity)
22 CASE(3, testNegativeInfinity)
23 CASE(4, testZero)
24 // CASE(5, testIEEEremainder)
25
26 default: name = ""; break; //needed to end loop
27 }
28 }
29
30 #if 0
31 void
32 PUtilTest::testIEEEremainder()
33 {
34 double pinf = uprv_getInfinity();
35 double ninf = -uprv_getInfinity();
36 double nan = uprv_getNaN();
37 double pzero = 0.0;
38 double nzero = 0.0;
39
40 nzero *= -1;
41
42 // simple remainder checks
43 remainderTest(7.0, 2.5, -0.5);
44 remainderTest(7.0, -2.5, -0.5);
45 #ifndef OS390
46 // ### TODO:
47 // The following tests fails on S/390 with IEEE support in release builds;
48 // debug builds work.
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);
52 #endif
53 remainderTest(5.0, 3.0, -1.0);
54
55 // this should work
56 //remainderTest(43.7, 2.5, 1.25);
57
58 /*
59
60 // infinity and real
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);
65
66 // test infinity and nan
67 remainderTest(ninf, pinf, 1.25);
68 remainderTest(ninf, nan, 1.25);
69 remainderTest(pinf, nan, 1.25);
70
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);
76 */
77 }
78
79 void
80 PUtilTest::remainderTest(double x, double y, double exp)
81 {
82 double result = uprv_IEEEremainder(x,y);
83
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);
88 }
89 else if(result != exp)
90 errln(UnicodeString("FAIL: IEEEremainder(") + x + ", " + y + ") is " + result + ", expected " + exp);
91 else
92 logln(UnicodeString("OK: IEEEremainder(") + x + ", " + y + ") is " + result);
93
94 }
95 #endif
96
97 void
98 PUtilTest::testMaxMin()
99 {
100 double pinf = uprv_getInfinity();
101 double ninf = -uprv_getInfinity();
102 double nan = uprv_getNaN();
103 double pzero = 0.0;
104 double nzero = 0.0;
105
106 nzero *= -1;
107
108 // +Inf with -Inf
109 maxMinTest(pinf, ninf, pinf, TRUE);
110 maxMinTest(pinf, ninf, ninf, FALSE);
111
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);
117
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);
123
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);
129
130 // NaN with NaN
131 maxMinTest(nan, nan, nan, TRUE);
132 maxMinTest(nan, nan, nan, FALSE);
133
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);
139
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);
149
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);
159
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);
169
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);
179 }
180
181 void
182 PUtilTest::maxMinTest(double a, double b, double exp, UBool max)
183 {
184 double result = 0.0;
185
186 if(max)
187 result = uprv_fmax(a, b);
188 else
189 result = uprv_fmin(a, b);
190
191 UBool nanResultOK = (uprv_isNaN(a) || uprv_isNaN(b));
192
193 if(uprv_isNaN(result) && ! nanResultOK) {
194 errln(UnicodeString("FAIL: got NaN as result without NaN as argument"));
195 if(max)
196 errln(UnicodeString(" max(") + a + ", " + b + ") is " + result + ", expected " + exp);
197 else
198 errln(UnicodeString(" min(") + a + ", " + b + ") is " + result + ", expected " + exp);
199 }
200 else if(result != exp && ! (uprv_isNaN(result) || uprv_isNaN(exp)))
201 if(max)
202 errln(UnicodeString("FAIL: max(") + a + ", " + b + ") is " + result + ", expected " + exp);
203 else
204 errln(UnicodeString("FAIL: min(") + a + ", " + b + ") is " + result + ", expected " + exp);
205 else
206 if(max)
207 logln(UnicodeString("OK: max(") + a + ", " + b + ") is " + result);
208 else
209 logln(UnicodeString("OK: min(") + a + ", " + b + ") is " + result);
210 }
211 //==============================
212
213 // NaN is weird- comparisons with NaN _always_ return false, with the
214 // exception of !=, which _always_ returns true
215 void
216 PUtilTest::testNaN(void)
217 {
218 logln("NaN tests may show that the expected NaN!=NaN etc. is not true on some");
219 logln("platforms; however, ICU does not rely on them because it defines");
220 logln("and uses uprv_isNaN(). Therefore, most failing NaN tests only report warnings.");
221
222 PUtilTest::testIsNaN();
223 PUtilTest::NaNGT();
224 PUtilTest::NaNLT();
225 PUtilTest::NaNGTE();
226 PUtilTest::NaNLTE();
227 PUtilTest::NaNE();
228 PUtilTest::NaNNE();
229
230 logln("End of NaN tests.");
231 }
232
233 //==============================
234
235 void
236 PUtilTest::testPositiveInfinity(void)
237 {
238 double pinf = uprv_getInfinity();
239 double ninf = -uprv_getInfinity();
240 double ten = 10.0;
241
242 if(uprv_isInfinite(pinf) != TRUE) {
243 errln("FAIL: isInfinite(+Infinity) returned FALSE, should be TRUE.");
244 }
245
246 if(uprv_isPositiveInfinity(pinf) != TRUE) {
247 errln("FAIL: isPositiveInfinity(+Infinity) returned FALSE, should be TRUE.");
248 }
249
250 if(uprv_isNegativeInfinity(pinf) != FALSE) {
251 errln("FAIL: isNegativeInfinity(+Infinity) returned TRUE, should be FALSE.");
252 }
253
254 if(pinf > DBL_MAX != TRUE) {
255 errln("FAIL: +Infinity > DBL_MAX returned FALSE, should be TRUE.");
256 }
257
258 if(pinf > DBL_MIN != TRUE) {
259 errln("FAIL: +Infinity > DBL_MIN returned FALSE, should be TRUE.");
260 }
261
262 if(pinf > ninf != TRUE) {
263 errln("FAIL: +Infinity > -Infinity returned FALSE, should be TRUE.");
264 }
265
266 if(pinf > ten != TRUE) {
267 errln("FAIL: +Infinity > 10.0 returned FALSE, should be TRUE.");
268 }
269 }
270
271 //==============================
272
273 void
274 PUtilTest::testNegativeInfinity(void)
275 {
276 double pinf = uprv_getInfinity();
277 double ninf = -uprv_getInfinity();
278 double ten = 10.0;
279
280 if(uprv_isInfinite(ninf) != TRUE) {
281 errln("FAIL: isInfinite(-Infinity) returned FALSE, should be TRUE.");
282 }
283
284 if(uprv_isNegativeInfinity(ninf) != TRUE) {
285 errln("FAIL: isNegativeInfinity(-Infinity) returned FALSE, should be TRUE.");
286 }
287
288 if(uprv_isPositiveInfinity(ninf) != FALSE) {
289 errln("FAIL: isPositiveInfinity(-Infinity) returned TRUE, should be FALSE.");
290 }
291
292 if(ninf < DBL_MAX != TRUE) {
293 errln("FAIL: -Infinity < DBL_MAX returned FALSE, should be TRUE.");
294 }
295
296 if(ninf < DBL_MIN != TRUE) {
297 errln("FAIL: -Infinity < DBL_MIN returned FALSE, should be TRUE.");
298 }
299
300 if(ninf < pinf != TRUE) {
301 errln("FAIL: -Infinity < +Infinity returned FALSE, should be TRUE.");
302 }
303
304 if(ninf < ten != TRUE) {
305 errln("FAIL: -Infinity < 10.0 returned FALSE, should be TRUE.");
306 }
307 }
308
309 //==============================
310
311 // notes about zero:
312 // -0.0 == 0.0 == TRUE
313 // -0.0 < 0.0 == FALSE
314 // generating -0.0 must be done at runtime. compiler apparently ignores sign?
315 void
316 PUtilTest::testZero(void)
317 {
318 // volatile is used to fake out the compiler optimizer. We really want to divide by 0.
319 volatile double pzero = 0.0;
320 volatile double nzero = 0.0;
321
322 nzero *= -1;
323
324 if(pzero == nzero != TRUE) {
325 errln("FAIL: 0.0 == -0.0 returned FALSE, should be TRUE.");
326 }
327
328 if(pzero > nzero != FALSE) {
329 errln("FAIL: 0.0 > -0.0 returned TRUE, should be FALSE.");
330 }
331
332 if(pzero >= nzero != TRUE) {
333 errln("FAIL: 0.0 >= -0.0 returned FALSE, should be TRUE.");
334 }
335
336 if(pzero < nzero != FALSE) {
337 errln("FAIL: 0.0 < -0.0 returned TRUE, should be FALSE.");
338 }
339
340 if(pzero <= nzero != TRUE) {
341 errln("FAIL: 0.0 <= -0.0 returned FALSE, should be TRUE.");
342 }
343 #ifndef OS400 /* OS/400 will generate divide by zero exception MCH1214 */
344 if(uprv_isInfinite(1/pzero) != TRUE) {
345 errln("FAIL: isInfinite(1/0.0) returned FALSE, should be TRUE.");
346 }
347
348 if(uprv_isInfinite(1/nzero) != TRUE) {
349 errln("FAIL: isInfinite(1/-0.0) returned FALSE, should be TRUE.");
350 }
351
352 if(uprv_isPositiveInfinity(1/pzero) != TRUE) {
353 errln("FAIL: isPositiveInfinity(1/0.0) returned FALSE, should be TRUE.");
354 }
355
356 if(uprv_isNegativeInfinity(1/nzero) != TRUE) {
357 errln("FAIL: isNegativeInfinity(1/-0.0) returned FALSE, should be TRUE.");
358 }
359 #endif
360 }
361
362 //==============================
363
364 void
365 PUtilTest::testIsNaN(void)
366 {
367 double pinf = uprv_getInfinity();
368 double ninf = -uprv_getInfinity();
369 double nan = uprv_getNaN();
370 double ten = 10.0;
371
372 if(uprv_isNaN(nan) == FALSE) {
373 errln("FAIL: isNaN() returned FALSE for NaN.");
374 }
375
376 if(uprv_isNaN(pinf) == TRUE) {
377 errln("FAIL: isNaN() returned TRUE for +Infinity.");
378 }
379
380 if(uprv_isNaN(ninf) == TRUE) {
381 errln("FAIL: isNaN() returned TRUE for -Infinity.");
382 }
383
384 if(uprv_isNaN(ten) == TRUE) {
385 errln("FAIL: isNaN() returned TRUE for 10.0.");
386 }
387 }
388
389 //==============================
390
391 void
392 PUtilTest::NaNGT(void)
393 {
394 double pinf = uprv_getInfinity();
395 double ninf = -uprv_getInfinity();
396 double nan = uprv_getNaN();
397 double ten = 10.0;
398
399 if(nan > nan != FALSE) {
400 logln("WARNING: NaN > NaN returned TRUE, should be FALSE");
401 }
402
403 if(nan > pinf != FALSE) {
404 logln("WARNING: NaN > +Infinity returned TRUE, should be FALSE");
405 }
406
407 if(nan > ninf != FALSE) {
408 logln("WARNING: NaN > -Infinity returned TRUE, should be FALSE");
409 }
410
411 if(nan > ten != FALSE) {
412 logln("WARNING: NaN > 10.0 returned TRUE, should be FALSE");
413 }
414 }
415
416 //==============================
417
418 void
419 PUtilTest::NaNLT(void)
420 {
421 double pinf = uprv_getInfinity();
422 double ninf = -uprv_getInfinity();
423 double nan = uprv_getNaN();
424 double ten = 10.0;
425
426 if(nan < nan != FALSE) {
427 logln("WARNING: NaN < NaN returned TRUE, should be FALSE");
428 }
429
430 if(nan < pinf != FALSE) {
431 logln("WARNING: NaN < +Infinity returned TRUE, should be FALSE");
432 }
433
434 if(nan < ninf != FALSE) {
435 logln("WARNING: NaN < -Infinity returned TRUE, should be FALSE");
436 }
437
438 if(nan < ten != FALSE) {
439 logln("WARNING: NaN < 10.0 returned TRUE, should be FALSE");
440 }
441 }
442
443 //==============================
444
445 void
446 PUtilTest::NaNGTE(void)
447 {
448 double pinf = uprv_getInfinity();
449 double ninf = -uprv_getInfinity();
450 double nan = uprv_getNaN();
451 double ten = 10.0;
452
453 if(nan >= nan != FALSE) {
454 logln("WARNING: NaN >= NaN returned TRUE, should be FALSE");
455 }
456
457 if(nan >= pinf != FALSE) {
458 logln("WARNING: NaN >= +Infinity returned TRUE, should be FALSE");
459 }
460
461 if(nan >= ninf != FALSE) {
462 logln("WARNING: NaN >= -Infinity returned TRUE, should be FALSE");
463 }
464
465 if(nan >= ten != FALSE) {
466 logln("WARNING: NaN >= 10.0 returned TRUE, should be FALSE");
467 }
468 }
469
470 //==============================
471
472 void
473 PUtilTest::NaNLTE(void)
474 {
475 double pinf = uprv_getInfinity();
476 double ninf = -uprv_getInfinity();
477 double nan = uprv_getNaN();
478 double ten = 10.0;
479
480 if(nan <= nan != FALSE) {
481 logln("WARNING: NaN <= NaN returned TRUE, should be FALSE");
482 }
483
484 if(nan <= pinf != FALSE) {
485 logln("WARNING: NaN <= +Infinity returned TRUE, should be FALSE");
486 }
487
488 if(nan <= ninf != FALSE) {
489 logln("WARNING: NaN <= -Infinity returned TRUE, should be FALSE");
490 }
491
492 if(nan <= ten != FALSE) {
493 logln("WARNING: NaN <= 10.0 returned TRUE, should be FALSE");
494 }
495 }
496
497 //==============================
498
499 void
500 PUtilTest::NaNE(void)
501 {
502 double pinf = uprv_getInfinity();
503 double ninf = -uprv_getInfinity();
504 double nan = uprv_getNaN();
505 double ten = 10.0;
506
507 if(nan == nan != FALSE) {
508 logln("WARNING: NaN == NaN returned TRUE, should be FALSE");
509 }
510
511 if(nan == pinf != FALSE) {
512 logln("WARNING: NaN == +Infinity returned TRUE, should be FALSE");
513 }
514
515 if(nan == ninf != FALSE) {
516 logln("WARNING: NaN == -Infinity returned TRUE, should be FALSE");
517 }
518
519 if(nan == ten != FALSE) {
520 logln("WARNING: NaN == 10.0 returned TRUE, should be FALSE");
521 }
522 }
523
524 //==============================
525
526 void
527 PUtilTest::NaNNE(void)
528 {
529 double pinf = uprv_getInfinity();
530 double ninf = -uprv_getInfinity();
531 double nan = uprv_getNaN();
532 double ten = 10.0;
533
534 if(nan != nan != TRUE) {
535 logln("WARNING: NaN != NaN returned FALSE, should be TRUE");
536 }
537
538 if(nan != pinf != TRUE) {
539 logln("WARNING: NaN != +Infinity returned FALSE, should be TRUE");
540 }
541
542 if(nan != ninf != TRUE) {
543 logln("WARNING: NaN != -Infinity returned FALSE, should be TRUE");
544 }
545
546 if(nan != ten != TRUE) {
547 logln("WARNING: NaN != 10.0 returned FALSE, should be TRUE");
548 }
549 }
550