summaryrefslogtreecommitdiff
path: root/Build/source/libs/icu/icu-xetex/test/intltest/tsputil.cpp
blob: 1f4b66ee2184c945b82cc7edf47c347795653cd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/********************************************************************
 * COPYRIGHT: 
 * Copyright (c) 1997-2006, International Business Machines Corporation and
 * others. All Rights Reserved.
 ********************************************************************/

#include "tsputil.h"

#include <float.h> // DBL_MAX, DBL_MIN
#include "putilimp.h"

#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;

void 
PUtilTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
{
    //if (exec) logln("TestSuite PUtilTest: ");
    switch (index) {
        CASE(0, testMaxMin)
        CASE(1, testNaN)
        CASE(2, testPositiveInfinity)
        CASE(3, testNegativeInfinity)
        CASE(4, testZero)
        CASE(5, testU_INLINE)
//        CASE(, testIEEEremainder)

        default: name = ""; break; //needed to end loop
    }
}

#if 0
void
PUtilTest::testIEEEremainder()
{
    double    pinf  = uprv_getInfinity();
    double    ninf  = -uprv_getInfinity();
    double    nan   = uprv_getNaN();
    double    pzero = 0.0;
    double    nzero = 0.0;

    nzero *= -1;

    // simple remainder checks
    remainderTest(7.0, 2.5, -0.5);
    remainderTest(7.0, -2.5, -0.5);
#ifndef OS390
    // ### TODO:
    // The following tests fails on S/390 with IEEE support in release builds;
    // debug builds work.
    // The functioning of ChoiceFormat is not affected by this bug.
    remainderTest(-7.0, 2.5, 0.5);
    remainderTest(-7.0, -2.5, 0.5);
#endif
    remainderTest(5.0, 3.0, -1.0);
    
    // this should work
    //remainderTest(43.7, 2.5, 1.25);

    /*

    // infinity and real
    remainderTest(pinf, 1.0, 1.25);
    remainderTest(1.0, pinf, 1.0);
    remainderTest(ninf, 1.0, 1.25);
    remainderTest(1.0, ninf, 1.0);

    // test infinity and nan
    remainderTest(ninf, pinf, 1.25);
    remainderTest(ninf, nan, 1.25);
    remainderTest(pinf, nan, 1.25);

    // test infinity and zero
    remainderTest(pinf, pzero, 1.25);
    remainderTest(pinf, nzero, 1.25);
    remainderTest(ninf, pzero, 1.25);
    remainderTest(ninf, nzero, 1.25);
*/
}

void
PUtilTest::remainderTest(double x, double y, double exp)
{
    double result = uprv_IEEEremainder(x,y);

    if(        uprv_isNaN(result) && 
        ! ( uprv_isNaN(x) || uprv_isNaN(y))) {
        errln(UnicodeString("FAIL: got NaN as result without NaN as argument"));
        errln(UnicodeString("      IEEEremainder(") + x + ", " + y + ") is " + result + ", expected " + exp);
    }
    else if(result != exp)
        errln(UnicodeString("FAIL: IEEEremainder(") + x + ", " + y + ") is " + result + ", expected " + exp);
    else
        logln(UnicodeString("OK: IEEEremainder(") + x + ", " + y + ") is " + result);

}
#endif

