Files
VCCPlusOSS/platforms/macos/Tests/CMOCFloatingPointTests.swift
T

92 lines
5.8 KiB
Swift

// SPDX-License-Identifier: GPL-3.0-or-later
import XCTest
final class CMOCFloatingPointTests: XCTestCase {
private func evaluator(_ bytes: [UInt8], format: String? = "coco-basic-40", size: Int? = nil,
address: Int = 0x3000, unavailable: Int? = nil, stale: Bool = false) -> CMOCWatchEvaluator {
let type = CMOCVariableDebug.CType(name: "float", kind: "floating", size: size ?? bytes.count, floatingFormat: format)
let map = CMOCProjectSourceMap(entryPoint: 0x2800, binarySHA256: "", sourceHashes: [:],
instructions: [.init(sourcePath: "main.c", sourceLine: 5, address: 0x2800, bytes: [0x12], assembly: "NOP")],
objectHashes: [:], listingHashes: [:], linkMapSHA256: "")
let metadata = CMOCVariableDebug(functions: [.init(name: "main", sourcePath: "main.c", entry: 0x2800, body: 0x2800, end: 0x2820, frame: true)],
variables: [.init(name: "value", sourcePath: "main.c", line: 1, type: type, address: address, offset: nil, scope: nil, isStatic: false)], sourceMap: map)
var memory = Data(repeating: 0, count: 65536), available = Data(repeating: 1, count: 65536)
memory[0x2800] = stale ? 0x39 : 0x12
for (offset, byte) in bytes.enumerated() where address + offset < 65536 { memory[address + offset] = byte }
if let unavailable { available[unavailable] = 0 }
return CMOCWatchEvaluator(metadata: metadata, context: .init(pc: 0x2800, u: 0x4000, memory: memory, available: available))
}
private func ieee(_ bits: UInt32) -> CMOCWatchEvaluator {
evaluator([UInt8(bits >> 24), UInt8(truncatingIfNeeded: bits >> 16), UInt8(truncatingIfNeeded: bits >> 8), UInt8(truncatingIfNeeded: bits)], format: "ieee754-binary32-be")
}
func testBasicStoredValuesAndPrecision() throws {
let cases: [([UInt8], String)] = [
([0, 0, 0, 0, 0], "0"), ([0, 255, 255, 255, 255], "0"),
([0x81, 0, 0, 0, 0], "1"), ([0x80, 0, 0, 0, 0], "0.5"),
([0x82, 0xa0, 0, 0, 0], "-2.5"), ([0x84, 0x48, 0, 0, 0], "12.5")]
for (bytes, expected) in cases {
let engine = evaluator(bytes)
XCTAssertEqual(engine.evaluate("value").value, expected)
XCTAssertNil(engine.evaluate("value").error)
XCTAssertEqual(engine.treeNode(at: .init(root: .variable("value"))).value, expected)
}
let next = evaluator([0x81, 0, 0, 0, 1]).evaluate("value")
XCTAssertEqual(next.value, "1.0000000005", "Keep BASIC's low significand bits; do not convert through Float32")
let small = try XCTUnwrap(Double(evaluator([1, 0, 0, 0, 0]).evaluate("value").value))
XCTAssertEqual(small, pow(2, -128), accuracy: pow(2, -128) * 1e-10)
let large = try XCTUnwrap(Double(evaluator([255, 0x7f, 255, 255, 255]).evaluate("value").value))
XCTAssertEqual(large, pow(2, 127) - pow(2, 95), accuracy: pow(2, 127) * 1e-10)
}
func testIEEEStoredValuesSpecialsAndSubnormals() throws {
for (bits, expected): (UInt32, String) in [(0, "0"), (0x80000000, "-0"), (0x3f800000, "1"),
(0xc0200000, "-2.5"), (0x7f800000, "Infinity"), (0xff800000, "-Infinity"),
(0x7fc00001, "NaN"), (0x7f800001, "NaN"), (0xffc00001, "NaN")] {
XCTAssertEqual(ieee(bits).evaluate("value").value, expected)
XCTAssertNil(ieee(bits).evaluate("value").error)
}
XCTAssertEqual(ieee(0x3f800001).evaluate("value").value, "1.00000012")
let minimumSubnormal: Double = pow(2.0, -149.0)
let minimumNormal: Double = pow(2.0, -126.0)
let maximumSubnormal: Double = minimumNormal - minimumSubnormal
let maximumSignificand: Double = 2.0 - pow(2.0, -23.0)
let maximumFinite: Double = maximumSignificand * pow(2.0, 127.0)
let finiteCases: [(UInt32, Double)] = [
(1, minimumSubnormal), (0x007fffff, maximumSubnormal),
(0x00800000, minimumNormal), (0x7f7fffff, maximumFinite)
]
for (bits, expected) in finiteCases {
let actual = try XCTUnwrap(Double(ieee(bits).evaluate("value").value))
XCTAssertEqual(actual, expected, accuracy: expected * 1e-8)
}
}
func testUnknownLegacyAndInconsistentFormatsStayUnavailable() {
for engine in [evaluator([0x81, 0, 0, 0, 0], format: nil),
evaluator([0x81, 0, 0, 0, 0], format: "unknown"),
evaluator([0x81, 0, 0, 0, 0], format: "ieee754-binary32-be"),
evaluator([0x3f, 0x80, 0, 0], format: "coco-basic-40"),
evaluator(Array(repeating: 0, count: 8), format: "unsupported")] {
XCTAssertNotNil(engine.evaluate("value").error)
XCTAssertEqual(engine.treeNode(at: .init(root: .watch("value"))).value, "Unavailable")
}
}
func testFloatInspectionKeepsMemoryAndBuildGuards() {
let bytes: [UInt8] = [0x81, 0, 0, 0, 0]
for engine in [evaluator(bytes, unavailable: 0x3004), evaluator(bytes, address: 65533), evaluator(bytes, stale: true)] {
XCTAssertNotNil(engine.evaluate("value").error)
XCTAssertNotNil(engine.treeNode(at: .init(root: .variable("value"))).error)
}
let engine = evaluator(bytes)
let before = engine.context.memory
XCTAssertEqual(engine.evaluate("value").value, "1")
XCTAssertEqual(engine.context.memory, before)
}
func testFloatingWatchArithmeticIsExplicitlyRejected() {
let engine = evaluator([0x81, 0, 0, 0, 0])
for expression in ["value + 1", "value == 1", "-value", "!value"] {
XCTAssertTrue(engine.evaluate(expression).error?.contains("floating-point watch arithmetic") == true)
}
XCTAssertEqual(engine.evaluate("(value)").value, "1")
XCTAssertEqual(engine.evaluate("&value").value, "$3000")
}
}