6469160: (fmt) general (%g) formatting of zero (0.0) with precision 0 or 1 throws ArrayOutOfBoundsException

For zero value ensure than an unpadded zero character is passed to Formatter.addZeros()

Reviewed-by: iris, darcy
This commit is contained in:
Brian Burkhalter 2013-06-24 14:17:14 -07:00 committed by Brian Burkhalter
parent 650898839b
commit 9c0605b8e6
9 changed files with 95 additions and 6 deletions

View File

@ -3297,18 +3297,29 @@ public final class Formatter implements Closeable, Flushable {
else if (precision == 0)
prec = 1;
FormattedFloatingDecimal fd
char[] exp;
char[] mant;
int expRounded;
if (value == 0.0) {
exp = null;
mant = new char[] {'0'};
expRounded = 0;
} else {
FormattedFloatingDecimal fd
= FormattedFloatingDecimal.valueOf(value, prec,
FormattedFloatingDecimal.Form.GENERAL);
exp = fd.getExponent();
mant = fd.getMantissa();
expRounded = fd.getExponentRounded();
}
char[] exp = fd.getExponent();
if (exp != null) {
prec -= 1;
} else {
prec = prec - (value == 0 ? 0 : fd.getExponentRounded()) - 1;
prec -= expRounded + 1;
}
char[] mant = addZeros(fd.getMantissa(), prec);
mant = addZeros(mant, prec);
// If the precision is zero and the '#' flag is set, add the
// requested decimal point.
if (f.contains(Flags.ALTERNATE) && (prec == 0))

View File

@ -1045,7 +1045,7 @@ public class FloatingDecimal{
this.nDigits = n;
}
/*
/**
* Takes a FloatingDecimal, which we presumably just scanned in,
* and finds out what its value is, as a double.
*

View File

@ -1177,6 +1177,13 @@ public class Basic$Type$ extends Basic {
test("%.3G", "1.00E-05", recip(create(100000.0)));
test("%.3G", "-1.00E-05", recip(create(-100000.0)));
test("%.1g", "-0", -0.0);
test("%3.0g", " -0", -0.0);
test("%.1g", "0", 0.0);
test("%3.0g", " 0", 0.0);
test("%.1g", "0", +0.0);
test("%3.0g", " 0", +0.0);
test("%3.0g", "1e-06", 0.000001);
test("%3.0g", "1e-05", 0.00001);
test("%3.0g", "1e-05", 0.0000099);

View File

@ -25,7 +25,7 @@
* @summary Unit test for formatter
* @bug 4906370 4962433 4973103 4989961 5005818 5031150 4970931 4989491 5002937
* 5005104 5007745 5061412 5055180 5066788 5088703 6317248 6318369 6320122
* 6344623 6369500 6534606 6282094 6286592 6476425 5063507
* 6344623 6369500 6534606 6282094 6286592 6476425 5063507 6469160
*
* @run shell/timeout=240 Basic.sh
*/

View File

@ -1177,6 +1177,13 @@ public class BasicBigDecimal extends Basic {
test("%.3G", "1.00E-05", recip(create(100000.0)));
test("%.3G", "-1.00E-05", recip(create(-100000.0)));
test("%.1g", "-0", -0.0);
test("%3.0g", " -0", -0.0);
test("%.1g", "0", 0.0);
test("%3.0g", " 0", 0.0);
test("%.1g", "0", +0.0);
test("%3.0g", " 0", +0.0);
test("%3.0g", "1e-06", 0.000001);
test("%3.0g", "1e-05", 0.00001);
test("%3.0g", "1e-05", 0.0000099);

View File

@ -1113,6 +1113,15 @@ public class BasicDouble extends Basic {
@ -1168,6 +1177,13 @@ public class BasicDouble extends Basic {
test("%.3G", "1.00E-05", recip(create(100000.0)));
test("%.3G", "-1.00E-05", recip(create(-100000.0)));
test("%.1g", "-0", -0.0);
test("%3.0g", " -0", -0.0);
test("%.1g", "0", 0.0);
test("%3.0g", " 0", 0.0);
test("%.1g", "0", +0.0);
test("%3.0g", " 0", +0.0);
test("%3.0g", "1e-06", 0.000001);
test("%3.0g", "1e-05", 0.00001);
test("%3.0g", "1e-05", 0.0000099);

View File

@ -1113,6 +1113,15 @@ public class BasicDoubleObject extends Basic {
@ -1168,6 +1177,13 @@ public class BasicDoubleObject extends Basic {
test("%.3G", "1.00E-05", recip(create(100000.0)));
test("%.3G", "-1.00E-05", recip(create(-100000.0)));
test("%.1g", "-0", -0.0);
test("%3.0g", " -0", -0.0);
test("%.1g", "0", 0.0);
test("%3.0g", " 0", 0.0);
test("%.1g", "0", +0.0);
test("%3.0g", " 0", +0.0);
test("%3.0g", "1e-06", 0.000001);
test("%3.0g", "1e-05", 0.00001);
test("%3.0g", "1e-05", 0.0000099);

View File

@ -1096,6 +1096,15 @@ public class BasicFloat extends Basic {
@ -1168,6 +1177,13 @@ public class BasicFloat extends Basic {
test("%.3G", "1.00E-05", recip(create(100000.0)));
test("%.3G", "-1.00E-05", recip(create(-100000.0)));
test("%.1g", "-0", -0.0);
test("%3.0g", " -0", -0.0);
test("%.1g", "0", 0.0);
test("%3.0g", " 0", 0.0);
test("%.1g", "0", +0.0);
test("%3.0g", " 0", +0.0);
test("%3.0g", "1e-06", 0.000001);
test("%3.0g", "1e-05", 0.00001);
test("%3.0g", "1e-05", 0.0000099);

View File

@ -1129,6 +1129,15 @@ public class BasicFloatObject extends Basic {
@ -1168,6 +1177,13 @@ public class BasicFloatObject extends Basic {
test("%.3G", "1.00E-05", recip(create(100000.0)));
test("%.3G", "-1.00E-05", recip(create(-100000.0)));
test("%.1g", "-0", -0.0);
test("%3.0g", " -0", -0.0);
test("%.1g", "0", 0.0);
test("%3.0g", " 0", 0.0);
test("%.1g", "0", +0.0);
test("%3.0g", " 0", +0.0);
test("%3.0g", "1e-06", 0.000001);
test("%3.0g", "1e-05", 0.00001);
test("%3.0g", "1e-05", 0.0000099);