Skip to content
Closed
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
20 changes: 20 additions & 0 deletions android/app/src/test/java/com/clhs/score/data/GradeAnalysisTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ class GradeAnalysisTest {
assertEquals("前 27%", percentile(60.0, 219)?.percentLabel)
}

@Test
fun percentileReturnsNullForInvalidOrMissingInputs() {
assertNull(percentile(null, 37))
assertNull(percentile(13.0, null))
assertNull(percentile(13.0, 0))
assertNull(percentile(13.0, -5))
assertNull(percentile(0.0, 37))
assertNull(percentile(-5.0, 37))
}

@Test
fun percentileCoercesToValidRangeWhenRankExceedsCount() {
assertEquals(100, percentile(40.0, 37)?.topPercent)
}
Comment on lines +26 to +29

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The test asserts that percentile(40.0, 37) returns a valid PercentileInfo with topPercent = 100. However, a rank of 40 in a class of 37 (rank > count) is an invalid state. Allowing this to succeed means the UI might display an invalid rank label like "40/37".

Consider whether percentile should return null when rank > count to prevent displaying invalid data. If so, you can update percentile in GradeAnalysis.kt as follows:

fun percentile(rank: Double?, count: Int?): PercentileInfo? {
    if (rank == null || count == null || count <= 0 || rank > count) return null
    // ...
}

And update this test to assert assertNull instead.

Suggested change
@Test
fun percentileCoercesToValidRangeWhenRankExceedsCount() {
assertEquals(100, percentile(40.0, 37)?.topPercent)
}
@Test
fun percentileReturnsNullWhenRankExceedsCount() {
assertNull(percentile(40.0, 37))
}


@Test
fun percentileCoercesToValidRangeForTopRank() {
assertEquals(1, percentile(1.0, 1000)?.topPercent)
}

@Test
fun strengthAndWeaknessAreSortedByClassAverageDiff() {
val analysis = buildGradeAnalysis(MockGradeSystem.generateReport(StudentScenario.NORMAL))
Expand Down
Loading