diff --git a/Tests/BasicContainersTests/RigidArrayTests.swift b/Tests/BasicContainersTests/RigidArrayTests.swift index 0dd57f46..2a7aeabb 100644 --- a/Tests/BasicContainersTests/RigidArrayTests.swift +++ b/Tests/BasicContainersTests/RigidArrayTests.swift @@ -98,6 +98,25 @@ 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) + var span = items.mutableSpan + checkPermutableContainer(&span, expectedContents: expected) + tracker.withInputSpan(layout: layout, using: { $0 }) { span in + checkPermutableContainer(&span, expectedContents: expected) + } + tracker.withOutputSpan(layout: layout, using: { $0 }) { span in + checkPermutableContainer(&span, expectedContents: expected) + } + } + } + } #endif func test_empty() { 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() { 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() { 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() { 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