Skip to content
Draft
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
@@ -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<TsTestValue.TsNumber, TsTestValue.TsBoolean>(
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<TsTestValue.TsBoolean>(
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")
}
27 changes: 27 additions & 0 deletions usvm-ts/src/test/resources/models/NativeBooleanCall.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading