let result = 3.3
XCTAssertEqual(result, 10/3, accuracy: 0.04)
In the code snippet above, the framework considers those two numbers are equal because they are just 0.033333333333333 different, which is less than the accuracy parameter 0.04.
Scientific Notation
In Swift, the double literal can be expressed in scientific notation like 1.234e10, which equals 12_340_000_000. The test in the example below will fail.
XCTAssertEqual(1.1234e10, 1.123e10)
To make the test successful, the accuracy parameter should be specified as following.
XCTAssertEqual(1.1234e10, 1.123e10, accuracy: 0.0004e10)
// 1.1234e10 = 11234_000_000
// 1.123e10 = 11230_000_000
// 0.0004e10 = 4_000_000
The accuracy value less than that such as 0.0003e10 or 0.0001e10 still make the test fail.
No comments:
Post a Comment