@@ -25,6 +25,7 @@ import org.utbot.framework.plugin.api.Coverage
2525import org.utbot.framework.plugin.api.EnvironmentModels
2626import org.utbot.framework.plugin.api.ExecutableId
2727import org.utbot.framework.plugin.api.Instruction
28+ import org.utbot.framework.plugin.api.UtArrayModel
2829import org.utbot.framework.plugin.api.UtAssembleModel
2930import org.utbot.framework.plugin.api.UtExecutableCallModel
3031import org.utbot.framework.plugin.api.UtExecution
@@ -34,12 +35,14 @@ import org.utbot.framework.plugin.api.UtExplicitlyThrownException
3435import org.utbot.framework.plugin.api.UtImplicitlyThrownException
3536import org.utbot.framework.plugin.api.UtInstrumentation
3637import org.utbot.framework.plugin.api.UtPrimitiveModel
38+ import org.utbot.framework.plugin.api.UtStatementCallModel
3739import org.utbot.framework.plugin.api.UtVoidModel
3840import org.utbot.framework.plugin.api.mapper.UtModelDeepMapper
3941import org.utbot.framework.plugin.api.util.executableId
4042import org.utbot.framework.plugin.api.util.jClass
4143import org.utbot.framework.plugin.api.util.utContext
4244import org.utbot.fuzzer.IdGenerator
45+ import java.util.IdentityHashMap
4346
4447private val logger = KotlinLogging .logger {}
4548
@@ -68,17 +71,19 @@ class JcToUtExecutionConverter(
6871
6972 val utUsvmExecution: UtUsvmExecution = when (val executionResult = jcExecution.uTestExecutionResult) {
7073 is UTestExecutionSuccessResult -> UtUsvmExecution (
71- stateBefore = convertState(executionResult.initialState, jcExecution.method, jcToUtModelConverter ),
72- stateAfter = convertState(executionResult.resultState, jcExecution.method, jcToUtModelConverter ),
74+ stateBefore = convertState(executionResult.initialState, EnvironmentStateKind . INITIAL , jcExecution.method ),
75+ stateAfter = convertState(executionResult.resultState, EnvironmentStateKind . FINAL , jcExecution.method ),
7376 // TODO usvm-sbft: ask why `UTestExecutionSuccessResult.result` is nullable
74- result = UtExecutionSuccess (executionResult.result?.let { jcToUtModelConverter.convert(it) } ? : UtVoidModel ),
77+ result = UtExecutionSuccess (executionResult.result?.let {
78+ jcToUtModelConverter.convert(it, EnvironmentStateKind .FINAL )
79+ } ? : UtVoidModel ),
7580 coverage = coverage,
7681 instrumentation = instrumentation,
7782 )
7883 is UTestExecutionExceptionResult -> {
7984 UtUsvmExecution (
80- stateBefore = convertState(executionResult.initialState, jcExecution.method, jcToUtModelConverter ),
81- stateAfter = convertState(executionResult.resultState, jcExecution.method, jcToUtModelConverter ),
85+ stateBefore = convertState(executionResult.initialState, EnvironmentStateKind . INITIAL , jcExecution.method ),
86+ stateAfter = convertState(executionResult.resultState, EnvironmentStateKind . FINAL , jcExecution.method ),
8287 result = createExecutionFailureResult(
8388 executionResult.cause,
8489 jcExecution.method,
@@ -108,7 +113,10 @@ class JcToUtExecutionConverter(
108113 }
109114 } ? : return null
110115
111- return utUsvmExecution.mapModels(constructAssemblingMapper())
116+ return utUsvmExecution
117+ .mapModels(constructAssemblingMapper())
118+ .mapModels(constructAssembleToCompositeModelMapper())
119+ .mapModels(constructConstArrayModelMapper())
112120 }
113121
114122 private fun constructAssemblingMapper (): UtModelDeepMapper = UtModelDeepMapper { model ->
@@ -145,6 +153,31 @@ class JcToUtExecutionConverter(
145153 } ? : model
146154 }
147155
156+ private fun constructConstArrayModelMapper (): UtModelDeepMapper = UtModelDeepMapper { model ->
157+ if (model is UtArrayModel ) {
158+ val storeGroups = model.stores.entries.groupByTo(IdentityHashMap ()) { it.value }
159+ val mostCommonStore = storeGroups.maxBy { it.value.size }
160+ if (mostCommonStore.value.size > 1 ) {
161+ model.constModel = mostCommonStore.key
162+ mostCommonStore.value.forEach { (index, _) -> model.stores.remove(index) }
163+ }
164+ }
165+ model
166+ }
167+
168+ private fun constructAssembleToCompositeModelMapper (): UtModelDeepMapper = UtModelDeepMapper { model ->
169+ if (model is UtAssembleModel
170+ && utilMethodProvider.createInstanceMethodId == model.instantiationCall.statement
171+ && model.modificationsChain.all {
172+ utilMethodProvider.setFieldMethodId == (it as ? UtStatementCallModel )?.statement
173+ }
174+ ) {
175+ model.origin ? : model
176+ } else {
177+ model
178+ }
179+ }
180+
148181 private fun convertException (exceptionDescriptor : UTestExceptionDescriptor ): Throwable =
149182 toValueConverter.buildObjectFromDescriptor(exceptionDescriptor.dropStaticFields(
150183 cache = mutableMapOf ()
@@ -160,18 +193,20 @@ class JcToUtExecutionConverter(
160193
161194 private fun convertState (
162195 state : UTestExecutionState ,
196+ stateKind : EnvironmentStateKind ,
163197 method : JcTypedMethod ,
164- modelConverter : JcToUtModelConverter ,
165- ): EnvironmentModels {
198+ ): EnvironmentModels {
166199 val thisInstance =
167200 if (method.isStatic) null
168201 else if (method.method.isConstructor) null
169- else modelConverter.convert(state.instanceDescriptor ? : error(" Unexpected null instanceDescriptor" ))
170- val parameters = state.argsDescriptors.map { modelConverter.convert(it ? : error(" Unexpected null argDescriptor" )) }
202+ else jcToUtModelConverter.convert(state.instanceDescriptor ? : error(" Unexpected null instanceDescriptor" ), stateKind)
203+ val parameters = state.argsDescriptors.map {
204+ jcToUtModelConverter.convert(it ? : error(" Unexpected null argDescriptor" ), stateKind)
205+ }
171206 val statics = state.statics
172207 .entries
173208 .associate { (jcField, uTestDescr) ->
174- jcField.fieldId to modelConverter .convert(uTestDescr)
209+ jcField.fieldId to jcToUtModelConverter .convert(uTestDescr, stateKind )
175210 }
176211 val executableId: ExecutableId = method.method.toExecutableId(jcClasspath)
177212 return EnvironmentModels (thisInstance, parameters, statics, executableId)
0 commit comments