From f48752c73a510657e9fc7e19b4acf2542382cf93 Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Fri, 16 Oct 2026 17:48:33 -0700 Subject: [PATCH] =?UTF-8?q?Test=20OutputSpan=E2=80=99s=20=5FconsumeAll()?= =?UTF-8?q?=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OutputSpanInputSpanHelpersTests.swift | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 Tests/SpanPreviewTests/OutputSpanInputSpanHelpersTests.swift diff --git a/Tests/SpanPreviewTests/OutputSpanInputSpanHelpersTests.swift b/Tests/SpanPreviewTests/OutputSpanInputSpanHelpersTests.swift new file mode 100644 index 00000000..f2336022 --- /dev/null +++ b/Tests/SpanPreviewTests/OutputSpanInputSpanHelpersTests.swift @@ -0,0 +1,90 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#if COLLECTIONS_SINGLE_MODULE +import Collections +#else +import _CollectionsTestSupport +import SpanPreview +#endif + +#if UnstableContainersPreview +final class OutputSpanInputSpanHelpersTests: CollectionTestCase { + struct TestError: Error, Equatable {} + + func withOutputSpan( + capacity: Int, + count: Int, + tracker: LifetimeTracker, + _ body: (inout OutputSpan>) -> Void + ) { + let buffer: UnsafeMutableBufferPointer> + unsafe buffer = .allocate(capacity: capacity) + for i in 0 ..< count { + unsafe buffer.initializeElement(at: i, to: tracker.instance(for: i)) + } + var span = unsafe OutputSpan(buffer: buffer, initializedCount: count) + body(&span) + _ = consume span + unsafe buffer.deallocate() + } + + func test_consumeAll() { + for capacity in [0, 1, 5, 10] { + for count in 0 ... capacity { + withLifetimeTracking { + withOutputSpan( + capacity: capacity, count: count, tracker: $0 + ) { + $0._consumeAll { expectEqual($0.count, count) } + expectTrue($0.isEmpty) + } + expectEqual($0.instances, 0) + } + } + } + } + + func test_consumeAll_throwingConsumer() { + for capacity in [1, 5, 10] { + for count in 1 ... capacity { + withLifetimeTracking { + withOutputSpan( + capacity: capacity, count: count, tracker: $0 + ) { + do throws(TestError) { + try $0._consumeAll { + span throws(TestError) in + expectEqual(span.count, count) + throw TestError() + } + expectFailure("Consumer should have thrown") + } catch { + expectEqual(error, TestError()) + } + expectEqual( + $0.count, 0, + "OutputSpan still claims ownership of destroyed items" + ) + // unsafe $0.withUnsafeMutableBufferPointer { $1 = 0 } + } + expectEqual( + $0.instances, 0, + "Unconsumed items should be destroyed along with the InputSpan" + ) + } + } + } + } +} +#endif