Skip to content

Commit 8a497ad

Browse files
[Andrew Polyakov] Add dimension parameter to NDArrayValueProvider
1 parent 4337876 commit 8a497ad

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@ fun MemoryObject.toPythonTree(
321321

322322
is NdarrayMemoryObject -> {
323323
val draft = when (this.qualname) {
324-
"np.ndarray" -> PythonTree.NdarrayNode(id, mutableMapOf())
325-
"numpy.ndarray" -> PythonTree.NdarrayNode(id, mutableMapOf())
324+
"np.ndarray" -> PythonTree.NdarrayNode(id, mutableMapOf(), listOf())
325+
"numpy.ndarray" -> PythonTree.NdarrayNode(id, mutableMapOf(), listOf())
326326
else -> PythonTree.ListNode(id, mutableMapOf())
327327
}
328328
visited[this.id] = draft

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,10 @@ object PythonTree {
157157
}
158158
class NdarrayNode(
159159
id: Long,
160-
val items: MutableMap<Int, PythonTreeNode>
160+
val items: MutableMap<Int, PythonTreeNode>,
161+
val dimention: List<Int>
161162
) : PythonTreeNode(id, pythonNdarrayClassId) {
162-
constructor(items: MutableMap<Int, PythonTreeNode>) : this(PythonIdGenerator.createId(), items)
163+
constructor(items: MutableMap<Int, PythonTreeNode>, dimention: List<Int>) : this(PythonIdGenerator.createId(), items, dimention)
163164

164165
override val children: List<PythonTreeNode>
165166
get() = items.values.toList()

utbot-python/src/main/kotlin/org/utbot/python/fuzzing/PythonApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ fun pythonDefaultValueProviders(typeStorage: PythonTypeHintsStorage) = listOf(
141141
ConstantValueProvider,
142142
TypeAliasValueProvider,
143143
IteratorValueProvider,
144-
NDArrayValueProvider,
144+
NDArrayValueProvider(typeStorage),
145145
SubtypeValueProvider(typeStorage)
146146
)
147147

utbot-python/src/main/kotlin/org/utbot/python/fuzzing/provider/NDArrayValueProvider.kt

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,63 @@ import org.utbot.fuzzing.Seed
66
import org.utbot.python.framework.api.python.PythonTree
77
import org.utbot.python.framework.api.python.util.pythonNdarrayClassId
88
import org.utbot.python.fuzzing.FuzzedUtType
9-
import org.utbot.python.fuzzing.FuzzedUtType.Companion.activateAnyIf
109
import org.utbot.python.fuzzing.FuzzedUtType.Companion.toFuzzed
1110
import org.utbot.python.fuzzing.PythonFuzzedValue
1211
import org.utbot.python.fuzzing.PythonMethodDescription
1312
import org.utbot.python.fuzzing.PythonValueProvider
14-
import org.utpython.types.pythonAnnotationParameters
13+
import org.utbot.python.fuzzing.provider.utils.*
14+
import org.utpython.types.*
15+
import kotlin.math.abs
1516

1617

1718
private val logger = KotlinLogging.logger {}
1819

19-
object NDArrayValueProvider : PythonValueProvider {
20+
class NDArrayValueProvider(
21+
private val typeStorage: PythonTypeHintsStorage
22+
) : PythonValueProvider {
2023
override fun accept(type: FuzzedUtType): Boolean {
2124
return type.pythonTypeName() == pythonNdarrayClassId.canonicalName
2225
}
2326

27+
@Suppress("UNCHECKED_CAST")
2428
override fun generate(
2529
description: PythonMethodDescription, type: FuzzedUtType
2630
) = sequence {
2731
val param = type.utType.pythonAnnotationParameters()
28-
yield(Seed.Collection( //TODO: Rewrite to construct NDArray objects
29-
construct = Routine.Collection {
32+
val intType = typeStorage.pythonInt
33+
val listType = typeStorage.pythonList
34+
35+
yield(Seed.Recursive(
36+
construct = Routine.Create(
37+
listOf(
38+
listType
39+
.pythonDescription()
40+
.createTypeWithNewAnnotationParameters(listType, listOf(intType)).toFuzzed()
41+
)
42+
) {
3043
PythonFuzzedValue(
44+
3145
PythonTree.NdarrayNode(
32-
emptyMap<Int, PythonTree.PythonTreeNode>().toMutableMap(),
46+
emptyMap<Int, PythonTree.PythonTreeNode>().toMutableMap(), // Generate new Python IntNode
47+
((it.first().tree as PythonTree.ListNode).items as Map<Int, PythonTree.PrimitiveNode>).values.map { node ->
48+
abs(node.repr.take(3).toInt()) % 10
49+
}
3350
), "%var% = ${type.pythonTypeRepresentation()}"
3451
)
3552
},
36-
modify = Routine.ForEach(param.toFuzzed().activateAnyIf(type)) { self, i, values ->
37-
(self.tree as PythonTree.NdarrayNode).items[i] = values.first().tree
38-
}))
53+
// TODO: Call to modify
54+
modify = sequence {
55+
yield(Routine.Call((0 until 10000). map{param[1]}.toFuzzed()) { instance, arguments ->
56+
val obj = instance.tree as PythonTree.NdarrayNode
57+
(0 until obj.dimention.fold(1, Int::times)).map {
58+
obj.items[it] = arguments.get(it).tree
59+
}
60+
61+
})
62+
},
63+
empty = Routine.Empty { PythonFuzzedValue(PythonTree.FakeNode) }
64+
)
65+
66+
)
3967
}
4068
}

0 commit comments

Comments
 (0)