Skip to content

Commit

Permalink
Use unified percentage calculation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Vampire committed May 26, 2023
1 parent 61fff8e commit 0ee4f1e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.pitest.coverage;

import static org.pitest.util.PercentageCalculator.getPercentage;

/**
* Basic summary of line coverage data
*/
Expand All @@ -22,8 +24,7 @@ public int getNumberOfCoveredLines() {
}

public int getCoverage() {
return this.numberOfLines == 0 ? 100 : Math
.round((100f * this.numberOfCoveredLines) / this.numberOfLines);
return getPercentage(numberOfLines, numberOfCoveredLines);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Locale;
import java.util.Set;

import static org.pitest.util.PercentageCalculator.getPercentage;

public final class MutationStatistics {
private final Iterable<Score> scores;
private final long totalMutations;
Expand Down Expand Up @@ -73,21 +75,8 @@ public Set<ClassName> mutatedClasses() {
return mutatedClasses;
}

public long getPercentageDetected() {
if (getTotalMutations() == 0) {
return 100;
}

if (getTotalDetectedMutations() == 0) {
return 0;
}

if (getTotalMutations() == getTotalDetectedMutations()) {
return 100;
}

return Math.min(99, Math.round((100f / getTotalMutations())
* getTotalDetectedMutations()));
public int getPercentageDetected() {
return getPercentage(getTotalMutations(), getTotalDetectedMutations());
}

public void report(final PrintStream out) {
Expand All @@ -98,7 +87,7 @@ public void report(final PrintStream out) {
+ ". Test strength " + this.getTestStrength() + "%");
out.println(">> Ran " + this.numberOfTestsRun + " tests ("
+ getTestsPerMutation() + " tests per mutation)");

out.println("Enhanced functionality available at https://www.arcmutate.com/");
}

Expand All @@ -113,16 +102,7 @@ private String getTestsPerMutation() {
.format(testsPerMutation);
}

public long getTestStrength() {
if (getTotalMutations() == 0) {
return 100;
}

if (getTotalMutationsWithCoverage() == 0) {
return 0;
}

return Math.round((100f / getTotalMutationsWithCoverage())
* getTotalDetectedMutations());
public int getTestStrength() {
return getPercentage(getTotalMutationsWithCoverage(), getTotalDetectedMutations());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import java.io.PrintStream;

import static org.pitest.util.PercentageCalculator.getPercentage;

public final class Score {

private final String mutatorName;
Expand Down Expand Up @@ -67,16 +69,7 @@ public long getTotalWithCoverage() {
}

public int getPercentageDetected() {
if (getTotalMutations() == 0) {
return 100;
}

if (getTotalDetectedMutations() == 0) {
return 0;
}

return Math.round((100f / getTotalMutations())
* getTotalDetectedMutations());
return getPercentage(getTotalMutations(), getTotalDetectedMutations());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.pitest.util;

public class PercentageCalculator {
public static int getPercentage(long total, long actual) {
if (total == 0) {
return 100;
}

if (actual == 0) {
return 0;
}

if (total == actual) {
return 100;
}

return Math.min(99, Math.round((100f / total) * actual));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.pitest.util;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.pitest.util.PercentageCalculator.getPercentage;

public class PercentageCalculatorTest {
@Test
public void shouldNotHaveHundredPercentIfNotAll() {
assertEquals(99, getPercentage(2000, 1999));
}

@Test
public void shouldHaveHundredPercentIfAll() {
assertEquals(100, getPercentage(2000, 2000));
}

@Test
public void shouldHaveHundredPercentIfNoTotal() {
assertEquals(100, getPercentage(0, 0));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.pitest.mutationtest.report.html;

import static org.pitest.util.PercentageCalculator.getPercentage;

public class MutationTotals {

private long numberOfFiles = 0;
Expand Down Expand Up @@ -50,24 +52,19 @@ public void addMutationsDetetcted(final long mutationsKilled) {
}

public int getLineCoverage() {
return this.numberOfLines == 0 ? 100 : Math
.round((100f * this.numberOfLinesCovered) / this.numberOfLines);
return getPercentage(numberOfLines, numberOfLinesCovered);
}

public int getMutationCoverage() {
return this.numberOfMutations == 0 ? 100
: Math.round((100f * this.numberOfMutationsDetected)
/ this.numberOfMutations);
return getPercentage(numberOfMutations, numberOfMutationsDetected);
}

public void addMutationsWithCoverage(final long mutationsWithCoverage) {
this.numberOfMutationsWithCoverage += mutationsWithCoverage;
}

public int getTestStrength() {
return this.numberOfMutationsWithCoverage == 0 ? 0
: Math.round((100f * this.numberOfMutationsDetected)
/ this.numberOfMutationsWithCoverage);
return getPercentage(numberOfMutationsWithCoverage, numberOfMutationsDetected);
}

public long getNumberOfMutationsWithCoverage() {
Expand All @@ -86,4 +83,4 @@ private void add(final long lines, final long files, final MutationTotals data)
this.addMutationsDetetcted(data.getNumberOfMutationsDetected());
this.addMutationsWithCoverage(data.getNumberOfMutationsWithCoverage());
}
}
}

0 comments on commit 0ee4f1e

Please sign in to comment.