|
| 1 | +import XCTest |
| 2 | +import VisuStructCore |
| 3 | + |
| 4 | +final class SimulationEngineTests: XCTestCase { |
| 5 | + func testCollatzExampleOutputsSequence() throws { |
| 6 | + let state = runSimulation(try ExampleFiles.document(named: "Collatz.visustruct")) |
| 7 | + XCTAssertEqual( |
| 8 | + state.outputLines, |
| 9 | + ["58", "29", "88", "44", "22", "11", "34", "17", "52", "26", "13", "40", "20", "10", "5", "16", "8", "4", "2", "1"] |
| 10 | + ) |
| 11 | + XCTAssertEqual(state.variables["number"], .int(1)) |
| 12 | + } |
| 13 | + |
| 14 | + func testFibonacciExampleOutputsFirstTenNumbers() throws { |
| 15 | + let state = runSimulation(try ExampleFiles.document(named: "Fibonacci.visustruct")) |
| 16 | + XCTAssertEqual(state.outputLines, ["1", "1", "2", "3", "5", "8", "13", "21", "34", "55"]) |
| 17 | + } |
| 18 | + |
| 19 | + func testNumberEvenExampleUsesInput() throws { |
| 20 | + let document = try ExampleFiles.document(named: "2026-04-23-NumberEven.visustruct") |
| 21 | + XCTAssertEqual(runSimulation(document, inputs: ["4"]).outputLines, ["number is even"]) |
| 22 | + XCTAssertEqual(runSimulation(document, inputs: ["7"]).outputLines, ["number is odd"]) |
| 23 | + } |
| 24 | + |
| 25 | + func testInvalidIntegerInputIsRejectedAndCanBeRetried() { |
| 26 | + let document = VisuStructDocument(elements: [ |
| 27 | + VisuStructElement(type: .anweisung, textLines: ["input: int n"]), |
| 28 | + VisuStructElement(type: .anweisung, textLines: ["print: n * 2"]), |
| 29 | + ]) |
| 30 | + var engine = VisuStructSimulationEngine(document: document) |
| 31 | + engine.step() |
| 32 | + XCTAssertEqual(engine.state.inputRequest?.variableName, "n") |
| 33 | + |
| 34 | + engine.provideInput("abc") |
| 35 | + XCTAssertNotNil(engine.state.inputError) |
| 36 | + XCTAssertNotNil(engine.state.inputRequest) |
| 37 | + |
| 38 | + engine.provideInput("21") |
| 39 | + XCTAssertNil(engine.state.inputRequest) |
| 40 | + engine.step() |
| 41 | + XCTAssertEqual(engine.state.outputLines, ["42"]) |
| 42 | + } |
| 43 | + |
| 44 | + func testCountingLoops() { |
| 45 | + let forLoop = VisuStructDocument(elements: [ |
| 46 | + VisuStructElement(type: .anweisung, textLines: ["int sum = 0"]), |
| 47 | + VisuStructElement( |
| 48 | + type: .forSchleife, |
| 49 | + textLines: ["int i = 1", "i <= 4", "i++"], |
| 50 | + loopBody: [VisuStructElement(type: .anweisung, textLines: ["sum += i"])] |
| 51 | + ), |
| 52 | + ]) |
| 53 | + XCTAssertEqual(runSimulation(forLoop).variables["sum"], .int(10)) |
| 54 | + |
| 55 | + let doUntil = VisuStructDocument(elements: [ |
| 56 | + VisuStructElement(type: .anweisung, textLines: ["int k = 0"]), |
| 57 | + VisuStructElement( |
| 58 | + type: .doUntilSchleife, |
| 59 | + textLines: ["k < 3"], |
| 60 | + loopBody: [VisuStructElement(type: .anweisung, textLines: ["k++"])] |
| 61 | + ), |
| 62 | + ]) |
| 63 | + XCTAssertEqual(runSimulation(doUntil).variables["k"], .int(3)) |
| 64 | + } |
| 65 | + |
| 66 | + func testSwitchFallsBackToDefaultCase() { |
| 67 | + func document(day: Int) -> VisuStructDocument { |
| 68 | + VisuStructDocument(elements: [ |
| 69 | + VisuStructElement(type: .anweisung, textLines: ["int day = \(day)"]), |
| 70 | + VisuStructElement( |
| 71 | + type: .fallauswahl, |
| 72 | + textLines: ["day"], |
| 73 | + cases: [ |
| 74 | + VisuStructCase(name: "1", elements: [VisuStructElement(type: .anweisung, textLines: ["print: \"Montag\""])]), |
| 75 | + VisuStructCase(name: "2", elements: [VisuStructElement(type: .anweisung, textLines: ["print: \"Dienstag\""])]), |
| 76 | + VisuStructCase(name: "default", elements: [VisuStructElement(type: .anweisung, textLines: ["print: \"anderer Tag\""])]), |
| 77 | + ] |
| 78 | + ), |
| 79 | + ]) |
| 80 | + } |
| 81 | + XCTAssertEqual(runSimulation(document(day: 2)).outputLines, ["Dienstag"]) |
| 82 | + XCTAssertEqual(runSimulation(document(day: 5)).outputLines, ["anderer Tag"]) |
| 83 | + } |
| 84 | + |
| 85 | + func testBooleanOperatorsInConditions() { |
| 86 | + func document(_ condition: String) -> VisuStructDocument { |
| 87 | + VisuStructDocument(elements: [ |
| 88 | + VisuStructElement(type: .anweisung, textLines: ["int a = 5"]), |
| 89 | + VisuStructElement( |
| 90 | + type: .verzweigung, |
| 91 | + textLines: [condition], |
| 92 | + cases: [ |
| 93 | + VisuStructCase(name: "true", elements: [VisuStructElement(type: .anweisung, textLines: ["print: \"ja\""])]), |
| 94 | + VisuStructCase(name: "false", elements: [VisuStructElement(type: .anweisung, textLines: ["print: \"nein\""])]), |
| 95 | + ] |
| 96 | + ), |
| 97 | + ]) |
| 98 | + } |
| 99 | + XCTAssertEqual(runSimulation(document("a > 3 && a < 10")).outputLines, ["ja"]) |
| 100 | + XCTAssertEqual(runSimulation(document("a < 3 || a == 5")).outputLines, ["ja"]) |
| 101 | + XCTAssertEqual(runSimulation(document("a != 5")).outputLines, ["nein"]) |
| 102 | + } |
| 103 | + |
| 104 | + func testDivisionByZeroReportsError() { |
| 105 | + let document = VisuStructDocument(elements: [ |
| 106 | + VisuStructElement(type: .anweisung, textLines: ["int x = 1 / 0"]), |
| 107 | + ]) |
| 108 | + var engine = VisuStructSimulationEngine(document: document) |
| 109 | + engine.step() |
| 110 | + XCTAssertEqual(engine.state.message, "Division by zero.") |
| 111 | + XCTAssertNil(engine.state.variables["x"]) |
| 112 | + } |
| 113 | +} |
0 commit comments