Skip to content

UTBot Python updates #2481

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 5 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions utbot-python/samples/samples/collections/recursive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import typing


def make_recursive_list(x: int):
xs = [1, 2]
xs.append(xs)
xs.append(x)
return xs


def make_recursive_dict(x: int, y: int):
d = {1: 2}
d[x] = d
d[y] = x
return d


if __name__ == '__main__':
make_recursive_dict(3, 4)
7 changes: 7 additions & 0 deletions utbot-python/samples/samples/primitives/regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ def check_regex(string: str) -> bool:
if re.match(pattern, string):
return True
return False


def create_pattern(string: str):
if len(string) > 10:
return re.compile(rf"{string}")
else:
return re.compile(string)
10 changes: 9 additions & 1 deletion utbot-python/samples/samples/primitives/str_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,12 @@ def starts_with(s: str):


def join_str(strings: typing.List[str]):
return "--".join(strings)
return "--".join(strings)


def separated_str(x: int):
if x == 1:
return r"fjalsdk\\nfjlask"
if 1 < x < 100:
return "fjalsd\n" * x
return x
15 changes: 13 additions & 2 deletions utbot-python/samples/test_configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@
}
]
},
{
"name": "recursive",
"groups": [
{
"classes": null,
"methods": null,
"timeout": 30,
"coverage": 100
}
]
},
{
"name": "sets",
"groups": [
Expand Down Expand Up @@ -433,7 +444,7 @@
{
"classes": null,
"methods": null,
"timeout": 40,
"timeout": 50,
"coverage": 100
}
]
Expand All @@ -445,7 +456,7 @@
"classes": null,
"methods": null,
"timeout": 160,
"coverage": 100
"coverage": 110
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ object PythonObjectParser {
class MemoryDump(
private val objects: MutableMap<String, MemoryObject>
) {
fun contains(id: String): Boolean {
return objects.containsKey(id)
}

fun getById(id: String): MemoryObject {
return objects[id]!!
}
Expand All @@ -41,7 +45,7 @@ class MemoryDump(
}
}

class TypeInfo(
data class TypeInfo(
val module: String,
val kind: String,
) {
Expand Down Expand Up @@ -88,82 +92,82 @@ class ReduceMemoryObject(
val dictitems: String
) : MemoryObject(id, typeinfo, comparable)

fun PythonTree.PythonTreeNode.toMemoryObject(memoryDump: MemoryDump): String {
fun PythonTree.PythonTreeNode.toMemoryObject(memoryDump: MemoryDump, reload: Boolean = false): String {
val id = this.id.toString()
if (memoryDump.contains(id) && !reload) return id

val typeinfo = TypeInfo(this.type.moduleName, this.type.typeName)
val obj = when (this) {
is PythonTree.PrimitiveNode -> {
ReprMemoryObject(
this.id.toString(),
TypeInfo(this.type.moduleName, this.type.typeName),
id,
typeinfo,
this.comparable,
this.repr
this.repr.replace("\n", "\\\n").replace("\r", "\\\r")
)
}

is PythonTree.ListNode -> {
val draft = ListMemoryObject(id, typeinfo, this.comparable, emptyList())
memoryDump.addObject(draft)

val items = this.items.entries
.sortedBy { it.key }
.map { it.value.toMemoryObject(memoryDump) }
ListMemoryObject(
this.id.toString(),
TypeInfo(this.type.moduleName, this.type.typeName),
this.comparable,
items
)
ListMemoryObject(id, typeinfo, this.comparable, items)
}

is PythonTree.TupleNode -> {
val items = this.items.entries
.sortedBy { it.key }
.map { it.value.toMemoryObject(memoryDump) }
ListMemoryObject(
this.id.toString(),
TypeInfo(this.type.moduleName, this.type.typeName),
this.comparable,
items
)
ListMemoryObject(id, typeinfo, this.comparable, items)
}

is PythonTree.SetNode -> {
val items = this.items.map { it.toMemoryObject(memoryDump) }
ListMemoryObject(
this.id.toString(),
TypeInfo(this.type.moduleName, this.type.typeName),
this.comparable,
items
)
ListMemoryObject(id, typeinfo, this.comparable, items)
}

is PythonTree.DictNode -> {
val draft = DictMemoryObject(id, typeinfo, this.comparable, emptyMap())
memoryDump.addObject(draft)

val items = this.items.entries
.associate {
it.key.toMemoryObject(memoryDump) to it.value.toMemoryObject(memoryDump)
}
DictMemoryObject(
this.id.toString(),
TypeInfo(this.type.moduleName, this.type.typeName),
this.comparable,
items
)
DictMemoryObject(id, typeinfo, this.comparable, items)
}

is PythonTree.ReduceNode -> {
val argsIds = PythonTree.ListNode(this.args.withIndex().associate { it.index to it.value }.toMutableMap())
val draft = ReduceMemoryObject(
id,
typeinfo,
this.comparable,
TypeInfo(
this.constructor.moduleName,
this.constructor.typeName,
),
argsIds.toMemoryObject(memoryDump),
"",
"",
"",
)
memoryDump.addObject(draft)

val stateObjId = if (this.customState) {
this.state["state"]!!
} else {
PythonTree.DictNode(this.state.entries.associate {
PythonTree.fromString(it.key) to it.value
}.toMutableMap())
}
val argsIds = PythonTree.ListNode(this.args.withIndex().associate { it.index to it.value }.toMutableMap())
val listItemsIds =
PythonTree.ListNode(this.listitems.withIndex().associate { it.index to it.value }.toMutableMap())
val dictItemsIds = PythonTree.DictNode(this.dictitems.toMutableMap())
ReduceMemoryObject(
this.id.toString(),
TypeInfo(
this.type.moduleName,
this.type.typeName,
),
id,
typeinfo,
this.comparable,
TypeInfo(
this.constructor.moduleName,
Expand Down Expand Up @@ -200,33 +204,36 @@ fun MemoryObject.toPythonTree(
}

is DictMemoryObject -> {
PythonTree.DictNode(
val draft = PythonTree.DictNode(
id,
items.entries.associate {
memoryDump.getById(it.key).toPythonTree(memoryDump, visited) to
memoryDump.getById(it.value).toPythonTree(memoryDump, visited)
}.toMutableMap()
mutableMapOf()
)
visited[this.id] = draft
items.entries.map {
draft.items[memoryDump.getById(it.key).toPythonTree(memoryDump, visited)] =
memoryDump.getById(it.value).toPythonTree(memoryDump, visited)
}
draft
}

is ListMemoryObject -> {
val elementsMap = items.withIndex().associate {
it.index to
memoryDump.getById(it.value).toPythonTree(memoryDump, visited)
}.toMutableMap()
when (this.qualname) {
"builtins.tuple" -> {
PythonTree.TupleNode(this.id.toLong(), elementsMap)
}

"builtins.set" -> {
PythonTree.SetNode(this.id.toLong(), elementsMap.values.toMutableSet())
}
val draft = when (this.qualname) {
"builtins.tuple" -> PythonTree.TupleNode(id, mutableMapOf())
"builtins.set" -> PythonTree.SetNode(id, mutableSetOf())
else -> PythonTree.ListNode(id, mutableMapOf())
}
visited[this.id] = draft

else -> {
PythonTree.ListNode(this.id.toLong(), elementsMap)
items.mapIndexed { index, valueId ->
val value = memoryDump.getById(valueId).toPythonTree(memoryDump, visited)
when (draft) {
is PythonTree.TupleNode -> draft.items[index] = value
is PythonTree.SetNode -> draft.items.add(value)
is PythonTree.ListNode -> draft.items[index] = value
else -> {}
}
}
draft
}

is ReduceMemoryObject -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ object PythonTree {

open val children: List<PythonTreeNode> = emptyList()

fun isRecursive(): Boolean {
return isRecursiveObject(this)
}

override fun toString(): String {
return type.name + children.toString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ val pythonSetClassId = PythonClassId("builtins.set")
val pythonBytearrayClassId = PythonClassId("builtins.bytearray")
val pythonBytesClassId = PythonClassId("builtins.bytes")
val pythonExceptionClassId = PythonClassId("builtins.Exception")
val pythonRePatternClassId = PythonClassId("re.Pattern")
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class PythonCgMethodConstructor(context: CgContext) : CgMethodConstructor(contex
emptyLineIfNeeded()
if (elementsHaveSameStructure) {
val index = newVar(pythonNoneClassId, keyName) {
CgLiteral(pythonNoneClassId, "None")
CgPythonRepr(pythonNoneClassId, "None")
}
forEachLoop {
innerBlock {
Expand Down Expand Up @@ -341,6 +341,12 @@ class PythonCgMethodConstructor(context: CgContext) : CgMethodConstructor(contex
depth: Int = maxDepth,
useExpectedAsValue: Boolean = false
) {
if (!expectedNode.comparable && expectedNode.isRecursive()) {
emptyLineIfNeeded()
comment("Cannot compare recursive objects") // TODO: add special function for recursive comparison
assertIsInstance(expected, actual)
return
}
if (expectedNode.comparable || depth == 0) {
val expectedValue = if (useExpectedAsValue) {
expected
Expand Down
Loading