mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-25 04:27:48 +00:00
Add a sequence that intersperses separator values
This commit is contained in:
1 parent
205414d19d
commit
8295e01ac8
2 files changed
+200
No files matched your search
@@ -0,0 +1,94 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Async Algorithms open source project
|
||||
//
|
||||
// Copyright (c) 2022 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import AsyncAlgorithms
|
||||
|
||||
final class TestInterspersed: XCTestCase {
|
||||
func test_interspersed() async {
|
||||
let source = [1, 2, 3, 4, 5]
|
||||
let expected = [1, 0, 2, 0, 3, 0, 4, 0, 5]
|
||||
let sequence = source.async.interspersed(with: 0)
|
||||
var actual = [Int]()
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
while let item = await iterator.next() {
|
||||
actual.append(item)
|
||||
}
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_interspersed_empty() async {
|
||||
let source = [Int]()
|
||||
let expected = [Int]()
|
||||
let sequence = source.async.interspersed(with: 0)
|
||||
var actual = [Int]()
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
while let item = await iterator.next() {
|
||||
actual.append(item)
|
||||
}
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_interspersed_with_throwing_upstream() async {
|
||||
let source = [1, 2, 3, -1, 4, 5]
|
||||
let expected = [1, 0, 2, 0, 3, 0]
|
||||
var actual = [Int]()
|
||||
let throwingSequence = source.async.map ({ (e) throws -> Int in
|
||||
if e < 0 {
|
||||
throw NSError(domain: NSCocoaErrorDomain, code: -1, userInfo: nil)
|
||||
}
|
||||
return e
|
||||
})
|
||||
|
||||
var iterator = throwingSequence.interspersed(with: 0).makeAsyncIterator()
|
||||
do {
|
||||
while let item = try await iterator.next() {
|
||||
actual.append(item)
|
||||
}
|
||||
XCTFail()
|
||||
} catch {
|
||||
XCTAssertEqual((error as NSError).code, -1)
|
||||
}
|
||||
let pastEnd = try! await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
|
||||
func test_removeDuplicates_cancellation() async {
|
||||
let source = Indefinite(value: "test")
|
||||
let sequence = source.async.interspersed(with: "sep")
|
||||
let finished = expectation(description: "finished")
|
||||
let iterated = expectation(description: "iterated")
|
||||
let task = Task {
|
||||
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
let _ = await iterator.next()
|
||||
iterated.fulfill()
|
||||
|
||||
while let _ = await iterator.next() { }
|
||||
|
||||
let pastEnd = await iterator.next()
|
||||
XCTAssertNil(pastEnd)
|
||||
|
||||
finished.fulfill()
|
||||
}
|
||||
// ensure the other task actually starts
|
||||
wait(for: [iterated], timeout: 1.0)
|
||||
// cancellation should ensure the loop finishes
|
||||
// without regards to the remaining underlying sequence
|
||||
task.cancel()
|
||||
wait(for: [finished], timeout: 1.0)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user