-
Notifications
You must be signed in to change notification settings - Fork 46
Mutate values to improve code coverage #213 #713
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
Markoutte
merged 4 commits into
main
from
pelevin/213_Mutate_values_to_improve_code_coverage
Aug 24, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
71 changes: 71 additions & 0 deletions
71
utbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/FuzzerStatistics.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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.utbot.fuzzer | ||
|
||
import kotlin.math.pow | ||
import kotlin.random.Random | ||
|
||
/** | ||
* Stores information that can be useful for fuzzing such as coverage, run count, etc. | ||
*/ | ||
interface FuzzerStatistics<K> { | ||
|
||
val seeds: Collection<K> | ||
|
||
/** | ||
* Returns a random seed to process. | ||
*/ | ||
fun randomSeed(random: Random): K? | ||
|
||
fun randomValues(random: Random): List<FuzzedValue>? | ||
|
||
fun executions(seed: K): Int | ||
|
||
operator fun get(seed: K): List<FuzzedValue>? | ||
|
||
fun isEmpty(): Boolean | ||
|
||
fun isNotEmpty(): Boolean { | ||
return !isEmpty() | ||
} | ||
} | ||
|
||
class TrieBasedFuzzerStatistics<V>( | ||
private val values: LinkedHashMap<Trie.Node<V>, List<FuzzedValue>> = linkedMapOf() | ||
) : FuzzerStatistics<Trie.Node<V>> { | ||
|
||
override val seeds: Collection<Trie.Node<V>> | ||
get() = values.keys | ||
|
||
override fun randomSeed(random: Random): Trie.Node<V>? { | ||
return values.keys.elementAtOrNull(randomIndex(random)) | ||
} | ||
|
||
override fun isEmpty(): Boolean { | ||
return values.isEmpty() | ||
} | ||
|
||
override fun isNotEmpty(): Boolean { | ||
return values.isNotEmpty() | ||
} | ||
|
||
override fun randomValues(random: Random): List<FuzzedValue>? { | ||
return values.values.elementAtOrNull(randomIndex(random)) | ||
} | ||
|
||
private fun randomIndex(random: Random): Int { | ||
val frequencies = DoubleArray(values.size).also { f -> | ||
values.keys.forEachIndexed { index, key -> | ||
f[index] = 1 / key.count.toDouble().pow(2) | ||
} | ||
} | ||
return random.chooseOne(frequencies) | ||
} | ||
|
||
override fun get(seed: Trie.Node<V>): List<FuzzedValue>? { | ||
return values[seed] | ||
} | ||
|
||
override fun executions(seed: Trie.Node<V>): Int { | ||
return seed.count | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
utbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/ModelMutator.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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.utbot.fuzzer | ||
|
||
import org.utbot.framework.plugin.api.UtModel | ||
import kotlin.random.Random | ||
|
||
/** | ||
* Mutates values and returns it. | ||
*/ | ||
interface ModelMutator { | ||
|
||
/** | ||
* Mutates values set. | ||
* | ||
* Default implementation iterates through values and delegates to `mutate(FuzzedMethodDescription, Int, Random)`. | ||
*/ | ||
fun mutate( | ||
description: FuzzedMethodDescription, | ||
parameters: List<FuzzedValue>, | ||
random: Random, | ||
) : List<FuzzedParameter> { | ||
return parameters | ||
.asSequence() | ||
.mapIndexedNotNull { index, fuzzedValue -> | ||
mutate(description, index, fuzzedValue, random)?.let { mutated -> | ||
FuzzedParameter(index, mutated) | ||
} | ||
} | ||
.toList() | ||
} | ||
|
||
/** | ||
* Mutate a single value if it is possible. | ||
*/ | ||
fun mutate( | ||
description: FuzzedMethodDescription, | ||
index: Int, | ||
value: FuzzedValue, | ||
random: Random | ||
) : FuzzedValue? { | ||
return null | ||
} | ||
|
||
fun UtModel.mutatedFrom(template: FuzzedValue, block: FuzzedValue.() -> Unit = {}): FuzzedValue { | ||
return FuzzedValue(this, template.createdBy).apply(block) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
utbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/RandomExtensions.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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.utbot.fuzzer | ||
|
||
import kotlin.random.Random | ||
|
||
/** | ||
* Chooses a random value using frequencies. | ||
* | ||
* If value has greater frequency value then it would be chosen with greater probability. | ||
* | ||
* @return the index of the chosen item. | ||
*/ | ||
fun Random.chooseOne(frequencies: DoubleArray): Int { | ||
val total = frequencies.sum() | ||
val value = nextDouble(total) | ||
var nextBound = 0.0 | ||
frequencies.forEachIndexed { index, bound -> | ||
check(bound >= 0) { "Frequency must not be negative" } | ||
nextBound += bound | ||
if (value < nextBound) return index | ||
} | ||
error("Cannot find next index") | ||
} | ||
|
||
/** | ||
* Tries a value. | ||
* | ||
* If a random value is less than [probability] returns true. | ||
*/ | ||
fun Random.flipCoin(probability: Int): Boolean { | ||
check(probability in 0 .. 100) { "probability must in range [0, 100] but $probability is provided" } | ||
return nextInt(1, 101) <= probability | ||
} | ||
|
||
fun Long.invertBit(bitIndex: Int): Long { | ||
return this xor (1L shl bitIndex) | ||
} |
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.
Perhaps, is frequencies could be a big array, we can create a custom collection for such frequencies that knowns information about its elements, such as total sum, statistics and other