]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/nmfmtrt.cpp
ICU-511.27.tar.gz
[apple/icu.git] / icuSources / test / intltest / nmfmtrt.cpp
index a27996a5e41e0ce46d0797f3a16cb7ee5c934b04..e4d8d3804e58564824c01479774e732da4c25209 100644 (file)
@@ -1,8 +1,8 @@
-/********************************************************************
+/***********************************************************************
  * COPYRIGHT: 
- * Copyright (c) 1997-2003, International Business Machines Corporation and
- * others. All Rights Reserved.
- ********************************************************************/
+ * Copyright (c) 1997-2011, International Business Machines Corporation
+ * and others. All Rights Reserved.
+ ***********************************************************************/
 
 #include "unicode/utypes.h"
 
 #include "unicode/dcfmtsym.h"
 #include "unicode/decimfmt.h"
 #include "unicode/locid.h"
+#include "putilimp.h"
 
 #include <float.h>
 #include <stdio.h>    // for sprintf
+#include <stdlib.h>
  
 // *****************************************************************************
 // class NumberFormatRoundTripTest
@@ -41,16 +43,45 @@ void NumberFormatRoundTripTest::runIndexedTest( int32_t index, UBool exec, const
 }
 
 UBool 
-NumberFormatRoundTripTest::failure(UErrorCode status, const char* msg)
+NumberFormatRoundTripTest::failure(UErrorCode status, const char* msg, UBool possibleDataError)
 {
     if(U_FAILURE(status)) {
-        errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
+        if (possibleDataError) {
+            dataerrln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
+        } else {
+            errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
+        }
         return TRUE;
     }
 
     return FALSE;
 }
 
