Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Calc9// File : Packages/ORIXUnits/Tests/ORIXUnitsTests/LookupTests.swift10// Description : Lookup by symbol, alias and name (case sensitivity),11// search, and incompatible-unit errors.12// Module : ORIXUnitsTests13// Repository : https://git.spboucher.ai/orix-calc14// Created : 2026-11-2015// Last Modified : 2026-11-2016//17// =============================================================================18// AUTHOR & PROFESSIONAL AFFILIATION19// =============================================================================20//21// Author : Simon-Pierre Boucher22// Position : Professor of Finance23// Institution : Université du Québec en Outaouais (UQO)24// Department : Département des sciences administratives25// Organization : SP Boucher26//27// Website : https://spboucher.ai28// Git : https://git.spboucher.ai29//30// =============================================================================31// OFFICIAL CONTACT DIRECTORY32// =============================================================================33//34// General : info@spboucher.ai35// Professional : contact@spboucher.ai36// Direct : simon@spboucher.ai37// Research : research@spboucher.ai38// Development : dev@spboucher.ai39// Support : support@spboucher.ai40// Security/Admin : admin@spboucher.ai41// Notifications : notifications@spboucher.ai42//43// Preferred Contact: support@spboucher.ai44//45// =============================================================================46// COPYRIGHT & INTELLECTUAL PROPERTY47// =============================================================================48//49// Copyright (c) 202650// Simon-Pierre Boucher51//52// All rights reserved where applicable.53//54// This file is governed by the applicable project license,55// copyright notices, institutional agreements and relevant law.56//57// Unless expressly authorized by the applicable license or58// rights holder, copying, redistribution, sublicensing,59// publication and commercial exploitation are not permitted.60//61// Existing third-party rights and notices remain unaffected.62//63// =============================================================================64// CONFIDENTIALITY & SECURITY65// =============================================================================66//67// Classification : Public68// Distribution : Public repository; use is governed by the project license69// Security Contact : admin@spboucher.ai70//71// Access to this source does not independently grant permission72// to redistribute or disclose confidential materials.73//74// Do not embed credentials, private keys, access tokens,75// certificates or confidential personal information.76//77// =============================================================================78// LICENSE & MAINTENANCE79// =============================================================================80//81// License : Proprietary, all rights reserved (see license file)82// License File : LICENSE83// Maintainer : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import Foundation91import Testing9293@testable import ORIXUnits9495@Suite("Lookup and search")96struct LookupTests {97 private func ids(_ token: String) -> [String] { catalog.units(matching: token).map(\.id) }9899 @Test("Every symbol and alias resolves to its own unit within its category")100 func everyToken() {101 for unit in catalog.allUnits {102 for token in [unit.symbol] + unit.aliases {103 #expect(catalog.unit(token, in: unit.categoryID) == unit, "'\(token)' should resolve to \(unit.id)")104 }105 #expect(catalog.unit(unit.name, in: unit.categoryID) == unit, "name '\(unit.name)' → \(unit.id)")106 }107 }108109 @Test("SI prefixes are case-sensitive")110 func caseSensitivity() {111 #expect(catalog.unit("mm", in: .length)?.id == "length.mm")112 #expect(catalog.unit("Mm", in: .length)?.id == "length.Mm")113 #expect(catalog.unit("mW", in: .power)?.id == "power.mW")114 #expect(catalog.unit("MW", in: .power)?.id == "power.MW")115 #expect(catalog.unit("mΩ", in: .resistance)?.id == "resistance.mohm")116 #expect(catalog.unit("MΩ", in: .resistance)?.id == "resistance.Mohm")117 #expect(catalog.unit("b", in: .dataStorage)?.id == "dataStorage.bit")118 #expect(catalog.unit("B", in: .dataStorage)?.id == "dataStorage.B")119 #expect(catalog.unit("Mb", in: .dataStorage)?.id == "dataStorage.Mbit")120 #expect(catalog.unit("MB", in: .dataStorage)?.id == "dataStorage.MB")121 #expect(catalog.unit("cal", in: .energy)?.id == "energy.cal")122 #expect(catalog.unit("Cal", in: .energy)?.id == "energy.kcal")123 #expect(catalog.unit("mPa", in: .pressure) == nil)124 #expect(catalog.unit("MPa", in: .pressure)?.id == "pressure.MPa")125 // Symbols never match in the wrong case, even when only one unit could be meant:126 // "mPa" (millipascal, absent from the catalog) must not become MPa.127 #expect(catalog.unit("KM", in: .length) == nil)128 #expect(catalog.unit("mw", in: .power) == nil)129 #expect(catalog.unit("MM", in: .length) == nil)130 #expect(catalog.unit("mb", in: .dataStorage) == nil)131 #expect(catalog.unit("KWH", in: .energy) == nil)132 #expect(catalog.unit("Mbar", in: .pressure) == nil)133 #expect(catalog.unit("MOHM", in: .resistance) == nil)134 #expect(catalog.unit("Mrad", in: .absorbedDose) == nil)135 #expect(catalog.units(matching: "mPa").isEmpty)136 }137138 @Test("ASCII and typographic aliases")139 func aliases() {140 #expect(catalog.unit("um", in: .length)?.id == "length.um")141 #expect(catalog.unit("µm", in: .length)?.id == "length.um")142 #expect(catalog.unit("\u{03BC}m", in: .length)?.id == "length.um")143 #expect(catalog.unit("deg", in: .angle)?.id == "angle.deg")144 #expect(catalog.unit("°", in: .angle)?.id == "angle.deg")145 #expect(catalog.unit("ohm", in: .resistance)?.id == "resistance.ohm")146 #expect(catalog.unit("Ω", in: .resistance)?.id == "resistance.ohm")147 #expect(catalog.unit("\u{2126}", in: .resistance)?.id == "resistance.ohm")148 #expect(catalog.unit("kohm", in: .resistance)?.id == "resistance.kohm")149 #expect(catalog.unit("uF", in: .capacitance)?.id == "capacitance.uF")150 #expect(catalog.unit("ug", in: .mass)?.id == "mass.ug")151 #expect(catalog.unit("degC", in: .temperature)?.id == "temperature.degC")152 #expect(catalog.unit("m2", in: .area)?.id == "area.m2")153 #expect(catalog.unit("m^2", in: .area)?.id == "area.m2")154 #expect(catalog.unit("sq ft", in: .area)?.id == "area.ft2")155 #expect(catalog.unit("cc", in: .volume)?.id == "volume.cm3")156 #expect(catalog.unit("angstrom", in: .length)?.id == "length.angstrom")157 #expect(catalog.unit("Å", in: .length)?.id == "length.angstrom")158 #expect(catalog.unit(" km ", in: .length)?.id == "length.km")159 #expect(catalog.unit("thou", in: .length)?.id == "length.mil")160 #expect(catalog.unit("gal", in: .volume)?.id == "volume.gal_us")161 #expect(catalog.unit("mpg", in: .fuelEconomy)?.id == "fuelEconomy.mpg_us")162 #expect(catalog.unit("L/100km", in: .fuelEconomy)?.id == "fuelEconomy.L_100km")163 #expect(catalog.unit("kph", in: .speed)?.id == "speed.km_h")164 #expect(catalog.unit("Da", in: .mass)?.id == "mass.u")165 }166167 @Test("Names, American spellings and plurals")168 func names() {169 #expect(catalog.unit("metre", in: .length)?.id == "length.m")170 #expect(catalog.unit("meter", in: .length)?.id == "length.m")171 #expect(catalog.unit("Kilometers", in: .length)?.id == "length.km")172 #expect(catalog.unit("inch", in: .length)?.id == "length.in")173 #expect(catalog.unit("inches", in: .length)?.id == "length.in")174 #expect(catalog.unit("feet", in: .length)?.id == "length.ft")175 #expect(catalog.unit("Feet", in: .length)?.id == "length.ft")176 #expect(catalog.unit("MEGAPASCAL", in: .pressure)?.id == "pressure.MPa")177 #expect(catalog.unit("Short Ton", in: .mass)?.id == "mass.ton_us")178 #expect(catalog.unit("seconds", in: .time)?.id == "time.s")179 #expect(catalog.unit("miles", in: .length)?.id == "length.mi")180 #expect(catalog.unit("liter", in: .volume)?.id == "volume.L")181 #expect(catalog.unit("pounds", in: .mass)?.id == "mass.lb")182 #expect(catalog.unit("Fahrenheit", in: .temperature)?.id == "temperature.degF")183 #expect(catalog.unit("hours", in: .time)?.id == "time.h")184 #expect(catalog.unit("year", in: .time)?.id == "time.a_gregorian")185 #expect(catalog.unit("ångström", in: .length)?.id == "length.angstrom")186 #expect(catalog.units(matching: "").isEmpty)187 #expect(catalog.units(matching: "zorkmid").isEmpty)188 #expect(catalog.unit(id: "length.nope") == nil)189 }190191 @Test("Symbols shared by several categories are disambiguated by the other unit")192 func sharedSymbols() throws {193 #expect(Set(ids("pt")) == ["volume.pt_us", "typography.pt"])194 #expect(Set(ids("rad")) == ["angle.rad", "absorbedDose.rad"])195 #expect(try catalog.resolve(from: "pt", to: "mL").from.id == "volume.pt_us")196 #expect(try catalog.resolve(from: "pt", to: "px").from.id == "typography.pt")197 #expect(try catalog.resolve(from: "rad", to: "deg").from.id == "angle.rad")198 #expect(try catalog.resolve(from: "rad", to: "Gy").from.id == "absorbedDose.rad")199 #expect(try catalog.resolve(from: "pc", to: "ly").from.id == "length.pc")200 #expect(try catalog.resolve(from: "pc", to: "pt").from.id == "typography.pc")201 #expect(try catalog.resolve(from: "in", to: "cm").to.id == "length.cm")202 #expect(try catalog.resolve(from: "'", to: "\"").from.id == "length.ft")203 #expect(try catalog.resolve(from: "°", to: "'").to.id == "angle.arcmin")204 }205206 @Test("Conversion by token")207 func convertByToken() throws {208 #expect(try catalog.convert(dec("1"), from: "mi", to: "km") == dec("1.609344"))209 #expect(try catalog.convert(dec("12"), from: "in", to: "ft") == 1)210 #expect(try catalog.convert(dec("100"), from: "°C", to: "°F") == 212)211 #expect(try catalog.convert(dec("1"), from: "kWh", to: "MJ") == dec("3.6"))212 #expect(try catalog.convert(dec("1"), from: "GiB", to: "MiB") == 1024)213 #expect(try catalog.convert(dec("1"), from: "um", to: "nm") == 1000)214 #expect(try catalog.convert(dec("1"), from: "Mm", to: "mm") == dec("1000000000"))215 #expect(try catalog.convert(dec("1"), from: "MW", to: "mW") == dec("1000000000"))216 }217218 @Test("Incompatible and unknown units are typed errors")219 func errors() throws {220 #expect(throws: UnitError.incompatible(from: "length.m", to: "mass.kg")) {221 try UnitConverter.convert(dec("1"), from: try unit("length.m"), to: try unit("mass.kg"))222 }223 #expect(throws: UnitError.incompatible(from: "length.m", to: "mass.kg")) {224 try UnitConverter.convert(1.0, from: try unit("length.m"), to: try unit("mass.kg"))225 }226 // Same dimensional signature, different quantity: still refused.227 #expect(try #require(catalog.category(.energy)).dimension == #require(catalog.category(.torque)).dimension)228 #expect(throws: UnitError.incompatible(from: "energy.J", to: "torque.Nm")) {229 try UnitConverter.convert(dec("1"), from: try unit("energy.J"), to: try unit("torque.Nm"))230 }231 #expect(throws: UnitError.incompatible(from: "absorbedDose.Gy", to: "doseEquivalent.Sv")) {232 try catalog.convert(dec("1"), from: "Gy", to: "Sv")233 }234 #expect(throws: UnitError.incompatible(from: "length.km", to: "mass.kg")) {235 try catalog.convert(dec("1"), from: "km", to: "kg")236 }237 #expect(throws: UnitError.incompatible(from: "length.m", to: "area.m2")) {238 try catalog.convert(dec("1"), from: "m", to: "m²")239 }240 #expect(throws: UnitError.unknownUnit("zorkmid")) { try catalog.convert(dec("1"), from: "zorkmid", to: "kg") }241 #expect(throws: UnitError.unknownUnit("mw")) { try catalog.convert(dec("1"), from: "W", to: "mw") }242 #expect(!UnitConverter.areCompatible(try unit("length.m"), try unit("mass.kg")))243 #expect(UnitConverter.areCompatible(try unit("length.m"), try unit("length.ly")))244 for error in [245 UnitError.incompatible(from: "a", to: "b"), .unknownUnit("x"), .undefinedAtZero(unit: "u"),246 .intervalNotSupported(unit: "u"), .notRepresentable, .rateOutOfDomain("r"), .unknownCurrency("XXX"),247 .invalidRate(code: "USD"), .malformedECB("m"),248 ] {249 #expect(!error.description.isEmpty)250 }251 }252253 @Test("Search")254 func search() {255 #expect(catalog.search("").isEmpty)256 #expect(catalog.search("kilometre").first?.id == "length.km")257 #expect(catalog.search("km").first?.id == "length.km")258 #expect(catalog.search("gallon").map(\.id).contains("volume.gal_us"))259 #expect(catalog.search("gallon").map(\.id).contains("volume.gal_imp"))260 #expect(catalog.search("GALLON", in: .volume).allSatisfy { $0.categoryID == .volume })261 #expect(catalog.search("angstrom").first?.id == "length.angstrom")262 #expect(catalog.search("fahr").first?.id == "temperature.degF")263 #expect(catalog.search("byte").first?.id == "dataStorage.B")264 #expect(catalog.search("Viscosity").count == 9)265 #expect(catalog.search("light").map(\.id).contains("length.ly"))266 #expect(catalog.search("qqqq").isEmpty)267 let metres = catalog.search("metre")268 #expect(metres.first?.id == "length.m")269 #expect(metres.count > 20)270 }271}272