Skip to content

Commit 1f180d7

Browse files
gormsterstmontgomery
authored andcommitted
Make XCTAssertEqual with accuracy more generic
Equality with accuracy isn't limited to floating point tests. Sometimes integer (or other numeric types) need to be accurate within certain bounds for the purposes of testing. This change allows any numeric type that has magnitude and distance to be used with `XCTAssertEqual(_:_:accuracy:)` and `XCTAssertNotEqual(_:_:accuracy:)`.
1 parent c21ac84 commit 1f180d7

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Sources/XCTest/Public/XCTAssert.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,12 @@ public func XCTAssertEqual<T: Equatable>(_ expression1: @autoclosure () throws -
171171
}
172172
}
173173

174-
public func XCTAssertEqual<T: FloatingPoint>(_ expression1: @autoclosure () throws -> T, _ expression2: @autoclosure () throws -> T, accuracy: T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
174+
public func XCTAssertEqual<T: Strideable & Numeric>(_ expression1: @autoclosure () throws -> T, _ expression2: @autoclosure () throws -> T, accuracy: T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
175175
_XCTEvaluateAssertion(.equalWithAccuracy, message: message(), file: file, line: line) {
176176
let (value1, value2) = (try expression1(), try expression2())
177177
// Test with equality first to handle comparing inf/-inf with itself.
178178
if value1 == value2 ||
179-
abs(value1.distance(to: value2)) <= abs(accuracy.distance(to: T(0))) {
179+
abs(value1.distance(to: value2)) <= abs(accuracy.distance(to: T.zero)) {
180180
return .success
181181
} else {
182182
return .expectedFailure("(\"\(value1)\") is not equal to (\"\(value2)\") +/- (\"\(accuracy)\")")
@@ -266,10 +266,10 @@ public func XCTAssertNotEqual<T: Equatable>(_ expression1: @autoclosure () throw
266266
}
267267
}
268268

269-
public func XCTAssertNotEqual<T: FloatingPoint>(_ expression1: @autoclosure () throws -> T, _ expression2: @autoclosure () throws -> T, accuracy: T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
269+
public func XCTAssertNotEqual<T: Strideable & Numeric>(_ expression1: @autoclosure () throws -> T, _ expression2: @autoclosure () throws -> T, accuracy: T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
270270
_XCTEvaluateAssertion(.notEqualWithAccuracy, message: message(), file: file, line: line) {
271271
let (value1, value2) = (try expression1(), try expression2())
272-
if abs(value1.distance(to: value2)) > abs(accuracy.distance(to: T(0))) {
272+
if abs(value1.distance(to: value2)) > abs(accuracy.distance(to: T.zero)) {
273273
return .success
274274
} else {
275275
return .expectedFailure("(\"\(value1)\") is equal to (\"\(value2)\") +/- (\"\(accuracy)\")")

0 commit comments

Comments
 (0)