+uint32_t
+NumberFormatRoundTripTest::randLong()
+{
+    // Assume 8-bit (or larger) rand values.  Also assume
+    // that the system rand() function is very poor, which it always is.
+    uint32_t d;
+    uint32_t i;
+    char* poke = (char*)&d;
+    for (i=0; i < sizeof(uint32_t); ++i)
+    {
+        poke[i] = (char)(rand() & 0xFF);
+    }
+    return d;
+}
+
+/**
+ * Return a random value from -range..+range.
+ */
+double 
+NumberFormatRoundTripTest::randomDouble(double range)
+{
+    double a = randFraction();
+    return (2.0 * range * a) - range;
+}
+
 void 
 NumberFormatRoundTripTest::start()
 {
@@ -63,18 +94,21 @@ NumberFormatRoundTripTest::start()
     logln("Default Locale");
 
     fmt = NumberFormat::createInstance(status);
-    failure(status, "NumberFormat::createInstance");
-    test(fmt);
+    if (!failure(status, "NumberFormat::createInstance", TRUE)){
+        test(fmt);
+    }
     delete fmt;
 
     fmt = NumberFormat::createCurrencyInstance(status);
-    failure(status, "NumberFormat::createCurrencyInstance");
-    test(fmt);
+    if (!failure(status, "NumberFormat::createCurrencyInstance", TRUE)){
+        test(fmt);
+    }
     delete fmt;
 
     fmt = NumberFormat::createPercentInstance(status);
-    failure(status, "NumberFormat::createPercentInstance");
-    test(fmt);
+    if (!failure(status, "NumberFormat::createPercentInstance", TRUE)){
+        test(fmt);
+    }
     delete fmt;
 
 
@@ -112,7 +146,7 @@ NumberFormatRoundTripTest::start()
 void 
 NumberFormatRoundTripTest::test(NumberFormat *fmt)
 {
-#if IEEE_754 && !defined(OS400)
+#if IEEE_754 && U_PLATFORM != U_PF_OS400
     test(fmt, uprv_getNaN());
     test(fmt, uprv_getInfinity());
     test(fmt, -uprv_getInfinity());
@@ -133,10 +167,10 @@ NumberFormatRoundTripTest::test(NumberFormat *fmt)
         test(fmt, uprv_floor((randomDouble(10000))));
         test(fmt, randomDouble(1e50));
         test(fmt, randomDouble(1e-50));
-#if !defined(OS390) && !defined(OS400)
+#if !(U_PF_OS390 <= U_PLATFORM && U_PLATFORM <= U_PF_OS400)
         test(fmt, randomDouble(1e100));
 #elif IEEE_754
-        test(fmt, randomDouble(1e75));    /*OS390 and OS400*/
+        test(fmt, randomDouble(1e75));
 #endif /* OS390 and OS400 */
         // {sfb} When formatting with a percent instance, numbers very close to
         // DBL_MAX will fail the round trip.  This is because:
@@ -148,47 +182,38 @@ NumberFormatRoundTripTest::test(NumberFormat *fmt)
         // I'll get around this by dividing by the multiplier to make sure
         // the double will stay in range.
         //if(fmt->getMultipler() == 1)
-        if(fmt->getDynamicClassID() == DecimalFormat::getStaticClassID())
+        DecimalFormat *df = dynamic_cast<DecimalFormat *>(fmt);
+        if(df != NULL)
         {
-#if !defined(OS390) && !defined(OS400)
+#if !(U_PF_OS390 <= U_PLATFORM && U_PLATFORM <= U_PF_OS400)
             /* DBL_MAX/2 is here because randomDouble does a *2 in the math */
-            test(fmt, randomDouble(DBL_MAX/2.0) / ((DecimalFormat*)fmt)->getMultiplier());
+            test(fmt, randomDouble(DBL_MAX/2.0) / df->getMultiplier());
 #elif IEEE_754
-            test(fmt, randomDouble(1e75) / ((DecimalFormat*)fmt)->getMultiplier());   
+            test(fmt, randomDouble(1e75) / df->getMultiplier());
 #else
-            test(fmt, randomDouble(1e65) / ((DecimalFormat*)fmt)->getMultiplier());   /*OS390*/
+            test(fmt, randomDouble(1e65) / df->getMultiplier());
 #endif
         }
 
-#if defined XP_MAC || defined __alpha__ || defined U_OSF
-// These machines don't support denormalized doubles,
-// so the low-end range doesn't match Windows
+#if (defined(_MSC_VER) && _MSC_VER < 1400) || defined(__alpha__) || defined(U_OSF)
+        // These machines and compilers don't fully support denormalized doubles,
         test(fmt, randomDouble(1e-292));
-#elif defined(OS390) || defined(OS400)
+        test(fmt, randomDouble(1e-100));
+#elif U_PF_OS390 <= U_PLATFORM && U_PLATFORM <= U_PF_OS400
+        // i5/OS (OS/400) throws exceptions on denormalized numbers
 #   if IEEE_754
-        test(fmt, randomDouble(1e-78));  /*OS390 and OS400*/
+        test(fmt, randomDouble(1e-78));
+        test(fmt, randomDouble(1e-78));
+        // #else we're using something like the old z/OS floating point.
 #   endif
 #else
-        test(fmt, randomDouble(1e-323));
-#endif /* OS390 and OS400*/
-#if !defined(OS390) && !defined(OS400)
+        // This is a normal machine that can support IEEE754 denormalized doubles without throwing an error.
+        test(fmt, randomDouble(DBL_MIN)); /* Usually 2.2250738585072014e-308 */
         test(fmt, randomDouble(1e-100));
-#elif IEEE_754
-        test(fmt, randomDouble(1e-78));  /*OS390 and OS400*/
-#endif /* OS390 and OS400*/
+#endif
     }
 }
 
-/**
- * Return a random value from -range..+range.
- */
-double 
-NumberFormatRoundTripTest::randomDouble(double range)
-{
-    double a = randFraction();
-    return (2.0 * range * a) - range;
-}
-
 void 
 NumberFormatRoundTripTest::test(NumberFormat *fmt, double value)
 {
@@ -205,7 +230,10 @@ void
 NumberFormatRoundTripTest::test(NumberFormat *fmt, const Formattable& value)
 {
     fmt->setMaximumFractionDigits(999);
-    
+    DecimalFormat *df = dynamic_cast<DecimalFormat *>(fmt);
+    if(df != NULL) {
+        df->setRoundingIncrement(0.0);
+    }
     UErrorCode status = U_ZERO_ERROR;
     UnicodeString s, s2, temp;
     if(isDouble(value))
@@ -261,8 +289,8 @@ NumberFormatRoundTripTest::test(NumberFormat *fmt, const Formattable& value)
 
     if (show) {
         errln(/*value.getString(temp) +*/ typeOf(value, temp) + " F> " +
-            escape(s) + " P> " +
-            /*n.getString(temp) +*/ typeOf(n, temp) + " F> " +
+            escape(s) + " P> " + (n.getType() == Formattable::kDouble ? n.getDouble() : (double)n.getLong())
+            /*n.getString(temp) */ + typeOf(n, temp) + " F> " +
             escape(s2));
     }
 }