void
PUtilTest::testMaxMin()
{
    double    pinf        = uprv_getInfinity();
    double    ninf        = -uprv_getInfinity();
    double    nan        = uprv_getNaN();
    double    pzero        = 0.0;
    double    nzero        = 0.0;

    nzero *= -1;

    // +Inf with -Inf
    maxMinTest(pinf, ninf, pinf, TRUE);
    maxMinTest(pinf, ninf, ninf, FALSE);

    // +Inf with +0 and -0
    maxMinTest(pinf, pzero, pinf, TRUE);
    maxMinTest(pinf, pzero, pzero, FALSE);
    maxMinTest(pinf, nzero, pinf, TRUE);
    maxMinTest(pinf, nzero, nzero, FALSE);

    // -Inf with +0 and -0
    maxMinTest(ninf, pzero, pzero, TRUE);
    maxMinTest(ninf, pzero, ninf, FALSE);
    maxMinTest(ninf, nzero, nzero, TRUE);
    maxMinTest(ninf, nzero, ninf, FALSE);

    // NaN with +Inf and -Inf
    maxMinTest(pinf, nan, nan, TRUE);
    maxMinTest(pinf, nan, nan, FALSE);
    maxMinTest(ninf, nan, nan, TRUE);
    maxMinTest(ninf, nan, nan, FALSE);

    // NaN with NaN
    maxMinTest(nan, nan, nan, TRUE);
    maxMinTest(nan, nan, nan, FALSE);

    // NaN with +0 and -0
    maxMinTest(nan, pzero, nan, TRUE);
    maxMinTest(nan, pzero, nan, FALSE);
    maxMinTest(nan, nzero, nan, TRUE);
    maxMinTest(nan, nzero, nan, FALSE);

    // +Inf with DBL_MAX and DBL_MIN
    maxMinTest(pinf, DBL_MAX, pinf, TRUE);
    maxMinTest(pinf, -DBL_MAX, pinf, TRUE);
    maxMinTest(pinf, DBL_MIN, pinf, TRUE);
    maxMinTest(pinf, -DBL_MIN, pinf, TRUE);
    maxMinTest(pinf, DBL_MIN, DBL_MIN, FALSE);
    maxMinTest(pinf, -DBL_MIN, -DBL_MIN, FALSE);
    maxMinTest(pinf, DBL_MAX, DBL_MAX, FALSE);
    maxMinTest(pinf, -DBL_MAX, -DBL_MAX, FALSE);

    // -Inf with DBL_MAX and DBL_MIN
    maxMinTest(ninf, DBL_MAX, DBL_MAX, TRUE);
    maxMinTest(ninf, -DBL_MAX, -DBL_MAX, TRUE);
    maxMinTest(ninf, DBL_MIN, DBL_MIN, TRUE);
    maxMinTest(ninf, -DBL_MIN, -DBL_MIN, TRUE);
    maxMinTest(ninf, DBL_MIN, ninf, FALSE);
    maxMinTest(ninf, -DBL_MIN, ninf, FALSE);
    maxMinTest(ninf, DBL_MAX, ninf, FALSE);
    maxMinTest(ninf, -DBL_MAX, ninf, FALSE);

    // +0 with DBL_MAX and DBL_MIN
    maxMinTest(pzero, DBL_MAX, DBL_MAX, TRUE);
    maxMinTest(pzero, -DBL_MAX, pzero, TRUE);
    maxMinTest(pzero, DBL_MIN, DBL_MIN, TRUE);
    maxMinTest(pzero, -DBL_MIN, pzero, TRUE);
    maxMinTest(pzero, DBL_MIN, pzero, FALSE);
    maxMinTest(pzero, -DBL_MIN, -DBL_MIN, FALSE);
    maxMinTest(pzero, DBL_MAX, pzero, FALSE);
    maxMinTest(pzero, -DBL_MAX, -DBL_MAX, FALSE);

    // -0 with DBL_MAX and DBL_MIN
    maxMinTest(nzero, DBL_MAX, DBL_MAX, TRUE);
    maxMinTest(nzero, -DBL_MAX, nzero, TRUE);
    maxMinTest(nzero, DBL_MIN, DBL_MIN, TRUE);
    maxMinTest(nzero, -DBL_MIN, nzero, TRUE);
    maxMinTest(nzero, DBL_MIN, nzero, FALSE);
    maxMinTest(nzero, -DBL_MIN, -DBL_MIN, FALSE);
    maxMinTest(nzero, DBL_MAX, nzero, FALSE);
    maxMinTest(nzero, -DBL_MAX, -DBL_MAX, FALSE);
}

