Skip to content

Commit

Permalink
Make fuzzer use generic field types #2430 (#2431)
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaMuravjov authored Jul 21, 2023
1 parent 1de5cf9 commit 5b4d643
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ internal fun findAccessibleModifiableFields(description: FuzzedDescription?, cla
FieldDescription(
name = field.name,
type = if (description != null) toFuzzerType(
field.type,
field.genericType,
description.typeCache
) else FuzzedType(field.type.id),
canBeSetDirectly = isAccessible(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.utbot.fuzzing.samples;

import java.util.List;

public class StringListHolder {
private List<String> strings;


public List<String> getStrings() {
return strings;
}


public void setStrings(List<String> strings) {
this.strings = strings;
}


public void methodUnderTest() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.utbot.framework.plugin.api.UtAssembleModel
import org.utbot.framework.plugin.api.UtNullModel
import org.utbot.framework.plugin.api.UtPrimitiveModel
import org.utbot.framework.plugin.api.util.*
import org.utbot.framework.plugin.api.util.constructor.ValueConstructor
import org.utbot.fuzzer.FuzzedConcreteValue
import org.utbot.fuzzing.samples.DeepNested
import org.utbot.fuzzer.FuzzedType
Expand All @@ -17,13 +18,15 @@ import org.utbot.fuzzer.IdentityPreservingIdGenerator
import org.utbot.fuzzing.providers.NullValueProvider
import org.utbot.fuzzing.samples.AccessibleObjects
import org.utbot.fuzzing.samples.FailToGenerateListGeneric
import org.utbot.fuzzing.samples.StringListHolder
import org.utbot.fuzzing.samples.Stubs
import org.utbot.fuzzing.utils.Trie
import java.lang.reflect.GenericArrayType
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.util.IdentityHashMap
import java.util.concurrent.atomic.AtomicInteger
import kotlin.reflect.jvm.javaMethod

internal object TestIdentityPreservingIdGenerator : IdentityPreservingIdGenerator<Int> {
private val cache = mutableMapOf<Any, Int>()
Expand Down Expand Up @@ -273,6 +276,31 @@ class JavaFuzzingTest {
}
}

@Test
fun `fuzzer correctly works with settable field that has a parameterized type`() {
val seenStringListHolders = mutableListOf<StringListHolder>()
var remainingRuns = 100
runBlockingWithContext {
runJavaFuzzing(
TestIdentityPreservingIdGenerator,
methodUnderTest = StringListHolder::methodUnderTest.javaMethod!!.executableId,
constants = emptyList(),
names = emptyList(),
) { thisInstance, _, _ ->
thisInstance?.let {
seenStringListHolders.add(
ValueConstructor().construct(listOf(it.model)).single().value as StringListHolder
)
}
remainingRuns--
BaseFeedback(Trie.emptyNode(), if (remainingRuns > 0) Control.CONTINUE else Control.STOP)
}
}
val seenStrings = seenStringListHolders.flatMap { it.strings.orEmpty().filterNotNull() }
assertNotEquals(emptyList<String>(), seenStrings)
seenStrings.forEach { assertInstanceOf(String::class.java, it) }
}

@Test
fun `value providers override every function of fuzzing in simple case`() {
val provided = MarkerValueProvider<FuzzedType, FuzzedValue, FuzzedDescription>("p")
Expand Down

0 comments on commit 5b4d643

Please sign in to comment.