Files
Joseph CursioandClaude Opus 5.5 0b3faf58d9 Test Producer.map with a throwing transform
Check that next() and reduce(into:_:) over the mapped producer rethrow
the transform's error, stop calling it after it throws, pass on only
items mapped before the failure, in order, and leak nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 07:52:21 -06:00

203 lines
6.6 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections 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
//
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
//
//===----------------------------------------------------------------------===//
import XCTest
#if COLLECTIONS_SINGLE_MODULE
import Collections
#else
import _CollectionsTestSupport
import InternalCollectionsUtilities
import SpanPreview
#endif
import ContainersPreview
#if compiler(>=6.4) && UnstableContainersPreview
private struct ClosureError: Error, Equatable {
let value: Int
}
@available(SwiftStdlib 6.4, *)
final class ProducerTests: CollectionTestCase {
/// A producer over `0 ..< count`.
@_lifetime(immortal)
func range(_ count: Int) -> UnfoldProducer<Int, Never, Int> {
producer(state: 0) { (state: inout Int) -> Int? in
guard state < count else { return nil }
defer { state += 1 }
return state
}
}
/// Generate into a span of the given capacity, returning the generated items
/// and the value returned by `generate(into:)`.
func generate<P: Producer<Int, Never> & ~Copyable & ~Escapable>(
_ producer: inout P, capacity: Int
) -> (returned: Int, items: [Int]) {
var items: [Int] = []
let returned = withUnsafeTemporaryAllocation(
of: Int.self, capacity: capacity
) { buffer in
var span = unsafe OutputSpan(buffer: buffer, initializedCount: 0)
let returned = producer.generate(into: &span)
span._consumeAll { src in
while let item = src.popFirst() { items.append(item) }
}
return returned
}
return (returned, items)
}
func test_map_generate_fills_target() {
withEvery("capacity", in: [1, 7, 8, 9, 16, 33, 100, 150]) { capacity in
var p = range(100).map { $0 * 2 }
let (returned, items) = generate(&p, capacity: capacity)
let expected = Swift.min(capacity, 100)
expectEqual(returned, expected)
expectEqualElements(items, (0 ..< expected).map { $0 * 2 })
}
}
func test_filter_generate_fills_target() {
withEvery("capacity", in: [1, 7, 8, 9, 16, 33, 50, 80]) { capacity in
var p = range(100).filter { $0 % 2 == 0 }
let (returned, items) = generate(&p, capacity: capacity)
let expected = Swift.min(capacity, 50)
expectEqual(returned, expected)
expectEqualElements(items, (0 ..< expected).map { $0 * 2 })
}
}
func test_filter_underestimatedCount() {
let p = range(100).filter { $0 % 2 == 0 }
expectEqual(p.underestimatedCount, 0)
}
func test_reduce_over_map_and_filter() {
expectEqual(range(100).map { $0 }.reduce(0, +), 4950)
expectEqual(range(100).filter { $0 % 2 == 0 }.reduce(0, +), 2450)
}
func test_reduce_throwing_closure() {
withEvery("failAt", in: [0, 1, 7, 8, 9, 15, 16, 19]) { failAt in
withLifetimeTracking { tracker in
var seen: [Int] = []
do throws(ClosureError) {
_ = try CustomProducer<LifetimeTrackedStruct<Int>, ClosureError> {
$0 < 20 ? tracker.structInstance(for: $0) : nil
}
.reduce(0) { count, item throws(ClosureError) in
seen.append(item.payload)
if item.payload == failAt { throw ClosureError(value: failAt) }
return count + 1
}
expectFailure("reduce did not throw")
} catch {
expectEqual(error, ClosureError(value: failAt))
}
expectEqual(seen, Array(0 ... failAt))
}
}
}
func test_reduce_into_throwing_closure() {
withEvery("failAt", in: [0, 1, 7, 8, 9, 15, 16, 19]) { failAt in
withLifetimeTracking { tracker in
var seen: [Int] = []
do throws(ClosureError) {
_ = try CustomProducer<LifetimeTrackedStruct<Int>, ClosureError> {
$0 < 20 ? tracker.structInstance(for: $0) : nil
}
.reduce(into: 0) { count, item throws(ClosureError) in
seen.append(item.payload)
if item.payload == failAt { throw ClosureError(value: failAt) }
count += 1
}
expectFailure("reduce(into:) did not throw")
} catch {
expectEqual(error, ClosureError(value: failAt))
}
expectEqual(seen, Array(0 ... failAt))
}
}
}
func test_map_throwing_transform_next() {
withEvery("failAt", in: [0, 1, 7, 8, 9, 15, 16, 19]) { failAt in
withLifetimeTracking { tracker in
var seen: [Int] = []
var mapped = CustomProducer<LifetimeTracked<Int>, ClosureError> {
$0 < 20 ? tracker.instance(for: $0) : nil
}
.map { item throws(ClosureError) in
seen.append(item.payload)
if item.payload == failAt { throw ClosureError(value: failAt) }
return item
}
var items: [Int] = []
do throws(ClosureError) {
while let item = try mapped.next() {
items.append(item.payload)
}
expectFailure("next() did not throw")
} catch {
expectEqual(error, ClosureError(value: failAt))
}
expectEqual(items, Array(0 ..< failAt))
expectEqual(seen, Array(0 ... failAt))
}
}
}
func test_map_throwing_transform_reduce_into() {
withEvery("failAt", in: [0, 1, 7, 8, 9, 15, 16, 19]) { failAt in
withLifetimeTracking { tracker in
var seen: [Int] = []
var reduced: [Int] = []
do throws(ClosureError) {
try CustomProducer<LifetimeTracked<Int>, ClosureError> {
$0 < 20 ? tracker.instance(for: $0) : nil
}
.map { item throws(ClosureError) in
seen.append(item.payload)
if item.payload == failAt { throw ClosureError(value: failAt) }
return item
}
.reduce(into: ()) { _, item throws(ClosureError) in
reduced.append(item.payload)
}
expectFailure("reduce(into:) did not throw")
} catch {
expectEqual(error, ClosureError(value: failAt))
}
expectEqual(seen, Array(0 ... failAt))
expectTrue((0 ..< failAt).starts(with: reduced))
}
}
}
func test_default_skip_by_does_not_overshoot() {
withEvery("n", in: [1, 5, 8, 10, 16, 40, 100, 150]) { n in
var p = range(100).filter { $0 % 2 == 0 }
var remaining = n
p.skip(by: &remaining)
let skipped = Swift.min(n, 50)
expectEqual(remaining, n - skipped)
expectEqual(p.next(), skipped < 50 ? skipped * 2 : nil)
}
}
}
#endif