Skip to content

Commit 31dd74d

Browse files
Supporting arrays classId in Contests (#2680)
* Try to get classId from arrays with JacoDb * Fix a bug * Add primitive arrays to a list
1 parent 092a27a commit 31dd74d

File tree

6 files changed

+29
-17
lines changed

6 files changed

+29
-17
lines changed

utbot-junit-contest/src/main/kotlin/org/utbot/contest/usvm/ContestUsvm.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ fun runUsvmGeneration(
221221
try {
222222
JcToUtExecutionConverter(
223223
jcExecution = it,
224+
jcClasspath = jcDbContainer.cp,
224225
idGenerator = idGenerator,
225226
instructionIdProvider = instructionIdProvider,
226227
utilMethodProvider = codeGenerator.context.utilMethodProvider

utbot-junit-contest/src/main/kotlin/org/utbot/contest/usvm/ConverterUtils.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package org.utbot.contest.usvm
22

33
import org.jacodb.analysis.library.analyzers.thisInstance
44
import org.jacodb.api.JcClassOrInterface
5+
import org.jacodb.api.JcClasspath
56
import org.jacodb.api.JcField
67
import org.jacodb.api.JcMethod
78
import org.jacodb.api.JcType
89
import org.jacodb.api.TypeName
10+
import org.jacodb.api.ext.findClassOrNull
911
import org.usvm.instrumentation.testcase.api.UTestInst
1012
import org.usvm.instrumentation.testcase.descriptor.UTestObjectDescriptor
1113
import org.usvm.instrumentation.testcase.descriptor.UTestValueDescriptor
@@ -21,15 +23,17 @@ import org.utbot.framework.plugin.api.util.id
2123
import org.utbot.framework.plugin.api.util.objectClassId
2224
import org.utbot.framework.plugin.api.util.utContext
2325

24-
fun JcMethod.toExecutableId(): ExecutableId {
26+
fun JcMethod.toExecutableId(classpath: JcClasspath): ExecutableId {
2527
val type = this.thisInstance.type.classId
26-
val parameters = this.parameters.map { it.type.classId }
28+
val parameters = this.parameters.map { it.type.findClassId(classpath) }
2729

2830
if (isConstructor) {
2931
return ConstructorId(type, parameters)
3032
}
3133

32-
return MethodId(type, this.name, this.returnType.classId, parameters)
34+
val returnClassId = this.returnType.findClassId(classpath)
35+
36+
return MethodId(type, this.name, returnClassId, parameters)
3337
}
3438

3539
val JcType?.classId: ClassId
@@ -39,9 +43,9 @@ val JcType?.classId: ClassId
3943
val JcClassOrInterface.classId: ClassId
4044
get() = this.toJavaClass(utContext.classLoader).id
4145

42-
//TODO usvm-sbft: incorrectly converts types of com.google.common.util.concurrent.AtomicDoubleArray.<init> parameters
43-
val TypeName.classId: ClassId
44-
get() = ClassId(this.typeName)
46+
fun TypeName.findClassId(classpath: JcClasspath): ClassId =
47+
classpath.findTypeOrNull(this.typeName)?.classId
48+
?: error("Can not construct classId for $this")
4549

4650
val JcField.fieldId: FieldId
4751
get() = toJavaField(utContext.classLoader)!!.fieldId

utbot-junit-contest/src/main/kotlin/org/utbot/contest/usvm/JcToUtExecutionConverter.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.utbot.contest.usvm
22

33
import mu.KotlinLogging
44
import org.jacodb.api.JcClassOrInterface
5+
import org.jacodb.api.JcClasspath
56
import org.jacodb.api.JcTypedMethod
67
import org.jacodb.api.cfg.JcInst
78
import org.jacodb.api.ext.jcdbSignature
@@ -36,6 +37,7 @@ private val logger = KotlinLogging.logger {}
3637

3738
class JcToUtExecutionConverter(
3839
private val jcExecution: JcExecution,
40+
private val jcClasspath: JcClasspath,
3941
private val idGenerator: IdGenerator<Int>,
4042
private val instructionIdProvider: InstructionIdProvider,
4143
utilMethodProvider: UtilMethodProvider,
@@ -45,10 +47,10 @@ class JcToUtExecutionConverter(
4547
private var jcToUtModelConverter: JcToUtModelConverter
4648

4749
init {
48-
val instToModelConverter = UTestInst2UtModelConverter(idGenerator, utilMethodProvider)
50+
val instToModelConverter = UTestInst2UtModelConverter(idGenerator, jcClasspath, utilMethodProvider)
4951

5052
instToModelConverter.processUTest(jcExecution.uTest)
51-
jcToUtModelConverter = JcToUtModelConverter(idGenerator, instToModelConverter)
53+
jcToUtModelConverter = JcToUtModelConverter(idGenerator, jcClasspath, instToModelConverter)
5254
}
5355

5456
fun convert(): UtExecution? {
@@ -129,7 +131,7 @@ class JcToUtExecutionConverter(
129131
.associate { (jcField, uTestDescr) ->
130132
jcField.fieldId to modelConverter.convert(uTestDescr)
131133
}
132-
val executableId: ExecutableId = method.method.toExecutableId()
134+
val executableId: ExecutableId = method.method.toExecutableId(jcClasspath)
133135
return EnvironmentModels(thisInstance, parameters, statics, executableId)
134136
}
135137

utbot-junit-contest/src/main/kotlin/org/utbot/contest/usvm/JcToUtModelConverter.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.utbot.contest.usvm
22

3+
import org.jacodb.api.JcClasspath
34
import org.usvm.instrumentation.testcase.api.UTestExpression
45
import org.usvm.instrumentation.testcase.descriptor.UTestArrayDescriptor
56
import org.usvm.instrumentation.testcase.descriptor.UTestClassDescriptor
@@ -27,6 +28,7 @@ import java.lang.Throwable
2728

2829
class JcToUtModelConverter(
2930
private val idGenerator: IdGenerator<Int>,
31+
private val jcClasspath: JcClasspath,
3032
private val instToUtModelConverter: UTestInst2UtModelConverter,
3133
) {
3234
private val descriptorToModelCache = mutableMapOf<UTestValueDescriptor, UtModel>()
@@ -57,7 +59,7 @@ class JcToUtModelConverter(
5759
fields += valueDescriptor.fields
5860
.entries
5961
.associate { (jcField, fieldDescr) ->
60-
val fieldId = FieldId(jcField.type.classId, jcField.name)
62+
val fieldId = FieldId(jcField.type.findClassId(jcClasspath), jcField.name)
6163
val fieldModel = convert(fieldDescr)
6264
fieldId to fieldModel
6365
}

utbot-junit-contest/src/main/kotlin/org/utbot/contest/usvm/UTestInst2UtModelConverter.kt

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

3+
import org.jacodb.api.JcClasspath
34
import org.usvm.instrumentation.testcase.UTest
45
import org.usvm.instrumentation.testcase.api.UTestAllocateMemoryCall
56
import org.usvm.instrumentation.testcase.api.UTestArithmeticExpression
@@ -50,6 +51,7 @@ import org.utbot.fuzzer.IdGenerator
5051

5152
class UTestInst2UtModelConverter(
5253
private val idGenerator: IdGenerator<Int>,
54+
private val jcClasspath: JcClasspath,
5355
private val utilMethodProvider: UtilMethodProvider,
5456
) {
5557
private val exprToModelCache = mutableMapOf<UTestExpression, UtModel>()
@@ -101,7 +103,7 @@ class UTestInst2UtModelConverter(
101103
val instanceModel = processExpr(instanceExpr)
102104
require(instanceModel is UtAssembleModel)
103105

104-
val fieldType = uTestInst.field.type.classId
106+
val fieldType = uTestInst.field.type.findClassId(jcClasspath)
105107
val fieldName = uTestInst.field.name
106108
val setValueModel = processExpr(uTestInst.value)
107109

@@ -147,7 +149,7 @@ class UTestInst2UtModelConverter(
147149
is UTestConstructorCall -> {
148150
val constructorCall = UtExecutableCallModel(
149151
instance = null,
150-
executable = uTestExpr.method.toExecutableId(),
152+
executable = uTestExpr.method.toExecutableId(jcClasspath),
151153
params = uTestExpr.args.map { arg ->
152154
processExpr(arg)
153155
},
@@ -167,7 +169,7 @@ class UTestInst2UtModelConverter(
167169

168170
val methodCall = UtExecutableCallModel(
169171
instance = instanceModel,
170-
executable = uTestExpr.method.toExecutableId(),
172+
executable = uTestExpr.method.toExecutableId(jcClasspath),
171173
params = uTestExpr.args.map { arg -> processExpr(arg) },
172174
)
173175

@@ -188,7 +190,7 @@ class UTestInst2UtModelConverter(
188190
modelName = "",
189191
instantiationCall = UtExecutableCallModel(
190192
instance = null,
191-
executable = uTestExpr.method.toExecutableId(),
193+
executable = uTestExpr.method.toExecutableId(jcClasspath),
192194
params = uTestExpr.args.map { arg -> processExpr(arg) },
193195
),
194196
)
@@ -234,7 +236,7 @@ class UTestInst2UtModelConverter(
234236
executable = utilMethodProvider.getFieldValueMethodId,
235237
params = listOf(
236238
instanceModel,
237-
UtPrimitiveModel(uTestExpr.field.type.classId.name),
239+
UtPrimitiveModel(uTestExpr.field.type),
238240
UtPrimitiveModel(uTestExpr.field.name),
239241
),
240242
)
@@ -252,7 +254,7 @@ class UTestInst2UtModelConverter(
252254
instance = null,
253255
executable = utilMethodProvider.getStaticFieldValueMethodId,
254256
params = listOf(
255-
UtPrimitiveModel(uTestExpr.field.type.classId.name),
257+
UtPrimitiveModel(uTestExpr.field.type),
256258
UtPrimitiveModel(uTestExpr.field.name),
257259
),
258260
)
@@ -287,7 +289,7 @@ class UTestInst2UtModelConverter(
287289
mocks += uTestExpr.methods
288290
.entries
289291
.associate { (jcMethod, uTestExprs) ->
290-
val executableId: ExecutableId = jcMethod.toExecutableId()
292+
val executableId: ExecutableId = jcMethod.toExecutableId(jcClasspath)
291293
val models = uTestExprs.map { expr ->
292294
processExpr(expr)
293295
}

utbot-junit-contest/src/main/resources/classes/samples/list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ org.utbot.examples.wrappers.DoubleWrapper
1919
org.utbot.examples.wrappers.CharacterWrapper
2020
org.utbot.examples.wrappers.ByteWrapper
2121
org.utbot.examples.wrappers.BooleanWrapper
22+
org.utbot.examples.arrays.PrimitiveArrays
2223

2324

0 commit comments

Comments
 (0)