-
Couldn't load subscription status.
- Fork 45
Enhance fuzzing for arrays #738 #755
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
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0dd9ca9
Sketch for ArrayModelProvider
volivan239 1a730a9
Add RecursiveModelProvider abstraction
volivan239 570809e
Moved common logic from ArrayMP and ObjectMP to RecursiveMP
volivan239 36c749e
Minor changes & documentation added
volivan239 d6d09f2
Return behavior of defaultModelProviders and minor fixes:
volivan239 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 54 additions & 23 deletions
77
utbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/providers/ArrayModelProvider.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,65 @@ | ||
| package org.utbot.fuzzer.providers | ||
|
|
||
| import org.utbot.framework.plugin.api.ClassId | ||
| import org.utbot.framework.plugin.api.UtArrayModel | ||
| import org.utbot.framework.plugin.api.UtModel | ||
| import org.utbot.framework.plugin.api.UtPrimitiveModel | ||
| import org.utbot.framework.plugin.api.util.defaultValueModel | ||
| import org.utbot.framework.plugin.api.util.intClassId | ||
| import org.utbot.framework.plugin.api.util.isArray | ||
| import org.utbot.fuzzer.FuzzedMethodDescription | ||
| import org.utbot.fuzzer.FuzzedParameter | ||
| import org.utbot.fuzzer.IdGenerator | ||
| import org.utbot.fuzzer.ModelProvider | ||
| import org.utbot.fuzzer.ModelProvider.Companion.yieldAllValues | ||
| import java.util.function.IntSupplier | ||
| import org.utbot.fuzzer.IdentityPreservingIdGenerator | ||
|
|
||
| class ArrayModelProvider( | ||
| private val idGenerator: IdGenerator<Int> | ||
| ) : ModelProvider { | ||
| override fun generate(description: FuzzedMethodDescription): Sequence<FuzzedParameter> = sequence { | ||
| description.parametersMap | ||
| .asSequence() | ||
| .filter { (classId, _) -> classId.isArray } | ||
| .forEach { (arrayClassId, indices) -> | ||
| yieldAllValues(indices, listOf(0, 10).map { arraySize -> | ||
| UtArrayModel( | ||
| id = idGenerator.createId(), | ||
| arrayClassId, | ||
| length = arraySize, | ||
| arrayClassId.elementClassId!!.defaultValueModel(), | ||
| mutableMapOf() | ||
| ).fuzzed { | ||
| this.summary = "%var% = ${arrayClassId.elementClassId!!.simpleName}[$arraySize]" | ||
| } | ||
| }) | ||
| idGenerator: IdentityPreservingIdGenerator<Int>, | ||
| recursionDepthLeft: Int = 1 | ||
| ) : RecursiveModelProvider(idGenerator, recursionDepthLeft) { | ||
| override fun createNewInstance(parentProvider: RecursiveModelProvider, newTotalLimit: Int): RecursiveModelProvider = | ||
| ArrayModelProvider(parentProvider.idGenerator, parentProvider.recursionDepthLeft - 1) | ||
| .copySettingsFrom(parentProvider) | ||
| .apply { | ||
| totalLimit = newTotalLimit | ||
| if (parentProvider is ArrayModelProvider) | ||
| branchingLimit = 1 // This improves performance but also forbids generating "jagged" arrays | ||
| } | ||
|
|
||
| override fun generateModelConstructors( | ||
| description: FuzzedMethodDescription, | ||
| classId: ClassId | ||
| ): List<ModelConstructor> { | ||
| if (!classId.isArray) | ||
| return listOf() | ||
| val lengths = generateArrayLengths(description) | ||
| return lengths.map { length -> | ||
| ModelConstructor(List(length) { classId.elementClassId!! }) { values -> | ||
| createFuzzedArrayModel(classId, length, values.map { it.model } ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun generateArrayLengths(description: FuzzedMethodDescription): List<Int> { | ||
| val fuzzedLengths = fuzzValuesRecursively( | ||
| types = listOf(intClassId), | ||
| baseMethodDescription = description, | ||
| modelProvider = ConstantsModelProvider | ||
| ) | ||
|
|
||
| // Firstly we will use most "interesting" default sizes, then we will use small sizes obtained from src | ||
| return listOf(3, 0) + fuzzedLengths | ||
| .map { (it.single().model as UtPrimitiveModel).value as Int } | ||
| .filter { it in 1..10 && it != 3 } | ||
| .toSortedSet() | ||
| .toList() | ||
| } | ||
|
|
||
| private fun createFuzzedArrayModel(arrayClassId: ClassId, length: Int, values: List<UtModel>?) = | ||
| UtArrayModel( | ||
| idGenerator.createId(), | ||
| arrayClassId, | ||
| length, | ||
| arrayClassId.elementClassId!!.defaultValueModel(), | ||
| values?.withIndex()?.associate { it.index to it.value }?.toMutableMap() ?: mutableMapOf() | ||
| ).fuzzed { | ||
| this.summary = "%var% = ${arrayClassId.elementClassId!!.simpleName}[$length]" | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note, that the other method called 'defaultModelProviders' without parameter with recursion has another set of model providers. Therefore. I'd recommend to change name for this method to avoid ambiguous calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method should have replaced the method without
recursionDepthparameter with no changes in behavior when this parameter is not specified. Seems like I accidently got duplication after rebase. Deleted method without parameter now. Could you please check that it didn't change the behavior?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It changes because
PrimitivesModelProviderisn't used anymore. By default I want to keep an ability to set different providers by default, because I usually want to use shallower value set for it. Right now this change is only about PrimitivesModelProvider, but in the future I'd want to add some providers that are used for generating flat parameters and aren't used for recursive.