1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
//===----------------------------------------------------------------------===//
//
// 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 TestCompacted: XCTestCase {
func test_compacted_is_equivalent_to_compactMap_when_input_as_nil_elements() async {
let source = [1, 2, nil, 3, 4, nil, 5]
let expected = source.compactMap { $0 }
let sequence = source.async.compacted()
var actual = [Int]()
for await item in sequence {
actual.append(item)
}
XCTAssertEqual(expected, actual)
}
func test_compacted_produces_nil_next_element_when_iteration_is_finished() async {
let source = [1, 2, nil, 3, 4, nil, 5]
let expected = source.compactMap { $0 }
let sequence = source.async.compacted()
var actual = [Int]()
var iterator = sequence.makeAsyncIterator()
while let item = await iterator.next() {
actual.append(item)
}
XCTAssertEqual(expected, actual)
let pastEnd = await iterator.next()
XCTAssertNil(pastEnd)
}
func test_compacted_is_equivalent_to_compactMap_when_input_as_no_nil_elements() async {
let source: [Int?] = [1, 2, 3, 4, 5]
let expected = source.compactMap { $0 }
let sequence = source.async.compacted()
var actual = [Int]()
for await item in sequence {
actual.append(item)
}
XCTAssertEqual(expected, actual)
}
func test_compacted_throws_when_root_sequence_throws() async throws {
let sequence = [1, nil, 3, 4, 5, nil, 7].async.map { try throwOn(4, $0) }.compacted()
var iterator = sequence.makeAsyncIterator()
var collected = [Int]()
do {
while let value = try await iterator.next() {
collected.append(value)
}
XCTFail()
} catch {
XCTAssertEqual(error as? Failure, Failure())
}
XCTAssertEqual(collected, [1, 3])
let pastEnd = try await iterator.next()
XCTAssertNil(pastEnd)
}
func test_compacted_finishes_when_iteration_task_is_cancelled() async {
let value: String? = "test"
let source = Indefinite(value: value)
let sequence = source.async.compacted()
let finished = expectation(description: "finished")
let iterated = expectation(description: "iterated")
let task = Task {
var firstIteration = false
for await _ in sequence {
if !firstIteration {
firstIteration = true
iterated.fulfill()
}
}
finished.fulfill()
}
// ensure the other task actually starts
await fulfillment(of: [iterated], timeout: 1.0)
// cancellation should ensure the loop finishes
// without regards to the remaining underlying sequence
task.cancel()
await fulfillment(of: [finished], timeout: 1.0)
}
}
|