-
Notifications
You must be signed in to change notification settings - Fork 46
Minimize UtExecution number produced by fuzzing and collect coverage statistics #465
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 all commits
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package org.utbot.fuzzer | ||
|
||
fun <T> trieOf(vararg values: Iterable<T>): Trie<T, T> = IdentityTrie<T>().apply { | ||
values.forEach(this::add) | ||
} | ||
|
||
fun stringTrieOf(vararg values: String): StringTrie = StringTrie().apply { | ||
values.forEach(this::add) | ||
} | ||
|
||
class StringTrie : IdentityTrie<Char>() { | ||
fun add(string: String) = super.add(string.toCharArray().asIterable()) | ||
fun removeCompletely(string: String) = super.removeCompletely(string.toCharArray().asIterable()) | ||
fun remove(string: String) = super.remove(string.toCharArray().asIterable()) | ||
operator fun get(string: String) = super.get(string.toCharArray().asIterable()) | ||
fun collect() = asSequence().map { String(it.toCharArray()) }.toSet() | ||
} | ||
|
||
open class IdentityTrie<T> : Trie<T, T>({ it }) | ||
|
||
/** | ||
* Implementation of a trie for any iterable values. | ||
*/ | ||
open class Trie<T, K>( | ||
private val keyExtractor: (T) -> K | ||
) : Iterable<List<T>> { | ||
|
||
private val roots = HashMap<K, NodeImpl<T, K>>() | ||
private val implementations = HashMap<Node<T>, NodeImpl<T, K>>() | ||
|
||
/** | ||
* Adds value into a trie. | ||
* | ||
* If value already exists then do nothing except increasing internal counter of added values. | ||
* The counter can be returned by [Node.count]. | ||
* | ||
* @return corresponding [Node] of the last element in the `values` | ||
*/ | ||
fun add(values: Iterable<T>): Node<T> { | ||
val root = try { values.first() } catch (e: NoSuchElementException) { error("Empty list are not allowed") } | ||
var key = keyExtractor(root) | ||
var node = roots.computeIfAbsent(key) { NodeImpl(root, null) } | ||
values.asSequence().drop(1).forEach { value -> | ||
key = keyExtractor(value) | ||
node = node.children.computeIfAbsent(key) { NodeImpl(value, node) } | ||
} | ||
node.count++ | ||
implementations[node] = node | ||
return node | ||
} | ||
|
||
/** | ||
* Decreases node counter value or removes the value completely if `counter == 1`. | ||
* | ||
* Use [removeCompletely] to remove the value from the trie regardless of counter value. | ||
* | ||
* @return removed node if value exists. | ||
*/ | ||
fun remove(values: Iterable<T>): Node<T>? { | ||
val node = findImpl(values) ?: return null | ||
return when { | ||
node.count == 1 -> removeCompletely(values) | ||
node.count > 1 -> node.apply { count-- } | ||
else -> throw IllegalStateException("count should be 1 or greater") | ||
} | ||
} | ||
|
||
/** | ||
* Removes value from a trie. | ||
* | ||
* The value is removed completely from the trie. Thus, the next code is true: | ||
* | ||
* ``` | ||
* trie.remove(someValue) | ||
* trie.get(someValue) == null | ||
* ``` | ||
* | ||
* Use [remove] to decrease counter value instead of removal. | ||
* | ||
* @return removed node if value exists | ||
*/ | ||
fun removeCompletely(values: Iterable<T>): Node<T>? { | ||
val node = findImpl(values) ?: return null | ||
if (node.count > 0 && node.children.isEmpty()) { | ||
var n: NodeImpl<T, K>? = node | ||
while (n != null) { | ||
val key = keyExtractor(n.data) | ||
n = n.parent | ||
if (n == null) { | ||
val removed = roots.remove(key) | ||
check(removed != null) | ||
} else { | ||
val removed = n.children.remove(key) | ||
check(removed != null) | ||
if (n.count != 0) { | ||
break | ||
} | ||
} | ||
} | ||
} | ||
return if (node.count > 0) { | ||
node.count = 0 | ||
implementations.remove(node) | ||
node | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
operator fun get(values: Iterable<T>): Node<T>? { | ||
return findImpl(values) | ||
} | ||
|
||
operator fun get(node: Node<T>): List<T>? { | ||
return implementations[node]?.let(this::buildValue) | ||
} | ||
|
||
private fun findImpl(values: Iterable<T>): NodeImpl<T, K>? { | ||
val root = try { values.first() } catch (e: NoSuchElementException) { return null } | ||
sergeypospelov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var key = keyExtractor(root) | ||
var node = roots[key] ?: return null | ||
values.asSequence().drop(1).forEach { value -> | ||
key = keyExtractor(value) | ||
node = node.children[key] ?: return null | ||
} | ||
return node.takeIf { it.count > 0 } | ||
} | ||
|
||
override fun iterator(): Iterator<List<T>> { | ||
return iterator { | ||
roots.values.forEach { node -> | ||
traverseImpl(node) | ||
} | ||
} | ||
} | ||
|
||
private suspend fun SequenceScope<List<T>>.traverseImpl(node: NodeImpl<T, K>) { | ||
val stack = ArrayDeque<NodeImpl<T, K>>() | ||
stack.addLast(node) | ||
while (stack.isNotEmpty()) { | ||
val n = stack.removeLast() | ||
if (n.count > 0) { | ||
yield(buildValue(n)) | ||
} | ||
n.children.values.forEach(stack::addLast) | ||
} | ||
} | ||
|
||
private fun buildValue(node: NodeImpl<T, K>): List<T> { | ||
return generateSequence(node) { it.parent }.map { it.data }.toList().asReversed() | ||
} | ||
|
||
interface Node<T> { | ||
val data: T | ||
val count: Int | ||
} | ||
|
||
/** | ||
* Trie node | ||
* | ||
* @param data data to be stored | ||
* @param parent reference to the previous element of the value | ||
* @param count number of value insertions | ||
* @param children list of children mapped by their key | ||
*/ | ||
private class NodeImpl<T, K>( | ||
override val data: T, | ||
val parent: NodeImpl<T, K>?, | ||
override var count: Int = 0, | ||
val children: MutableMap<K, NodeImpl<T, K>> = HashMap(), | ||
) : Node<T> | ||
} |
131 changes: 131 additions & 0 deletions
131
utbot-fuzzers/src/test/kotlin/org/utbot/framework/plugin/api/TrieTest.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,131 @@ | ||
package org.utbot.framework.plugin.api | ||
|
||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.Test | ||
import org.utbot.fuzzer.Trie | ||
import org.utbot.fuzzer.stringTrieOf | ||
import org.utbot.fuzzer.trieOf | ||
|
||
class TrieTest { | ||
|
||
@Test | ||
fun simpleTest() { | ||
val trie = stringTrieOf() | ||
assertThrows(java.lang.IllegalStateException::class.java) { | ||
trie.add(emptyList()) | ||
} | ||
assertEquals(1, trie.add("Tree").count) | ||
assertEquals(2, trie.add("Tree").count) | ||
assertEquals(1, trie.add("Trees").count) | ||
assertEquals(1, trie.add("Treespss").count) | ||
assertEquals(1, trie.add("Game").count) | ||
assertEquals(1, trie.add("Gamer").count) | ||
assertEquals(1, trie.add("Games").count) | ||
assertEquals(2, trie["Tree"]?.count) | ||
assertEquals(1, trie["Trees"]?.count) | ||
assertEquals(1, trie["Gamer"]?.count) | ||
assertNull(trie["Treesp"]) | ||
assertNull(trie["Treessss"]) | ||
|
||
assertEquals(setOf("Tree", "Trees", "Treespss", "Game", "Gamer", "Games"), trie.collect()) | ||
} | ||
|
||
@Test | ||
fun testSingleElement() { | ||
val trie = trieOf(listOf(1)) | ||
assertEquals(1, trie.toList().size) | ||
} | ||
|
||
@Test | ||
fun testRemoval() { | ||
val trie = stringTrieOf() | ||
trie.add("abc") | ||
assertEquals(1, trie.toList().size) | ||
trie.add("abcd") | ||
assertEquals(2, trie.toList().size) | ||
trie.add("abcd") | ||
assertEquals(2, trie.toList().size) | ||
trie.add("abcde") | ||
assertEquals(3, trie.toList().size) | ||
|
||
assertNotNull(trie.removeCompletely("abcd")) | ||
assertEquals(2, trie.toList().size) | ||
|
||
assertNull(trie.removeCompletely("ffff")) | ||
assertEquals(2, trie.toList().size) | ||
|
||
assertNotNull(trie.removeCompletely("abcde")) | ||
assertEquals(1, trie.toList().size) | ||
|
||
assertNotNull(trie.removeCompletely("abc")) | ||
assertEquals(0, trie.toList().size) | ||
} | ||
|
||
@Test | ||
fun testSearchingAfterDeletion() { | ||
val trie = stringTrieOf("abc", "abc", "abcde") | ||
assertEquals(2, trie.toList().size) | ||
assertEquals(2, trie["abc"]?.count) | ||
|
||
val removed1 = trie.remove("abc") | ||
assertNotNull(removed1) | ||
|
||
val find = trie["abc"] | ||
assertNotNull(find) | ||
assertEquals(1, find!!.count) | ||
|
||
val removed2 = trie.remove("abc") | ||
assertNotNull(removed2) | ||
} | ||
|
||
@Test | ||
fun testTraverse() { | ||
val trie = Trie(Data::id).apply { | ||
add((1..10).map { Data(it.toLong(), it) }) | ||
add((1..10).mapIndexed { index, it -> if (index == 5) Data(3L, it) else Data(it.toLong(), it) }) | ||
} | ||
|
||
val paths = trie.toList() | ||
assertEquals(2, paths.size) | ||
assertNotEquals(paths[0], paths[1]) | ||
} | ||
|
||
@Test | ||
fun testNoDuplications() { | ||
val trie = trieOf( | ||
(1..10), | ||
(1..10), | ||
(1..10), | ||
(1..10), | ||
(1..10), | ||
) | ||
|
||
assertEquals(1, trie.toList().size) | ||
assertEquals(5, trie[(1..10)]!!.count) | ||
} | ||
|
||
@Test | ||
fun testAcceptsNulls() { | ||
val trie = trieOf( | ||
listOf(null), | ||
listOf(null, null), | ||
listOf(null, null, null), | ||
) | ||
|
||
assertEquals(3, trie.toList().size) | ||
for (i in 1 .. 3) { | ||
assertEquals(1, trie[(1..i).map { null }]!!.count) | ||
} | ||
} | ||
|
||
@Test | ||
fun testAddPrefixAfterWord() { | ||
val trie = stringTrieOf() | ||
trie.add("Hello, world!") | ||
trie.add("Hello") | ||
|
||
assertEquals(setOf("Hello, world!", "Hello"), trie.collect()) | ||
} | ||
|
||
data class Data(val id: Long, val number: Int) | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.