-
Notifications
You must be signed in to change notification settings - Fork 263
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
stmontgomery
merged 3 commits into
swiftlang:main
from
stmontgomery:GenericEqualWithAccuracy
Dec 13, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
// 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 |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.