2000-01-15 02:00:06 +00:00
|
|
|
/********************************************************************
|
|
|
|
* COPYRIGHT:
|
2001-03-21 20:09:56 +00:00
|
|
|
* Copyright (c) 1997-2001, International Business Machines Corporation and
|
2000-01-15 02:00:06 +00:00
|
|
|
* others. All Rights Reserved.
|
|
|
|
********************************************************************/
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-09-21 00:43:14 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "nmfmtrt.h"
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/dcfmtsym.h"
|
|
|
|
#include "unicode/decimfmt.h"
|
|
|
|
#include "unicode/locid.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
#include <float.h>
|
|
|
|
#include <stdio.h> // for sprintf
|
|
|
|
|
|
|
|
// *****************************************************************************
|
|
|
|
// class NumberFormatRoundTripTest
|
|
|
|
// *****************************************************************************
|
|
|
|
|
2000-08-11 22:19:54 +00:00
|
|
|
UBool NumberFormatRoundTripTest::verbose = FALSE;
|
|
|
|
UBool NumberFormatRoundTripTest::STRING_COMPARE = TRUE;
|
|
|
|
UBool NumberFormatRoundTripTest::EXACT_NUMERIC_COMPARE = FALSE;
|
|
|
|
UBool NumberFormatRoundTripTest::DEBUG = FALSE;
|
|
|
|
double NumberFormatRoundTripTest::MAX_ERROR = 1e-14;
|
|
|
|
double NumberFormatRoundTripTest::max_numeric_error = 0.0;
|
|
|
|
double NumberFormatRoundTripTest::min_numeric_error = 1.0;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
|
|
|
|
|
2000-08-23 19:11:16 +00:00
|
|
|
void NumberFormatRoundTripTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
// if (exec) logln((UnicodeString)"TestSuite NumberFormatRoundTripTest");
|
|
|
|
switch (index) {
|
|
|
|
CASE(0, start)
|
|
|
|
default: name = ""; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
1999-08-16 21:50:52 +00:00
|
|
|
NumberFormatRoundTripTest::failure(UErrorCode status, const char* msg)
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(status)) {
|
1999-12-08 23:31:17 +00:00
|
|
|
errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
|
1999-08-16 21:50:52 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2000-08-11 22:19:54 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
NumberFormatRoundTripTest::start()
|
|
|
|
{
|
|
|
|
// test(NumberFormat.getInstance(new Locale("sr", "", "")));
|
2000-08-11 22:19:54 +00:00
|
|
|
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
NumberFormat *fmt = NULL;
|
|
|
|
|
|
|
|
logln("Default Locale");
|
|
|
|
|
|
|
|
fmt = NumberFormat::createInstance(status);
|
|
|
|
failure(status, "NumberFormat::createInstance");
|
|
|
|
test(fmt);
|
|
|
|
delete fmt;
|
2000-08-11 22:19:54 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
fmt = NumberFormat::createCurrencyInstance(status);
|
|
|
|
failure(status, "NumberFormat::createCurrencyInstance");
|
|
|
|
test(fmt);
|
|
|
|
delete fmt;
|
2000-08-11 22:19:54 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
fmt = NumberFormat::createPercentInstance(status);
|
|
|
|
failure(status, "NumberFormat::createPercentInstance");
|
|
|
|
test(fmt);
|
|
|
|
delete fmt;
|
|
|
|
|
|
|
|
|
|
|
|
int32_t locCount = 0;
|
|
|
|
const Locale *loc = NumberFormat::getAvailableLocales(locCount);
|
|
|
|
if(quick) {
|
|
|
|
if(locCount > 5)
|
|
|
|
locCount = 5;
|
|
|
|
logln("Quick mode: only testing first 5 Locales");
|
|
|
|
}
|
|
|
|
for(int i = 0; i < locCount; ++i) {
|
|
|
|
UnicodeString name;
|
|
|
|
logln(loc[i].getDisplayName(name));
|
|
|
|
|
|
|
|
fmt = NumberFormat::createInstance(loc[i], status);
|
|
|
|
failure(status, "NumberFormat::createInstance");
|
|
|
|
test(fmt);
|
|
|
|
delete fmt;
|
|
|
|
|
|
|
|
fmt = NumberFormat::createCurrencyInstance(loc[i], status);
|
|
|
|
failure(status, "NumberFormat::createCurrencyInstance");
|
|
|
|
test(fmt);
|
|
|
|
delete fmt;
|
|
|
|
|
|
|
|
fmt = NumberFormat::createPercentInstance(loc[i], status);
|
|
|
|
failure(status, "NumberFormat::createPercentInstance");
|
|
|
|
test(fmt);
|
|
|
|
delete fmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
logln(UnicodeString("Numeric error ") + min_numeric_error + " to " + max_numeric_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
NumberFormatRoundTripTest::test(NumberFormat *fmt)
|
|
|
|
{
|
2002-08-08 23:47:09 +00:00
|
|
|
#if IEEE_754 && !defined(OS400)
|
2000-08-11 22:19:54 +00:00
|
|
|
test(fmt, uprv_getNaN());
|
|
|
|
test(fmt, uprv_getInfinity());
|
|
|
|
test(fmt, -uprv_getInfinity());
|
2000-03-22 23:17:42 +00:00
|
|
|
#endif
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-08-11 22:19:54 +00:00
|
|
|
test(fmt, (int32_t)500);
|
|
|
|
test(fmt, (int32_t)0);
|
|
|
|
test(fmt, (int32_t)-0);
|
|
|
|
test(fmt, 0.0);
|
|
|
|
double negZero = 0.0; negZero /= -1.0;
|
|
|
|
test(fmt, negZero);
|
|
|
|
test(fmt, 9223372036854775808.0);
|
|
|
|
test(fmt, -9223372036854775809.0);
|
|
|
|
|
|
|
|
for(int i = 0; i < 10; ++i) {
|
|
|
|
test(fmt, randomDouble(1));
|
|
|
|
test(fmt, randomDouble(10000));
|
|
|
|
test(fmt, uprv_floor((randomDouble(10000))));
|
|
|
|
test(fmt, randomDouble(1e50));
|
|
|
|
test(fmt, randomDouble(1e-50));
|
2002-08-08 23:47:09 +00:00
|
|
|
#if !defined(OS390) && !defined(OS400)
|
2000-08-11 22:19:54 +00:00
|
|
|
test(fmt, randomDouble(1e100));
|
2000-04-06 16:45:03 +00:00
|
|
|
#elif IEEE_754
|
2002-08-08 23:47:09 +00:00
|
|
|
test(fmt, randomDouble(1e75)); /*OS390 and OS400*/
|
|
|
|
#endif /* OS390 and OS400 */
|
2000-08-11 22:19:54 +00:00
|
|
|
// {sfb} When formatting with a percent instance, numbers very close to
|
|
|
|
// DBL_MAX will fail the round trip. This is because:
|
|
|
|
// 1) Format the double into a string --> INF% (since 100 * double > DBL_MAX)
|
|
|
|
// 2) Parse the string into a double --> INF
|
|
|
|
// 3) Re-format the double --> INF%
|
|
|
|
// 4) The strings are equal, so that works.
|
|
|
|
// 5) Calculate the proportional error --> INF, so the test will fail
|
|
|
|
// 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())
|
2001-04-06 22:33:47 +00:00
|
|
|
{
|
2002-08-08 23:47:09 +00:00
|
|
|
#if !defined(OS390) && !defined(OS400)
|
2003-02-06 01:19:58 +00:00
|
|
|
/* DBL_MAX/2 is here because randomDouble does a *2 in the math */
|
|
|
|
test(fmt, randomDouble(DBL_MAX/2.0) / ((DecimalFormat*)fmt)->getMultiplier());
|
2001-04-06 22:33:47 +00:00
|
|
|
#elif IEEE_754
|
2000-08-11 22:19:54 +00:00
|
|
|
test(fmt, randomDouble(1e75) / ((DecimalFormat*)fmt)->getMultiplier());
|
2001-04-06 22:33:47 +00:00
|
|
|
#else
|
2000-08-11 22:19:54 +00:00
|
|
|
test(fmt, randomDouble(1e65) / ((DecimalFormat*)fmt)->getMultiplier()); /*OS390*/
|
2001-04-06 22:33:47 +00:00
|
|
|
#endif
|
|
|
|
}
|
2000-03-22 23:17:42 +00:00
|
|
|
|
2002-08-08 23:47:09 +00:00
|
|
|
#if defined XP_MAC || defined __alpha__ || defined U_OSF
|
2001-04-06 22:33:47 +00:00
|
|
|
// These machines don't support denormalized doubles,
|
|
|
|
// so the low-end range doesn't match Windows
|
|
|
|
test(fmt, randomDouble(1e-292));
|
2002-08-08 23:47:09 +00:00
|
|
|
#elif defined(OS390) || defined(OS400)
|
2000-04-06 16:45:03 +00:00
|
|
|
# if IEEE_754
|
2002-08-08 23:47:09 +00:00
|
|
|
test(fmt, randomDouble(1e-78)); /*OS390 and OS400*/
|
2000-03-22 23:17:42 +00:00
|
|
|
# endif
|
|
|
|
#else
|
2000-08-11 22:19:54 +00:00
|
|
|
test(fmt, randomDouble(1e-323));
|
2002-08-08 23:47:09 +00:00
|
|
|
#endif /* OS390 and OS400*/
|
|
|
|
#if !defined(OS390) && !defined(OS400)
|
2000-08-11 22:19:54 +00:00
|
|
|
test(fmt, randomDouble(1e-100));
|
2000-04-06 16:45:03 +00:00
|
|
|
#elif IEEE_754
|
2002-08-08 23:47:09 +00:00
|
|
|
test(fmt, randomDouble(1e-78)); /*OS390 and OS400*/
|
|
|
|
#endif /* OS390 and OS400*/
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
test(fmt, Formattable(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NumberFormatRoundTripTest::test(NumberFormat *fmt, int32_t value)
|
|
|
|
{
|
|
|
|
test(fmt, Formattable(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NumberFormatRoundTripTest::test(NumberFormat *fmt, const Formattable& value)
|
|
|
|
{
|
|
|
|
fmt->setMaximumFractionDigits(999);
|
|
|
|
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString s, s2, temp;
|
|
|
|
if(isDouble(value))
|
|
|
|
s = fmt->format(value.getDouble(), s);
|
|
|
|
else
|
|
|
|
s = fmt->format(value.getLong(), s);
|
|
|
|
|
|
|
|
Formattable n;
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool show = verbose;
|
1999-08-16 21:50:52 +00:00
|
|
|
if(DEBUG)
|
2000-11-02 02:45:32 +00:00
|
|
|
logln(/*value.getString(temp) +*/ " F> " + escape(s));
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
fmt->parse(s, n, status);
|
|
|
|
failure(status, "fmt->parse");
|
|
|
|
if(DEBUG)
|
2000-11-02 02:45:32 +00:00
|
|
|
logln(escape(s) + " P> " /*+ n.getString(temp)*/);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
if(isDouble(n))
|
|
|
|
s2 = fmt->format(n.getDouble(), s2);
|
|
|
|
else
|
|
|
|
s2 = fmt->format(n.getLong(), s2);
|
|
|
|
|
|
|
|
if(DEBUG)
|
2000-11-02 02:45:32 +00:00
|
|
|
logln(/*n.getString(temp) +*/ " F> " + escape(s2));
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
if(STRING_COMPARE) {
|
|
|
|
if (s != s2) {
|
2000-11-02 02:45:32 +00:00
|
|
|
errln("*** STRING ERROR \"" + escape(s) + "\" != \"" + escape(s2) + "\"");
|
1999-08-16 21:50:52 +00:00
|
|
|
show = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(EXACT_NUMERIC_COMPARE) {
|
|
|
|
if(value != n) {
|
2000-11-02 02:45:32 +00:00
|
|
|
errln("*** NUMERIC ERROR");
|
1999-08-16 21:50:52 +00:00
|
|
|
show = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Compute proportional error
|
|
|
|
double error = proportionalError(value, n);
|
|
|
|
|
|
|
|
if(error > MAX_ERROR) {
|
2000-11-02 02:45:32 +00:00
|
|
|
errln(UnicodeString("*** NUMERIC ERROR ") + error);
|
1999-08-16 21:50:52 +00:00
|
|
|
show = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error > max_numeric_error)
|
|
|
|
max_numeric_error = error;
|
|
|
|
if (error < min_numeric_error)
|
|
|
|
min_numeric_error = error;
|
|
|
|
}
|
|
|
|
|
2002-06-17 17:36:55 +00:00
|
|
|
if (show) {
|
|
|
|
errln(/*value.getString(temp) +*/ typeOf(value, temp) + " F> " +
|
1999-08-16 21:50:52 +00:00
|
|
|
escape(s) + " P> " +
|
|
|
|
/*n.getString(temp) +*/ typeOf(n, temp) + " F> " +
|
|
|
|
escape(s2));
|
2002-06-17 17:36:55 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
NumberFormatRoundTripTest::proportionalError(const Formattable& a, const Formattable& b)
|
|
|
|
{
|
|
|
|
double aa,bb;
|
|
|
|
|
|
|
|
if(isDouble(a))
|
|
|
|
aa = a.getDouble();
|
|
|
|
else
|
|
|
|
aa = a.getLong();
|
|
|
|
|
|
|
|
if(isDouble(b))
|
|
|
|
bb = b.getDouble();
|
|
|
|
else
|
|
|
|
bb = b.getLong();
|
|
|
|
|
|
|
|
double error = aa - bb;
|
|
|
|
if(aa != 0 && bb != 0)
|
|
|
|
error /= aa;
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
return uprv_fabs(error);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
NumberFormatRoundTripTest::typeOf(const Formattable& n, UnicodeString& result)
|
|
|
|
{
|
|
|
|
if(n.getType() == Formattable::kLong) {
|
|
|
|
result = UnicodeString(" Long");
|
|
|
|
}
|
|
|
|
else if(n.getType() == Formattable::kDouble) {
|
|
|
|
result = UnicodeString(" Double");
|
|
|
|
}
|
|
|
|
else if(n.getType() == Formattable::kString) {
|
|
|
|
result = UnicodeString(" UnicodeString");
|
|
|
|
UnicodeString temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
NumberFormatRoundTripTest::escape(UnicodeString& s)
|
|
|
|
{
|
|
|
|
UnicodeString copy(s);
|
|
|
|
s.remove();
|
1999-12-08 02:11:04 +00:00
|
|
|
for(int i = 0; i < copy.length(); ++i) {
|
1999-08-16 21:50:52 +00:00
|
|
|
UChar c = copy[i];
|
|
|
|
if(c < 0x00FF)
|
|
|
|
s += c;
|
|
|
|
else {
|
|
|
|
s += "+U";
|
|
|
|
char temp[16];
|
|
|
|
sprintf(temp, "%4X", c); // might not work
|
|
|
|
s += temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
2002-09-21 00:43:14 +00:00
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|