Repository Public License
1// ORIX Alarm2// File: Packages/AlarmCore/Tests/AlarmCoreTests/MathEngineTests.swift3// Description: Math Blitz engine tests: determinism, independent re-evaluation of every question, unique answers, level contents.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import XCTest8@testable import AlarmCore910/// Independent recursive-descent evaluator for the prompts (`+ − × ÷` and parentheses). Fails on inexact or zero division.11private struct Evaluator {12 enum Failure: Error { case syntax, divisionByZero, inexactDivision }13 private var tokens: [String] = []14 private var position = 01516 static func evaluate(_ text: String) throws -> Int {17 var e = Evaluator()18 var number = ""19 for ch in text where ch != " " {20 if ch.isNumber { number.append(ch) } else {21 if !number.isEmpty { e.tokens.append(number); number = "" }22 e.tokens.append(String(ch))23 }24 }25 if !number.isEmpty { e.tokens.append(number) }26 let value = try e.expression()27 guard e.position == e.tokens.count else { throw Failure.syntax }28 return value29 }3031 private var peek: String? { position < tokens.count ? tokens[position] : nil }3233 private mutating func expression() throws -> Int {34 var value = try term()35 while let op = peek, op == "+" || op == "−" {36 position += 137 let rhs = try term()38 value = op == "+" ? value + rhs : value - rhs39 }40 return value41 }4243 private mutating func term() throws -> Int {44 var value = try factor()45 while let op = peek, op == "×" || op == "÷" {46 position += 147 let rhs = try factor()48 if op == "×" { value *= rhs } else {49 guard rhs != 0 else { throw Failure.divisionByZero }50 guard value % rhs == 0 else { throw Failure.inexactDivision }51 value /= rhs52 }53 }54 return value55 }5657 private mutating func factor() throws -> Int {58 guard let token = peek else { throw Failure.syntax }59 position += 160 if token == "(" {61 let value = try expression()62 guard peek == ")" else { throw Failure.syntax }63 position += 164 return value65 }66 guard let value = Int(token) else { throw Failure.syntax }67 return value68 }69}7071final class MathEngineTests: XCTestCase {72 private let perLevel = 2_5007374 private func puzzles(level: Int) -> [MathEngine.Puzzle] {75 (0..<perLevel).map { MathEngine.generate(level: level, round: $0 % 10, seed: UInt64($0) &* 7_919 &+ UInt64(level)) }76 }7778 func testDeterministic() {79 for level in 1...4 {80 for round in 0..<10 {81 XCTAssertEqual(MathEngine.generate(level: level, round: round, seed: 42), MathEngine.generate(level: level, round: round, seed: 42))82 }83 }84 let a = (0..<10).map { MathEngine.generate(level: 2, round: $0, seed: 1).prompt }85 let b = (0..<10).map { MathEngine.generate(level: 2, round: $0, seed: 2).prompt }86 XCTAssertNotEqual(a, b)87 }8889 func testLevelIsClamped() {90 XCTAssertEqual(MathEngine.generate(level: 0, round: 0, seed: 5), MathEngine.generate(level: 1, round: 0, seed: 5))91 XCTAssertEqual(MathEngine.generate(level: 9, round: 0, seed: 5), MathEngine.generate(level: 4, round: 0, seed: 5))92 }9394 func testConsecutiveRoundsDiffer() {95 for level in 1...4 {96 for seed in 0..<300 as Range<UInt64> {97 for round in 1..<10 {98 XCTAssertNotEqual(MathEngine.generate(level: level, round: round, seed: seed).prompt,99 MathEngine.generate(level: level, round: round - 1, seed: seed).prompt)100 }101 }102 }103 }104105 func testEveryQuestionHasOneCorrectAnswer() throws {106 for level in 1...4 {107 var seen = Set<MathEngine.Category>()108 for puzzle in puzzles(level: level) {109 seen.insert(puzzle.category)110 XCTAssertTrue(MathEngine.categories(level: level).contains(puzzle.category))111 XCTAssertLessThanOrEqual(String(abs(puzzle.answer)).count, MathEngine.maxAnswerDigits)112 XCTAssertTrue(MathEngine.validate(puzzle, answer: puzzle.answer))113 XCTAssertFalse(MathEngine.validate(puzzle, answer: puzzle.answer + 1))114 XCTAssertFalse(MathEngine.validate(puzzle, answer: puzzle.answer - 1))115 XCTAssertFalse(MathEngine.validate(puzzle, answer: -puzzle.answer - 1))116 switch puzzle.category {117 case .addition, .subtraction, .multiplication, .division, .multiStep:118 XCTAssertTrue(puzzle.choices.isEmpty)119 XCTAssertEqual(try Evaluator.evaluate(puzzle.prompt), puzzle.answer, puzzle.prompt)120 if puzzle.category == .multiStep { XCTAssertTrue(puzzle.prompt.contains("("), puzzle.prompt) }121 case .missingNumber:122 try checkMissingNumber(puzzle)123 case .sequence:124 checkSequence(puzzle)125 case .estimation:126 try checkEstimation(puzzle)127 }128 }129 XCTAssertEqual(seen, Set(MathEngine.categories(level: level)), "level \(level) never produced some categories")130 }131 }132133 func testEasyLevelsStaySimple() {134 for puzzle in puzzles(level: 1) {135 XCTAssertTrue([.addition, .subtraction].contains(puzzle.category))136 XCTAssertTrue((0...40).contains(puzzle.answer), puzzle.prompt)137 }138 for puzzle in puzzles(level: 2) { XCTAssertGreaterThanOrEqual(puzzle.answer, 0, puzzle.prompt) }139 }140141 /// Exactly one integer makes the equation true, and it is the stored answer.142 private func checkMissingNumber(_ puzzle: MathEngine.Puzzle) throws {143 let sides = puzzle.prompt.components(separatedBy: " = ")144 XCTAssertEqual(sides.count, 2, puzzle.prompt)145 XCTAssertEqual(puzzle.prompt.filter { $0 == "?" }.count, 1, puzzle.prompt)146 let right = try XCTUnwrap(Int(sides[1]), puzzle.prompt)147 var solutions: [Int] = []148 for candidate in 0...3_000 {149 if let value = try? Evaluator.evaluate(sides[0].replacingOccurrences(of: "?", with: String(candidate))), value == right {150 solutions.append(candidate)151 }152 }153 XCTAssertEqual(solutions, [puzzle.answer], puzzle.prompt)154 }155156 /// The shown terms and the answer follow one of the documented rules.157 private func checkSequence(_ puzzle: MathEngine.Puzzle) {158 XCTAssertTrue(puzzle.prompt.hasSuffix(", ?"), puzzle.prompt)159 let terms = puzzle.prompt.dropLast(3).components(separatedBy: ", ").compactMap { Int($0) }160 XCTAssertGreaterThanOrEqual(terms.count, 4, puzzle.prompt)161 let all = terms + [puzzle.answer]162 XCTAssertTrue(all.allSatisfy { $0 > 0 }, puzzle.prompt)163 let diffs = zip(all.dropFirst(), all).map { $0 - $1 }164 let second = zip(diffs.dropFirst(), diffs).map { $0 - $1 }165 let arithmetic = Set(diffs).count == 1 && diffs[0] != 0166 let geometric = (2...3).contains { ratio in zip(all.dropFirst(), all).allSatisfy { $0 == $1 * ratio } }167 let growing = Set(second).count == 1 && second[0] > 0168 let additive = (2..<all.count).allSatisfy { all[$0] == all[$0 - 1] + all[$0 - 2] }169 XCTAssertTrue(arithmetic || geometric || growing || additive, puzzle.prompt + " -> \(puzzle.answer)")170 }171172 /// The answer is strictly the nearest choice to the exact value.173 private func checkEstimation(_ puzzle: MathEngine.Puzzle) throws {174 XCTAssertTrue(puzzle.prompt.hasSuffix(" ≈ ?"), puzzle.prompt)175 let exact = try Evaluator.evaluate(String(puzzle.prompt.dropLast(4)))176 XCTAssertEqual(puzzle.exactValue, exact)177 XCTAssertEqual(puzzle.choices.count, 4)178 XCTAssertEqual(Set(puzzle.choices).count, 4)179 XCTAssertEqual(puzzle.choices, puzzle.choices.sorted())180 XCTAssertTrue(puzzle.choices.allSatisfy { $0 > 0 })181 XCTAssertTrue(puzzle.choices.contains(puzzle.answer))182 let distance = abs(puzzle.answer - exact)183 for choice in puzzle.choices where choice != puzzle.answer {184 XCTAssertGreaterThan(abs(choice - exact), distance * 2, puzzle.prompt)185 XCTAssertFalse(MathEngine.validate(puzzle, answer: choice))186 }187 }188}189