|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Async Algorithms open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2026 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +#if UnstableAsyncStreaming && compiler(>=6.4) |
| 13 | + |
| 14 | +import AsyncStreaming |
| 15 | +import BasicContainers |
| 16 | +import ContainersPreview |
| 17 | +import Testing |
| 18 | + |
| 19 | +@Suite |
| 20 | +struct AsyncReaderforEachChunkTests { |
| 21 | + @Test |
| 22 | + @available(macOS 26.2, iOS 26.2, watchOS 26.2, tvOS 26.2, visionOS 26.2, *) |
| 23 | + func forEachChunkIteratesAllSpans() async throws { |
| 24 | + let reader = UniqueArrayAsyncReader(storage: UniqueArray(capacity: 5, copying: [1, 2, 3, 4, 5])) |
| 25 | + var elementCount = 0 |
| 26 | + |
| 27 | + await reader.forEachChunk { span in |
| 28 | + elementCount += span.count |
| 29 | + } |
| 30 | + |
| 31 | + #expect(elementCount == 5) |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + @available(macOS 26.2, iOS 26.2, watchOS 26.2, tvOS 26.2, visionOS 26.2, *) |
| 36 | + func forEachChunkProcessesElements() async throws { |
| 37 | + let reader = UniqueArrayAsyncReader(storage: UniqueArray(capacity: 3, copying: [10, 20, 30])) |
| 38 | + var sum = 0 |
| 39 | + |
| 40 | + await reader.forEachChunk { span in |
| 41 | + for i in span.indices { |
| 42 | + sum += span[i] |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + #expect(sum == 60) |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + @available(macOS 26.2, iOS 26.2, watchOS 26.2, tvOS 26.2, visionOS 26.2, *) |
| 51 | + func forEachChunkWithEmptyReader() async throws { |
| 52 | + let reader = UniqueArrayAsyncReader(storage: UniqueArray(capacity: 0, copying: [])) |
| 53 | + var callCount = 0 |
| 54 | + |
| 55 | + await reader.forEachChunk { span in |
| 56 | + callCount += 1 |
| 57 | + } |
| 58 | + |
| 59 | + #expect(callCount == 0) |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + @available(macOS 26.2, iOS 26.2, watchOS 26.2, tvOS 26.2, visionOS 26.2, *) |
| 64 | + func forEachChunkWithThrowingBody() async { |
| 65 | + enum TestError: Error { |
| 66 | + case failed |
| 67 | + } |
| 68 | + |
| 69 | + let reader = UniqueArrayAsyncReader(storage: UniqueArray(capacity: 3, copying: [1, 2, 3])) |
| 70 | + |
| 71 | + do { |
| 72 | + try await reader.forEachChunk { (span) throws(TestError) -> Void in |
| 73 | + throw TestError.failed |
| 74 | + } |
| 75 | + Issue.record("Expected error to be thrown") |
| 76 | + } catch { |
| 77 | + #expect(error == EitherError.second(TestError.failed)) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + @available(macOS 26.2, iOS 26.2, watchOS 26.2, tvOS 26.2, visionOS 26.2, *) |
| 83 | + func forEachChunkWithNeverFailingReader() async { |
| 84 | + enum TestError: Error { |
| 85 | + case failed |
| 86 | + } |
| 87 | + |
| 88 | + let reader = UniqueArrayAsyncReader(storage: UniqueArray(capacity: 3, copying: [1, 2, 3])) |
| 89 | + var count = 0 |
| 90 | + |
| 91 | + do { |
| 92 | + try await reader.forEachChunk { (span) throws(TestError) -> Void in |
| 93 | + count += span.count |
| 94 | + } |
| 95 | + } catch { |
| 96 | + Issue.record("No error should be thrown from reader") |
| 97 | + } |
| 98 | + |
| 99 | + #expect(count == 3) |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + @available(macOS 26.2, iOS 26.2, watchOS 26.2, tvOS 26.2, visionOS 26.2, *) |
| 104 | + func forEachChunkWithAsyncWork() async throws { |
| 105 | + let reader = UniqueArrayAsyncReader(storage: UniqueArray(capacity: 3, copying: [1, 2, 3])) |
| 106 | + var results: [Int] = [] |
| 107 | + |
| 108 | + await reader.forEachChunk { span in |
| 109 | + await Task.yield() |
| 110 | + for i in span.indices { |
| 111 | + results.append(span[i]) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + #expect(results == [1, 2, 3]) |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + @available(macOS 26.2, iOS 26.2, watchOS 26.2, tvOS 26.2, visionOS 26.2, *) |
| 120 | + func forEachChunkMultipleSpans() async { |
| 121 | + var reader = UniqueArrayAsyncReader(storage: UniqueArray(capacity: 6, copying: [1, 2, 3, 4, 5, 6])) |
| 122 | + var spanCounts: [Int] = [] |
| 123 | + |
| 124 | + // Force reading in smaller chunks |
| 125 | + while true { |
| 126 | + let hasMore = try! await reader.read(maximumCount: 2) { span in |
| 127 | + if span.count > 0 { |
| 128 | + spanCounts.append(span.count) |
| 129 | + return true |
| 130 | + } |
| 131 | + return false |
| 132 | + } |
| 133 | + if !hasMore { |
| 134 | + break |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + #expect(spanCounts == [2, 2, 2]) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +#endif |
0 commit comments