|
13 | 13 | import AsyncAlgorithms
|
14 | 14 |
|
15 | 15 | final class TestAdjacentPairs: XCTestCase {
|
16 |
| - func test_adjacentPairs() async { |
17 |
| - let source = 1...5 |
18 |
| - let expected = [(1,2), (2,3), (3,4), (4,5)] |
19 |
| - let sequence = source.async.adjacentPairs() |
20 |
| - var actual: [(Int, Int)] = [] |
21 |
| - for await item in sequence { |
22 |
| - actual.append(item) |
23 |
| - } |
24 |
| - XCTAssertEqual(expected, actual) |
| 16 | + func test_adjacentPairs_produces_tuples_of_adjacent_values_of_original_element() async { |
| 17 | + let source = 1...5 |
| 18 | + let expected = Array(zip(source, source.dropFirst())) |
| 19 | + |
| 20 | + let sequence = source.async.adjacentPairs() |
| 21 | + var actual: [(Int, Int)] = [] |
| 22 | + for await item in sequence { |
| 23 | + actual.append(item) |
25 | 24 | }
|
26 | 25 |
|
27 |
| - func test_empty() async { |
28 |
| - let source = 0..<1 |
29 |
| - let expected: [(Int, Int)] = [] |
30 |
| - let sequence = source.async.adjacentPairs() |
31 |
| - var actual: [(Int, Int)] = [] |
32 |
| - for await item in sequence { |
33 |
| - actual.append(item) |
34 |
| - } |
35 |
| - XCTAssertEqual(expected, actual) |
| 26 | + XCTAssertEqual(expected, actual) |
| 27 | + } |
| 28 | + |
| 29 | + func test_adjacentPairs_forwards_termination_from_source_when_iteration_is_finished() async { |
| 30 | + let source = 1...5 |
| 31 | + |
| 32 | + var iterator = source.async.adjacentPairs().makeAsyncIterator() |
| 33 | + while let _ = await iterator.next() {} |
| 34 | + |
| 35 | + let pastEnd = await iterator.next() |
| 36 | + XCTAssertNil(pastEnd) |
| 37 | + } |
| 38 | + |
| 39 | + func test_adjacentPairs_produces_empty_sequence_when_source_sequence_is_empty() async { |
| 40 | + let source = 0..<1 |
| 41 | + let expected: [(Int, Int)] = [] |
| 42 | + |
| 43 | + let sequence = source.async.adjacentPairs() |
| 44 | + var actual: [(Int, Int)] = [] |
| 45 | + for await item in sequence { |
| 46 | + actual.append(item) |
36 | 47 | }
|
37 | 48 |
|
38 |
| - func test_cancellation() async { |
39 |
| - let source = Indefinite(value: 0) |
40 |
| - let sequence = source.async.adjacentPairs() |
41 |
| - let finished = expectation(description: "finished") |
42 |
| - let iterated = expectation(description: "iterated") |
43 |
| - let task = Task { |
44 |
| - var firstIteration = false |
45 |
| - for await _ in sequence { |
46 |
| - if !firstIteration { |
47 |
| - firstIteration = true |
48 |
| - iterated.fulfill() |
49 |
| - } |
50 |
| - } |
51 |
| - finished.fulfill() |
| 49 | + XCTAssertEqual(expected, actual) |
| 50 | + } |
| 51 | + |
| 52 | + func test_adjacentPairs_throws_when_source_sequence_throws() async throws { |
| 53 | + let source = 1...5 |
| 54 | + let expected = [(1, 2), (2, 3)] |
| 55 | + |
| 56 | + let sequence = source.async.map { try throwOn(4, $0) }.adjacentPairs() |
| 57 | + var iterator = sequence.makeAsyncIterator() |
| 58 | + var actual = [(Int, Int)]() |
| 59 | + do { |
| 60 | + while let value = try await iterator.next() { |
| 61 | + actual.append(value) |
| 62 | + } |
| 63 | + XCTFail(".adjacentPairs should throw when the source sequence throws") |
| 64 | + } catch { |
| 65 | + XCTAssertEqual(error as? Failure, Failure()) |
| 66 | + } |
| 67 | + |
| 68 | + XCTAssertEqual(actual, expected) |
| 69 | + let pastEnd = try await iterator.next() |
| 70 | + XCTAssertNil(pastEnd) |
| 71 | + } |
| 72 | + |
| 73 | + func test_adjacentPairs_finishes_when_iteration_task_is_cancelled() async { |
| 74 | + let source = Indefinite(value: 0) |
| 75 | + let sequence = source.async.adjacentPairs() |
| 76 | + let finished = expectation(description: "finished") |
| 77 | + let iterated = expectation(description: "iterated") |
| 78 | + |
| 79 | + let task = Task { |
| 80 | + var firstIteration = false |
| 81 | + for await _ in sequence { |
| 82 | + if !firstIteration { |
| 83 | + firstIteration = true |
| 84 | + iterated.fulfill() |
52 | 85 | }
|
53 |
| - // ensure the other task actually starts |
54 |
| - wait(for: [iterated], timeout: 1.0) |
55 |
| - // cancellation should ensure the loop finishes |
56 |
| - // without regards to the remaining underlying sequence |
57 |
| - task.cancel() |
58 |
| - wait(for: [finished], timeout: 1.0) |
| 86 | + } |
| 87 | + finished.fulfill() |
59 | 88 | }
|
| 89 | + |
| 90 | + // ensure the other task actually starts |
| 91 | + wait(for: [iterated], timeout: 1.0) |
| 92 | + // cancellation should ensure the loop finishes |
| 93 | + // without regards to the remaining underlying sequence |
| 94 | + task.cancel() |
| 95 | + wait(for: [finished], timeout: 1.0) |
| 96 | + } |
60 | 97 | }
|
0 commit comments