diff --git a/utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt b/utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt index d0cee4777d..bb4936b8da 100644 --- a/utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt +++ b/utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt @@ -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() + + 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" } + } } /** diff --git a/utbot-framework-test/src/test/kotlin/org/utbot/examples/lambda/PredicateNotExampleTest.kt b/utbot-framework-test/src/test/kotlin/org/utbot/examples/lambda/PredicateNotExampleTest.kt new file mode 100644 index 0000000000..f57ac5359b --- /dev/null +++ b/utbot-framework-test/src/test/kotlin/org/utbot/examples/lambda/PredicateNotExampleTest.kt @@ -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 }, + ) + } +} diff --git a/utbot-framework/src/main/kotlin/org/utbot/engine/TypeResolver.kt b/utbot-framework/src/main/kotlin/org/utbot/engine/TypeResolver.kt index 64f950a83b..6ad34adc84 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/engine/TypeResolver.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/engine/TypeResolver.kt @@ -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 { @@ -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() - val concreteTypes = mutableSetOf() - + 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) } /** diff --git a/utbot-sample/src/main/java/org/utbot/examples/lambda/PredicateNotExample.java b/utbot-sample/src/main/java/org/utbot/examples/lambda/PredicateNotExample.java new file mode 100644 index 0000000000..3339750760 --- /dev/null +++ b/utbot-sample/src/main/java/org/utbot/examples/lambda/PredicateNotExample.java @@ -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; + } + } +}