1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
//===--- ApproximateEqualityTests.swift -----------------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import RealModule
import XCTest
import _TestSupport
final class ApproximateEqualityTests: XCTestCase {
func testSpecials<T: Real>(absolute tol: T) {
let zero = T.zero
let gfm = T.greatestFiniteMagnitude
let inf = T.infinity
let nan = T.nan
XCTAssertTrue(zero.isApproximatelyEqual(to: zero, absoluteTolerance: tol))
XCTAssertTrue(zero.isApproximatelyEqual(to:-zero, absoluteTolerance: tol))
XCTAssertFalse(inf.isApproximatelyEqual(to: gfm, absoluteTolerance: tol))
XCTAssertFalse(gfm.isApproximatelyEqual(to: inf, absoluteTolerance: tol))
XCTAssertTrue(inf.isApproximatelyEqual(to: inf, absoluteTolerance: tol))
XCTAssertTrue(inf.isApproximatelyEqual(to: inf, absoluteTolerance: tol))
XCTAssertFalse(nan.isApproximatelyEqual(to: nan, absoluteTolerance: tol))
}
func testSpecials<T: Real>(relative tol: T) {
let zero = T.zero
let gfm = T.greatestFiniteMagnitude
let inf = T.infinity
let nan = T.nan
XCTAssertTrue(zero.isApproximatelyEqual(to: zero, relativeTolerance: tol))
XCTAssertTrue(zero.isApproximatelyEqual(to:-zero, relativeTolerance: tol))
XCTAssertFalse(inf.isApproximatelyEqual(to: gfm, relativeTolerance: tol))
XCTAssertFalse(gfm.isApproximatelyEqual(to: inf, relativeTolerance: tol))
XCTAssertTrue(inf.isApproximatelyEqual(to: inf, relativeTolerance: tol))
XCTAssertTrue(inf.isApproximatelyEqual(to: inf, relativeTolerance: tol))
XCTAssertFalse(nan.isApproximatelyEqual(to: nan, relativeTolerance: tol))
}
func testSpecials<T: Real>(_ type: T.Type) {
XCTAssertTrue(T.zero.isApproximatelyEqual(to: .zero))
XCTAssertTrue(T.zero.isApproximatelyEqual(to:-.zero))
testSpecials(absolute: T.zero)
testSpecials(absolute: T.leastNormalMagnitude)
testSpecials(absolute: T.greatestFiniteMagnitude)
testSpecials(relative: T.zero)
testSpecials(relative: T.ulpOfOne)
testSpecials(relative: T(1).nextDown)
testSpecials(relative: T(1))
}
func testDefaults<T: Real>(_ type: T.Type) {
let e = T.ulpOfOne.squareRoot()
XCTAssertTrue(T(1).isApproximatelyEqual(to: 1 + e))
XCTAssertTrue(T(1).isApproximatelyEqual(to: 1 - e/2))
XCTAssertFalse(T(1).isApproximatelyEqual(to: 1 + 2*e))
XCTAssertFalse(T(1).isApproximatelyEqual(to: 1 - 3*e/2))
}
func testRandom<T>(_ type: T.Type) where T: FixedWidthFloatingPoint & Real {
var g = SystemRandomNumberGenerator()
// Generate a bunch of random values in a small interval and a tolerance
// and use them to check that various properties that we would like to
// hold actually do.
var x = [1] + (0 ..< 64).map {
_ in T.random(in: 1 ..< 2, using: &g)
} + [2]
x.sort()
// We have 66 values in 1 ... 2, so if we use a tolerance of around 1/64,
// at least some of the pairs will compare equal with tolerance.
let tol = T.random(in: 1/64 ... 1/32, using: &g)
// We're going to walk the values in order, validating that some common-
// sense properties hold.
for i in x.indices {
// reflexivity
XCTAssertTrue(x[i].isApproximatelyEqual(to: x[i]))
XCTAssertTrue(x[i].isApproximatelyEqual(to: x[i], relativeTolerance: tol))
XCTAssertTrue(x[i].isApproximatelyEqual(to: x[i], absoluteTolerance: tol))
for j in i ..< x.endIndex {
// commutativity
XCTAssertTrue(
x[i].isApproximatelyEqual(to: x[j], relativeTolerance: tol) ==
x[j].isApproximatelyEqual(to: x[i], relativeTolerance: tol)
)
XCTAssertTrue(
x[i].isApproximatelyEqual(to: x[j], absoluteTolerance: tol) ==
x[j].isApproximatelyEqual(to: x[i], absoluteTolerance: tol)
)
// scale invariance for relative comparisons
let scale = T(
sign:.plus,
exponent: T.Exponent.random(in: T.leastNormalMagnitude.exponent ..< T.greatestFiniteMagnitude.exponent),
significand: 1
)
XCTAssertTrue(
x[i].isApproximatelyEqual(to: x[j], relativeTolerance: tol) ==
(scale*x[i]).isApproximatelyEqual(to: scale*x[j], relativeTolerance: tol)
)
}
// if a ≤ b ≤ c, and a ≈ c, then a ≈ b and b ≈ c (relative tolerance)
var left = x.firstIndex { x[i].isApproximatelyEqual(to: $0, relativeTolerance: tol) }
var right = x.lastIndex { x[i].isApproximatelyEqual(to: $0, relativeTolerance: tol) }
if let l = left, let r = right {
for j in l ..< r {
XCTAssertTrue(x[i].isApproximatelyEqual(to: x[j], relativeTolerance: tol))
}
}
// if a ≤ b ≤ c, and a ≈ c, then a ≈ b and b ≈ c (absolute tolerance)
left = x.firstIndex { x[i].isApproximatelyEqual(to: $0, absoluteTolerance: tol) }
right = x.lastIndex { x[i].isApproximatelyEqual(to: $0, absoluteTolerance: tol) }
if let l = left, let r = right {
for j in l ..< r {
XCTAssertTrue(x[i].isApproximatelyEqual(to: x[j], absoluteTolerance: tol))
}
}
}
}
func testFloat() {
testSpecials(Float.self)
testDefaults(Float.self)
testRandom(Float.self)
}
func testDouble() {
testSpecials(Double.self)
testDefaults(Double.self)
testRandom(Double.self)
}
#if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android)
func testFloat80() {
testSpecials(Float80.self)
testDefaults(Float80.self)
testRandom(Float80.self)
}
#endif
}
|