ICU-5491 fixed a problem with negative multipliers, added tests for it

X-SVN-Rev: 23351
This commit is contained in:
Andrew J Macheret 2008-02-05 04:25:48 +00:00
parent 00e3cac87e
commit 7a0b18985e
2 changed files with 26 additions and 8 deletions

View File

@ -640,9 +640,9 @@ DecimalFormat::format(int64_t number,
// check for this before multiplying, and if it happens we use doubles
// instead, trading off accuracy for range.
if (fRoundingIncrement != NULL
|| (fMultiplier != 0 && (number > (U_INT64_MAX / fMultiplier)
|| number < (U_INT64_MIN / fMultiplier)
|| number == U_INT64_MIN && fMultiplier < 0)))
|| (fMultiplier > 0 && (number > U_INT64_MAX / fMultiplier || number < U_INT64_MIN / fMultiplier))
|| (fMultiplier < 0 && (number == U_INT64_MIN || -number > U_INT64_MAX / -fMultiplier || -number < U_INT64_MIN / -fMultiplier))
)
{
digits.set(((double) number) * fMultiplier,
precision(FALSE),

View File

@ -1,6 +1,6 @@
/********************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2007, International Business Machines Corporation and
* Copyright (c) 1997-2008, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/* Modification History:
@ -2438,11 +2438,29 @@ void NumberFormatTest::TestNonpositiveMultiplier() {
expect(df, "1.2", -1.2);
expect(df, "-1.2", 1.2);
// TODO: comment once BigInteger is ported
// TODO: change all the following int64_t tests once BigInteger is ported
// (right now the big numbers get turned into doubles and lose tons of accuracy)
expect(df, U_INT64_MIN, "9223372036854780000");
char* posOutOfRange = "9223372036854780000";
char* negOutOfRange = "-9223372036854780000";
// TODO: uncomment (and fix up) once BigInteger is ported and DecimalFormat can handle it
expect(df, U_INT64_MIN, posOutOfRange);
expect(df, U_INT64_MIN+1, "9223372036854775807");
expect(df, (int64_t)-123, "123");
expect(df, (int64_t)123, "-123");
expect(df, U_INT64_MAX-1, "-9223372036854775806");
expect(df, U_INT64_MAX, "-9223372036854775807");
df.setMultiplier(-2);
expect(df, -(U_INT64_MIN/2)-1, "-9223372036854775806");
expect(df, -(U_INT64_MIN/2), "-9223372036854775808");
expect(df, -(U_INT64_MIN/2)+1, negOutOfRange);
df.setMultiplier(-7);
expect(df, -(U_INT64_MAX/7)-1, posOutOfRange);
expect(df, -(U_INT64_MAX/7), "9223372036854775807");
expect(df, -(U_INT64_MAX/7)+1, "9223372036854775800");
// TODO: uncomment (and fix up) all the following int64_t tests once BigInteger is ported
// (right now the big numbers get turned into doubles and lose tons of accuracy)
//expect2(df, U_INT64_MAX, Int64ToUnicodeString(-U_INT64_MAX));
//expect2(df, U_INT64_MIN, UnicodeString(Int64ToUnicodeString(U_INT64_MIN), 1));