Skip to content

Make XCTAssertEqual with accuracy more generic #319

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

Merged
merged 3 commits into from
Dec 13, 2020
Merged
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
33 changes: 29 additions & 4 deletions Sources/XCTest/Public/XCTAssert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,29 @@ public func XCTAssertEqual<T: Equatable>(_ expression1: @autoclosure () throws -
}
}

private func areEqual<T: Numeric>(_ exp1: T, _ exp2: T, accuracy: T) -> Bool {
// Test with equality first to handle comparing inf/-inf with itself.
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
}
}

public func XCTAssertEqual<T: FloatingPoint>(_ expression1: @autoclosure () throws -> T, _ expression2: @autoclosure () throws -> T, accuracy: T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
_XCTAssertEqual(try expression1(), try expression2(), accuracy: accuracy, message(), file: file, line: line)
}

public func XCTAssertEqual<T: Numeric>(_ expression1: @autoclosure () throws -> T, _ expression2: @autoclosure () throws -> T, accuracy: T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
_XCTAssertEqual(try expression1(), try expression2(), accuracy: accuracy, message(), file: file, line: line)
}

private func _XCTAssertEqual<T: Numeric>(_ expression1: @autoclosure () throws -> T, _ expression2: @autoclosure () throws -> T, accuracy: T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
_XCTEvaluateAssertion(.equalWithAccuracy, message: message(), file: file, line: line) {
let (value1, value2) = (try expression1(), try expression2())
// Test with equality first to handle comparing inf/-inf with itself.
if value1 == value2 ||
abs(value1.distance(to: value2)) <= abs(accuracy.distance(to: T(0))) {
if areEqual(value1, value2, accuracy: accuracy) {
return .success
} else {
return .expectedFailure("(\"\(value1)\") is not equal to (\"\(value2)\") +/- (\"\(accuracy)\")")
Expand Down Expand Up @@ -267,9 +284,17 @@ 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) {
_XCTAssertNotEqual(try expression1(), try expression2(), accuracy: accuracy, message(), file: file, line: line)
}

public func XCTAssertNotEqual<T: Numeric>(_ expression1: @autoclosure () throws -> T, _ expression2: @autoclosure () throws -> T, accuracy: T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
_XCTAssertNotEqual(try expression1(), try expression2(), accuracy: accuracy, message(), file: file, line: line)
}

private func _XCTAssertNotEqual<T: 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 !areEqual(value1, value2, accuracy: accuracy) {
return .success
} else {
return .expectedFailure("(\"\(value1)\") is equal to (\"\(value2)\") +/- (\"\(accuracy)\")")
Expand Down
160 changes: 160 additions & 0 deletions Tests/Functional/EqualityWithAccuracy/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// RUN: %{swiftc} %s -o %T/EqualityWithAccuracy
// RUN: %T/EqualityWithAccuracy > %t || true
// RUN: %{xctest_checker} %t %s

#if os(macOS)
import SwiftXCTest
#else
import XCTest
#endif

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

Choose a reason for hiding this comment

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

This new test source file merges together the two other test files deleted below, incorporating both of their tests and adding several more examples under the "passing" methods.


// 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+

// CHECK: Test Suite 'EqualityWithAccuracyTests' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
class EqualityWithAccuracyTests: XCTestCase {
static var allTests = {
return [
("test_equalWithAccuracy_passes", test_equalWithAccuracy_passes),
("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),
("test_equalWithAccuracy_infinity_fails", test_equalWithAccuracy_infinity_fails),
("test_notEqualWithAccuracy_infinity_fails", test_notEqualWithAccuracy_infinity_fails),
("test_equalWithAccuracy_nan_fails", test_equalWithAccuracy_nan_fails),
]
}()

// CHECK: Test Case 'EqualityWithAccuracyTests.test_equalWithAccuracy_passes' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: Test Case 'EqualityWithAccuracyTests.test_equalWithAccuracy_passes' passed \(\d+\.\d+ seconds\)
func test_equalWithAccuracy_passes() {
XCTAssertEqual(0, 0.1, accuracy: -0.1)
XCTAssertEqual(1, 1, accuracy: 0)
XCTAssertEqual(0, 0, accuracy: 0)
XCTAssertEqual(0, 1, accuracy: 1)
XCTAssertEqual(0, 1, accuracy: 1.01)
XCTAssertEqual(1, 1.09, accuracy: 0.1)
XCTAssertEqual(1 as Float, 1.09, accuracy: 0.1)
XCTAssertEqual(1 as Float32, 1.09, accuracy: 0.1)
XCTAssertEqual(1 as Float64, 1.09, accuracy: 0.1)
XCTAssertEqual(1 as CGFloat, 1.09, accuracy: 0.1)
XCTAssertEqual(1 as Double, 1.09, accuracy: 0.1)
XCTAssertEqual(1 as Int, 2, accuracy: 5)
XCTAssertEqual(1 as UInt, 4, accuracy: 5)
XCTAssertEqual(1, -1, accuracy: 2)
XCTAssertEqual(-2, -4, accuracy: 2)
XCTAssertEqual(Double.infinity, .infinity, accuracy: 0)
XCTAssertEqual(Double.infinity, .infinity, accuracy: 1)
XCTAssertEqual(Double.infinity, .infinity, accuracy: 1e-6)
XCTAssertEqual(-Double.infinity, -.infinity, accuracy: 1)
XCTAssertEqual(Double.infinity, .infinity, accuracy: 1e-6)
XCTAssertEqual(Double.infinity, .infinity, accuracy: 1e6)
XCTAssertEqual(Double.infinity, .infinity, accuracy: .infinity)
XCTAssertEqual(Double.infinity, -.infinity, accuracy: .infinity)
XCTAssertEqual(Float.infinity, .infinity, accuracy: 1e-6)
XCTAssertEqual(Double.infinity, .infinity - 1, accuracy: 0)
}

// CHECK: Test Case 'EqualityWithAccuracyTests.test_equalWithAccuracy_fails' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: .*[/\\]EqualityWithAccuracy[/\\]main.swift:[[@LINE+3]]: error: EqualityWithAccuracyTests.test_equalWithAccuracy_fails : XCTAssertEqual failed: \("0\.0"\) is not equal to \("0\.2"\) \+\/- \("-0\.1"\) - $
// CHECK: Test Case 'EqualityWithAccuracyTests.test_equalWithAccuracy_fails' failed \(\d+\.\d+ seconds\)
func test_equalWithAccuracy_fails() {
XCTAssertEqual(0, 0.2, accuracy: -0.1)
}

// CHECK: Test Case 'EqualityWithAccuracyTests.test_notEqualWithAccuracy_passes' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: Test Case 'EqualityWithAccuracyTests.test_notEqualWithAccuracy_passes' passed \(\d+\.\d+ seconds\)
func test_notEqualWithAccuracy_passes() {
XCTAssertNotEqual(1.0, 2.0, accuracy: -0.5)
XCTAssertNotEqual(0, 1, accuracy: 0.1)
XCTAssertNotEqual(1, 1.11, accuracy: 0.1)
XCTAssertNotEqual(1 as Float, 1.11, accuracy: 0.1)
XCTAssertNotEqual(1 as Float32, 1.11, accuracy: 0.1)
XCTAssertNotEqual(1 as Float64, 1.11, accuracy: 0.1)
XCTAssertNotEqual(1 as CGFloat, 1.11, accuracy: 0.1)
XCTAssertNotEqual(1 as Double, 1.11, accuracy: 0.1)
XCTAssertNotEqual(1 as Int, 10, accuracy: 5)
XCTAssertNotEqual(1 as UInt, 10, accuracy: 5)
XCTAssertNotEqual(1, -1, accuracy: 1)
XCTAssertNotEqual(-2, -4, accuracy: 1)
XCTAssertNotEqual(Double.nan, Double.nan, accuracy: 0)
XCTAssertNotEqual(1, Double.nan, accuracy: 0)
XCTAssertNotEqual(Double.nan, 1, accuracy: 0)
XCTAssertNotEqual(Double.nan, 1, accuracy: .nan)
XCTAssertNotEqual(Double.infinity, -.infinity, accuracy: 0)
XCTAssertNotEqual(Double.infinity, -.infinity, accuracy: 1)
XCTAssertNotEqual(Double.infinity, -.infinity, accuracy: 1e-6)
XCTAssertNotEqual(Double.infinity, -.infinity, accuracy: 1e6)
}

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

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

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

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

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

// CHECK: Test Case 'EqualityWithAccuracyTests.test_equalWithAccuracy_infinity_fails' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: .*[/\\]EqualityWithAccuracy[/\\]main.swift:[[@LINE+3]]: error: EqualityWithAccuracyTests.test_equalWithAccuracy_infinity_fails : XCTAssertEqual failed: \(\"-inf\"\) is not equal to \(\"inf\"\) \+\/- \(\"1e-06"\) - $
// CHECK: Test Case 'EqualityWithAccuracyTests.test_equalWithAccuracy_infinity_fails' failed \(\d+\.\d+ seconds\)
func test_equalWithAccuracy_infinity_fails() {
XCTAssertEqual(-Double.infinity, .infinity, accuracy: 1e-6)
}

// CHECK: Test Case 'EqualityWithAccuracyTests.test_notEqualWithAccuracy_infinity_fails' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: .*[/\\]EqualityWithAccuracy[/\\]main.swift:[[@LINE+3]]: error: EqualityWithAccuracyTests.test_notEqualWithAccuracy_infinity_fails : XCTAssertNotEqual failed: \("-inf"\) is equal to \("-inf"\) \+/- \("1e-06"\) - $
// CHECK: Test Case 'EqualityWithAccuracyTests.test_notEqualWithAccuracy_infinity_fails' failed \(\d+\.\d+ seconds\)
func test_notEqualWithAccuracy_infinity_fails() {
XCTAssertNotEqual(-Double.infinity, -.infinity, accuracy: 1e-6)
}

// CHECK: Test Case 'EqualityWithAccuracyTests.test_equalWithAccuracy_nan_fails' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: .*[/\\]EqualityWithAccuracy[/\\]main.swift:[[@LINE+3]]: error: EqualityWithAccuracyTests.test_equalWithAccuracy_nan_fails : XCTAssertEqual failed: \("nan"\) is not equal to \("nan"\) \+/- \("0.0"\) - $
// CHECK: Test Case 'EqualityWithAccuracyTests.test_equalWithAccuracy_nan_fails' failed \(\d+\.\d+ seconds\)
func test_equalWithAccuracy_nan_fails() {
XCTAssertEqual(Double.nan, Double.nan, accuracy: 0)
}

}
// CHECK: Test Suite 'EqualityWithAccuracyTests' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 11 tests, with 7 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds

XCTMain([testCase(EqualityWithAccuracyTests.allTests)])

// CHECK: Test Suite '.*\.xctest' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 11 tests, with 7 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 11 tests, with 7 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
70 changes: 0 additions & 70 deletions Tests/Functional/InfinityAccuracyTestCase/main.swift

This file was deleted.

Loading