Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use unified percentage calculation logic #1221

Merged
merged 1 commit into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,13 @@ public void shouldCorrectlyCalculateMutationCoverageWhenSomeMutationUndetected()

@Test
public void shouldCorrectlyCalculateTestStrengthWhenNoMutationsPresent() {
assertEquals(0, this.testee.getTestStrength());
assertEquals(100, this.testee.getTestStrength());
}

@Test
public void shouldCorrectlyCalculateTestStrengthWhenNoMutationsDetected() {
this.testee.addMutations(100);
assertEquals(0, this.testee.getTestStrength());
}

@Test
public void shouldCorrectlyCalculateTestStrengthWhenAllMutationsWithNoCoverage() {
this.testee.addMutations(100);
assertEquals(0, this.testee.getTestStrength());
assertEquals(100, this.testee.getTestStrength());
}

@Test
Expand Down