From 1467ee70f5790434266c6b487e02ce18ca9fed82 Mon Sep 17 00:00:00 2001 From: Sergei Kharitontcev-Beglov Date: Tue, 20 Aug 2024 15:37:38 +0200 Subject: [PATCH] Added a minor generation problems Added a cache to comprehend possible cycles in the generation loops as well as instantiated all the array's elements --- .../research/kex/parameters/Parameters.kt | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/kex-core/src/main/kotlin/org/vorpal/research/kex/parameters/Parameters.kt b/kex-core/src/main/kotlin/org/vorpal/research/kex/parameters/Parameters.kt index fc7a2b7cd..2b44c5284 100644 --- a/kex-core/src/main/kotlin/org/vorpal/research/kex/parameters/Parameters.kt +++ b/kex-core/src/main/kotlin/org/vorpal/research/kex/parameters/Parameters.kt @@ -103,38 +103,44 @@ fun Parameters.replaceNullsWithDefaultValues(method: Method, cm: Cla return Parameters(instance, parametersWithoutNulls, statics) } -private fun transformExistingDescriptor(descriptor: Descriptor, type: KexType, cm: ClassManager): Descriptor { +private fun transformExistingDescriptor(descriptor: Descriptor, type: KexType, cm: ClassManager, alreadyCopied: MutableMap = mutableMapOf()): Descriptor { + if (descriptor in alreadyCopied) return alreadyCopied[descriptor]!! return when (descriptor) { is ConstantDescriptor.Null -> type.newInstance(cm) is ConstantDescriptor -> descriptor is ArrayDescriptor -> ArrayDescriptor(descriptor.elementType, descriptor.length) .apply { - descriptor.elements.forEach { (index, value) -> - this[index] = transformExistingDescriptor(value, descriptor.elementType, cm) + (0 until descriptor.length).forEach { index -> + this[index] = transformExistingDescriptor( + descriptor[index] ?: ConstantDescriptor.Null, + descriptor.elementType, + cm, + alreadyCopied + ) } } is ObjectDescriptor -> ObjectDescriptor(descriptor.klass).apply { descriptor.fields.forEach { (key, desc) -> val (name, fieldType) = key - set(name to fieldType, transformExistingDescriptor(desc, fieldType, cm)) + set(name to fieldType, transformExistingDescriptor(desc, fieldType, cm, alreadyCopied)) } } is ClassDescriptor -> ClassDescriptor(descriptor.klass).apply { descriptor.fields.forEach { (key, desc) -> val (name, fieldType) = key - set(name to fieldType, transformExistingDescriptor(desc, fieldType, cm)) + set(name to fieldType, transformExistingDescriptor(desc, fieldType, cm, alreadyCopied)) } } is MockDescriptor -> MockDescriptor(descriptor.klass).apply { descriptor.fields.forEach { (key, desc) -> val (name, fieldType) = key - set(name to fieldType, transformExistingDescriptor(desc, fieldType, cm)) + set(name to fieldType, transformExistingDescriptor(desc, fieldType, cm, alreadyCopied)) } } - } + }.also { alreadyCopied[descriptor] = it } } private fun KexType.newInstance(cm: ClassManager): Descriptor { @@ -143,10 +149,10 @@ private fun KexType.newInstance(cm: ClassManager): Descriptor { val kfgClass = (this as KexClass).kfgClass(cm.type) kfgClass .fields - .filterNot { it.isStatic} + .filterNot { it.isStatic } .forEach { - if (!it.isStatic) - descriptor[it.name to this] = it.type.kexType.newInstance(cm) - } + if (!it.isStatic) + descriptor[it.name to this] = it.type.kexType.newInstance(cm) + } return descriptor }