Skip to content

Improve fuzzer recursive model providers #1039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,28 @@ import org.utbot.fuzzer.fuzzNumbers

class ArrayModelProvider(
idGenerator: IdentityPreservingIdGenerator<Int>,
recursionDepthLeft: Int = 1
recursionDepthLeft: Int = 2
) : RecursiveModelProvider(idGenerator, recursionDepthLeft) {

override fun newInstance(parentProvider: RecursiveModelProvider): RecursiveModelProvider =
ArrayModelProvider(parentProvider.idGenerator, parentProvider.recursionDepthLeft - 1)
.copySettings(parentProvider)
override fun newInstance(parentProvider: RecursiveModelProvider, constructor: ModelConstructor): RecursiveModelProvider {
val provider = ArrayModelProvider(parentProvider.idGenerator, parentProvider.recursionDepthLeft - 1)
provider.copySettings(parentProvider)
provider.totalLimit = minOf(parentProvider.totalLimit, constructor.limit)
return provider
}

override fun generateModelConstructors(
description: FuzzedMethodDescription,
parameterIndex: Int,
classId: ClassId,
): Sequence<ModelConstructor> = sequence {
if (!classId.isArray) return@sequence
val lengths = fuzzNumbers(description.concreteValues, 0, 3) { it in 1..10 }
val lengths = fuzzNumbers(description.concreteValues, 0, 3) { it in 1..10 }.toList()
lengths.forEach { length ->
yield(ModelConstructor(listOf(FuzzedType(classId.elementClassId!!)), repeat = length) { values ->
createFuzzedArrayModel(classId, length, values.map { it.model } )
}.apply {
limit = (totalLimit / lengths.size).coerceAtLeast(1)
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import org.utbot.fuzzer.objects.create

class CollectionWithModificationModelProvider(
idGenerator: IdentityPreservingIdGenerator<Int>,
recursionDepthLeft: Int = 1,
recursionDepthLeft: Int = 2,
private var defaultModificationCount: IntArray = intArrayOf(0, 1, 3)
) : RecursiveModelProvider(idGenerator, recursionDepthLeft) {

Expand Down Expand Up @@ -55,7 +55,7 @@ class CollectionWithModificationModelProvider(
)
private var modificationCount = 7

override fun newInstance(parentProvider: RecursiveModelProvider): RecursiveModelProvider {
override fun newInstance(parentProvider: RecursiveModelProvider, constructor: ModelConstructor): RecursiveModelProvider {
val newInstance = CollectionWithModificationModelProvider(
parentProvider.idGenerator, parentProvider.recursionDepthLeft - 1
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import org.utbot.fuzzer.objects.assembleModel
*/
class ObjectModelProvider(
idGenerator: IdentityPreservingIdGenerator<Int>,
recursionDepthLeft: Int = 1,
recursionDepthLeft: Int = 2,
) : RecursiveModelProvider(idGenerator, recursionDepthLeft) {
override fun newInstance(parentProvider: RecursiveModelProvider): RecursiveModelProvider {
override fun newInstance(parentProvider: RecursiveModelProvider, constructor: ModelConstructor): RecursiveModelProvider {
val newInstance = ObjectModelProvider(parentProvider.idGenerator, parentProvider.recursionDepthLeft - 1)
newInstance.copySettings(parentProvider)
newInstance.branchingLimit = 1
Expand All @@ -62,9 +62,9 @@ class ObjectModelProvider(
)

constructors.forEach { constructorId ->
yield(ModelConstructor(constructorId.parameters.map { classId -> FuzzedType(classId) }) {
assembleModel(idGenerator.createId(), constructorId, it)
})
// When branching limit = 1 this block tries to create new values
// and mutate some fields. Only if there's no option next block
// with empty constructor should be used.
if (constructorId.parameters.isEmpty()) {
val fields = findSuitableFields(constructorId.classId, description)
if (fields.isNotEmpty()) {
Expand All @@ -75,6 +75,9 @@ class ObjectModelProvider(
)
}
}
yield(ModelConstructor(constructorId.parameters.map { classId -> FuzzedType(classId) }) {
assembleModel(idGenerator.createId(), constructorId, it)
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ abstract class RecursiveModelProvider(
/**
* Creates instance of the class on which it is called, assuming that it will be called recursively from [parentProvider]
*/
protected abstract fun newInstance(parentProvider: RecursiveModelProvider): RecursiveModelProvider
protected abstract fun newInstance(parentProvider: RecursiveModelProvider, constructor: ModelConstructor): RecursiveModelProvider

/**
* Creates [ModelProvider]s that will be used to generate values recursively. The order of elements in returned list is important:
Expand Down Expand Up @@ -101,16 +101,16 @@ abstract class RecursiveModelProvider(
neededTypes[index % neededTypes.size] // because we can repeat neededTypes several times
}
}
return fuzz(syntheticMethodDescription, nextModelProvider())
return fuzz(syntheticMethodDescription, nextModelProvider(this))
.map { createModel(it) }
.take(limit)
}

private fun nextModelProvider(): ModelProvider =
private fun nextModelProvider(constructor: ModelConstructor): ModelProvider =
if (recursionDepthLeft > 0) {
modelProviderForRecursiveCalls.map {
if (it is RecursiveModelProvider) {
it.newInstance(this)
it.newInstance(this, constructor)
} else { it }
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ class ModelProviderTest {

withUtContext(UtContext(this::class.java.classLoader)) {
val result = collect(
ObjectModelProvider(ReferencePreservingIntIdGenerator(0)),
ObjectModelProvider(ReferencePreservingIntIdGenerator(0), recursionDepthLeft = 1),
parameters = listOf(MyA::class.java.id)
)
assertEquals(1, result.size)
Expand Down Expand Up @@ -478,14 +478,14 @@ class ModelProviderTest {
)

withUtContext(UtContext(this::class.java.classLoader)) {
val result = collect(ObjectModelProvider(ReferencePreservingIntIdGenerator(0)).apply {
val result = collect(ObjectModelProvider(ReferencePreservingIntIdGenerator(0), recursionDepthLeft = 1).apply {
modelProviderForRecursiveCalls = PrimitiveDefaultsModelProvider
}, parameters = listOf(FieldSetterClass::class.java.id))
assertEquals(1, result.size)
assertEquals(2, result[0]!!.size)
assertEquals(0, (result[0]!![0] as UtAssembleModel).modificationsChain.size) { "One of models must be without any modifications" }
assertEquals(0, (result[0]!![1] as UtAssembleModel).modificationsChain.size) { "One of models must be without any modifications" }
val expectedModificationSize = 3
val modificationsChain = (result[0]!![1] as UtAssembleModel).modificationsChain
val modificationsChain = (result[0]!![0] as UtAssembleModel).modificationsChain
val actualModificationSize = modificationsChain.size
assertEquals(expectedModificationSize, actualModificationSize) { "In target class there's only $expectedModificationSize fields that can be changed, but generated $actualModificationSize modifications" }

Expand Down Expand Up @@ -513,10 +513,10 @@ class ModelProviderTest {
}
assertEquals(1, result.size)
assertEquals(3, result[0]!!.size)
assertEquals(0, (result[0]!![0] as UtAssembleModel).modificationsChain.size) { "One of models must be without any modifications" }
assertEquals(0, (result[0]!![2] as UtAssembleModel).modificationsChain.size) { "Modification by constructor doesn't change fields" }
assertEquals(0, (result[0]!![2] as UtAssembleModel).modificationsChain.size) { "One of models must be without any modifications" }
assertEquals(0, (result[0]!![1] as UtAssembleModel).modificationsChain.size) { "Modification by constructor doesn't change fields" }
val expectedModificationSize = 1
val modificationsChain = (result[0]!![1] as UtAssembleModel).modificationsChain
val modificationsChain = (result[0]!![0] as UtAssembleModel).modificationsChain
val actualModificationSize = modificationsChain.size
assertEquals(expectedModificationSize, actualModificationSize) { "In target class there's only $expectedModificationSize fields that can be changed, but generated $actualModificationSize modifications" }

Expand Down