Skip to content

Make XCTAssertEqual with accuracy more generic #294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Sources/XCTest/Public/XCTAssert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ public func XCTAssertEqual<T: Equatable>(_ expression1: @autoclosure () throws -
}
}

public func XCTAssertEqual<T: FloatingPoint>(_ expression1: @autoclosure () throws -> T, _ expression2: @autoclosure () throws -> T, accuracy: T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done some local experimentation with the version of this code which is included in Xcode, and I believe it's possible to simplify this code to only require conformance to the Numeric protocol and remove the Strideable requirement. I think the latter is needed currently because the code uses distance(to:) to calculate differences, but I'm not sure if that way of getting the difference is strictly necessary. If we distill the core "are two numbers equal w/accuracy" calculation down to a helper function, I think that can look something like this:

private func areEqual<T : Numeric>(_ exp1: T, _ exp2: T, accuracy: T) -> Bool {
    if exp1 == exp2 {
        return true
    } else {
        // NaN values are handled implicitly, since the <= operator returns false when comparing any value to NaN.
        let difference = (exp1.magnitude > exp2.magnitude) ? exp1 - exp2 : exp2 - exp1
        return difference.magnitude <= accuracy.magnitude
    }
}

And this removes the need for distance(to:) and anything else which currently requires Strideable.

Could we employ this technique in this PR too, so we can change the type requirements to only need Numeric? (Or, if anyone knows a strong reason to keep the current implementation, let me know.) Thanks!

_XCTEvaluateAssertion(.equalWithAccuracy, message: message(), file: file, line: line) {
let (value1, value2) = (try expression1(), try expression2())
if abs(value1.distance(to: value2)) <= abs(accuracy.distance(to: T(0))) {
if abs(value1.distance(to: value2)) <= abs(accuracy.distance(to: T.zero)) {
return .success
} else {
return .expectedFailure("(\"\(value1)\") is not equal to (\"\(value2)\") +/- (\"\(accuracy)\")")
Expand Down Expand Up @@ -264,10 +264,10 @@ public func XCTAssertNotEqual<T: Equatable>(_ expression1: @autoclosure () throw
}
}

public func XCTAssertNotEqual<T: FloatingPoint>(_ expression1: @autoclosure () throws -> T, _ expression2: @autoclosure () throws -> T, accuracy: T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
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) {
_XCTEvaluateAssertion(.notEqualWithAccuracy, message: message(), file: file, line: line) {
let (value1, value2) = (try expression1(), try expression2())
if abs(value1.distance(to: value2)) > abs(accuracy.distance(to: T(0))) {
if abs(value1.distance(to: value2)) > abs(accuracy.distance(to: T.zero)) {
return .success
} else {
return .expectedFailure("(\"\(value1)\") is equal to (\"\(value2)\") +/- (\"\(accuracy)\")")
Expand Down
44 changes: 38 additions & 6 deletions Tests/Functional/NegativeAccuracyTestCase/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#endif

// Regression test for https://github.com/apple/swift-corelibs-xctest/pull/7
// and https://github.com/apple/swift-corelibs-xctest/pull/294

// CHECK: Test Suite 'All tests' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: Test Suite '.*\.xctest' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
Expand All @@ -21,6 +22,10 @@ class NegativeAccuracyTestCase: XCTestCase {
("test_equalWithAccuracy_fails", test_equalWithAccuracy_fails),
("test_notEqualWithAccuracy_passes", test_notEqualWithAccuracy_passes),
("test_notEqualWithAccuracy_fails", test_notEqualWithAccuracy_fails),
("test_equalWithAccuracy_int_passes", test_equalWithAccuracy_int_passes),
("test_equalWithAccuracy_int_fails", test_equalWithAccuracy_int_fails),
("test_notEqualWithAccuracy_int_passes", test_notEqualWithAccuracy_int_passes),
("test_notEqualWithAccuracy_int_fails", test_notEqualWithAccuracy_int_fails),
]
}()

Expand All @@ -31,7 +36,7 @@ class NegativeAccuracyTestCase: XCTestCase {
}

// CHECK: Test Case 'NegativeAccuracyTestCase.test_equalWithAccuracy_fails' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: .*[/\\]NegativeAccuracyTestCase[/\\]main.swift:[[@LINE+3]]: error: NegativeAccuracyTestCase.test_equalWithAccuracy_fails : XCTAssertEqual failed: \(\"0\.0\"\) is not equal to \(\"0\.2\"\) \+\/- \(\"-0\.1\"\) - $
// CHECK: .*[/\\]NegativeAccuracyTestCase[/\\]main.swift:[[@LINE+3]]: error: NegativeAccuracyTestCase.test_equalWithAccuracy_fails : XCTAssertEqual failed: \("0\.0"\) is not equal to \("0\.2"\) \+\/- \("-0\.1"\) - $
// CHECK: Test Case 'NegativeAccuracyTestCase.test_equalWithAccuracy_fails' failed \(\d+\.\d+ seconds\)
func test_equalWithAccuracy_fails() {
XCTAssertEqual(0, 0.2, accuracy: -0.1)
Expand All @@ -40,22 +45,49 @@ class NegativeAccuracyTestCase: XCTestCase {
// CHECK: Test Case 'NegativeAccuracyTestCase.test_notEqualWithAccuracy_passes' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: Test Case 'NegativeAccuracyTestCase.test_notEqualWithAccuracy_passes' passed \(\d+\.\d+ seconds\)
func test_notEqualWithAccuracy_passes() {
XCTAssertNotEqual(1, 2, accuracy: -0.5)
XCTAssertNotEqual(1.0, 2.0, accuracy: -0.5)
}

// CHECK: Test Case 'NegativeAccuracyTestCase.test_notEqualWithAccuracy_fails' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: .*[/\\]NegativeAccuracyTestCase[/\\]main.swift:[[@LINE+3]]: error: NegativeAccuracyTestCase.test_notEqualWithAccuracy_fails : XCTAssertNotEqual failed: \("1\.0"\) is equal to \("2\.0"\) \+/- \("-1\.0"\) - $
// CHECK: Test Case 'NegativeAccuracyTestCase.test_notEqualWithAccuracy_fails' failed \(\d+\.\d+ seconds\)
func test_notEqualWithAccuracy_fails() {
XCTAssertNotEqual(1, 2, accuracy: -1)
XCTAssertNotEqual(1.0, 2.0, accuracy: -1.0)
}

// CHECK: Test Case 'NegativeAccuracyTestCase.test_equalWithAccuracy_int_passes' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: Test Case 'NegativeAccuracyTestCase.test_equalWithAccuracy_int_passes' passed \(\d+\.\d+ seconds\)
func test_equalWithAccuracy_int_passes() {
XCTAssertEqual(10, 11, accuracy: 1)
}

// CHECK: Test Case 'NegativeAccuracyTestCase.test_equalWithAccuracy_int_fails' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: .*[/\\]NegativeAccuracyTestCase[/\\]main.swift:[[@LINE+3]]: error: NegativeAccuracyTestCase.test_equalWithAccuracy_int_fails : XCTAssertEqual failed: \("10"\) is not equal to \("8"\) \+\/- \("1"\) - $
// CHECK: Test Case 'NegativeAccuracyTestCase.test_equalWithAccuracy_int_fails' failed \(\d+\.\d+ seconds\)
func test_equalWithAccuracy_int_fails() {
XCTAssertEqual(10, 8, accuracy: 1)
}

// CHECK: Test Case 'NegativeAccuracyTestCase.test_notEqualWithAccuracy_int_passes' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: Test Case 'NegativeAccuracyTestCase.test_notEqualWithAccuracy_int_passes' passed \(\d+\.\d+ seconds\)
func test_notEqualWithAccuracy_int_passes() {
XCTAssertNotEqual(-1, 5, accuracy: 5)
}

// CHECK: Test Case 'NegativeAccuracyTestCase.test_notEqualWithAccuracy_int_fails' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: .*[/\\]NegativeAccuracyTestCase[/\\]main.swift:[[@LINE+3]]: error: NegativeAccuracyTestCase.test_notEqualWithAccuracy_int_fails : XCTAssertNotEqual failed: \("0"\) is equal to \("5"\) \+/- \("5"\) - $
// CHECK: Test Case 'NegativeAccuracyTestCase.test_notEqualWithAccuracy_int_fails' failed \(\d+\.\d+ seconds\)
func test_notEqualWithAccuracy_int_fails() {
XCTAssertNotEqual(0, 5, accuracy: 5)
}

}
// CHECK: Test Suite 'NegativeAccuracyTestCase' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 4 tests, with 2 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: \t Executed 8 tests, with 4 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds

XCTMain([testCase(NegativeAccuracyTestCase.allTests)])

// CHECK: Test Suite '.*\.xctest' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 4 tests, with 2 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: \t Executed 8 tests, with 4 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: Test Suite 'All tests' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 4 tests, with 2 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: \t Executed 8 tests, with 4 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds