mirror of
https://github.com/apple/swift-collections
synced 2026-11-22 04:20:40 +00:00
- withEveryDeque/withEveryFullDeque now visit capacity 0. - checkEquatable reports transitivity violations instead of trapping when pairs arrive out of order (non-transitive oracles, maxSamples). - Minimal range-replaceable collections no longer trap in removeLast() and popLast(). - LifetimeTracked's "Double deinit" precondition can now fire. - withEvery, withEveryRange and withSomeArrayLayouts include the failing value in their "Throwing trace". - CustomCountedProducer drops its unused chunkSize parameter, and the two loops that reran identical cases over it are removed. Adds regression tests for the first three. Fixes #810
163 lines
4.3 KiB
Swift
163 lines
4.3 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 2025 - 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 SpanPreview
|
|
import BasicContainers
|
|
#endif
|
|
import ContainersPreview
|
|
|
|
@usableFromInline
|
|
package struct ArrayLayout {
|
|
@usableFromInline
|
|
package var capacity: Int
|
|
|
|
@usableFromInline
|
|
package var count: Int
|
|
|
|
@usableFromInline
|
|
package init(capacity: Int, count: Int) {
|
|
precondition(count >= 0 && count <= capacity)
|
|
self.capacity = capacity
|
|
self.count = count
|
|
}
|
|
|
|
@usableFromInline
|
|
package var isFull: Bool { count == capacity }
|
|
|
|
@usableFromInline
|
|
package var isEmpty: Bool { count == 0 }
|
|
|
|
@usableFromInline
|
|
package var freeCapacity: Int { capacity - count }
|
|
}
|
|
|
|
package func withSomeArrayLayouts<E: Error>(
|
|
_ label: String,
|
|
ofCapacities capacities: some Sequence<Int>,
|
|
file: StaticString = #filePath,
|
|
line: UInt = #line,
|
|
run body: (ArrayLayout) throws(E) -> Void
|
|
) throws(E) {
|
|
let context = TestContext.current
|
|
for capacity in capacities {
|
|
var counts: Set<Int> = []
|
|
counts.insert(0)
|
|
counts.insert(capacity)
|
|
counts.insert(capacity / 2)
|
|
if capacity >= 1 {
|
|
counts.insert(1)
|
|
counts.insert(capacity - 1)
|
|
}
|
|
if capacity >= 2 {
|
|
counts.insert(2)
|
|
counts.insert(capacity - 2)
|
|
}
|
|
for count in counts.sorted() {
|
|
let layout = ArrayLayout(capacity: capacity, count: count)
|
|
let entry = context.push("\(label): \(layout)", file: file, line: line)
|
|
|
|
var done = false
|
|
defer {
|
|
if !done {
|
|
print(context.currentTrace(title: "Throwing trace"))
|
|
}
|
|
context.pop(entry)
|
|
}
|
|
try body(layout)
|
|
done = true
|
|
}
|
|
}
|
|
}
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension RigidArray where Element: ~Copyable {
|
|
@inlinable
|
|
package init(layout: ArrayLayout, using generator: (Int) -> Element) {
|
|
self.init(capacity: layout.capacity) { span in
|
|
for i in 0 ..< layout.count {
|
|
span.append(generator(i))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension UniqueArray where Element: ~Copyable {
|
|
@inlinable
|
|
package init(layout: ArrayLayout, using generator: (Int) -> Element) {
|
|
self.init(consuming: RigidArray(layout: layout, using: generator))
|
|
}
|
|
}
|
|
|
|
extension LifetimeTracker {
|
|
@available(SwiftStdlib 5.0, *)
|
|
@inlinable
|
|
package func rigidArray<Element>(
|
|
layout: ArrayLayout,
|
|
using generator: (Int) -> Element = { $0 }
|
|
) -> RigidArray<LifetimeTracked<Element>> {
|
|
RigidArray(layout: layout, using: { self.instance(for: generator($0)) })
|
|
}
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
@inlinable
|
|
package func uniqueArray<Element>(
|
|
layout: ArrayLayout,
|
|
using generator: (Int) -> Element = { $0 }
|
|
) -> UniqueArray<LifetimeTracked<Element>> {
|
|
let contents = RigidArray(layout: layout) {
|
|
self.instance(for: generator($0))
|
|
}
|
|
return UniqueArray(consuming: contents)
|
|
}
|
|
|
|
#if UnstableContainersPreview
|
|
@available(SwiftStdlib 5.0, *)
|
|
@inlinable
|
|
package func withInputSpan<Element>(
|
|
layout: ArrayLayout,
|
|
using generator: (Int) -> Element,
|
|
_ body: (inout InputSpan<LifetimeTracked<Element>>) -> Void
|
|
) {
|
|
let buffer = UnsafeMutableBufferPointer<LifetimeTracked<Element>>
|
|
.allocate(capacity: layout.capacity)
|
|
for i in 0 ..< layout.count {
|
|
unsafe buffer.initializeElement(
|
|
at: layout.capacity - layout.count + i,
|
|
to: self.instance(for: generator(i)))
|
|
}
|
|
var span = unsafe InputSpan(buffer: buffer, initializedCount: layout.count)
|
|
body(&span)
|
|
_ = consume span
|
|
unsafe buffer.deallocate()
|
|
}
|
|
#endif
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
@inlinable
|
|
package func withOutputSpan<Element>(
|
|
layout: ArrayLayout,
|
|
using generator: (Int) -> Element,
|
|
_ body: (inout OutputSpan<LifetimeTracked<Element>>) -> Void
|
|
) {
|
|
var contents = RigidArray(layout: layout) {
|
|
self.instance(for: generator($0))
|
|
}
|
|
contents.edit(body)
|
|
}
|
|
}
|