132 lines
4.4 KiB
Swift
132 lines
4.4 KiB
Swift
import Testing
|
|
|
|
@testable import Units
|
|
|
|
@Test func unitCreationSubscriptTests() async throws {
|
|
// Test creating UnitValue using string subscript on numeric literals
|
|
let tenKilosByString = 10.0["kg"]
|
|
#expect(tenKilosByString != nil)
|
|
#expect(tenKilosByString?.value == 10.0)
|
|
#expect(tenKilosByString?.unit == .kilogram)
|
|
print(
|
|
"Created 10 kg by string subscript: \(String(describing: tenKilosByString))"
|
|
)
|
|
|
|
let fiveMetersByString = 5.0["m"]
|
|
#expect(fiveMetersByString != nil)
|
|
#expect(fiveMetersByString?.value == 5.0)
|
|
#expect(fiveMetersByString?.unit == .meter)
|
|
print(
|
|
"Created 5 m by string subscript: \(String(describing: fiveMetersByString))"
|
|
)
|
|
|
|
// Test creating UnitValue using enum subscript on numeric literals
|
|
let twentyLitersByEnum = 20.0[.liter]
|
|
#expect(twentyLitersByEnum.value == 20.0)
|
|
#expect(twentyLitersByEnum.unit == .liter)
|
|
print("Created 20 L by enum subscript: \(twentyLitersByEnum)")
|
|
|
|
let thirtySecondsByEnum = 30.0[.second]
|
|
#expect(thirtySecondsByEnum.value == 30.0)
|
|
#expect(thirtySecondsByEnum.unit == .second)
|
|
print("Created 30 s by enum subscript: \(thirtySecondsByEnum)")
|
|
|
|
// Test invalid unit string for creation
|
|
let invalidUnitCreation = 100.0["xyz"]
|
|
#expect(invalidUnitCreation == nil)
|
|
print(
|
|
"Attempted invalid unit creation (should be nil): \(String(describing: invalidUnitCreation))"
|
|
)
|
|
}
|
|
|
|
@Test func unitConversionSubscriptTests() async throws {
|
|
// Test conversion using string subscript on UnitValue
|
|
if let tenKilos = 10.0["kg"] {
|
|
let pounds = tenKilos["lbs"]
|
|
#expect(pounds != nil)
|
|
#expect(pounds?.value == 22.046226218487757) // Exact conversion value
|
|
#expect(pounds?.unit == .pound)
|
|
print("10 kg to lbs by string subscript: \(String(describing: pounds))")
|
|
} else {
|
|
#expect(false, "Failed to create initial 10 kg unit.")
|
|
}
|
|
|
|
// Test conversion using enum subscript on UnitValue
|
|
let fiveMeters = 5.0.m
|
|
let feet = fiveMeters[.foot]
|
|
#expect(feet != nil)
|
|
#expect(feet?.value == 16.404199475065617) // Exact conversion value
|
|
#expect(feet?.unit == .foot)
|
|
print("5 m to ft by enum subscript: \(String(describing: feet))")
|
|
|
|
// Test chained conversions
|
|
if let initialValue = 100.0["km"] {
|
|
let miles = initialValue["mi"]
|
|
#expect(miles != nil)
|
|
#expect(miles?.value == 62.13711922373339) // Exact conversion value
|
|
#expect(miles?.unit == .mile)
|
|
print(
|
|
"100 km to mi (chained string subscript): \(String(describing: miles))"
|
|
)
|
|
|
|
let feetFromMiles = miles?["ft"]
|
|
#expect(feetFromMiles != nil)
|
|
#expect(feetFromMiles?.value == 328083.9895013123) // Exact conversion value (100km -> mi -> ft)
|
|
#expect(feetFromMiles?.unit == .foot)
|
|
print(
|
|
"100 km to mi to ft (chained string subscript): \(String(describing: feetFromMiles))"
|
|
)
|
|
} else {
|
|
#expect(false, "Failed to create initial 100 km unit.")
|
|
}
|
|
|
|
// Test invalid conversion (different categories)
|
|
let tenKilos = 10.0.kg
|
|
let metersFromKilos = tenKilos["m"]
|
|
#expect(metersFromKilos == nil)
|
|
print(
|
|
"Attempted invalid conversion (mass to length, should be nil): \(String(describing: metersFromKilos))"
|
|
)
|
|
|
|
// Test invalid target unit string for conversion
|
|
let invalidTargetConversion = tenKilos["xyz"]
|
|
#expect(invalidTargetConversion == nil)
|
|
print(
|
|
"Attempted conversion to invalid unit string (should be nil): \(String(describing: invalidTargetConversion))"
|
|
)
|
|
}
|
|
|
|
@Test func temperatureConversionSubscriptTests() async throws {
|
|
// Test Celsius to Fahrenheit
|
|
let twentyFiveC = 25.0[.celsius]
|
|
let fahrenheitFromC = twentyFiveC[.fahrenheit]
|
|
#expect(fahrenheitFromC != nil)
|
|
#expect(fahrenheitFromC?.value == 77.0)
|
|
#expect(fahrenheitFromC?.unit == .fahrenheit)
|
|
print("25°C to °F: \(String(describing: fahrenheitFromC))")
|
|
|
|
// Test Fahrenheit to Celsius
|
|
let ninetyEightF = 98.6[.fahrenheit]
|
|
let celsiusFromF = ninetyEightF[.celsius]
|
|
#expect(celsiusFromF != nil)
|
|
#expect(celsiusFromF?.value == 37.0)
|
|
#expect(celsiusFromF?.unit == .celsius)
|
|
print("98.6°F to °C: \(String(describing: celsiusFromF))")
|
|
|
|
// Test Celsius to Kelvin
|
|
let zeroC = 0.0[.celsius]
|
|
let kelvinFromC = zeroC[.kelvin]
|
|
#expect(kelvinFromC != nil)
|
|
#expect(kelvinFromC?.value == 273.15)
|
|
#expect(kelvinFromC?.unit == .kelvin)
|
|
print("0°C to K: \(String(describing: kelvinFromC))")
|
|
|
|
// Test Kelvin to Celsius
|
|
let twoSeventyThreeK = 273.15[.kelvin]
|
|
let celsiusFromK = twoSeventyThreeK[.celsius]
|
|
#expect(celsiusFromK != nil)
|
|
#expect(celsiusFromK?.value == 0.0)
|
|
#expect(celsiusFromK?.unit == .celsius)
|
|
print("273.15 K to °C: \(String(describing: celsiusFromK))")
|
|
}
|