Skip to content

Commit 7e4957a

Browse files
committed
Splitting improved
1 parent d2200d8 commit 7e4957a

File tree

2 files changed

+33
-34
lines changed
  • utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree
  • utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api

2 files changed

+33
-34
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import org.utbot.framework.plugin.api.util.isPrimitive
2929
import org.utbot.framework.plugin.api.util.jClass
3030
import org.utbot.framework.plugin.api.util.longClassId
3131
import org.utbot.framework.plugin.api.util.method
32+
import org.utbot.framework.plugin.api.util.objectClassId
3233
import org.utbot.framework.plugin.api.util.primitiveTypeJvmNameOrNull
3334
import org.utbot.framework.plugin.api.util.shortClassId
3435
import org.utbot.framework.plugin.api.util.toReferenceTypeBytecodeSignature
@@ -98,26 +99,46 @@ data class UtMethodTestSet(
9899
val clustersInfo: List<Pair<UtClusterInfo?, IntRange>> = listOf(null to executions.indices)
99100
)
100101

101-
data class CgMethodTestSet internal constructor(
102+
data class CgMethodTestSet private constructor(
102103
val executableId: ExecutableId,
103-
val executions: List<UtExecution> = emptyList(),
104104
val jimpleBody: JimpleBody? = null,
105105
val errors: Map<String, Int> = emptyMap(),
106-
val clustersInfo: List<Pair<UtClusterInfo?, IntRange>> = listOf(null to executions.indices)
106+
val clustersInfo: List<Pair<UtClusterInfo?, IntRange>>,
107107
) {
108+
var executions: List<UtExecution> = emptyList()
109+
private set
110+
108111
constructor(from: UtMethodTestSet) : this(
109112
from.method.callable.executableId,
110-
from.executions,
111113
from.jimpleBody,
112114
from.errors,
113115
from.clustersInfo
114-
)
115-
}
116+
) {
117+
executions = from.executions
118+
}
116119

117-
fun CgMethodTestSet.substituteExecutions(executions: List<UtExecution>): CgMethodTestSet {
118-
return CgMethodTestSet(this.executableId, executions, this.jimpleBody, this.errors, this.clustersInfo)
120+
/**
121+
* Splits [CgMethodTestSet] into separate test sets having
122+
* unique result model [ClassId] in each subset.
123+
*/
124+
fun splitExecutionsByResult() : List<CgMethodTestSet> {
125+
val executionsByResult: Map<ClassId, List<UtExecution>> =
126+
if (executions.any { it.result is UtExecutionSuccess })
127+
executions
128+
.filter { it.result is UtExecutionSuccess }
129+
.groupBy { (it.result as UtExecutionSuccess).model.classId }
130+
else {
131+
mapOf(objectClassId to executions)
132+
}
133+
134+
return executionsByResult.map{ (_, executions) -> substituteExecutions(executions) }
135+
}
136+
137+
private fun substituteExecutions(newExecutions: List<UtExecution>): CgMethodTestSet =
138+
this.copy().apply { executions = newExecutions }
119139
}
120140

141+
121142
data class Step(
122143
val stmt: Stmt,
123144
val depth: Int,

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgTestClassConstructor.kt

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ import org.utbot.framework.codegen.model.visitor.importUtilMethodDependencies
2626
import org.utbot.framework.plugin.api.CgMethodTestSet
2727
import org.utbot.framework.plugin.api.ExecutableId
2828
import org.utbot.framework.plugin.api.MethodId
29-
import org.utbot.framework.plugin.api.UtExecutionSuccess
3029
import org.utbot.framework.plugin.api.UtMethodTestSet
31-
import org.utbot.framework.plugin.api.substituteExecutions
3230
import org.utbot.framework.plugin.api.util.description
3331
import org.utbot.framework.plugin.api.util.kClass
3432
import kotlin.reflect.KClass
@@ -85,7 +83,7 @@ internal class CgTestClassConstructor(val context: CgContext) :
8583
return null
8684
}
8785

88-
val (methodUnderTest, executions, _, _, clustersInfo) = testSet
86+
val (methodUnderTest, _, _, clustersInfo) = testSet
8987
val regions = mutableListOf<CgRegion<CgMethod>>()
9088
val requiredFields = mutableListOf<CgParameterDeclaration>()
9189

@@ -96,7 +94,7 @@ internal class CgTestClassConstructor(val context: CgContext) :
9694
emptyLineIfNeeded()
9795
for (i in executionIndices) {
9896
runCatching {
99-
currentTestCaseTestMethods += methodConstructor.createTestMethod(methodUnderTest, executions[i])
97+
currentTestCaseTestMethods += methodConstructor.createTestMethod(methodUnderTest, testSet.executions[i])
10098
}.onFailure { e -> processFailure(testSet, e) }
10199
}
102100
val clusterHeader = clusterSummary?.header
@@ -109,28 +107,8 @@ internal class CgTestClassConstructor(val context: CgContext) :
109107
}
110108
}
111109
ParametrizedTestSource.PARAMETRIZE -> {
112-
val groupedExecutions = executions
113-
.filter { it.result is UtExecutionSuccess }
114-
.groupBy { (it.result as UtExecutionSuccess).model.classId }
115-
116-
if (groupedExecutions.isEmpty()) {
117-
processParametrizedTestSet(
118-
testSet,
119-
requiredFields,
120-
regions,
121-
methodUnderTest,
122-
)
123-
} else {
124-
for (executionsGroup in groupedExecutions.values) {
125-
val testSet = testSet.substituteExecutions(executionsGroup)
126-
127-
processParametrizedTestSet(
128-
testSet,
129-
requiredFields,
130-
regions,
131-
methodUnderTest,
132-
)
133-
}
110+
for (currentTestSet in testSet.splitExecutionsByResult()) {
111+
processParametrizedTestSet(currentTestSet, requiredFields, regions, methodUnderTest)
134112
}
135113
}
136114
}

0 commit comments

Comments
 (0)