Skip to content

Fix for filtering anonymous function types #984

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 3 commits into from
Sep 21, 2022
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
15 changes: 9 additions & 6 deletions utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,16 @@ object FileUtil {
}

// https://stackoverflow.com/a/68822715
fun byteCountToDisplaySize(bytes: Long): String =
when {
bytes >= 1 shl 30 -> "%.1f GB".format(bytes / (1 shl 30))
bytes >= 1 shl 20 -> "%.1f MB".format(bytes / (1 shl 20))
bytes >= 1 shl 10 -> "%.0f kB".format(bytes / (1 shl 10))
else -> "$bytes bytes"
fun byteCountToDisplaySize(bytes: Long): String {
val bytesInDouble = bytes.toDouble()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this change for?


return when {
bytesInDouble >= 1 shl 30 -> "%.1f GB".format(bytesInDouble / (1 shl 30))
bytesInDouble >= 1 shl 20 -> "%.1f MB".format(bytesInDouble / (1 shl 20))
bytesInDouble >= 1 shl 10 -> "%.0f kB".format(bytesInDouble / (1 shl 10))
else -> "$bytesInDouble bytes"
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.utbot.examples.lambda

import org.junit.jupiter.api.Test
import org.utbot.testcheckers.eq
import org.utbot.tests.infrastructure.UtValueTestCaseChecker

class PredicateNotExampleTest : UtValueTestCaseChecker(testClass = PredicateNotExample::class) {
@Test
fun testPredicateNotExample() {
check(
PredicateNotExample::predicateNotExample,
eq(2),
{ a, r -> a == 5 && r == false },
{ a, r -> a != 5 && r == true },
)
}
}
54 changes: 24 additions & 30 deletions utbot-framework/src/main/kotlin/org/utbot/engine/TypeResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class TypeResolver(private val typeRegistry: TypeRegistry, private val hierarchy
if (numDimensions == 0) baseType else baseType.makeArrayType(numDimensions)
}

return TypeStorage(type, concretePossibleTypes).filterInappropriateClassesForCodeGeneration()
return TypeStorage(type, concretePossibleTypes).removeInappropriateTypes()
}

private fun isInappropriateOrArrayOfMocksOrLocals(numDimensions: Int, baseType: Type?): Boolean {
Expand Down Expand Up @@ -182,46 +182,40 @@ class TypeResolver(private val typeRegistry: TypeRegistry, private val hierarchy
else -> error("Unexpected type $type")
}

return TypeStorage(type, possibleTypes).filterInappropriateClassesForCodeGeneration()
return TypeStorage(type, possibleTypes).removeInappropriateTypes()
}

/**
* Where possible, remove types that are not currently supported by code generation.
* For example, we filter out artificial entities (lambdas are an example of them)
* if the least common type is **not** artificial itself.
* Remove wrapper types and, if any other type is available, artificial entities.
*/
private fun TypeStorage.filterInappropriateClassesForCodeGeneration(): TypeStorage {
val unwantedTypes = mutableSetOf<Type>()
val concreteTypes = mutableSetOf<Type>()

private fun TypeStorage.removeInappropriateTypes(): TypeStorage {
val leastCommonSootClass = (leastCommonType as? RefType)?.sootClass
val keepArtificialEntities = leastCommonSootClass?.isArtificialEntity == true

possibleConcreteTypes.forEach {
val sootClass = (it.baseType as? RefType)?.sootClass ?: run {
// All not RefType should be included in the concreteTypes, e.g., arrays
concreteTypes += it
return@forEach
val appropriateTypes = possibleConcreteTypes.filter {
// All not RefType should be included in the concreteTypes, e.g., arrays
val sootClass = (it.baseType as? RefType)?.sootClass ?: return@filter true

// All artificial entities except anonymous functions should be filtered out if we have another types
if (sootClass.isArtificialEntity) {
if (sootClass.isLambda) {
return@filter true
}

return@filter keepArtificialEntities
}
when {
sootClass.isUtMock -> unwantedTypes += it
sootClass.isArtificialEntity -> {
if (sootClass.isLambda) {
unwantedTypes += it
} else if (keepArtificialEntities) {
concreteTypes += it
}

// All wrappers should filtered out because they could not be instantiated
workaround(WorkaroundReason.HACK) {
if (leastCommonSootClass == OBJECT_TYPE && sootClass.isOverridden) {
return@filter false
}
workaround(WorkaroundReason.HACK) { leastCommonSootClass == OBJECT_TYPE && sootClass.isOverridden } -> Unit
else -> concreteTypes += it
}
}

return if (concreteTypes.isEmpty()) {
copy(possibleConcreteTypes = unwantedTypes)
} else {
copy(possibleConcreteTypes = concreteTypes)
}
return@filter true
}.toSet()

return copy(possibleConcreteTypes = appropriateTypes)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.utbot.examples.lambda;

import java.util.function.*;

public class PredicateNotExample {
public boolean predicateNotExample(int a) {
if (Predicate.not(i -> i.equals(5)).test(a)) {
return true;
} else {
return false;
}
}
}