ICU-13634 Fixes for NumberFormatTest/TestExponential.

X-SVN-Rev: 41177
This commit is contained in:
Shane Carr 2018-03-30 07:22:24 +00:00
parent af4435133a
commit 02669ad1bc
6 changed files with 21 additions and 14 deletions

View File

@ -768,17 +768,14 @@ Formattable::adoptDecimalQuantity(DecimalQuantity *dq) {
}
// Set the value into the Union of simple type values.
// Cannot use the set() functions because they would delete the fDecimalNum value,
// TODO: fDecimalQuantity->fitsInInt() to kLong type.
/*
if (fDecimalQuantity->fitsInInt()) {
fType = kLong;
fValue.fInt64 = fDecimalNum->getLong();
} else
*/
// Cannot use the set() functions because they would delete the fDecimalNum value.
if (fDecimalQuantity->fitsInLong()) {
fType = kInt64;
fValue.fInt64 = fDecimalQuantity->toLong();
if (fValue.fInt64 <= INT32_MAX && fValue.fInt64 >= INT32_MIN) {
fType = kLong;
} else {
fType = kInt64;
}
} else {
fType = kDouble;
fValue.fDouble = fDecimalQuantity->toDouble();

View File

@ -202,6 +202,10 @@ void DecimalQuantity::multiplyBy(int32_t multiplicand) {
setToDouble(temp);
}
void DecimalQuantity::negate() {
flags ^= NEGATIVE_FLAG;
}
int32_t DecimalQuantity::getMagnitude() const {
U_ASSERT(precision != 0);
return scale + precision - 1;

View File

@ -95,6 +95,9 @@ class U_I18N_API DecimalQuantity : public IFixedDecimal, public UMemory {
*/
void multiplyBy(int32_t multiplicand);
/** Flips the sign from positive to negative and back. C++-only: not currently needed in Java. */
void negate();
/**
* Scales the number by a power of ten. For example, if the value is currently "1234.56", calling
* this method with delta=-3 will change the value to "1.23456".

View File

@ -108,7 +108,7 @@ void ParsedNumber::populateFormattable(Formattable& output) const {
// All other numbers
LocalPointer<DecimalQuantity> actualQuantity(new DecimalQuantity(quantity));
if (0 != (flags & FLAG_NEGATIVE)) {
actualQuantity->multiplyBy(-1);
actualQuantity->negate();
}
output.adoptDecimalQuantity(actualQuantity.orphan());
}

View File

@ -296,6 +296,7 @@ void DecimalQuantityTest::testHardDoubleConversion() {
}
void DecimalQuantityTest::testToDouble() {
IcuTestErrorCode status(*this, "testToDouble");
static const struct TestCase {
const char* input; // char* for the decNumber constructor
double expected;
@ -304,8 +305,9 @@ void DecimalQuantityTest::testToDouble() {
{ "-3.142E-271", -3.142e-271 } };
for (auto& cas : cases) {
status.setScope(cas.input);
DecimalQuantity q;
q.setToDecNumber({cas.input, -1});
q.setToDecNumber({cas.input, -1}, status);
double actual = q.toDouble();
assertEquals("Doubles should exactly equal", cas.expected, actual);
}

View File

@ -927,7 +927,7 @@ NumberFormatTest::TestExponential(void)
#endif
}
else {
errln((UnicodeString)"FAIL: Non-numeric Formattable returned");
errln(UnicodeString("FAIL: Non-numeric Formattable returned: ") + pattern + " " + s);
continue;
}
if (pos.getIndex() == s.length())
@ -938,7 +938,8 @@ NumberFormatTest::TestExponential(void)
(uprv_fabs(a - valParse[v+ival]) / a > (2*DBL_EPSILON))) ||
(!useEpsilon && a != valParse[v+ival]))
{
errln((UnicodeString)"FAIL: Expected " + valParse[v+ival]);
errln((UnicodeString)"FAIL: Expected " + valParse[v+ival] + " but got " + a
+ " on input " + s);
}
}
else {
@ -965,7 +966,7 @@ NumberFormatTest::TestExponential(void)
{
logln((UnicodeString)" -parse-> " + a);
if (a != lvalParse[v+ilval])
errln((UnicodeString)"FAIL: Expected " + lvalParse[v+ilval]);
errln((UnicodeString)"FAIL: Expected " + lvalParse[v+ilval] + " but got " + a);
}
else
errln((UnicodeString)"FAIL: Partial parse (" + pos.getIndex() + " chars) -> " + a);