【发布时间】:2020-04-20 13:15:43
【问题描述】:
主题标题是 testPrimesUpTo100ShouldBe25() 方法中的 xCode Assert Equal 测试相关的编译器错误。
我注意到的一件事是,当从 testPrimePerformance() 和 testPrimesUpTo100ShouldBe25() 调用 PrimeCalculator 中的 calculate() 方法时,只有在 testPrimePerformance() 中“着色”为绿色。
完整的swift源文件如下:
import XCTest
struct PrimeCalculator {
static func calculate(upTo max: Int) -> [Int] {
guard max > 1 else {
return []
}
var sieve = [Bool](repeating: true, count: max)
sieve[0] = false
sieve[1] = false
for number in 2 ..< max {
if sieve[number] == true {
for multiple in stride(from: number * number, to: sieve.count, by: number) {
sieve[multiple] = false
}
}
}
// collapse our results down to a single array of primes
let primes = sieve.enumerated().compactMap { $1 == true ? $0 : nil }
return primes
}
}
class AsynchronousTests: XCTestCase {
func testPrimePerformance() {
measure {
_ = PrimeCalculator.calculate(upTo: 1_000_000)
}
}
func testPrimesUpTo100ShouldBe25() {
// given
let maximumCount = 100
// when
let progress = PrimeCalculator.calculate(upTo: maximumCount) {
XCTAssertEqual($0.count, 25)
}
// then
let predicate = NSPredicate(format: "@.completedUnitCount == %@", argumentArray: [progress, maximumCount])
let expectation = XCTNSPredicateExpectation(predicate: predicate, object: progress)
wait(for: [expectation], timeout: 10)
}
override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
measure {
// Put the code you want to measure the time of here.
}
}
}
【问题讨论】:
-
这是一个“请解决我的域特定编译错误”的问题。不适合这个论坛。
标签: swift xcode xctest xctestcase