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 62186d1153..94e3197f98 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt @@ -339,7 +339,7 @@ class UtBotSymbolicEngine( var attempts = 0 val attemptsLimit = UtSettings.fuzzingMaxAttempts val names = graph.body.method.tags.filterIsInstance().firstOrNull()?.names ?: emptyList() - + var testEmittedByFuzzer = 0 runJavaFuzzing( defaultIdGenerator, methodUnderTest, @@ -349,6 +349,7 @@ class UtBotSymbolicEngine( ) { thisInstance, descr, values -> if (controller.job?.isActive == false || System.currentTimeMillis() >= until) { logger.info { "Fuzzing overtime: $methodUnderTest" } + logger.info { "Test created by fuzzer: $testEmittedByFuzzer" } return@runJavaFuzzing BaseFeedback(result = Trie.emptyNode(), control = Control.STOP) } @@ -404,6 +405,7 @@ class UtBotSymbolicEngine( ) ) + testEmittedByFuzzer++ BaseFeedback(result = trieNode ?: Trie.emptyNode(), control = Control.CONTINUE) } } diff --git a/utbot-fuzzers/src/main/kotlin/org/utbot/fuzzing/JavaLanguage.kt b/utbot-fuzzers/src/main/kotlin/org/utbot/fuzzing/JavaLanguage.kt index 4ff6df412f..ab0b9cbf1f 100644 --- a/utbot-fuzzers/src/main/kotlin/org/utbot/fuzzing/JavaLanguage.kt +++ b/utbot-fuzzers/src/main/kotlin/org/utbot/fuzzing/JavaLanguage.kt @@ -1,5 +1,6 @@ package org.utbot.fuzzing +import mu.KotlinLogging import org.utbot.framework.plugin.api.ClassId import org.utbot.framework.plugin.api.ExecutableId import org.utbot.framework.plugin.api.Instruction @@ -8,6 +9,10 @@ import org.utbot.fuzzer.* import org.utbot.fuzzing.providers.* import org.utbot.fuzzing.utils.Trie import java.lang.reflect.* +import java.util.concurrent.TimeUnit +import kotlin.system.measureNanoTime + +private val logger = KotlinLogging.logger {} typealias JavaValueProvider = ValueProvider @@ -91,12 +96,25 @@ suspend fun runJavaFuzzing( val tracer = Trie(Instruction::id) val descriptionWithOptionalThisInstance = FuzzedDescription(createFuzzedMethodDescription(thisInstance), tracer) val descriptionWithOnlyParameters = FuzzedDescription(createFuzzedMethodDescription(null), tracer) - runFuzzing(ValueProvider.of(providers), descriptionWithOptionalThisInstance) { _, t -> - if (thisInstance == null) { - exec(null, descriptionWithOnlyParameters, t) - } else { - exec(t.first(), descriptionWithOnlyParameters, t.drop(1)) + try { + logger.info { "Starting fuzzing for method: $methodUnderTest" } + logger.info { "\tuse thisInstance = ${thisInstance != null}" } + logger.info { "\tparameters = $parameters" } + var totalExecutionCalled = 0 + val totalFuzzingTime = measureNanoTime { + runFuzzing(ValueProvider.of(providers), descriptionWithOptionalThisInstance) { _, t -> + totalExecutionCalled++ + if (thisInstance == null) { + exec(null, descriptionWithOnlyParameters, t) + } else { + exec(t.first(), descriptionWithOnlyParameters, t.drop(1)) + } + } } + logger.info { "Finishing fuzzing for method: $methodUnderTest in ${TimeUnit.NANOSECONDS.toMillis(totalFuzzingTime)} ms" } + logger.info { "\tTotal execution called: $totalExecutionCalled" } + } catch (t: Throwable) { + logger.info(t) { "Fuzzing is stopped because of an error" } } } diff --git a/utbot-fuzzers/src/test/java/org/utbot/fuzzing/samples/DeepNested.java b/utbot-fuzzers/src/test/java/org/utbot/fuzzing/samples/DeepNested.java new file mode 100644 index 0000000000..88c1748719 --- /dev/null +++ b/utbot-fuzzers/src/test/java/org/utbot/fuzzing/samples/DeepNested.java @@ -0,0 +1,14 @@ +package org.utbot.fuzzing.samples; + +public class DeepNested { + public class Nested1 { + public class Nested2 { + public int f(int i) { + if (i > 0) { + return 10; + } + return 0; + } + } + } +} diff --git a/utbot-fuzzers/src/test/kotlin/org/utbot/fuzzing/JavaFuzzingTest.kt b/utbot-fuzzers/src/test/kotlin/org/utbot/fuzzing/JavaFuzzingTest.kt index 6561ff6f99..53b8ade416 100644 --- a/utbot-fuzzers/src/test/kotlin/org/utbot/fuzzing/JavaFuzzingTest.kt +++ b/utbot-fuzzers/src/test/kotlin/org/utbot/fuzzing/JavaFuzzingTest.kt @@ -3,16 +3,39 @@ package org.utbot.fuzzing import kotlinx.coroutines.runBlocking import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertDoesNotThrow import org.utbot.framework.plugin.api.MethodId import org.utbot.framework.plugin.api.TestIdentityPreservingIdGenerator import org.utbot.framework.plugin.api.UtPrimitiveModel import org.utbot.framework.plugin.api.util.* import org.utbot.fuzzer.FuzzedConcreteValue +import org.utbot.fuzzing.samples.DeepNested import org.utbot.fuzzing.samples.Stubs import org.utbot.fuzzing.utils.Trie class JavaFuzzingTest { + @Test + fun `fuzzing doesn't throw an exception when type is unknown`() { + assertDoesNotThrow { + runBlockingWithContext { + runJavaFuzzing( + TestIdentityPreservingIdGenerator, + methodUnderTest = MethodId( + DeepNested.Nested1.Nested2::class.id, + "f", + intClassId, + listOf(intClassId) + ), + constants = emptyList(), + names = emptyList(), + ) { _, _, _ -> + Assertions.fail("This method is never called") + } + } + } + } + @Test fun `string generates same values`() { fun collect(): List {