void
PUtilTest::maxMinTest(double a, double b, double exp, UBool max)
{
    double result = 0.0;

    if(max)
        result = uprv_fmax(a, b);
    else
        result = uprv_fmin(a, b);

    UBool nanResultOK = (uprv_isNaN(a) || uprv_isNaN(b));

    if(uprv_isNaN(result) && ! nanResultOK) {
        errln(UnicodeString("FAIL: got NaN as result without NaN as argument"));
        if(max)
            errln(UnicodeString("      max(") + a + ", " + b + ") is " + result + ", expected " + exp);
        else
            errln(UnicodeString("      min(") + a + ", " + b + ") is " + result + ", expected " + exp);
    }
    else if(result != exp && ! (uprv_isNaN(result) || uprv_isNaN(exp)))
        if(max)
            errln(UnicodeString("FAIL: max(") + a + ", " + b + ") is " + result + ", expected " + exp);
        else
            errln(UnicodeString("FAIL: min(") + a + ", " + b + ") is " + result + ", expected " + exp);
    else {
        if (verbose) {
            if(max)
                logln(UnicodeString("OK: max(") + a + ", " + b + ") is " + result);
            else
                logln(UnicodeString("OK: min(") + a + ", " + b + ") is " + result);
        }
    }
}
//==============================

// NaN is weird- comparisons with NaN _always_ return false, with the
// exception of !=, which _always_ returns true
void
PUtilTest::testNaN(void)
{
    logln("NaN tests may show that the expected NaN!=NaN etc. is not true on some");
    logln("platforms; however, ICU does not rely on them because it defines");
    logln("and uses uprv_isNaN(). Therefore, most failing NaN tests only report warnings.");

    PUtilTest::testIsNaN();
    PUtilTest::NaNGT();
    PUtilTest::NaNLT();
    PUtilTest::NaNGTE();
    PUtilTest::NaNLTE();
    PUtilTest::NaNE();
    PUtilTest::NaNNE();

    logln("End of NaN tests.");
}

//==============================

void 
PUtilTest::testPositiveInfinity(void)
{
    double  pinf    = uprv_getInfinity();
    double  ninf    = -uprv_getInfinity();
    double  ten     = 10.0;

    if(uprv_isInfinite(pinf) != TRUE) {
        errln("FAIL: isInfinite(+Infinity) returned FALSE, should be TRUE.");
    }

    if(uprv_isPositiveInfinity(pinf) != TRUE) {
        errln("FAIL: isPositiveInfinity(+Infinity) returned FALSE, should be TRUE.");
    }

    if(uprv_isNegativeInfinity(pinf) != FALSE) {
        errln("FAIL: isNegativeInfinity(+Infinity) returned TRUE, should be FALSE.");
    }

    if(pinf > DBL_MAX != TRUE) {
        errln("FAIL: +Infinity > DBL_MAX returned FALSE, should be TRUE.");
    }

    if(pinf > DBL_MIN != TRUE) {
        errln("FAIL: +Infinity > DBL_MIN returned FALSE, should be TRUE.");
    }

    if(pinf > ninf != TRUE) {
        errln("FAIL: +Infinity > -Infinity returned FALSE, should be TRUE.");
    }

    if(pinf > ten != TRUE) {
        errln("FAIL: +Infinity > 10.0 returned FALSE, should be TRUE.");
    }
}

//==============================

void           
PUtilTest::testNegativeInfinity(void)
{
    double  pinf    = uprv_getInfinity();
    double  ninf    = -uprv_getInfinity();
    double  ten     = 10.0;

    if(uprv_isInfinite(ninf) != TRUE) {
        errln("FAIL: isInfinite(-Infinity) returned FALSE, should be TRUE.");
    }

    if(uprv_isNegativeInfinity(ninf) != TRUE) {
        errln("FAIL: isNegativeInfinity(-Infinity) returned FALSE, should be TRUE.");
    }

    if(uprv_isPositiveInfinity(ninf) != FALSE) {
        errln("FAIL: isPositiveInfinity(-Infinity) returned TRUE, should be FALSE.");
    }

    if(ninf < DBL_MAX != TRUE) {
        errln("FAIL: -Infinity < DBL_MAX returned FALSE, should be TRUE.");
    }

    if(ninf < DBL_MIN != TRUE) {
        errln("FAIL: -Infinity < DBL_MIN returned FALSE, should be TRUE.");
    }

    if(ninf < pinf != TRUE) {
        errln("FAIL: -Infinity < +Infinity returned FALSE, should be TRUE.");
    }

    if(ninf < ten != TRUE) {
        errln("FAIL: -Infinity < 10.0 returned FALSE, should be TRUE.");
    }
}

