Skip to content

Commit 5ba7322

Browse files
[Andrew Polyakov] Support for numpy for python fuzzing
1 parent 2b37f07 commit 5ba7322

File tree

14 files changed

+97
-14
lines changed

14 files changed

+97
-14
lines changed

utbot-cli-python/src/main/kotlin/org/utbot/cli/language/python/Application.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private val logger = KotlinLogging.logger {}
1818
class UtBotPythonCli : CliktCommand(name = "UnitTestBot Python Command Line Interface") {
1919
private val verbosity by option("--verbosity", help = "Changes verbosity level, case insensitive")
2020
.enum<Level>(ignoreCase = true)
21-
.default(Level.INFO)
21+
.default(Level.DEBUG)
2222

2323
override fun run() = setVerbosity(verbosity)
2424

utbot-cli-python/src/main/kotlin/org/utbot/cli/language/python/TestWriter.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class TestWriter {
99

1010
fun generateTestCode(): String {
1111
val (importLines, code) = testCode.fold(mutableListOf<String>() to StringBuilder()) { acc, s ->
12-
val lines = s.split(System.lineSeparator())
12+
// val lines = s.split(System.lineSeparator())
13+
val lines = s.split("(\\r\\n|\\r|\\n)".toRegex())
1314
val firstClassIndex = lines.indexOfFirst { it.startsWith("class") }
1415
lines.take(firstClassIndex).forEach { line -> if (line !in acc.first) acc.first.add(line) }
1516
lines.drop(firstClassIndex).forEach { line -> acc.second.append(line + System.lineSeparator()) }

utbot-python/src/main/kotlin/org/utbot/python/evaluation/PythonWorkerManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class PythonWorkerManager(
8282
coverageReceiver.address().first,
8383
coverageReceiver.address().second,
8484
"--logfile", logfile.absolutePath,
85-
"--loglevel", logLevel, // "DEBUG", "INFO", "WARNING", "ERROR"
85+
"--loglevel", "DEBUG", // "DEBUG", "INFO", "WARNING", "ERROR"
8686
"--coverage_type", coverageMeasureMode.toString(), // "lines", "instructions"
8787
sendCoverageContinuously.toSendCoverageContinuouslyString(), // "--send_coverage", "--no-send_coverage"
8888
doNotGenerateStateAssertions.toDoNotGenerateStateAssertionsString(), // "--generate_state_assertions", // "--no-generate_state_assertions"

utbot-python/src/main/kotlin/org/utbot/python/evaluation/serialization/ExecutionResultDeserializer.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ object ExecutionResultDeserializer {
1919
.withSubtype(DictMemoryObject::class.java, "dict")
2020
.withSubtype(ReduceMemoryObject::class.java, "reduce")
2121
.withSubtype(IteratorMemoryObject::class.java, "iterator")
22+
.withSubtype(NdarrayMemoryObject::class.java, "ndarray")
2223
)
2324
.addLast(KotlinJsonAdapterFactory())
2425
.build()

utbot-python/src/main/kotlin/org/utbot/python/evaluation/serialization/PythonObjectParser.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ object PythonObjectParser {
1515
.withSubtype(DictMemoryObject::class.java, "dict")
1616
.withSubtype(ReduceMemoryObject::class.java, "reduce")
1717
.withSubtype(IteratorMemoryObject::class.java, "iterator")
18+
.withSubtype(NdarrayMemoryObject::class.java, "ndarray")
1819
)
1920
.addLast(KotlinJsonAdapterFactory())
2021
.build()
@@ -101,6 +102,13 @@ class ReduceMemoryObject(
101102
val dictitems: String
102103
) : MemoryObject(id, typeinfo, comparable)
103104

105+
class NdarrayMemoryObject(
106+
id: String,
107+
typeinfo: TypeInfo,
108+
comparable: Boolean,
109+
val items: List<String>,
110+
) : MemoryObject(id, typeinfo, comparable)
111+
104112
fun PythonTree.PythonTreeNode.toMemoryObject(memoryDump: MemoryDump, reload: Boolean = false): String {
105113
val id = this.id.toString()
106114
if (memoryDump.contains(id) && !reload) return id
@@ -116,6 +124,12 @@ fun PythonTree.PythonTreeNode.toMemoryObject(memoryDump: MemoryDump, reload: Boo
116124
)
117125
}
118126

127+
is PythonTree.NdarrayNode -> { // TODO: Optimize for ndarray
128+
val items = this.items.entries
129+
.map { it.value.toMemoryObject(memoryDump) }
130+
NdarrayMemoryObject(id, typeinfo, this.comparable, items) // TODO: Ndarray to memory object
131+
}
132+
119133
is PythonTree.ListNode -> {
120134
val draft = ListMemoryObject(id, typeinfo, this.comparable, emptyList())
121135
memoryDump.addObject(draft)
@@ -304,6 +318,24 @@ fun MemoryObject.toPythonTree(
304318
}
305319
prevObj
306320
}
321+
322+
is NdarrayMemoryObject -> {
323+
val draft = when (this.qualname) {
324+
"np.ndarray" -> PythonTree.NdarrayNode(id, mutableMapOf())
325+
"numpy.ndarray" -> PythonTree.NdarrayNode(id, mutableMapOf())
326+
else -> PythonTree.ListNode(id, mutableMapOf())
327+
}
328+
visited[this.id] = draft
329+
330+
items.mapIndexed { index, valueId ->
331+
val value = memoryDump.getById(valueId).toPythonTree(memoryDump, visited)
332+
when (draft) {
333+
is PythonTree.NdarrayNode -> draft.items[index] = value
334+
else -> {}
335+
}
336+
}
337+
draft
338+
}
307339
}
308340
obj.comparable = this.comparable
309341
return obj

utbot-python/src/main/kotlin/org/utbot/python/framework/api/python/PythonTree.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.utbot.python.framework.api.python.util.pythonDictClassId
55
import org.utbot.python.framework.api.python.util.pythonFloatClassId
66
import org.utbot.python.framework.api.python.util.pythonIntClassId
77
import org.utbot.python.framework.api.python.util.pythonIteratorClassId
8+
import org.utbot.python.framework.api.python.util.pythonNdarrayClassId
89
import org.utbot.python.framework.api.python.util.pythonListClassId
910
import org.utbot.python.framework.api.python.util.pythonNoneClassId
1011
import org.utbot.python.framework.api.python.util.pythonObjectClassId
@@ -154,6 +155,23 @@ object PythonTree {
154155
else false
155156
}
156157
}
158+
class NdarrayNode(
159+
id: Long,
160+
val items: MutableMap<Int, PythonTreeNode>
161+
) : PythonTreeNode(id, pythonNdarrayClassId) {
162+
constructor(items: MutableMap<Int, PythonTreeNode>) : this(PythonIdGenerator.createId(), items)
163+
164+
override val children: List<PythonTreeNode>
165+
get() = items.values.toList()
166+
167+
override fun typeEquals(other: Any?): Boolean {
168+
return if (other is NdarrayNode)
169+
children.zip(other.children).all {
170+
it.first.typeEquals(it.second)
171+
}
172+
else false
173+
}
174+
}
157175

158176
class DictNode(
159177
id: Long,

utbot-python/src/main/kotlin/org/utbot/python/framework/api/python/util/PythonIdUtils.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ val pythonBytesClassId = PythonClassId("builtins.bytes")
2222
val pythonExceptionClassId = PythonClassId("builtins.Exception")
2323
val pythonIteratorClassId = PythonClassId("typing.Iterator")
2424
val pythonRePatternClassId = PythonClassId("re.Pattern")
25-
val pythonStopIterationClassId = PythonClassId("builtins.StopIteration")
25+
val pythonStopIterationClassId = PythonClassId("builtins.StopIteration")
26+
val pythonNdarrayClassId = PythonClassId("numpy.ndarray")

utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/tree/PythonCgVariableConstructor.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,7 @@ import org.utbot.python.framework.api.python.util.pythonDictClassId
2222
import org.utbot.python.framework.api.python.util.pythonListClassId
2323
import org.utbot.python.framework.api.python.util.pythonNoneClassId
2424
import org.utbot.python.framework.codegen.PythonCgLanguageAssistant
25-
import org.utbot.python.framework.codegen.model.tree.CgPythonDict
26-
import org.utbot.python.framework.codegen.model.tree.CgPythonIndex
27-
import org.utbot.python.framework.codegen.model.tree.CgPythonIterator
28-
import org.utbot.python.framework.codegen.model.tree.CgPythonList
29-
import org.utbot.python.framework.codegen.model.tree.CgPythonRepr
30-
import org.utbot.python.framework.codegen.model.tree.CgPythonSet
31-
import org.utbot.python.framework.codegen.model.tree.CgPythonTree
32-
import org.utbot.python.framework.codegen.model.tree.CgPythonTuple
25+
import org.utbot.python.framework.codegen.model.tree.*
3326

3427
class PythonCgVariableConstructor(cgContext: CgContext) : CgVariableConstructor(cgContext) {
3528
private val nameGenerator = CgComponents.getNameGeneratorBy(context)
@@ -74,6 +67,11 @@ class PythonCgVariableConstructor(cgContext: CgContext) : CgVariableConstructor(
7467
}
7568
}
7669

70+
is PythonTree.NdarrayNode -> {
71+
val items = objectNode.items.values.map { pythonBuildObject(it) }
72+
Pair(CgPythonNdarray(items.map { it.first }), items.flatMap { it.second })
73+
}
74+
7775
is PythonTree.TupleNode -> {
7876
val items = objectNode.items.values.map { pythonBuildObject(it) }
7977
Pair(CgPythonTuple(items.map {it.first}), items.flatMap { it.second })
@@ -194,6 +192,8 @@ class PythonCgVariableConstructor(cgContext: CgContext) : CgVariableConstructor(
194192
return Pair(obj, context.currentBlock.toList())
195193
}
196194

195+
196+
// TODO: NDArray
197197
else -> {
198198
throw UnsupportedOperationException()
199199
}

utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/visitor/CgPythonRenderer.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import org.utbot.python.framework.codegen.model.tree.CgPythonFunctionCall
7171
import org.utbot.python.framework.codegen.model.tree.CgPythonIndex
7272
import org.utbot.python.framework.codegen.model.tree.CgPythonIterator
7373
import org.utbot.python.framework.codegen.model.tree.CgPythonList
74+
import org.utbot.python.framework.codegen.model.tree.CgPythonNdarray
7475
import org.utbot.python.framework.codegen.model.tree.CgPythonNamedArgument
7576
import org.utbot.python.framework.codegen.model.tree.CgPythonRange
7677
import org.utbot.python.framework.codegen.model.tree.CgPythonRepr
@@ -559,6 +560,17 @@ internal class CgPythonRenderer(
559560
print("]")
560561
}
561562

563+
override fun visit(element: CgPythonNdarray) {
564+
if (element.elements.isEmpty()) {
565+
print("numpy.array([])")
566+
} else{
567+
print("numpy.array([")
568+
element.elements.renderSeparated()
569+
print("])")
570+
}
571+
572+
}
573+
562574
override fun visit(element: CgPythonTuple) {
563575
if (element.elements.isEmpty()) {
564576
print("tuple()")

utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/visitor/CgPythonVisitor.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import org.utbot.python.framework.codegen.model.tree.CgPythonTree
1515
import org.utbot.python.framework.codegen.model.tree.CgPythonTuple
1616
import org.utbot.python.framework.codegen.model.tree.CgPythonWith
1717
import org.utbot.python.framework.codegen.model.tree.CgPythonZip
18+
import org.utbot.python.framework.codegen.model.tree.CgPythonNdarray
1819

1920
interface CgPythonVisitor<R> : CgVisitor<R> {
2021

@@ -26,6 +27,7 @@ interface CgPythonVisitor<R> : CgVisitor<R> {
2627
fun visit(element: CgPythonDict): R
2728
fun visit(element: CgPythonTuple): R
2829
fun visit(element: CgPythonList): R
30+
fun visit(element: CgPythonNdarray): R
2931
fun visit(element: CgPythonSet): R
3032
fun visit(element: CgPythonIterator): R
3133
fun visit(element: CgPythonTree): R

0 commit comments

Comments
 (0)