SPB Git

Repository Public MIT

ReferenceTableTests.swift 3.3 KB 81 lines swift

1// ORIX Calc2// File: Packages/ORIXUnits/Tests/ORIXUnitsTests/ReferenceTableTests.swift3// Description: Checks every conversion of the independent Python/Fraction reference table against the engine.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai78import Foundation9import Testing1011@testable import ORIXUnits1213struct ReferenceRow {14    let value: String15    let from: String16    let to: String17    let expected: String18    let isExact: Bool19}2021func referenceRows() -> [ReferenceRow] {22    referenceConversionsTSV.split(separator: "\n").map { line in23        let fields = line.split(separator: "\t", omittingEmptySubsequences: false).map(String.init)24        precondition(fields.count == 5, "bad reference row: \(line)")25        return ReferenceRow(value: fields[0], from: fields[1], to: fields[2], expected: fields[3], isExact: fields[4] == "exact")26    }27}2829@Suite("Reference table (Python fractions.Fraction)")30struct ReferenceTableTests {31    @Test("The table is large and covers every unit of the catalog")32    func coverage() {33        let rows = referenceRows()34        #expect(rows.count >= 150)35        #expect(rows.filter(\.isExact).count >= 150)36        let referenced = Set(rows.flatMap { [$0.from, $0.to] })37        let known = Set(catalog.allUnits.map(\.id))38        #expect(referenced == known, "difference: \(referenced.symmetricDifference(known).sorted())")39        #expect(referenceUnitCount == catalog.allUnits.count)40    }4142    @Test("Decimal path: exact rows are equal to the last digit, rounded rows agree to 33 digits")43    func decimalPath() throws {44        var exactChecked = 045        for row in referenceRows() {46            let from = try unit(row.from)47            let to = try unit(row.to)48            let expected = dec(row.expected)49            let result = try UnitConverter.convert(dec(row.value), from: from, to: to)50            if row.isExact {51                exactChecked += 152                #expect(result == expected, "\(row.value) \(row.from) → \(row.to): got \(result), expected \(row.expected)")53            } else {54                #expect(55                    isClose(result, expected, relative: dec("1e-33")),56                    "\(row.value) \(row.from) → \(row.to): got \(result), expected \(row.expected)")57            }58        }59        #expect(exactChecked >= 150)60    }6162    @Test("Double path agrees with the reference to 2 ulp")63    func doublePath() throws {64        for row in referenceRows() {65            let from = try unit(row.from)66            let to = try unit(row.to)67            let value = try #require(Double(row.value))68            let expected = try #require(Double(row.expected))69            let result = try UnitConverter.convert(value, from: from, to: to)70            #expect(71                isClose(result, expected, relative: 4.5e-16),72                "\(row.value) \(row.from) → \(row.to): got \(result), expected \(expected)")73            let binary = try UnitConverter.binaryConvert(value, from: from, to: to, mode: .absolute)74            let tolerance = from.categoryID == .temperature ? 1e-12 : 1e-1475            #expect(76                isClose(binary, expected, relative: tolerance) || abs(binary - expected) < 1e-11,77                "binary \(row.value) \(row.from) → \(row.to): got \(binary), expected \(expected)")78        }79    }80}81
Files main
Menu

Theme