Merge pull request #823 from Joseph-Cursio/fix-816-permutable-noncopyable

Add checkPermutableContainer
This commit is contained in:
Karoy Lorentey authored and GitHub committed 2026-11-10 11:01:28 -08:00
commit 612865a2d0
5 files changed
+130

No files matched your search

@@ -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() {
@@ -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() {
+10
View File
@@ -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() {
+10
View File
@@ -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() {
@@ -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<C.Element>
>(
_ 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<C.Element>
>(
_ 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