Skip to content

Commit ca8a318

Browse files
authored
Fix coverage calculation (#856)
1 parent 1fe386c commit ca8a318

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

utbot-junit-contest/src/main/kotlin/org/utbot/contest/Contest.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,7 @@ fun runGeneration(
211211
logger.error("Seems like classloader for cut not valid (maybe it was backported to system): ${cut.classLoader}")
212212
}
213213

214-
val statsForClass = StatsForClass(Type.getInternalName(cut.classId.jClass))
215-
216-
val testedClasses = ConcurrentSkipListSet<String>()
214+
val statsForClass = StatsForClass()
217215

218216
val codeGenerator = CodeGenerator(
219217
cut.classId,
@@ -321,17 +319,17 @@ fun runGeneration(
321319
is UtExecution -> {
322320
try {
323321
val testMethodName = testMethodName(method.toString(), ++testsCounter)
324-
val className = method.clazz.qualifiedName
322+
val className = Type.getInternalName(method.clazz.java)
325323
logger.debug { "--new testCase collected, to generate: $testMethodName" }
326324
statsForMethod.testsGeneratedCount++
327325
result.coverage?.let {
328326
statsForClass.updateCoverage(
329327
newCoverage = it,
330-
isNewClass = !testedClasses.contains(className),
328+
isNewClass = !statsForClass.testedClassNames.contains(className),
331329
fromFuzzing = result is UtFuzzedExecution
332330
)
333331
}
334-
testedClasses.add(className)
332+
statsForClass.testedClassNames.add(className)
335333

336334
//TODO: it is a strange hack to create fake test case for one [UtResult]
337335
testSets.add(UtMethodTestSet(method, listOf(result)))

utbot-junit-contest/src/main/kotlin/org/utbot/contest/Statistics.kt

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.utbot.contest
22

33
import java.io.File
4+
import java.util.concurrent.ConcurrentSkipListSet
45
import org.utbot.common.MutableMultiset
56
import org.utbot.common.mutableMultisetOf
67
import org.utbot.framework.plugin.api.Instruction
@@ -48,21 +49,21 @@ class GlobalStats {
4849
get() = statsForClasses.count { it.failedToCompile }
4950

5051
val coveredInstructions: Int
51-
get() = statsForClasses.sumOf { it.coverage.getCoverageInfo(it.className).covered }
52+
get() = statsForClasses.sumOf { it.coverage.getCoverageInfo(it.testedClassNames).covered }
5253

5354
val coveredInstructionsByFuzzing: Int
54-
get() = statsForClasses.sumOf { it.fuzzedCoverage.getCoverageInfo(it.className).covered }
55+
get() = statsForClasses.sumOf { it.fuzzedCoverage.getCoverageInfo(it.testedClassNames).covered }
5556

5657
val coveredInstructionsByConcolic: Int
57-
get() = statsForClasses.sumOf { it.concolicCoverage.getCoverageInfo(it.className).covered }
58+
get() = statsForClasses.sumOf { it.concolicCoverage.getCoverageInfo(it.testedClassNames).covered }
5859

5960
val totalInstructions: Int
6061
get() = statsForClasses.sumOf { it.coverage.totalInstructions.toInt() }
6162

6263
val avgCoverage: Double
6364
get() = statsForClasses
6465
.filter { it.coverage.totalInstructions != 0L }
65-
.map { it.coverage.getCoverageInfo(it.className).run { 100.0 * covered / total } }
66+
.map { it.coverage.getCoverageInfo(it.testedClassNames).run { 100.0 * covered / total } }
6667
.average().run {
6768
if (isNaN()) 0.0
6869
else this
@@ -108,7 +109,9 @@ class GlobalStats {
108109
avgCoverage.format(PRECISION) + " %"
109110
}
110111

111-
class StatsForClass(val className: String) {
112+
class StatsForClass {
113+
val testedClassNames: MutableSet<String> = ConcurrentSkipListSet()
114+
112115
var methodsCount: Int = -1
113116
val statsForMethods = mutableListOf<StatsForMethod>()
114117

@@ -123,8 +126,15 @@ class StatsForClass(val className: String) {
123126
var fuzzedCoverage = CoverageInstructionsSet()
124127
var concolicCoverage = CoverageInstructionsSet()
125128

129+
/**
130+
* Add class [className] to respect coverage from this class.
131+
*/
132+
fun addTestedClass(className: String) {
133+
testedClassNames.add(className)
134+
}
135+
126136
private fun CoverageInstructionsSet.prettyInfo(): String =
127-
getCoverageInfo(className).run { "$covered/$total" }
137+
getCoverageInfo(testedClassNames).run { "$covered/$total" }
128138

129139
override fun toString(): String = "\n<StatsForClass> :" +
130140
"\n\tcanceled by timeout = $canceledByTimeout" +
@@ -191,12 +201,12 @@ data class CoverageInstructionsSet(
191201
data class CoverageStatistic(val covered: Int, val total: Int)
192202

193203
/**
194-
* Compute coverage of class named [className] with its anonymous, nested and inner classes.
204+
* Compute coverage of classes with names in [classNames].
195205
*/
196-
private fun CoverageInstructionsSet?.getCoverageInfo(className: String): CoverageStatistic = this?.run {
206+
private fun CoverageInstructionsSet?.getCoverageInfo(classNames: Set<String>): CoverageStatistic = this?.run {
197207
CoverageStatistic(
198208
coveredInstructions.filter {
199-
instr -> instr.className.startsWith(className)
209+
instr -> classNames.contains(instr.className)
200210
}.toSet().size,
201211
totalInstructions.toInt()
202212
)

0 commit comments

Comments
 (0)