Skip to content
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

Disable null values #120

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import org.vorpal.research.kex.config.kexConfig
import org.vorpal.research.kex.descriptor.*
import org.vorpal.research.kex.ktype.KexClass
import org.vorpal.research.kex.ktype.KexRtManager.rtMapped
import org.vorpal.research.kex.ktype.KexType
import org.vorpal.research.kex.ktype.kexType
import org.vorpal.research.kex.util.KfgTargetFilter
import org.vorpal.research.kfg.ClassManager
import org.vorpal.research.kfg.ir.Method
import kotlin.random.Random

@Serializable
Expand Down Expand Up @@ -92,3 +95,68 @@ fun Parameters<Descriptor>.filterIgnoredStatic(): Parameters<Descriptor> {
}
return Parameters(instance, arguments, filteredStatics)
}

fun Parameters<Descriptor>.replaceNullsWithDefaultValues(method: Method, cm: ClassManager): Parameters<Descriptor> {
val parametersWithoutNulls = arguments
.zip(method.argTypes)
.map { (descriptor, type) -> transformExistingDescriptor(descriptor, type.kexType, cm) }
return Parameters(instance, parametersWithoutNulls, statics)
}

private fun transformExistingDescriptor(descriptor: Descriptor, type: KexType, cm: ClassManager, alreadyCopied: MutableMap<Descriptor, Descriptor> = mutableMapOf()): Descriptor {
if (descriptor in alreadyCopied) return alreadyCopied[descriptor]!!
return when (descriptor) {
khbminus marked this conversation as resolved.
Show resolved Hide resolved
is ConstantDescriptor.Null -> type.newInstance(cm).also { alreadyCopied[descriptor] = it }
is ConstantDescriptor -> descriptor.also { alreadyCopied[descriptor] = it }
is ArrayDescriptor -> ArrayDescriptor(descriptor.elementType, descriptor.length)
.apply {
alreadyCopied[descriptor] = this
(0 until descriptor.length).forEach { index ->
this[index] = transformExistingDescriptor(
descriptor[index] ?: ConstantDescriptor.Null,
descriptor.elementType,
cm,
alreadyCopied
)
}
}

is ObjectDescriptor -> ObjectDescriptor(descriptor.klass).apply {
alreadyCopied[descriptor] = this
descriptor.fields.forEach { (key, desc) ->
val (name, fieldType) = key
set(name to fieldType, transformExistingDescriptor(desc, fieldType, cm, alreadyCopied))
}
}

is ClassDescriptor -> ClassDescriptor(descriptor.klass).apply {
alreadyCopied[descriptor] = this
descriptor.fields.forEach { (key, desc) ->
val (name, fieldType) = key
set(name to fieldType, transformExistingDescriptor(desc, fieldType, cm, alreadyCopied))
}
}

is MockDescriptor -> MockDescriptor(descriptor.klass).apply {
alreadyCopied[descriptor] = this
descriptor.fields.forEach { (key, desc) ->
val (name, fieldType) = key
set(name to fieldType, transformExistingDescriptor(desc, fieldType, cm, alreadyCopied))
}
}
}
}

