From afa8580b15989d8d855c17070831245118c3ce35 Mon Sep 17 00:00:00 2001 From: Joseph Cursio Date: Thu, 5 Nov 2026 14:25:53 -0600 Subject: [PATCH 1/8] Add checkPermutableContainer Check swapAt(_:_:), the one requirement PermutableContainer adds to Container, against an Array model. The checker swaps every pair of indices, including an index with itself, and then repeats the same swaps in reverse order, comparing the whole container after each swap. That leaves the container holding the expected contents in their original order. It first runs validateContainer. Co-Authored-By: Claude Opus 5.5 --- .../CheckPermutableContainer.swift | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Tests/_CollectionsTestSupport/ConformanceCheckers/CheckPermutableContainer.swift diff --git a/Tests/_CollectionsTestSupport/ConformanceCheckers/CheckPermutableContainer.swift b/Tests/_CollectionsTestSupport/ConformanceCheckers/CheckPermutableContainer.swift new file mode 100644 index 00000000..146b837c --- /dev/null +++ b/Tests/_CollectionsTestSupport/ConformanceCheckers/CheckPermutableContainer.swift @@ -0,0 +1,80 @@ +//===----------------------------------------------------------------------===// +// +// 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 +import ContainersPreview + +#if compiler(>=6.4) && UnstableContainersPreview +@available(SwiftStdlib 6.4, *) +@inlinable +public func checkPermutableContainer< + C: PermutableContainer & ~Copyable & ~Escapable, + Expected: Sequence +>( + _ container: inout C, + expectedContents: Expected, + file: StaticString = #filePath, + line: UInt = #line +) where C.Element: Equatable { + let entry = TestContext.current.push( + "checkPermutableContainer", file: file, line: line) + defer { TestContext.current.pop(entry) } + + do { + try validatePermutableContainer( + &container, expectedContents: expectedContents) + } catch { + expectFailure(error.message, file: file, line: line) + } +} + +@available(SwiftStdlib 6.4, *) +@inlinable +public func validatePermutableContainer< + C: PermutableContainer & ~Copyable & ~Escapable, + Expected: Sequence +>( + _ container: inout C, + expectedContents: Expected +) throws(ValidationError) where C.Element: Equatable { + let expectedContents = Array(expectedContents) + try validateContainer( + container, + expectedContents: expectedContents, + by: ==, + printer: { "\($0)" }) + + let count = container.count + var allIndices = [container.startIndex] + for offset in 0 ..< count { + allIndices.append(container.index(after: allIndices[offset])) + } + var model = expectedContents + let pairs = (0 ..< count).flatMap { i in (0 ..< count).map { (i, $0) } } + for (i, j) in pairs + pairs.reversed() { + container.swapAt(allIndices[i], allIndices[j]) + model.swapAt(i, j) + for offset in 0 ..< count { + guard container[allIndices[offset]] == model[offset] else { + throw ValidationError( + "`swapAt(_:_:)` must exchange the items at the two indices", + ["i": allIndices[i], + "j": allIndices[j], + "offset": offset, + "expected": model[offset], + "actual": container[allIndices[offset]]]) + } + } + } +} +#endif From 69a2d1539512c369dc651fe48d1914f299d581b4 Mon Sep 17 00:00:00 2001 From: Joseph Cursio Date: Thu, 5 Nov 2026 14:25:53 -0600 Subject: [PATCH 2/8] Check RigidArray with checkPermutableContainer Run the permutable container checker against RigidArray, over arrays of up to ten items, full and with spare capacity. Co-Authored-By: Claude Opus 5.5 --- Tests/BasicContainersTests/RigidArrayTests.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Tests/BasicContainersTests/RigidArrayTests.swift b/Tests/BasicContainersTests/RigidArrayTests.swift index 0dd57f46..0bf5933c 100644 --- a/Tests/BasicContainersTests/RigidArrayTests.swift +++ b/Tests/BasicContainersTests/RigidArrayTests.swift @@ -98,6 +98,17 @@ class RigidArrayTests: CollectionTestCase { } } } + + @available(SwiftStdlib 6.4, *) + func test_validate_PermutableContainer() { + withSomeArrayLayouts("layout", ofCapacities: [0, 10]) { layout in + withLifetimeTracking { tracker in + var items = tracker.rigidArray(layout: layout) + let expected = (0 ..< layout.count).map { tracker.instance(for: $0) } + checkPermutableContainer(&items, expectedContents: expected) + } + } + } #endif func test_empty() { From a9967e32eb42287cd6217565a93cd7cc8bd656e5 Mon Sep 17 00:00:00 2001 From: Joseph Cursio Date: Thu, 5 Nov 2026 14:25:53 -0600 Subject: [PATCH 3/8] Check MutableSpan with checkPermutableContainer Also run the permutable container checker against the mutable span of a RigidArray, over the same layouts. MutableSpan is nonescapable, so this also runs the checker on a nonescapable container. Co-Authored-By: Claude Opus 5.5 --- Tests/BasicContainersTests/RigidArrayTests.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tests/BasicContainersTests/RigidArrayTests.swift b/Tests/BasicContainersTests/RigidArrayTests.swift index 0bf5933c..e9a73e93 100644 --- a/Tests/BasicContainersTests/RigidArrayTests.swift +++ b/Tests/BasicContainersTests/RigidArrayTests.swift @@ -106,6 +106,8 @@ class RigidArrayTests: CollectionTestCase { var items = tracker.rigidArray(layout: layout) let expected = (0 ..< layout.count).map { tracker.instance(for: $0) } checkPermutableContainer(&items, expectedContents: expected) + var span = items.mutableSpan + checkPermutableContainer(&span, expectedContents: expected) } } } From 8275b719e3c91c63a80b49e7fb8f3bb7d792cc6b Mon Sep 17 00:00:00 2001 From: Joseph Cursio Date: Thu, 5 Nov 2026 14:25:53 -0600 Subject: [PATCH 4/8] Check UniqueArray with checkPermutableContainer Run the permutable container checker against UniqueArray, over arrays of up to ten items, full and with spare capacity. Co-Authored-By: Claude Opus 5.5 --- Tests/BasicContainersTests/UniqueArrayTests.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Tests/BasicContainersTests/UniqueArrayTests.swift b/Tests/BasicContainersTests/UniqueArrayTests.swift index d0ccf57f..8fd75116 100644 --- a/Tests/BasicContainersTests/UniqueArrayTests.swift +++ b/Tests/BasicContainersTests/UniqueArrayTests.swift @@ -98,6 +98,17 @@ class UniqueArrayTests: CollectionTestCase { } } } + + @available(SwiftStdlib 6.4, *) + func test_validate_PermutableContainer() { + withSomeArrayLayouts("layout", ofCapacities: [0, 10]) { layout in + withLifetimeTracking { tracker in + var items = UniqueArray(consuming: tracker.rigidArray(layout: layout)) + let expected = (0 ..< layout.count).map { tracker.instance(for: $0) } + checkPermutableContainer(&items, expectedContents: expected) + } + } + } #endif func test_basics() { From 68be4ea868e0a2391a483ac615b16be86de76afd Mon Sep 17 00:00:00 2001 From: Joseph Cursio Date: Thu, 5 Nov 2026 14:25:53 -0600 Subject: [PATCH 5/8] Check RigidDeque with checkPermutableContainer Run the permutable container checker against RigidDeque over the same storage layouts as its mutable container test, including wrapped ones, where swapped pairs can span both ends of the storage. Co-Authored-By: Claude Opus 5.5 --- Tests/DequeTests/RigidDequeTests.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Tests/DequeTests/RigidDequeTests.swift b/Tests/DequeTests/RigidDequeTests.swift index 47f1739a..ba148f3a 100644 --- a/Tests/DequeTests/RigidDequeTests.swift +++ b/Tests/DequeTests/RigidDequeTests.swift @@ -154,6 +154,16 @@ final class RigidDequeTests: CollectionTestCase { } } } + + @available(SwiftStdlib 6.4, *) + func test_validate_PermutableContainer() { + withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in + withLifetimeTracking { tracker in + var data = tracker.rigidDeque(with: layout) + checkPermutableContainer(&data.deque, expectedContents: data.contents) + } + } + } #endif func test_basicProperties() { From 7460d35e2b184a0f068527c6e8627ae58328a317 Mon Sep 17 00:00:00 2001 From: Joseph Cursio Date: Thu, 5 Nov 2026 14:25:53 -0600 Subject: [PATCH 6/8] Check UniqueDeque with checkPermutableContainer Run the permutable container checker against UniqueDeque over the same storage layouts as its mutable container test, including wrapped ones. Co-Authored-By: Claude Opus 5.5 --- Tests/DequeTests/UniqueDequeTests.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Tests/DequeTests/UniqueDequeTests.swift b/Tests/DequeTests/UniqueDequeTests.swift index 7bb54178..4c36bbde 100644 --- a/Tests/DequeTests/UniqueDequeTests.swift +++ b/Tests/DequeTests/UniqueDequeTests.swift @@ -137,6 +137,16 @@ final class UniqueDequeTests: CollectionTestCase { } } } + + @available(SwiftStdlib 6.4, *) + func test_validate_PermutableContainer() { + withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in + withLifetimeTracking { tracker in + var data = tracker.uniqueDeque(with: layout) + checkPermutableContainer(&data.deque, expectedContents: data.contents) + } + } + } #endif func test_basicProperties() { From 71d10cd2f4ec2b28453fec7fb0b8e675c30b69e0 Mon Sep 17 00:00:00 2001 From: Joseph Cursio Date: Thu, 5 Nov 2026 14:25:53 -0600 Subject: [PATCH 7/8] Check InputSpan with checkPermutableContainer Also run the permutable container checker against an InputSpan, over the same layouts as RigidArray. InputSpan.swapAt(_:_:) is the only span implementation of swapAt in this package, and a no-op version of it passes every other test. Co-Authored-By: Claude Opus 5.5 --- Tests/BasicContainersTests/RigidArrayTests.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Tests/BasicContainersTests/RigidArrayTests.swift b/Tests/BasicContainersTests/RigidArrayTests.swift index e9a73e93..a834caba 100644 --- a/Tests/BasicContainersTests/RigidArrayTests.swift +++ b/Tests/BasicContainersTests/RigidArrayTests.swift @@ -108,6 +108,9 @@ class RigidArrayTests: CollectionTestCase { checkPermutableContainer(&items, expectedContents: expected) var span = items.mutableSpan checkPermutableContainer(&span, expectedContents: expected) + tracker.withInputSpan(layout: layout, using: { $0 }) { span in + checkPermutableContainer(&span, expectedContents: expected) + } } } } From 476fdaff1b2fc6cf4b9b597ae7fc2ab8e27e9268 Mon Sep 17 00:00:00 2001 From: Joseph Cursio Date: Thu, 5 Nov 2026 14:25:53 -0600 Subject: [PATCH 8/8] Check OutputSpan with checkPermutableContainer Also run the permutable container checker against an OutputSpan, over the same layouts as RigidArray. Co-Authored-By: Claude Opus 5.5 --- Tests/BasicContainersTests/RigidArrayTests.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Tests/BasicContainersTests/RigidArrayTests.swift b/Tests/BasicContainersTests/RigidArrayTests.swift index a834caba..2a7aeabb 100644 --- a/Tests/BasicContainersTests/RigidArrayTests.swift +++ b/Tests/BasicContainersTests/RigidArrayTests.swift @@ -111,6 +111,9 @@ class RigidArrayTests: CollectionTestCase { tracker.withInputSpan(layout: layout, using: { $0 }) { span in checkPermutableContainer(&span, expectedContents: expected) } + tracker.withOutputSpan(layout: layout, using: { $0 }) { span in + checkPermutableContainer(&span, expectedContents: expected) + } } } }