Files
swift-async-algorithms/Tests/AsyncStreamingTests/AsyncReader/AsyncReaderTests.swift
T
Franz Busch be2467e0c0 Add new experimental _AsyncStreaming module (#400)
This PR adds an experimental module for asynchronous streaming interfaces as a potential evolution to async sequence. These APIs are considered highly unstable and are mostly for experimentation purposes; hence, they are part of an underscored module and all APIs are hidden behind a trait. I expect significant changes to those APIs to offer more functionality such as gather scatter and more.
2026-05-11 14:58:21 +02:00

83 lines
2.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Async Algorithms open source project
//
// Copyright (c) 2026 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
//
//===----------------------------------------------------------------------===//
#if UnstableAsyncStreaming && compiler(>=6.4)
import AsyncStreaming
import BasicContainers
import ContainersPreview
import Testing
@Suite
struct AsyncReaderTests {
@Test
@available(macOS 26.2, iOS 26.2, watchOS 26.2, tvOS 26.2, visionOS 26.2, *)
func readWithMaximumCount() async {
var reader = UniqueArrayAsyncReader(storage: UniqueArray(capacity: 5, copying: [1, 2, 3, 4, 5]))
let result = try! await reader.read(maximumCount: 3) { span in
return Array(span)
}
#expect(result == [1, 2, 3])
}
@Test
@available(macOS 26.2, iOS 26.2, watchOS 26.2, tvOS 26.2, visionOS 26.2, *)
func readWithoutMaximumCount() async {
var reader = UniqueArrayAsyncReader(storage: UniqueArray(capacity: 5, copying: [1, 2, 3, 4, 5]))
let result = try! await reader.read(maximumCount: .max) { span in
return Array(span)
}
#expect(result == [1, 2, 3, 4, 5])
}
@Test
@available(macOS 26.2, iOS 26.2, watchOS 26.2, tvOS 26.2, visionOS 26.2, *)
func readEmptySpanAtEnd() async {
var reader = UniqueArrayAsyncReader(storage: UniqueArray(capacity: 3, copying: [1, 2, 3]))
// Read all data
_ = try! await reader.read { span in
return Array(span)
}
// Next read should return empty span
let result = try! await reader.read { span in
return span.count
}
#expect(result == 0)
}
@Test
@available(macOS 26.2, iOS 26.2, watchOS 26.2, tvOS 26.2, visionOS 26.2, *)
func readMultipleChunks() async {
var reader = UniqueArrayAsyncReader(storage: UniqueArray(capacity: 6, copying: [1, 2, 3, 4, 5, 6]))
var chunks: [[Int]] = []
while true {
let chunk = try! await reader.read(maximumCount: 2) { span in
return Array(span)
}
print(chunk)
if chunk.isEmpty {
break
}
chunks.append(chunk)
}
#expect(chunks == [[1, 2], [3, 4], [5, 6]])
}
}
#endif