From 0ee4f1e7063159b3c838316cda0cc04d87ecca49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Kautler?= Date: Fri, 26 May 2023 12:09:52 +0200 Subject: [PATCH] Use unified percentage calculation logic --- .../org/pitest/coverage/CoverageSummary.java | 5 +-- .../statistics/MutationStatistics.java | 34 ++++--------------- .../pitest/mutationtest/statistics/Score.java | 13 ++----- .../org/pitest/util/PercentageCalculator.java | 19 +++++++++++ .../pitest/util/PercentageCalculatorTest.java | 23 +++++++++++++ .../report/html/MutationTotals.java | 15 ++++---- 6 files changed, 61 insertions(+), 48 deletions(-) create mode 100644 pitest-entry/src/main/java/org/pitest/util/PercentageCalculator.java create mode 100644 pitest-entry/src/test/java/org/pitest/util/PercentageCalculatorTest.java diff --git a/pitest-entry/src/main/java/org/pitest/coverage/CoverageSummary.java b/pitest-entry/src/main/java/org/pitest/coverage/CoverageSummary.java index 118bd1661..0662e97d6 100644 --- a/pitest-entry/src/main/java/org/pitest/coverage/CoverageSummary.java +++ b/pitest-entry/src/main/java/org/pitest/coverage/CoverageSummary.java @@ -1,5 +1,7 @@ package org.pitest.coverage; +import static org.pitest.util.PercentageCalculator.getPercentage; + /** * Basic summary of line coverage data */ @@ -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); } } diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/statistics/MutationStatistics.java b/pitest-entry/src/main/java/org/pitest/mutationtest/statistics/MutationStatistics.java index dced00cf4..78220ca99 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/statistics/MutationStatistics.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/statistics/MutationStatistics.java @@ -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 scores; private final long totalMutations; @@ -73,21 +75,8 @@ public Set 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) { @@ -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/"); } @@ -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()); } } diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/statistics/Score.java b/pitest-entry/src/main/java/org/pitest/mutationtest/statistics/Score.java index a8643a732..8459792e0 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/statistics/Score.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/statistics/Score.java @@ -16,6 +16,8 @@ import java.io.PrintStream; +import static org.pitest.util.PercentageCalculator.getPercentage; + public final class Score { private final String mutatorName; @@ -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()); } } diff --git a/pitest-entry/src/main/java/org/pitest/util/PercentageCalculator.java b/pitest-entry/src/main/java/org/pitest/util/PercentageCalculator.java new file mode 100644 index 000000000..4fe4be808 --- /dev/null +++ b/pitest-entry/src/main/java/org/pitest/util/PercentageCalculator.java @@ -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)); + } +} diff --git a/pitest-entry/src/test/java/org/pitest/util/PercentageCalculatorTest.java b/pitest-entry/src/test/java/org/pitest/util/PercentageCalculatorTest.java new file mode 100644 index 000000000..0037a088e --- /dev/null +++ b/pitest-entry/src/test/java/org/pitest/util/PercentageCalculatorTest.java @@ -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)); + } +} diff --git a/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/MutationTotals.java b/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/MutationTotals.java index cd852546f..2b02fb033 100644 --- a/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/MutationTotals.java +++ b/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/MutationTotals.java @@ -1,5 +1,7 @@ package org.pitest.mutationtest.report.html; +import static org.pitest.util.PercentageCalculator.getPercentage; + public class MutationTotals { private long numberOfFiles = 0; @@ -50,14 +52,11 @@ 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) { @@ -65,9 +64,7 @@ public void addMutationsWithCoverage(final long mutationsWithCoverage) { } public int getTestStrength() { - return this.numberOfMutationsWithCoverage == 0 ? 0 - : Math.round((100f * this.numberOfMutationsDetected) - / this.numberOfMutationsWithCoverage); + return getPercentage(numberOfMutationsWithCoverage, numberOfMutationsDetected); } public long getNumberOfMutationsWithCoverage() { @@ -86,4 +83,4 @@ private void add(final long lines, final long files, final MutationTotals data) this.addMutationsDetetcted(data.getNumberOfMutationsDetected()); this.addMutationsWithCoverage(data.getNumberOfMutationsWithCoverage()); } -} \ No newline at end of file +}