private fun KexType.newInstance(cm: ClassManager): Descriptor {
val descriptor = descriptor { default(this@newInstance, nullable = false) }
if (descriptor !is ClassDescriptor) return descriptor
val kfgClass = (this as KexClass).kfgClass(cm.type)
kfgClass
.fields
.filterNot { it.isStatic }
.forEach {
if (!it.isStatic)
descriptor[it.name to this] = it.type.kexType.newInstance(cm)
}
return descriptor
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import org.vorpal.research.kex.ktype.KexRtManager.rtMapped
import org.vorpal.research.kex.ktype.kexType
import org.vorpal.research.kex.mocking.performMocking
import org.vorpal.research.kex.mocking.withoutMocksOrNull
import org.vorpal.research.kex.parameters.Parameters
import org.vorpal.research.kex.parameters.concreteParameters
import org.vorpal.research.kex.parameters.filterIgnoredStatic
import org.vorpal.research.kex.parameters.filterStaticFinals
import org.vorpal.research.kex.parameters.*
import org.vorpal.research.kex.smt.AsyncChecker
import org.vorpal.research.kex.smt.AsyncIncrementalChecker
import org.vorpal.research.kex.smt.Result
Expand Down Expand Up @@ -79,6 +76,12 @@ suspend fun Method.checkAsync(
.parameters
.filterStaticFinals(ctx.cm)
.concreteParameters(ctx.cm, ctx.accessLevel, ctx.random)
.let {
if (!kexConfig.getBooleanValue("kex", "generateNulls", true))
it.replaceNullsWithDefaultValues(this, ctx.cm)
else
it
}
.also { log.debug { "Generated params:\n$it" } }
.filterIgnoredStatic()
} catch (e: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.vorpal.research.kex.concolic

import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import org.junit.Test
import org.vorpal.research.kex.config.RuntimeConfig
import kotlin.time.ExperimentalTime

@ExperimentalTime
@ExperimentalSerializationApi
@InternalSerializationApi
@DelicateCoroutinesApi
class DisableNullsTest : ConcolicTest("do-not-generate-nulls") {
init {
RuntimeConfig.setValue("kex", "generateNulls", false)
}
@Test
fun boxedTypeWithoutNullsTest() {
assertCoverage(cm["org/vorpal/research/kex/test/nullability/BoxedTypeNulls"], 7.0 / 9.0, eps = 0.1)
}
@Test
fun primitiveArrayWithoutNullsTest() {
assertCoverage(cm["org/vorpal/research/kex/test/nullability/PrimitiveArrayNulls"], 7.0 / 9.0, eps = 0.1)
}
@Test
fun boxedArrayWithoutNullsTest() {
assertCoverage(cm["org/vorpal/research/kex/test/nullability/BoxedArrayNulls"], 20.0 / 24.0, eps = 0.1)
}
@Test
fun listWithoutNullsTest() {
assertCoverage(cm["org/vorpal/research/kex/test/nullability/ListNulls"], 20.0 / 24.0, eps = 0.1)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.vorpal.research.kex.concolic

import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import org.junit.Test
import org.vorpal.research.kex.config.RuntimeConfig
import kotlin.time.ExperimentalTime

@ExperimentalTime
@ExperimentalSerializationApi
@InternalSerializationApi
@DelicateCoroutinesApi
class GenerateNullsTest : ConcolicTest("generate-nulls") {
init {
RuntimeConfig.setValue("kex", "generateNulls", true)
}
@Test
fun boxedTypeWithNullsTest() {
assertCoverage(cm["org/vorpal/research/kex/test/nullability/BoxedTypeNulls"], 1.0, eps = 0.1)
}
@Test
fun primitiveArrayWithNullsTest() {
assertCoverage(cm["org/vorpal/research/kex/test/nullability/PrimitiveArrayNulls"], 1.0, eps = 0.1)
}
@Test
fun boxedArrayWithNullsTest() {
assertCoverage(cm["org/vorpal/research/kex/test/nullability/BoxedArrayNulls"], 1.0, eps = 0.1)
}
@Test
fun listWithNullsTest() {
assertCoverage(cm["org/vorpal/research/kex/test/nullability/ListNulls"], 1.0, eps = 0.1)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.vorpal.research.kex.test.nullability;

public class BoxedArrayNulls {
int function(Integer[] x) {
if (x == null) {
return -1;
}
for (int i = 0; i < x.length; i++) {
if (x[i] == null) {
return i;
}
}
return x.length;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.vorpal.research.kex.test.nullability;

public class BoxedTypeNulls {
int function(Integer x) {
if (x == null) {
return 1;
}
return 2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.vorpal.research.kex.test.nullability;

import java.util.List;

public class ListNulls {
int function(List<Integer> x) {
if (x == null) {
return -1;
}
for (int i = 0; i < x.size(); i++) {
if (x.get(i) == null) {
return i;
}
}
return x.size();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.vorpal.research.kex.test.nullability;

public class PrimitiveArrayNulls {
int function(int[] x) {
if (x == null) {
return -1;
}
return 0;
}
}
Loading