home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / intltest / tsputil.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-16  |  6.7 KB  |  205 lines

  1. /*
  2. *****************************************************************************************
  3. *                                                                                       *
  4. * COPYRIGHT:                                                                            *
  5. *   (C) Copyright International Business Machines Corporation,  1998                    *
  6. *   Licensed Material - Program-Property of IBM - All Rights Reserved.                  *
  7. *   US Government Users Restricted Rights - Use, duplication, or disclosure             *
  8. *   restricted by GSA ADP Schedule Contract with IBM Corp.                              *
  9. *                                                                                       *
  10. *****************************************************************************************
  11. */
  12.  
  13. #include "tsputil.h"
  14.  
  15. #include <float.h> // DBL_MAX, DBL_MIN
  16.  
  17. #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
  18.  
  19. void 
  20. PUtilTest::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  21. {
  22.     //if (exec) logln("TestSuite PUtilTest: ");
  23.     switch (index) {
  24.  
  25.         CASE(0, testIEEEremainder)
  26.         CASE(1, testMaxMin)
  27.  
  28.         default: name = ""; break; //needed to end loop
  29.     }
  30. }
  31.  
  32. void
  33. PUtilTest::testIEEEremainder()
  34. {
  35.     double    pinf        = icu_getInfinity();
  36.     double    ninf        = -icu_getInfinity();
  37.     double    nan            = icu_getNaN();
  38.     double    pzero        = 0.0;
  39.     double    nzero        = 0.0;
  40.  
  41.     nzero *= -1;
  42.  
  43.     // simple remainder checks
  44.     remainderTest(7.0, 2.5, -0.5);
  45.     remainderTest(7.0, -2.5, -0.5);
  46.     remainderTest(-7.0, 2.5, 0.5);
  47.     remainderTest(-7.0, -2.5, 0.5);
  48.     remainderTest(5.0, 3.0, -1.0);
  49.     
  50.     // this should work
  51.     //remainderTest(43.7, 2.5, 1.25);
  52.  
  53.     /*
  54.  
  55.     // infinity and real
  56.     remainderTest(pinf, 1.0, 1.25);
  57.     remainderTest(1.0, pinf, 1.0);
  58.     remainderTest(ninf, 1.0, 1.25);
  59.     remainderTest(1.0, ninf, 1.0);
  60.  
  61.     // test infinity and nan
  62.     remainderTest(ninf, pinf, 1.25);
  63.     remainderTest(ninf, nan, 1.25);
  64.     remainderTest(pinf, nan, 1.25);
  65.  
  66.     // test infinity and zero
  67.     remainderTest(pinf, pzero, 1.25);
  68.     remainderTest(pinf, nzero, 1.25);
  69.     remainderTest(ninf, pzero, 1.25);
  70.     remainderTest(ninf, nzero, 1.25);
  71. */
  72. }
  73.  
  74. void
  75. PUtilTest::remainderTest(double x, double y, double exp)
  76. {
  77.     double result = icu_IEEEremainder(x,y);
  78.  
  79.     if(        icu_isNaN(result) && 
  80.         ! ( icu_isNaN(x) || icu_isNaN(y))) {
  81.         errln(UnicodeString("FAIL: got NaN as result without NaN as argument"));
  82.         errln(UnicodeString("      IEEEremainder(") + x + ", " + y + ") is " + result + ", expected " + exp);
  83.     }
  84.     else if(result != exp)
  85.         errln(UnicodeString("FAIL: IEEEremainder(") + x + ", " + y + ") is " + result + ", expected " + exp);
  86.     else
  87.         logln(UnicodeString("OK: IEEEremainder(") + x + ", " + y + ") is " + result);
  88.  
  89. }
  90.  
  91. void
  92. PUtilTest::testMaxMin()
  93. {
  94.     double    pinf        = icu_getInfinity();
  95.     double    ninf        = -icu_getInfinity();
  96.     double    nan        = icu_getNaN();
  97.     double    pzero        = 0.0;
  98.     double    nzero        = 0.0;
  99.  
  100.     nzero *= -1;
  101.  
  102.     // +Inf with -Inf
  103.     maxMinTest(pinf, ninf, pinf, TRUE);
  104.     maxMinTest(pinf, ninf, ninf, FALSE);
  105.  
  106.     // +Inf with +0 and -0
  107.     maxMinTest(pinf, pzero, pinf, TRUE);
  108.     maxMinTest(pinf, pzero, pzero, FALSE);
  109.     maxMinTest(pinf, nzero, pinf, TRUE);
  110.     maxMinTest(pinf, nzero, nzero, FALSE);
  111.  
  112.     // -Inf with +0 and -0
  113.     maxMinTest(ninf, pzero, pzero, TRUE);
  114.     maxMinTest(ninf, pzero, ninf, FALSE);
  115.     maxMinTest(ninf, nzero, nzero, TRUE);
  116.     maxMinTest(ninf, nzero, ninf, FALSE);
  117.  
  118.     // NaN with +Inf and -Inf
  119.     maxMinTest(pinf, nan, nan, TRUE);
  120.     maxMinTest(pinf, nan, nan, FALSE);
  121.     maxMinTest(ninf, nan, nan, TRUE);
  122.     maxMinTest(ninf, nan, nan, FALSE);
  123.  
  124.     // NaN with NaN
  125.     maxMinTest(nan, nan, nan, TRUE);
  126.     maxMinTest(nan, nan, nan, FALSE);
  127.  
  128.     // NaN with +0 and -0
  129.     maxMinTest(nan, pzero, nan, TRUE);
  130.     maxMinTest(nan, pzero, nan, FALSE);
  131.     maxMinTest(nan, nzero, nan, TRUE);
  132.     maxMinTest(nan, nzero, nan, FALSE);
  133.  
  134.     // +Inf with DBL_MAX and DBL_MIN
  135.     maxMinTest(pinf, DBL_MAX, pinf, TRUE);
  136.     maxMinTest(pinf, -DBL_MAX, pinf, TRUE);
  137.     maxMinTest(pinf, DBL_MIN, pinf, TRUE);
  138.     maxMinTest(pinf, -DBL_MIN, pinf, TRUE);
  139.     maxMinTest(pinf, DBL_MIN, DBL_MIN, FALSE);
  140.     maxMinTest(pinf, -DBL_MIN, -DBL_MIN, FALSE);
  141.     maxMinTest(pinf, DBL_MAX, DBL_MAX, FALSE);
  142.     maxMinTest(pinf, -DBL_MAX, -DBL_MAX, FALSE);
  143.  
  144.     // -Inf with DBL_MAX and DBL_MIN
  145.     maxMinTest(ninf, DBL_MAX, DBL_MAX, TRUE);
  146.     maxMinTest(ninf, -DBL_MAX, -DBL_MAX, TRUE);
  147.     maxMinTest(ninf, DBL_MIN, DBL_MIN, TRUE);
  148.     maxMinTest(ninf, -DBL_MIN, -DBL_MIN, TRUE);
  149.     maxMinTest(ninf, DBL_MIN, ninf, FALSE);
  150.     maxMinTest(ninf, -DBL_MIN, ninf, FALSE);
  151.     maxMinTest(ninf, DBL_MAX, ninf, FALSE);
  152.     maxMinTest(ninf, -DBL_MAX, ninf, FALSE);
  153.  
  154.     // +0 with DBL_MAX and DBL_MIN
  155.     maxMinTest(pzero, DBL_MAX, DBL_MAX, TRUE);
  156.     maxMinTest(pzero, -DBL_MAX, pzero, TRUE);
  157.     maxMinTest(pzero, DBL_MIN, DBL_MIN, TRUE);
  158.     maxMinTest(pzero, -DBL_MIN, pzero, TRUE);
  159.     maxMinTest(pzero, DBL_MIN, pzero, FALSE);
  160.     maxMinTest(pzero, -DBL_MIN, -DBL_MIN, FALSE);
  161.     maxMinTest(pzero, DBL_MAX, pzero, FALSE);
  162.     maxMinTest(pzero, -DBL_MAX, -DBL_MAX, FALSE);
  163.  
  164.     // -0 with DBL_MAX and DBL_MIN
  165.     maxMinTest(nzero, DBL_MAX, DBL_MAX, TRUE);
  166.     maxMinTest(nzero, -DBL_MAX, nzero, TRUE);
  167.     maxMinTest(nzero, DBL_MIN, DBL_MIN, TRUE);
  168.     maxMinTest(nzero, -DBL_MIN, nzero, TRUE);
  169.     maxMinTest(nzero, DBL_MIN, nzero, FALSE);
  170.     maxMinTest(nzero, -DBL_MIN, -DBL_MIN, FALSE);
  171.     maxMinTest(nzero, DBL_MAX, nzero, FALSE);
  172.     maxMinTest(nzero, -DBL_MAX, -DBL_MAX, FALSE);
  173. }
  174.  
  175. void
  176. PUtilTest::maxMinTest(double a, double b, double exp, bool_t max)
  177. {
  178.   double result = 0.0;
  179.   
  180.   if(max)
  181.     result = icu_fmax(a, b);
  182.   else
  183.     result = icu_fmin(a, b);
  184.   
  185.   bool_t nanResultOK = (icu_isNaN(a) || icu_isNaN(b));
  186.   
  187.   if(icu_isNaN(result) && ! nanResultOK) {
  188.     errln(UnicodeString("FAIL: got NaN as result without NaN as argument"));
  189.     if(max)
  190.       errln(UnicodeString("      max(") + a + ", " + b + ") is " + result + ", expected " + exp);
  191.     else
  192.       errln(UnicodeString("      min(") + a + ", " + b + ") is " + result + ", expected " + exp);
  193.   }
  194.   else if(result != exp && ! (icu_isNaN(result) || icu_isNaN(exp)))
  195.     if(max)
  196.       errln(UnicodeString("FAIL: max(") + a + ", " + b + ") is " + result + ", expected " + exp);
  197.     else
  198.       errln(UnicodeString("FAIL: min(") + a + ", " + b + ") is " + result + ", expected " + exp);
  199.   else
  200.     if(max)
  201.       logln(UnicodeString("OK: max(") + a + ", " + b + ") is " + result);
  202.     else
  203.       logln(UnicodeString("OK: min(") + a + ", " + b + ") is " + result);
  204. }
  205.