From eb8ece90efa93238d4931c52cb802b87d1246f9c Mon Sep 17 00:00:00 2001 From: IlyaMuravjov <71839386+IlyaMuravjov@users.noreply.github.com> Date: Thu, 31 Aug 2023 10:53:21 +0300 Subject: [PATCH] Consider dynamically created mocks when pre minimizing fuzzer output and properly initialize @Mock fields (#2569) * Consider dynamically created mocks when pre minimizing fuzzer output * Clear already initialized field models after constructing class fields --- .../src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt | 2 +- .../codegen/tree/CgAbstractSpringTestClassConstructor.kt | 4 ++++ .../framework/codegen/tree/CgSpringVariableConstructor.kt | 4 ++++ .../codegen/tree/fieldmanager/ClassFieldManagerFacade.kt | 4 ++++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt b/utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt index 6ceb5ca7ba..1cdde48e30 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt @@ -516,7 +516,7 @@ class UtBotSymbolicEngine( trieNode = descr.tracer.add(coveredInstructions) val earlierStateBeforeSize = coverageToMinStateBeforeSize[trieNode] - val curStateBeforeSize = stateBefore.calculateSize() + val curStateBeforeSize = concreteExecutionResult.stateBefore.calculateSize() if (earlierStateBeforeSize == null || curStateBeforeSize < earlierStateBeforeSize) coverageToMinStateBeforeSize[trieNode] = curStateBeforeSize diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgAbstractSpringTestClassConstructor.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgAbstractSpringTestClassConstructor.kt index cc2f86b4f1..9f46e6abef 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgAbstractSpringTestClassConstructor.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgAbstractSpringTestClassConstructor.kt @@ -31,6 +31,10 @@ abstract class CgAbstractSpringTestClassConstructor(context: CgContext) : fields += constructClassFields(testClassModel) clearUnwantedVariableModels() + // constructClassFields may mark fields as initialized, while they are in + // fact not initialized, so we clearAlreadyInitializedFieldModels() + variableConstructor.clearAlreadyInitializedFieldModels() + constructAdditionalTestMethods()?.let { methodRegions += it } for ((testSetIndex, testSet) in testClassModel.methodTestSets.withIndex()) { diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringVariableConstructor.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringVariableConstructor.kt index ac4649ef8a..2ca542e6c6 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringVariableConstructor.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringVariableConstructor.kt @@ -14,6 +14,10 @@ class CgSpringVariableConstructor(context: CgContext) : CgVariableConstructor(co private val fieldManagerFacade = ClassFieldManagerFacade(context) + fun clearAlreadyInitializedFieldModels() { + fieldManagerFacade.clearAlreadyInitializedModels() + } + override fun getOrCreateVariable(model: UtModel, name: String?): CgValue { val variable = fieldManagerFacade.constructVariableForField(model) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/fieldmanager/ClassFieldManagerFacade.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/fieldmanager/ClassFieldManagerFacade.kt index 687e199612..791bcba608 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/fieldmanager/ClassFieldManagerFacade.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/fieldmanager/ClassFieldManagerFacade.kt @@ -11,6 +11,10 @@ class ClassFieldManagerFacade(context: CgContext) : CgContextOwner by context { private val alreadyInitializedModels = mutableSetOf() + fun clearAlreadyInitializedModels() { + alreadyInitializedModels.clear() + } + fun constructVariableForField(model: UtModel): CgValue? { relevantFieldManagers.forEach { manager -> val alreadyCreatedVariable = manager.findCgValueByModel(model, manager.annotatedModels)