Repository Public MIT
1// ORIX Calc2// File: Packages/ORIXStatistics/Tests/ORIXStatisticsTests/ReferenceSupport.swift3// Description: Loads the reference JSON generated by Reference/*.py and provides error metrics shared by the test suites.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9@testable import ORIXStatistics1011enum Reference {12 /// Parsed content of ReferenceData/<name>.json (bundled with the test target).13 static func load(_ name: String) throws -> [String: Any] {14 guard let url = Bundle.module.url(forResource: name, withExtension: "json", subdirectory: "ReferenceData") else {15 throw StatisticsError.invalidArgument("Missing reference file \(name).json — run Reference/generate_all.sh")16 }17 let object = try JSONSerialization.jsonObject(with: Data(contentsOf: url))18 guard let dict = object as? [String: Any] else { throw StatisticsError.invalidArgument("\(name).json is not an object") }19 return dict20 }2122 static func cases(_ name: String) throws -> [[String: Any]] {23 (try load(name)["cases"] as? [[String: Any]]) ?? []24 }25}2627extension Dictionary where Key == String, Value == Any {28 func double(_ key: String) -> Double { (self[key] as? NSNumber)?.doubleValue ?? .nan }29 func int(_ key: String) -> Int { (self[key] as? NSNumber)?.intValue ?? 0 }30 func string(_ key: String) -> String { self[key] as? String ?? "" }31 func doubles(_ key: String) -> [Double] { (self[key] as? [NSNumber])?.map(\.doubleValue) ?? [] }32 func matrix(_ key: String) -> [[Double]] { (self[key] as? [[NSNumber]])?.map { $0.map(\.doubleValue) } ?? [] }33 func dict(_ key: String) -> [String: Any] { self[key] as? [String: Any] ?? [:] }34 func has(_ key: String) -> Bool { self[key] != nil && !(self[key] is NSNull) }35}3637/// |a − e| / |e| (absolute error when e = 0).38func relativeError(_ actual: Double, _ expected: Double) -> Double {39 if actual == expected { return 0 }40 if expected == 0 { return abs(actual) }41 return abs(actual - expected) / abs(expected)42}4344/// True when the two numbers agree to `rtol` relatively or `atol` absolutely.45func close(_ actual: Double, _ expected: Double, rtol: Double = 1e-10, atol: Double = 0) -> Bool {46 if actual == expected { return true }47 guard actual.isFinite, expected.isFinite else { return false }48 return abs(actual - expected) <= max(rtol * abs(expected), atol)49}5051func allClose(_ actual: [Double], _ expected: [Double], rtol: Double = 1e-10, atol: Double = 0) -> Bool {52 actual.count == expected.count && zip(actual, expected).allSatisfy { close($0, $1, rtol: rtol, atol: atol) }53}5455/// Accumulates the worst relative error of a family of cases.56struct ErrorTracker {57 var worst = 0.058 var worstCase = ""59 var count = 060 mutating func record(_ actual: Double, _ expected: Double, _ label: @autoclosure () -> String) {61 count += 162 let e = actual.isFinite ? relativeError(actual, expected) : Double.infinity63 if e > worst { worst = e; worstCase = label() }64 }65}66