Skip to content

Commit

Permalink
Merge pull request #392 from uhafner/scale-max-score
Browse files Browse the repository at this point in the history
Scale scores that are based on percentages by maxScore
  • Loading branch information
uhafner authored Nov 3, 2024
2 parents 8bf89f9 + e9ea8d6 commit 17f1039
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/main/java/edu/hm/hafner/grading/CoverageScore.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ public int getImpact() {

int change = 0;

change = change + configuration.getMissedPercentageImpact() * getMissedPercentage();
change = change + configuration.getCoveredPercentageImpact() * getCoveredPercentage();
change = change + scale(configuration.getMissedPercentageImpact(), getMissedPercentage());
change = change + scale(configuration.getCoveredPercentageImpact(), getCoveredPercentage());

return change;
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/edu/hm/hafner/grading/Score.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ public int getPercentage() {
return MAX_PERCENTAGE;

Check warning on line 125 in src/main/java/edu/hm/hafner/grading/Score.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Mutation survived

One mutation survived in line 125 (PrimitiveReturnsMutator)
Raw output
Survived mutations:
- replaced int return with 0 for edu/hm/hafner/grading/Score::getPercentage (org.pitest.mutationtest.engine.gregor.mutators.returns.PrimitiveReturnsMutator)
}

protected int scale(final int impact, final int percentage) {
var ratio = getMaxScore() / 100.0d;

return Math.toIntExact(Math.round(ratio * impact * percentage));
}

/**
* Renders a short summary text of the specific score.
*
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/edu/hm/hafner/grading/TestScore.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ public int getImpact() {
change = change + configuration.getSkippedImpact() * getSkippedSize();
}
else {
change = change + configuration.getSuccessRateImpact() * getSuccessRate();
change = change + configuration.getFailureRateImpact() * getFailureRate();
change = change + scale(configuration.getSuccessRateImpact(), getSuccessRate());
change = change + scale(configuration.getFailureRateImpact(), getFailureRate());
}
return change;
}
Expand Down
26 changes: 24 additions & 2 deletions src/test/java/edu/hm/hafner/grading/CoverageScoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ void shouldCalculateTotalImpact() {
assertThat(coverageScore).hasImpact(296).hasValue(100);
}

@Test
void shouldScaleImpactWithMaxScore() {
var coverageConfiguration = createCoverageConfiguration(0, 1, 50);
var coverageScore = new CoverageScoreBuilder()
.withConfiguration(coverageConfiguration)
.withReport(createReport(Metric.LINE, "50/100"), Metric.LINE)
.build();

assertThat(coverageScore).hasImpact(25).hasValue(25);
}

@Test
void shouldScaleImpactWithLargerMaxScore() {
var coverageConfiguration = createCoverageConfiguration(0, 1, 200);
var coverageScore = new CoverageScoreBuilder()
.withConfiguration(coverageConfiguration)
.withReport(createReport(Metric.LINE, "50/100"), Metric.LINE)
.build();

assertThat(coverageScore).hasImpact(100).hasValue(100);
}

@Test
void shouldCreateSubScores() {
var first = new CoverageScoreBuilder()
Expand All @@ -127,10 +149,10 @@ void shouldCreateSubScores() {
.hasOnlySubScores(first, second);

var overflow = new CoverageScoreBuilder()
.withConfiguration(createCoverageConfiguration(0, 1, 5))
.withConfiguration(createCoverageConfiguration(0, 20, 100))
.withScores(List.of(first, second))
.build();
assertThat(overflow).hasImpact(10).hasValue(5);
assertThat(overflow).hasImpact(200).hasValue(100);
}

private CoverageConfiguration createCoverageConfiguration(final int missedImpact, final int coveredImpact) {
Expand Down
71 changes: 71 additions & 0 deletions src/test/java/edu/hm/hafner/grading/TestScoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,77 @@ void shouldUseRelativeValues() {
.hasImpact(100).hasValue(100);
}

@Test
void shouldScaleRelativeValuesWithMaxScore() {
var configuration = createConfiguration("""
{
"tests": {
"tools": [
{
"id": "tests",
"name": "Tests",
"pattern": "target/tests.xml"
}
],
"maxScore": 50,
"successRateImpact": 1,
"failureRateImpact": 0
}
}
""");

var builder = new TestScoreBuilder()
.withId(ID)
.withName(NAME)
.withConfiguration(configuration);
var ten = builder.withReport(createTestReport(2, 3, 5)).build();
assertThat(ten)
.hasId(ID).hasName(NAME).hasConfiguration(configuration)
.hasFailedSize(5).hasSkippedSize(3).hasTotalSize(10)
.hasMaxScore(50)
.hasImpact(10)
.hasValue(10);

assertThat(builder.withReport(createTestReport(12, 3, 5)).build())
.hasImpact(30).hasValue(30);
assertThat(builder.withReport(createTestReport(95, 5, 0)).build())
.hasImpact(48).hasValue(48);
assertThat(builder.withReport(createTestReport(100, 0, 0)).build())
.hasImpact(50).hasValue(50);
}

@Test
void shouldScaleRelativeValuesWithLargerMaxScore() {
var configuration = createConfiguration("""
{
"tests": {
"tools": [
{
"id": "tests",
"name": "Tests",
"pattern": "target/tests.xml"
}
],
"maxScore": 200,
"successRateImpact": 1,
"failureRateImpact": 0
}
}
""");

var builder = new TestScoreBuilder()
.withId(ID)
.withName(NAME)
.withConfiguration(configuration);
var ten = builder.withReport(createTestReport(2, 3, 5)).build();
assertThat(ten)
.hasId(ID).hasName(NAME).hasConfiguration(configuration)
.hasFailedSize(5).hasSkippedSize(3).hasTotalSize(10)
.hasMaxScore(200)
.hasImpact(40)
.hasValue(40);
}

@Test
void shouldUseRelativeFailureValues() {
var configuration = createConfiguration("""
Expand Down

0 comments on commit 17f1039

Please sign in to comment.