diff --git a/usvm-ts/src/test/kotlin/org/usvm/machine/expr/NativeBooleanCallTest.kt b/usvm-ts/src/test/kotlin/org/usvm/machine/expr/NativeBooleanCallTest.kt new file mode 100644 index 0000000000..ccd4826f09 --- /dev/null +++ b/usvm-ts/src/test/kotlin/org/usvm/machine/expr/NativeBooleanCallTest.kt @@ -0,0 +1,86 @@ +package org.usvm.machine.expr + +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.usvm.api.TsTestValue +import org.usvm.machine.TsMachine +import org.usvm.util.TsMethodTestRunner +import org.usvm.util.assertNodeReplay +import org.usvm.util.getResourcePath +import java.nio.file.Path +import kotlin.io.path.readText +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class NativeBooleanCallTest : TsMethodTestRunner() { + private val sourcePath = "/models/NativeBooleanCall.ts" + + override val scene = loadScene(sourcePath) + + @TempDir + lateinit var directory: Path + + @Test + fun `native call preserves symbolic number truthiness`() { + val method = getMethod(methodName = "convertsNumber", className = "NativeBooleanCall") + + discoverProperties( + method = method, + { _, result -> result.value }, + { _, result -> !result.value }, + invariants = arrayOf({ input, result -> + val truthy = input.number != 0.0 && !input.number.isNaN() + result.value == truthy + }), + ) + } + + @Test + fun `execution evaluates all arguments and retains the selected binding`() { + for (methodName in controlMethods) { + val method = getMethod(methodName = methodName, className = "NativeBooleanCall") + + discoverProperties( + method = method, + { result -> result.value }, + invariants = arrayOf({ result -> result.value }), + ) + } + } + + @Test + fun `spread calls stop at an explicit unsupported boundary`() { + val method = getMethod(methodName = "evaluatesFixedSpread", className = "NativeBooleanCall") + + val result = TsMachine(scene = scene, options = options, tsOptions = tsOptions).use { machine -> + machine.analyzeWithMetadata(methods = listOf(method)) + } + + assertFalse(result.engineFailed) + assertTrue(result.states.isEmpty()) + assertTrue(result.unsupportedPaths.isNotEmpty()) + } + + @Test + fun `Node confirms coercion and the control cases`() { + val source = getResourcePath(sourcePath).readText() + val nativeMethods = controlMethods + "evaluatesFixedSpread" + val checks = nativeMethods.joinToString(separator = "\n") { method -> + "if (!NativeBooleanCall.$method()) throw Error('$method');" + } + val numericChecks = """ + for (const input of [0, -0, NaN, 1, -1, Infinity]) { + if (NativeBooleanCall.convertsNumber(input) !== !!input) throw Error('truthiness'); + } + """.trimIndent() + + assertNodeReplay( + source = "$source\n$checks\n$numericChecks", + directory = directory, + name = "native-boolean-call", + timeoutMessage = "Native Boolean call regression timed out", + ) + } + + private val controlMethods = listOf("noArguments", "evaluatesExtras", "respectsLocalBinding") +} diff --git a/usvm-ts/src/test/resources/models/NativeBooleanCall.ts b/usvm-ts/src/test/resources/models/NativeBooleanCall.ts new file mode 100644 index 0000000000..b0b73dcdd7 --- /dev/null +++ b/usvm-ts/src/test/resources/models/NativeBooleanCall.ts @@ -0,0 +1,27 @@ +export class NativeBooleanCall { + static convertsNumber(value: number): boolean { + if (Boolean(value)) return true; + return false; + } + + static noArguments(): boolean { + return Boolean() === false; + } + + static evaluatesExtras(): boolean { + let extra = 0; + const result = Boolean(0, extra = 19); + return !result && extra === 19; + } + + static evaluatesFixedSpread(): boolean { + let extra = 0; + const result = Boolean(...[0, extra = 31]); + return !result && extra === 31; + } + + static respectsLocalBinding(): boolean { + const Boolean = (value: number): number => value + 41; + return Boolean(1) === 42; + } +}