//==============================

// notes about zero:
// -0.0 == 0.0 == TRUE
// -0.0 <  0.0 == FALSE
// generating -0.0 must be done at runtime.  compiler apparently ignores sign?
void           
PUtilTest::testZero(void)
{
    // volatile is used to fake out the compiler optimizer.  We really want to divide by 0.
    volatile double pzero   = 0.0;
    volatile double nzero   = 0.0;

    nzero *= -1;

    if(pzero == nzero != TRUE) {
        errln("FAIL: 0.0 == -0.0 returned FALSE, should be TRUE.");
    }

    if(pzero > nzero != FALSE) {
        errln("FAIL: 0.0 > -0.0 returned TRUE, should be FALSE.");
    }

    if(pzero >= nzero != TRUE) {
        errln("FAIL: 0.0 >= -0.0 returned FALSE, should be TRUE.");
    }

    if(pzero < nzero != FALSE) {
        errln("FAIL: 0.0 < -0.0 returned TRUE, should be FALSE.");
    }

    if(pzero <= nzero != TRUE) {
        errln("FAIL: 0.0 <= -0.0 returned FALSE, should be TRUE.");
    }
#ifndef OS400 /* OS/400 will generate divide by zero exception MCH1214 */
    if(uprv_isInfinite(1/pzero) != TRUE) {
        errln("FAIL: isInfinite(1/0.0) returned FALSE, should be TRUE.");
    }

    if(uprv_isInfinite(1/nzero) != TRUE) {
        errln("FAIL: isInfinite(1/-0.0) returned FALSE, should be TRUE.");
    }

    if(uprv_isPositiveInfinity(1/pzero) != TRUE) {
        errln("FAIL: isPositiveInfinity(1/0.0) returned FALSE, should be TRUE.");
    }

    if(uprv_isNegativeInfinity(1/nzero) != TRUE) {
        errln("FAIL: isNegativeInfinity(1/-0.0) returned FALSE, should be TRUE.");
    }
#endif
}

//==============================

void
PUtilTest::testIsNaN(void)
{
    double  pinf    = uprv_getInfinity();
    double  ninf    = -uprv_getInfinity();
    double  nan     = uprv_getNaN();
    double  ten     = 10.0;

    if(uprv_isNaN(nan) == FALSE) {
        errln("FAIL: isNaN() returned FALSE for NaN.");
    }

    if(uprv_isNaN(pinf) == TRUE) {
        errln("FAIL: isNaN() returned TRUE for +Infinity.");
    }

    if(uprv_isNaN(ninf) == TRUE) {
        errln("FAIL: isNaN() returned TRUE for -Infinity.");
    }

    if(uprv_isNaN(ten) == TRUE) {
        errln("FAIL: isNaN() returned TRUE for 10.0.");
    }
}

//==============================

void
PUtilTest::NaNGT(void)
{
    double  pinf    = uprv_getInfinity();
    double  ninf    = -uprv_getInfinity();
    double  nan     = uprv_getNaN();
    double  ten     = 10.0;

    if(nan > nan != FALSE) {
        logln("WARNING: NaN > NaN returned TRUE, should be FALSE");
    }

    if(nan > pinf != FALSE) {
        logln("WARNING: NaN > +Infinity returned TRUE, should be FALSE");
    }

    if(nan > ninf != FALSE) {
        logln("WARNING: NaN > -Infinity returned TRUE, should be FALSE");
    }

    if(nan > ten != FALSE) {
        logln("WARNING: NaN > 10.0 returned TRUE, should be FALSE");
    }
}

//==============================

