示例工程 02-values-variables-types
02-values-variables-types/Tests/Ch02KitTests/FloatingPointTests.swift
import Testing
@testable import Ch02Kit
@Suite("浮点与十进制")
struct FloatingPointTests {
@Test func tenTenthsDoNotEqualOneExactly() {
#expect(FloatingPointTraps.equalsOneExactly == false)
#expect(FloatingPointTraps.fixed(FloatingPointTraps.sumOfTenTenths) == "1.000")
#expect(FloatingPointTraps.approximatelyEqual(FloatingPointTraps.sumOfTenTenths, 1.0))
}
@Test func nanIsNotEqualToItself() {
#expect(FloatingPointTraps.nanEqualsItself == false)
#expect(Double.nan.isNaN)
}
@Test func negativeZeroEqualsZero() {
#expect(FloatingPointTraps.negativeZeroEqualsZero)
#expect((-0.0).sign == .minus)
#expect((0.0).sign == .plus)
}
@Test func decimalAvoidsBinaryRepresentationError() {
#expect(FloatingPointTraps.decimalTenthTimesTenEqualsOne)
}
@Test func toleranceRejectsRealDifferences() {
#expect(FloatingPointTraps.approximatelyEqual(1.0, 1.0 + 1e-12))
#expect(!FloatingPointTraps.approximatelyEqual(1.0, 1.01))
}
}