Skip to content
Permalink
Browse files

JENKINS-43866 Don't round up 99.x% coverage to 100% (#94)

  • Loading branch information
cizezsy authored and jeffpearce committed Mar 3, 2018
1 parent 0db4272 commit c612ade943ca91ea43eb2d07d02607ece8666245
@@ -1,6 +1,7 @@
package hudson.plugins.cobertura;

import java.io.Serializable;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;

@@ -35,11 +36,19 @@ private String print(float f) {

/**
* Gets the percentage in integer.
* If float percentage is less than 100 and larger than 95.5, then return rounded down value,
* else return rounded off value
*
* @return percentage
*/
public int getPercentage() {
return Math.round(getPercentageFloat());
float floatPercentage = getPercentageFloat();
int intPercentage = Math.round(floatPercentage);
if (intPercentage == 100 && (int) floatPercentage < 100) {
return (int) floatPercentage;
} else {
return intPercentage;
}
}

/**
@@ -54,14 +63,28 @@ public float getPercentageFloat() {
}

static NumberFormat dataFormat = new DecimalFormat("000.00");
static NumberFormat roundDownDataFormat;

static {
roundDownDataFormat = new DecimalFormat("000.000");
roundDownDataFormat.setRoundingMode(RoundingMode.DOWN);
}


/**
* Gets the percentage as a formatted string used for sorting the html table
* Gets the percentage as a formatted string used for sorting the html table.
* If float percentage is less than 100 and larger than 99.995, then return rounded down value,
* else return rounded off value.
*
* @return percentage
*/
public String getPercentageString() {
return dataFormat.format(getPercentageFloat());
float floatPercentage = getPercentageFloat();
if (Float.compare(floatPercentage, 99.995f) >= 0) {
return roundDownDataFormat.format(floatPercentage);
} else {
return dataFormat.format(floatPercentage);
}
}

/**
@@ -98,12 +121,9 @@ public int hashCode() {

/**
* Creates a new instance of {@link Ratio}.
*/
/**
*
* @param x numerator
* @param y denominator
*
* @return the ratio
*/
public static Ratio create(float x, float y) {
@@ -19,6 +19,20 @@ final void assertRatio(Ratio r, float numerator, float denominator) {
* @throws Exception
*/
public void testParseValue() throws Exception {
assertRatio(Ratio.create(1,2), 1.0f, 2.0f);
assertRatio(Ratio.create(1, 2), 1.0f, 2.0f);
}

public void testGetPercentage() {
assertEquals(98, Ratio.create(246, 250).getPercentage());
assertEquals(99, Ratio.create(247, 250).getPercentage());
assertEquals(99, Ratio.create(248, 250).getPercentage());
assertEquals(99, Ratio.create(249, 250).getPercentage());
assertEquals(100, Ratio.create(250, 250).getPercentage());
}

public void testGetPercentageFloat() {
assertEquals("099.99", Ratio.create(24998, 25000).getPercentageString());
assertEquals("099.996", Ratio.create(24999, 25000).getPercentageString());
assertEquals("100.000", Ratio.create(25000, 25000).getPercentageString());
}
}

0 comments on commit c612ade

Please sign in to comment.
You can’t perform that action at this time.