-
Notifications
You must be signed in to change notification settings - Fork 45
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 1 commit
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
Next
Next commit
Minimize UtExecution number produced by fuzzing and collect coverage …
…statistic
- Loading branch information
commit 3c39682de8ae56ab3527f44881e4c4d9a4db3cb8
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,130 @@ | ||
| 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 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}) | ||
|
|
||
| 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>>() | ||
|
|
||
| fun add(values: Iterable<T>): Node<T> { | ||
| val root = try { values.first() } catch (e: NoSuchElementException) { error("Empty list are not allowed") } | ||
|
sergeypospelov marked this conversation as resolved.
|
||
| 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 | ||
| } | ||
|
|
||
| fun remove(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.
|
||
| 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>{ | ||
|
sergeypospelov marked this conversation as resolved.
Outdated
|
||
| 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> | ||
| } | ||
114 changes: 114 additions & 0 deletions
114
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,114 @@ | ||
| 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.remove("abcd")) | ||
| assertEquals(2, trie.toList().size) | ||
|
|
||
| assertNull(trie.remove("ffff")) | ||
| assertEquals(2, trie.toList().size) | ||
|
|
||
| assertNotNull(trie.remove("abcde")) | ||
| assertEquals(1, trie.toList().size) | ||
|
|
||
| assertNotNull(trie.remove("abc")) | ||
| assertEquals(0, trie.toList().size) | ||
| } | ||
|
|
||
| @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.