Adjust comments and add slightly more test-coverage

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1868982 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2019-10-26 05:26:52 +00:00
parent 04e85aa22d
commit 14812bee45
2 changed files with 32 additions and 7 deletions

View File

@ -61,13 +61,15 @@ public class FractionFormat extends Format {
private final int maxDenom; private final int maxDenom;
private final String wholePartFormatString; private final String wholePartFormatString;
/** /**
* Single parameter ctor * Single parameter ctor
* @param denomFormatString The format string for the denominator * @param denomFormatString The format string for the denominator
*/ */
public FractionFormat(String wholePartFormatString, String denomFormatString) { public FractionFormat(String wholePartFormatString, String denomFormatString) {
this.wholePartFormatString = wholePartFormatString; this.wholePartFormatString = wholePartFormatString;
//init exactDenom and maxDenom
// initialize exactDenom and maxDenom
Matcher m = DENOM_FORMAT_PATTERN.matcher(denomFormatString); Matcher m = DENOM_FORMAT_PATTERN.matcher(denomFormatString);
int tmpExact = -1; int tmpExact = -1;
int tmpMax = -1; int tmpMax = -1;
@ -81,7 +83,10 @@ public class FractionFormat extends Format {
tmpExact = -1; tmpExact = -1;
} }
} catch (NumberFormatException e){ } catch (NumberFormatException e){
//should never happen // should not happen because the pattern already verifies that this is a number,
// but a number larger than Integer.MAX_VALUE can cause it,
// so throw an exception if we somehow end up here
throw new IllegalStateException(e);
} }
} else if (m.group(1) != null) { } else if (m.group(1) != null) {
int len = m.group(1).length(); int len = m.group(1).length();
@ -132,7 +137,7 @@ public class FractionFormat extends Format {
return sb.toString(); return sb.toString();
} }
SimpleFraction fract = null; final SimpleFraction fract;
try { try {
//this should be the case because of the constructor //this should be the case because of the constructor
if (exactDenom > 0){ if (exactDenom > 0){

View File

@ -27,6 +27,7 @@ import java.io.InputStreamReader;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
/** /**
@ -35,15 +36,34 @@ import org.junit.Test;
*/ */
public final class TestFractionFormat { public final class TestFractionFormat {
@Test @Test
public void testSingle() throws Exception { public void testSingle() {
FractionFormat f = new FractionFormat("", "##"); FractionFormat f = new FractionFormat("", "##");
double val = 321.321; double val = 321.321;
String ret = f.format(val); String ret = f.format(val);
assertEquals("26027/81", ret); assertEquals("26027/81", ret);
} }
@Test(expected = IllegalStateException.class)
public void testInvalid() {
FractionFormat f = new FractionFormat("", "9999999999999999999999999999");
double val = 321.321;
String ret = f.format(val);
assertEquals("26027/81", ret);
}
@Ignore("Runs for some longer time")
@Test @Test
public void testWithBigWholePart() throws Exception { public void microBenchmark() {
FractionFormat f = new FractionFormat("", "##");
double val = 321.321;
for(int i = 0;i < 1000000;i++) {
String ret = f.format(val);
assertEquals("26027/81", ret);
}
}
@Test
public void testWithBigWholePart() {
FractionFormat f = new FractionFormat("#", "???/???"); FractionFormat f = new FractionFormat("#", "???/???");
assertEquals("10100136259702", f.format(10100136259702d)); assertEquals("10100136259702", f.format(10100136259702d));