void 
PUtilTest::NaNLT(void)
{
    double  pinf    = uprv_getInfinity();
    double  ninf    = -uprv_getInfinity();
    double  nan     = uprv_getNaN();
    double  ten     = 10.0;

    if(nan < nan != FALSE) {
        logln("WARNING: NaN < NaN returned TRUE, should be FALSE");
    }

    if(nan < pinf != FALSE) {
        logln("WARNING: NaN < +Infinity returned TRUE, should be FALSE");
    }

    if(nan < ninf != FALSE) {
        logln("WARNING: NaN < -Infinity returned TRUE, should be FALSE");
    }

    if(nan < ten != FALSE) {
        logln("WARNING: NaN < 10.0 returned TRUE, should be FALSE");
    }
}

//==============================

void                   
PUtilTest::NaNGTE(void)
{
    double  pinf    = uprv_getInfinity();
    double  ninf    = -uprv_getInfinity();
    double  nan     = uprv_getNaN();
    double  ten     = 10.0;

    if(nan >= nan != FALSE) {
        logln("WARNING: NaN >= NaN returned TRUE, should be FALSE");
    }

    if(nan >= pinf != FALSE) {
        logln("WARNING: NaN >= +Infinity returned TRUE, should be FALSE");
    }

    if(nan >= ninf != FALSE) {
        logln("WARNING: NaN >= -Infinity returned TRUE, should be FALSE");
    }

    if(nan >= ten != FALSE) {
        logln("WARNING: NaN >= 10.0 returned TRUE, should be FALSE");
    }
}

//==============================

void                   
PUtilTest::NaNLTE(void)
{
    double  pinf    = uprv_getInfinity();
    double  ninf    = -uprv_getInfinity();
    double  nan     = uprv_getNaN();
    double  ten     = 10.0;

    if(nan <= nan != FALSE) {
        logln("WARNING: NaN <= NaN returned TRUE, should be FALSE");
    }

    if(nan <= pinf != FALSE) {
        logln("WARNING: NaN <= +Infinity returned TRUE, should be FALSE");
    }

    if(nan <= ninf != FALSE) {
        logln("WARNING: NaN <= -Infinity returned TRUE, should be FALSE");
    }

    if(nan <= ten != FALSE) {
        logln("WARNING: NaN <= 10.0 returned TRUE, should be FALSE");
    }
}

//==============================

void                   
PUtilTest::NaNE(void)
{
    double  pinf    = uprv_getInfinity();
    double  ninf    = -uprv_getInfinity();
    double  nan     = uprv_getNaN();
    double  ten     = 10.0;

    if(nan == nan != FALSE) {
        logln("WARNING: NaN == NaN returned TRUE, should be FALSE");
    }

    if(nan == pinf != FALSE) {
        logln("WARNING: NaN == +Infinity returned TRUE, should be FALSE");
    }

    if(nan == ninf != FALSE) {
        logln("WARNING: NaN == -Infinity returned TRUE, should be FALSE");
    }

    if(nan == ten != FALSE) {
        logln("WARNING: NaN == 10.0 returned TRUE, should be FALSE");
    }
}

//==============================

void 
PUtilTest::NaNNE(void)
{
    double  pinf    = uprv_getInfinity();
    double  ninf    = -uprv_getInfinity();
    double  nan     = uprv_getNaN();
    double  ten     = 10.0;

    if(nan != nan != TRUE) {
        logln("WARNING: NaN != NaN returned FALSE, should be TRUE");
    }

    if(nan != pinf != TRUE) {
        logln("WARNING: NaN != +Infinity returned FALSE, should be TRUE");
    }

    if(nan != ninf != TRUE) {
        logln("WARNING: NaN != -Infinity returned FALSE, should be TRUE");
    }

    if(nan != ten != TRUE) {
        logln("WARNING: NaN != 10.0 returned FALSE, should be TRUE");
    }
}

U_INLINE int32_t inlineTriple(int32_t x) {
    return 3*x;
}

// "code" coverage test for Jitterbug 4515 RFE: in C++, use U_INLINE=inline
void
PUtilTest::testU_INLINE() {
    if(inlineTriple(2)!=6 || inlineTriple(-55)!=-165) {
        errln("inlineTriple() failed");
    }
}