mirror of
https://github.com/apple/swift-collections
synced 2026-11-27 04:54:21 +00:00
Merge pull request #462 from lorentey/containers
Flesh out DynamicArray
This commit is contained in:
24 files changed
+2987
-647
No files matched your search
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -85,6 +85,123 @@ public protocol Container<Element>: ~Copyable, ~Escapable {
|
||||
|
||||
@available(SwiftCompatibilitySpan 5.0, *)
|
||||
extension Container where Self: ~Copyable & ~Escapable {
|
||||
@inlinable
|
||||
public func _defaultIndex(_ i: Index, advancedBy distance: Int) -> Index {
|
||||
precondition(
|
||||
distance >= 0,
|
||||
"Only BidirectionalContainers can be advanced by a negative amount")
|
||||
|
||||
var i = index(alignedDown: i)
|
||||
var distance = distance
|
||||
|
||||
// FIXME: This implementation can be wasteful for contiguous containers,
|
||||
// as iterating over spans will overshoot the target immediately.
|
||||
// Reintroducing `nextSpan(after:maximumCount:)` would help avoid
|
||||
// having to use a second loop to refine the result, but it would
|
||||
// complicate conformances.
|
||||
|
||||
// Skip forward until we find the span that contains our target.
|
||||
while distance > 0 {
|
||||
var j = i
|
||||
let span = self.nextSpan(after: &j)
|
||||
precondition(
|
||||
!span.isEmpty,
|
||||
"Can't advance index beyond the end of the container")
|
||||
guard span.count <= distance else { break }
|
||||
i = j
|
||||
distance &-= span.count
|
||||
}
|
||||
// Step through to find the precise target.
|
||||
while distance > 0 {
|
||||
self.formIndex(after: &i)
|
||||
distance &-= 1
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func _defaultDistance(from start: Index, to end: Index) -> Int {
|
||||
var start = index(alignedDown: start)
|
||||
let end = index(alignedDown: end)
|
||||
if start > end {
|
||||
return -_defaultDistance(from: end, to: start)
|
||||
}
|
||||
var count = 0
|
||||
while start != end {
|
||||
count = count + 1
|
||||
formIndex(after: &start)
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func _defaultFormIndex(
|
||||
_ i: inout Index,
|
||||
advancedBy distance: inout Int,
|
||||
limitedBy limit: Index
|
||||
) {
|
||||
precondition(
|
||||
distance >= 0,
|
||||
"Only BidirectionalContainers can be advanced by a negative amount")
|
||||
|
||||
i = index(alignedDown: i)
|
||||
let limit = index(alignedDown: limit)
|
||||
|
||||
// Skip forward until we find the span that contains our target.
|
||||
while distance > 0 {
|
||||
var j = i
|
||||
let span = self.nextSpan(after: &j)
|
||||
precondition(
|
||||
!span.isEmpty,
|
||||
"Can't advance index beyond the end of the container")
|
||||
guard span.count <= distance, j < limit else {
|
||||
break
|
||||
}
|
||||
i = j
|
||||
distance &-= span.count
|
||||
}
|
||||
// Step through to find the precise target.
|
||||
while distance != 0 {
|
||||
if i == limit {
|
||||
return
|
||||
}
|
||||
formIndex(after: &i)
|
||||
distance &-= 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftCompatibilitySpan 5.0, *)
|
||||
extension Container where Self: ~Copyable & ~Escapable {
|
||||
@inlinable
|
||||
public var isEmpty: Bool {
|
||||
return startIndex == endIndex
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func formIndex(after i: inout Index) {
|
||||
i = self.index(after: i)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func distance(from start: Index, to end: Index) -> Int {
|
||||
_defaultDistance(from: start, to: end)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func index(_ index: Index, offsetBy n: Int) -> Index {
|
||||
_defaultIndex(index, advancedBy: n)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func formIndex(
|
||||
_ i: inout Index,
|
||||
offsetBy distance: inout Int,
|
||||
limitedBy limit: Index
|
||||
) {
|
||||
_defaultFormIndex(&i, advancedBy: &distance, limitedBy: limit)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func index(alignedDown index: Index) -> Index { index }
|
||||
|
||||
@@ -98,8 +215,40 @@ extension Container where Self: ~Copyable & ~Escapable {
|
||||
public func _customLastIndexOfEquatableElement(_ element: borrowing Element) -> Index?? { nil }
|
||||
}
|
||||
|
||||
@available(SwiftCompatibilitySpan 5.0, *)
|
||||
extension Container where Self: Sequence {
|
||||
@inlinable
|
||||
public var underestimatedCount: Int { count }
|
||||
}
|
||||
|
||||
@available(SwiftCompatibilitySpan 5.0, *)
|
||||
extension Container where Self: Collection {
|
||||
// Resolve ambiguities between default implementations between Collection
|
||||
// and Container.
|
||||
|
||||
@inlinable
|
||||
public var underestimatedCount: Int { count }
|
||||
|
||||
@inlinable
|
||||
public var isEmpty: Bool {
|
||||
return startIndex == endIndex
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func formIndex(after i: inout Index) {
|
||||
i = self.index(after: i)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func distance(from start: Index, to end: Index) -> Int {
|
||||
_defaultDistance(from: start, to: end)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func index(_ index: Index, offsetBy n: Int) -> Index {
|
||||
_defaultIndex(index, advancedBy: n)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func _customIndexOfEquatableElement(_ element: borrowing Element) -> Index?? { nil }
|
||||
|
||||
|
||||
@@ -10,18 +10,38 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@available(SwiftCompatibilitySpan 5.0, *)
|
||||
public protocol ContiguousContainer<Element>: /*RandomAccessContainer*/ ~Copyable, ~Escapable {
|
||||
associatedtype Element: ~Copyable/* & ~Escapable*/
|
||||
|
||||
var span: Span<Element> { @lifetime(copy self) get }
|
||||
@available(SwiftStdlib 6.2, *)
|
||||
public protocol ContiguousContainer<Element>
|
||||
: RandomAccessContainer, ~Copyable, ~Escapable
|
||||
{
|
||||
var span: Span<Element> { @lifetime(borrow self) get }
|
||||
}
|
||||
|
||||
@available(SwiftCompatibilitySpan 5.0, *)
|
||||
extension Span: ContiguousContainer where Element: ~Copyable {
|
||||
public var span: Self {
|
||||
@lifetime(copy self)
|
||||
get { self }
|
||||
@available(SwiftStdlib 6.2, *)
|
||||
extension ContiguousContainer
|
||||
where Self: ~Copyable & ~Escapable, Index == Int {
|
||||
@inlinable
|
||||
@lifetime(borrow self)
|
||||
public func borrowElement(at index: Index) -> Borrow<Element> {
|
||||
span.borrowElement(at: index - startIndex)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@lifetime(borrow self)
|
||||
public func nextSpan(after index: inout Index) -> Span<Element> {
|
||||
var i = index - startIndex
|
||||
let result = span.nextSpan(after: &i)
|
||||
index = i + startIndex
|
||||
return result
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@lifetime(borrow self)
|
||||
public func previousSpan(before index: inout Index) -> Span<Element> {
|
||||
var i = index - startIndex
|
||||
let result = span.previousSpan(before: &i)
|
||||
index = i + startIndex
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +53,3 @@ extension ContiguousArray: ContiguousContainer {}
|
||||
|
||||
@available(SwiftStdlib 6.2, *)
|
||||
extension CollectionOfOne: ContiguousContainer {}
|
||||
|
||||
@available(SwiftStdlib 6.2, *)
|
||||
extension String.UTF8View: ContiguousContainer {}
|
||||
@@ -66,6 +66,19 @@ where Self: ~Copyable & ~Escapable, Index: Strideable, Index.Stride == Int
|
||||
// Note: Range checks are deferred until element access.
|
||||
index.advance(by: &distance, limitedBy: limit)
|
||||
}
|
||||
|
||||
|
||||
@lifetime(borrow self)
|
||||
@inlinable
|
||||
public func nextSpan(
|
||||
after index: inout Index, maximumCount: Int
|
||||
) -> Span<Element> {
|
||||
precondition(maximumCount >= 0, "Maximum count must be non-negative")
|
||||
let span = self.nextSpan(after: &index)
|
||||
if span.count <= maximumCount { return span }
|
||||
index = index.advanced(by: maximumCount - span.count)
|
||||
return span._extracting(first: maximumCount)
|
||||
}
|
||||
}
|
||||
|
||||
// Disambiguate parallel extensions on RandomAccessContainer and RandomAccessCollection
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2025 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@available(SwiftStdlib 6.2, *)
|
||||
extension UnsafeBufferPointer: @unsafe ContiguousContainer
|
||||
where Element: ~Copyable
|
||||
{
|
||||
public var span: Span<Element> {
|
||||
@inlinable
|
||||
@lifetime(borrow self)
|
||||
get {
|
||||
unsafe Span(_unsafeElements: self)
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@lifetime(borrow self)
|
||||
public func borrowElement(at index: Int) -> Borrow<Element> {
|
||||
precondition(index >= 0 && index < count, "Index out of bounds")
|
||||
let ptr = unsafe baseAddress.unsafelyUnwrapped + index
|
||||
return unsafe Borrow(unsafeAddress: ptr, borrowing: self)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2025 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@available(SwiftStdlib 6.2, *)
|
||||
extension UnsafeMutableBufferPointer: @unsafe ContiguousContainer
|
||||
where Element: ~Copyable
|
||||
{
|
||||
/// The contents of `self` must not be mutated while the returned span exists.
|
||||
public var span: Span<Element> {
|
||||
@inlinable
|
||||
@lifetime(borrow self)
|
||||
get {
|
||||
unsafe Span(_unsafeElements: self)
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@lifetime(borrow self)
|
||||
public func borrowElement(at index: Int) -> Borrow<Element> {
|
||||
precondition(index >= 0 && index < count, "Index out of bounds")
|
||||
let ptr = unsafe baseAddress.unsafelyUnwrapped + index
|
||||
return unsafe Borrow(unsafeAddress: ptr, borrowing: self)
|
||||
}
|
||||
}
|
||||
+8
@@ -39,6 +39,14 @@ extension Span: RandomAccessContainer where Element: ~Copyable {
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 6.2, *)
|
||||
extension Span: ContiguousContainer where Element: ~Copyable {
|
||||
public var span: Self {
|
||||
@lifetime(copy self)
|
||||
get { self }
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 6.2, *)
|
||||
extension Span where Element: ~Copyable {
|
||||
@usableFromInline
|
||||
@@ -49,37 +49,35 @@ extension UnsafeMutableRawBufferPointer {
|
||||
|
||||
|
||||
extension UnsafeMutableBufferPointer {
|
||||
/// Initialize slots at the start of this buffer by copying data from `buffer`, then
|
||||
/// shrink `self` to drop all initialized items from its front, leaving it addressing the
|
||||
/// uninitialized remainder.
|
||||
/// Initialize slots at the start of this buffer by copying data from `source`.
|
||||
///
|
||||
/// If `Element` is not bitwise copyable, then the memory region addressed by `self` must be
|
||||
/// entirely uninitialized, while `buffer` must be fully initialized.
|
||||
/// entirely uninitialized, while `source` must be fully initialized.
|
||||
///
|
||||
/// The count of `buffer` must not be greater than `self.count`.
|
||||
/// The `source` buffer must fit entirely in `self`.
|
||||
///
|
||||
/// - Returns: The index after the last item that was initialized in this buffer.
|
||||
@inlinable
|
||||
internal mutating func _initializeAndDropPrefix(copying buffer: UnsafeBufferPointer<Element>) {
|
||||
if buffer.isEmpty { return }
|
||||
precondition(buffer.count <= self.count)
|
||||
internal func _initializePrefix(copying source: UnsafeBufferPointer<Element>) -> Int {
|
||||
if source.isEmpty { return 0 }
|
||||
precondition(source.count <= self.count)
|
||||
unsafe self.baseAddress.unsafelyUnwrapped.initialize(
|
||||
from: buffer.baseAddress.unsafelyUnwrapped, count: buffer.count)
|
||||
unsafe self = self.extracting(buffer.count...)
|
||||
from: source.baseAddress.unsafelyUnwrapped, count: source.count)
|
||||
return source.count
|
||||
}
|
||||
|
||||
/// Initialize slots at the start of this buffer by copying data from `span`, then
|
||||
/// shrink `self` to drop all initialized items from its front, leaving it addressing the
|
||||
/// uninitialized remainder.
|
||||
/// Initialize slots at the start of this buffer by copying data from `source`.
|
||||
///
|
||||
/// If `Element` is not bitwise copyable, then the memory region addressed by `self` must be
|
||||
/// entirely uninitialized.
|
||||
///
|
||||
/// The count of `span` must not be greater than `self.count`.
|
||||
/// The `source` span must fit entirely in `self`.
|
||||
///
|
||||
/// - Returns: The index after the last item that was initialized in this buffer.
|
||||
@available(SwiftCompatibilitySpan 5.0, *)
|
||||
@inlinable
|
||||
internal mutating func _initializeAndDropPrefix(copying span: Span<Element>) {
|
||||
unsafe span.withUnsafeBufferPointer { buffer in
|
||||
unsafe self._initializeAndDropPrefix(copying: buffer)
|
||||
}
|
||||
internal func _initializePrefix(copying source: Span<Element>) -> Int {
|
||||
unsafe source.withUnsafeBufferPointer { unsafe self._initializePrefix(copying: $0) }
|
||||
}
|
||||
|
||||
/// Initialize all slots in this buffer by copying data from `items`, which must fit entirely
|
||||
@@ -114,4 +112,34 @@ extension UnsafeMutableBufferPointer {
|
||||
}
|
||||
return (self.count - target.count, i)
|
||||
}
|
||||
|
||||
/// Initialize slots at the start of this buffer by copying data from `buffer`, then
|
||||
/// shrink `self` to drop all initialized items from its front, leaving it addressing the
|
||||
/// uninitialized remainder.
|
||||
///
|
||||
/// If `Element` is not bitwise copyable, then the memory region addressed by `self` must be
|
||||
/// entirely uninitialized, while `buffer` must be fully initialized.
|
||||
///
|
||||
/// The count of `buffer` must not be greater than `self.count`.
|
||||
@inlinable
|
||||
internal mutating func _initializeAndDropPrefix(copying source: UnsafeBufferPointer<Element>) {
|
||||
let i = unsafe _initializePrefix(copying: source)
|
||||
unsafe self = self.extracting(i...)
|
||||
}
|
||||
|
||||
/// Initialize slots at the start of this buffer by copying data from `span`, then
|
||||
/// shrink `self` to drop all initialized items from its front, leaving it addressing the
|
||||
/// uninitialized remainder.
|
||||
///
|
||||
/// If `Element` is not bitwise copyable, then the memory region addressed by `self` must be
|
||||
/// entirely uninitialized.
|
||||
///
|
||||
/// The count of `span` must not be greater than `self.count`.
|
||||
@available(SwiftCompatibilitySpan 5.0, *)
|
||||
@inlinable
|
||||
internal mutating func _initializeAndDropPrefix(copying span: Span<Element>) {
|
||||
unsafe span.withUnsafeBufferPointer { buffer in
|
||||
unsafe self._initializeAndDropPrefix(copying: buffer)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,13 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// RepeatingContainerTests.swift
|
||||
// swift-collections
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Created by Karoy Lorentey on 2025-04-21.
|
||||
// Copyright (c) 2025 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2025 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import _CollectionsTestSupport
|
||||
import Future
|
||||
|
||||
struct ArrayLayout {
|
||||
var capacity: Int
|
||||
var count: Int
|
||||
|
||||
init(capacity: Int, count: Int) {
|
||||
precondition(count >= 0 && count <= capacity)
|
||||
self.capacity = capacity
|
||||
self.count = count
|
||||
}
|
||||
}
|
||||
|
||||
func withSomeArrayLayouts<E: Error>(
|
||||
_ label: String,
|
||||
ofCapacities capacities: some Sequence<Int>,
|
||||
file: StaticString = #file,
|
||||
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 {
|
||||
let layout = ArrayLayout(capacity: capacity, count: count)
|
||||
let entry = context.push("\(label): \(layout)", file: file, line: line)
|
||||
|
||||
var done = false
|
||||
defer {
|
||||
context.pop(entry)
|
||||
if !done {
|
||||
print(context.currentTrace(title: "Throwing trace"))
|
||||
}
|
||||
}
|
||||
try body(layout)
|
||||
done = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension RigidArray where Element: ~Copyable {
|
||||
init(layout: ArrayLayout, using generator: (Int) -> Element) {
|
||||
self.init(
|
||||
capacity: layout.capacity,
|
||||
count: layout.count,
|
||||
initializedWith: generator)
|
||||
}
|
||||
}
|
||||
|
||||
extension DynamicArray where Element: ~Copyable {
|
||||
init(layout: ArrayLayout, using generator: (Int) -> Element) {
|
||||
self.init(consuming: RigidArray(layout: layout, using: generator))
|
||||
}
|
||||
}
|
||||
|
||||
extension LifetimeTracker {
|
||||
func rigidArray<Element>(
|
||||
layout: ArrayLayout,
|
||||
using generator: (Int) -> Element = { $0 }
|
||||
) -> RigidArray<LifetimeTracked<Element>> {
|
||||
RigidArray(layout: layout, using: { self.instance(for: generator($0)) })
|
||||
}
|
||||
|
||||
func dynamicArray<Element>(
|
||||
layout: ArrayLayout,
|
||||
using generator: (Int) -> Element = { $0 }
|
||||
) -> DynamicArray<LifetimeTracked<Element>> {
|
||||
let contents = RigidArray(layout: layout) {
|
||||
self.instance(for: generator($0))
|
||||
}
|
||||
return DynamicArray(consuming: contents)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,804 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2025 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import _CollectionsTestSupport
|
||||
import Future
|
||||
import Synchronization
|
||||
|
||||
class DynamicArrayTests: CollectionTestCase {
|
||||
func test_validate_Container() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
let items = DynamicArray(consuming: tracker.rigidArray(layout: layout))
|
||||
let expected = (0 ..< layout.count).map { tracker.instance(for: $0) }
|
||||
expectEqual(tracker.instances, 2 * layout.count)
|
||||
checkContainer(items, expectedContents: expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_basics() {
|
||||
withLifetimeTracking { tracker in
|
||||
typealias Value = LifetimeTrackedStruct<Int>
|
||||
|
||||
var array = DynamicArray<Value>()
|
||||
expectTrue(array.isEmpty)
|
||||
expectEqual(array.count, 0)
|
||||
expectEqual(array.capacity, 0)
|
||||
expectEqual(tracker.instances, 0)
|
||||
|
||||
array.append(tracker.structInstance(for: 10))
|
||||
expectFalse(array.isEmpty)
|
||||
expectEqual(array.count, 1)
|
||||
expectEqual(array.capacity, 1) // This assumes a specific growth behavior
|
||||
expectEqual(array[0].payload, 10)
|
||||
expectEqual(tracker.instances, 1)
|
||||
|
||||
array.append(tracker.structInstance(for: 20))
|
||||
expectFalse(array.isEmpty)
|
||||
expectEqual(array.count, 2)
|
||||
expectEqual(array.capacity, 2) // This assumes a specific growth behavior
|
||||
expectEqual(array[0].payload, 10)
|
||||
expectEqual(array[1].payload, 20)
|
||||
expectEqual(tracker.instances, 2)
|
||||
|
||||
let old = array.remove(at: 0)
|
||||
expectEqual(old.payload, 10)
|
||||
expectFalse(array.isEmpty)
|
||||
expectEqual(array.count, 1)
|
||||
expectEqual(array.capacity, 2) // This assumes a specific growth behavior
|
||||
expectEqual(array[0].payload, 20)
|
||||
expectEqual(tracker.instances, 2)
|
||||
_ = consume old
|
||||
expectEqual(tracker.instances, 1)
|
||||
|
||||
let old2 = array.remove(at: 0)
|
||||
expectEqual(old2.payload, 20)
|
||||
expectEqual(array.count, 0)
|
||||
expectEqual(array.capacity, 2) // This assumes a specific growth behavior
|
||||
expectTrue(array.isEmpty)
|
||||
expectEqual(tracker.instances, 1)
|
||||
_ = consume old2
|
||||
expectEqual(tracker.instances, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func test_init_capacity() {
|
||||
do {
|
||||
let a = DynamicArray<Int>(capacity: 0)
|
||||
expectEqual(a.capacity, 0)
|
||||
expectEqual(a.count, 0)
|
||||
expectEqual(a.freeCapacity, 0)
|
||||
expectTrue(a.isEmpty)
|
||||
}
|
||||
|
||||
do {
|
||||
let a = DynamicArray<Int>(capacity: 10)
|
||||
expectEqual(a.capacity, 10)
|
||||
expectEqual(a.count, 0)
|
||||
expectEqual(a.freeCapacity, 10)
|
||||
expectTrue(a.isEmpty)
|
||||
}
|
||||
}
|
||||
|
||||
func test_init_generator() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
let a = tracker.dynamicArray(layout: layout)
|
||||
expectEqual(a.capacity, layout.capacity)
|
||||
expectEqual(a.count, layout.count)
|
||||
expectEqual(a.freeCapacity, layout.capacity - layout.count)
|
||||
expectEqual(a.isEmpty, layout.count == 0)
|
||||
expectContainerContents(a, equivalentTo: 0 ..< layout.count, by: { $0.payload == $1 })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_init_repeating() {
|
||||
withEvery("c", in: [0, 10, 100]) { c in
|
||||
withLifetimeTracking { tracker in
|
||||
let value = tracker.instance(for: 0)
|
||||
let a = DynamicArray(repeating: value, count: c)
|
||||
expectEqual(a.capacity, c)
|
||||
expectEqual(a.count, c)
|
||||
expectEqual(a.freeCapacity, 0)
|
||||
expectEqual(a.isEmpty, c == 0)
|
||||
for i in 0 ..< c {
|
||||
expectIdentical(a[i], value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_init_copying_Sequence() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
let a = DynamicArray(
|
||||
capacity: layout.capacity,
|
||||
copying: (0 ..< layout.count).map { tracker.instance(for: $0) })
|
||||
expectEqual(tracker.instances, layout.count)
|
||||
expectEqual(a.capacity, layout.capacity)
|
||||
expectEqual(a.count, layout.count)
|
||||
expectEqual(a.isEmpty, layout.count == 0)
|
||||
for i in 0 ..< layout.count {
|
||||
expectEqual(a[i].payload, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_init_copying_Container() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withEvery("spanCounts", in: [
|
||||
[1],
|
||||
[3, 5, 7],
|
||||
[10, 3],
|
||||
] as [[Int]]) { spanCounts in
|
||||
withLifetimeTracking { tracker in
|
||||
let additions = StaccatoContainer(
|
||||
contents: RigidArray(
|
||||
copying: (0 ..< layout.count).map { tracker.instance(for: $0) }),
|
||||
spanCounts: spanCounts)
|
||||
|
||||
let array = DynamicArray(
|
||||
capacity: layout.capacity, copying: additions)
|
||||
expectEqual(tracker.instances, layout.count)
|
||||
expectEqual(array.capacity, layout.capacity)
|
||||
expectEqual(array.count, layout.count)
|
||||
for i in 0 ..< layout.count {
|
||||
expectEqual(array[i].payload, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_span() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
let a = tracker.dynamicArray(layout: layout)
|
||||
let span = a.span
|
||||
expectEqual(span.count, layout.count)
|
||||
for i in 0 ..< span.count {
|
||||
expectEqual(span[i].payload, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_mutableSpan() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
var span = a.mutableSpan
|
||||
expectEqual(span.count, layout.count)
|
||||
for i in 0 ..< layout.count {
|
||||
expectEqual(span[i].payload, i)
|
||||
span[i] = tracker.instance(for: -i)
|
||||
}
|
||||
for i in 0 ..< layout.count {
|
||||
expectEqual(span[i].payload, -i)
|
||||
}
|
||||
for i in 0 ..< layout.count {
|
||||
expectEqual(a[i].payload, -i)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_nextSpan() {
|
||||
// DynamicArray is expected to have exactly one span.
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
let a = tracker.dynamicArray(layout: layout)
|
||||
let whole = a.span
|
||||
var i = 0
|
||||
let first = a.nextSpan(after: &i)
|
||||
expectEqual(i, layout.count)
|
||||
expectTrue(first.isIdentical(to: whole))
|
||||
let second = a.nextSpan(after: &i)
|
||||
expectEqual(i, layout.count)
|
||||
expectTrue(second.isEmpty)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_previousSpan() {
|
||||
// RigidArray is expected to have exactly one span.
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
let a = tracker.dynamicArray(layout: layout)
|
||||
let whole = a.span
|
||||
var i = layout.count
|
||||
let first = a.previousSpan(before: &i)
|
||||
expectEqual(i, 0)
|
||||
expectTrue(first.isIdentical(to: whole))
|
||||
let second = a.previousSpan(before: &i)
|
||||
expectEqual(i, 0)
|
||||
expectTrue(second.isEmpty)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_nextMutableSpan() {
|
||||
// RigidArray is expected to have exactly one span.
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
var i = 0
|
||||
var span = a.nextMutableSpan(after: &i)
|
||||
expectEqual(i, layout.count)
|
||||
expectEqual(span.count, layout.count)
|
||||
span = a.nextMutableSpan(after: &i)
|
||||
expectEqual(i, layout.count)
|
||||
expectTrue(span.isEmpty)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_index_properties() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
let a = DynamicArray(layout: layout, using: { $0 })
|
||||
expectEqual(a.startIndex, 0)
|
||||
expectEqual(a.endIndex, layout.count)
|
||||
expectEqual(a.indices, 0 ..< layout.count)
|
||||
}
|
||||
}
|
||||
|
||||
func test_swapAt() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
withEvery("i", in: 0 ..< layout.count / 2) { i in
|
||||
a.swapAt(i, layout.count - 1 - i)
|
||||
}
|
||||
let expected = (0 ..< layout.count).reversed()
|
||||
expectContainerContents(
|
||||
a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
expectEqual(tracker.instances, layout.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_borrowElement() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
let a = tracker.dynamicArray(layout: layout)
|
||||
for i in 0 ..< layout.count {
|
||||
let item = a.borrowElement(at: i)
|
||||
expectEqual(item[].payload, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_mutateElement() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
for i in 0 ..< layout.count {
|
||||
var item = a.mutateElement(at: i)
|
||||
expectEqual(item[].payload, i)
|
||||
item[] = tracker.instance(for: -i)
|
||||
expectEqual(tracker.instances, layout.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_withUnsafeMutableBufferPointer() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
unsafe a.withUnsafeMutableBufferPointer { buffer, count in
|
||||
expectEqual(buffer.count, layout.capacity)
|
||||
expectEqual(count, layout.count)
|
||||
unsafe buffer.extracting(0 ..< count).deinitialize()
|
||||
expectEqual(tracker.instances, 0)
|
||||
count = 0
|
||||
}
|
||||
expectEqual(a.count, 0)
|
||||
|
||||
unsafe a.withUnsafeMutableBufferPointer { buffer, count in
|
||||
expectEqual(buffer.count, layout.capacity)
|
||||
expectEqual(count, 0)
|
||||
for i in 0 ..< buffer.count {
|
||||
unsafe buffer.initializeElement(at: i, to: tracker.instance(for: -i))
|
||||
count += 1
|
||||
}
|
||||
expectEqual(tracker.instances, layout.capacity)
|
||||
}
|
||||
expectEqual(a.count, layout.capacity)
|
||||
|
||||
struct TestError: Error {}
|
||||
|
||||
expectThrows({
|
||||
unsafe try a.withUnsafeMutableBufferPointer { buffer, count in
|
||||
expectEqual(tracker.instances, layout.capacity)
|
||||
while count > 0 {
|
||||
if count == layout.count { break }
|
||||
unsafe buffer.deinitializeElement(at: count - 1)
|
||||
count -= 1
|
||||
}
|
||||
throw TestError()
|
||||
}
|
||||
}) { error in
|
||||
expectTrue(error is TestError)
|
||||
}
|
||||
expectContainerContents(
|
||||
a,
|
||||
equivalentTo: (0 ..< layout.count).map { -$0 },
|
||||
by: { $0.payload == $1 })
|
||||
expectEqual(tracker.instances, layout.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_reallocate() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withEvery(
|
||||
"newCapacity",
|
||||
in: [
|
||||
layout.capacity, layout.count, layout.count + 1, layout.capacity + 1
|
||||
] as Set
|
||||
) { newCapacity in
|
||||
withLifetimeTracking { tracker in
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
expectEqual(a.count, layout.count)
|
||||
expectEqual(a.capacity, layout.capacity)
|
||||
a.reallocate(capacity: newCapacity)
|
||||
expectEqual(a.count, layout.count)
|
||||
expectEqual(a.capacity, newCapacity)
|
||||
expectEqual(tracker.instances, layout.count)
|
||||
expectContainerContents(
|
||||
a, equivalentTo: 0 ..< layout.count, by: { $0.payload == $1 })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_reserveCapacity() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withEvery(
|
||||
"newCapacity",
|
||||
in: [
|
||||
0, layout.count - 1, layout.count, layout.count + 1,
|
||||
layout.capacity, layout.capacity + 1
|
||||
] as Set
|
||||
) { newCapacity in
|
||||
withLifetimeTracking { tracker in
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
expectEqual(a.count, layout.count)
|
||||
expectEqual(a.capacity, layout.capacity)
|
||||
a.reserveCapacity(newCapacity)
|
||||
expectEqual(a.count, layout.count)
|
||||
expectEqual(a.capacity, Swift.max(layout.capacity, newCapacity))
|
||||
expectEqual(tracker.instances, layout.count)
|
||||
expectContainerContents(
|
||||
a, equivalentTo: 0 ..< layout.count, by: { $0.payload == $1 })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_removeAll() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
a.removeAll()
|
||||
expectTrue(a.isEmpty)
|
||||
expectEqual(a.capacity, 0)
|
||||
expectEqual(tracker.instances, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_removeAll_keepingCapacity() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
a.removeAll(keepingCapacity: true)
|
||||
expectTrue(a.isEmpty)
|
||||
expectEqual(a.capacity, layout.capacity)
|
||||
expectEqual(tracker.instances, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_removeLast() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
withEvery("i", in: 0 ..< layout.count) { i in
|
||||
let old = a.removeLast()
|
||||
expectEqual(old.payload, layout.count - 1 - i)
|
||||
expectEqual(a.count, layout.count - 1 - i)
|
||||
expectEqual(a.capacity, layout.capacity)
|
||||
}
|
||||
expectEqual(tracker.instances, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_removeLast_k() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withEvery("k", in: 0 ..< layout.count) { k in
|
||||
withLifetimeTracking { tracker in
|
||||
var expected = Array(0 ..< layout.count)
|
||||
expected.removeLast(k)
|
||||
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
expectEqual(tracker.instances, layout.count)
|
||||
a.removeLast(k)
|
||||
expectEqual(tracker.instances, layout.count - k)
|
||||
expectContainerContents(
|
||||
a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_remove_at() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withEvery("i", in: 0 ..< layout.count) { i in
|
||||
withLifetimeTracking { tracker in
|
||||
var expected = Array(0 ..< layout.count)
|
||||
expected.remove(at: i)
|
||||
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
let old = a.remove(at: i)
|
||||
expectEqual(old.payload, i)
|
||||
expectContainerContents(a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_removeSubrange() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withEveryRange("range", in: 0 ..< layout.count) { range in
|
||||
withLifetimeTracking { tracker in
|
||||
var expected = Array(0 ..< layout.count)
|
||||
expected.removeSubrange(range)
|
||||
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
a.removeSubrange(range)
|
||||
expectContainerContents(a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_removeAll_where() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
var expected = Array(0 ..< layout.count)
|
||||
expected.removeAll(where: { $0.isMultiple(of: 2) })
|
||||
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
a.removeAll(where: { $0.payload.isMultiple(of: 2) })
|
||||
expectContainerContents(
|
||||
a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_popLast() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
var expected = Array(0 ..< layout.count)
|
||||
let expectedItem = expected.popLast()
|
||||
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
let item = a.popLast()
|
||||
|
||||
expectEquivalent(item, expectedItem, by: { $0?.payload == $1 })
|
||||
expectContainerContents(
|
||||
a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_append_geometric_growth() {
|
||||
// This test depends on the precise growth curve of DynamicArray,
|
||||
// which is not part of its stable API. The test may need to be updated
|
||||
// accordingly.
|
||||
withLifetimeTracking { tracker in
|
||||
typealias Value = LifetimeTracked<Int>
|
||||
|
||||
var array = DynamicArray<Value>()
|
||||
expectEqual(array.capacity, 0)
|
||||
|
||||
array.append(tracker.instance(for: 0))
|
||||
expectEqual(array.capacity, 1)
|
||||
array.append(tracker.instance(for: 1))
|
||||
expectEqual(array.capacity, 2)
|
||||
array.append(tracker.instance(for: 2))
|
||||
expectEqual(array.capacity, 4)
|
||||
array.append(tracker.instance(for: 3))
|
||||
expectEqual(array.capacity, 4)
|
||||
array.append(tracker.instance(for: 4))
|
||||
expectEqual(array.capacity, 8)
|
||||
array.append(tracker.instance(for: 5))
|
||||
expectEqual(array.capacity, 8)
|
||||
array.append(tracker.instance(for: 6))
|
||||
expectEqual(array.capacity, 8)
|
||||
array.append(tracker.instance(for: 7))
|
||||
expectEqual(array.capacity, 8)
|
||||
array.append(tracker.instance(for: 8))
|
||||
expectEqual(array.capacity, 16)
|
||||
|
||||
for i in 9 ..< 100 {
|
||||
array.append(tracker.instance(for: i))
|
||||
}
|
||||
expectEqual(tracker.instances, 100)
|
||||
expectEqual(array.count, 100)
|
||||
expectEqual(array.capacity, 128)
|
||||
|
||||
|
||||
array.append(
|
||||
copying: RigidArray(count: 300) { tracker.instance(for: 100 + $0) })
|
||||
expectEqual(array.capacity, 400)
|
||||
|
||||
expectEqual(tracker.instances, 400)
|
||||
for i in 0 ..< 400 {
|
||||
expectEqual(array.borrowElement(at: i)[].payload, i)
|
||||
expectEqual(array[i].payload, i)
|
||||
}
|
||||
|
||||
_ = consume array
|
||||
expectEqual(tracker.instances, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func test_append() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
let c = 2 * layout.capacity + 10
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
for i in layout.count ..< c {
|
||||
a.append(tracker.instance(for: i))
|
||||
expectEqual(a.count, i + 1)
|
||||
expectContainerContents(a, equivalentTo: 0 ..< i + 1, by: { $0.payload == $1 })
|
||||
}
|
||||
expectEqual(tracker.instances, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_append_copying_MinimalSequence() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withEvery("isContiguous", in: [false, true]) { isContiguous in
|
||||
withLifetimeTracking { tracker in
|
||||
let c = 2 * layout.capacity + 10
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
a.append(copying: MinimalSequence(
|
||||
elements: (layout.count ..< c).map { tracker.instance(for: $0) },
|
||||
underestimatedCount: .half,
|
||||
isContiguous: isContiguous))
|
||||
expectContainerContents(
|
||||
a, equivalentTo: 0 ..< c, by: { $0.payload == $1})
|
||||
expectEqual(tracker.instances, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_append_copying_Span() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withEvery("additions", in: [0, 1, 10, 100]) { additions in
|
||||
withLifetimeTracking { tracker in
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
let b = RigidArray(count: additions) {
|
||||
tracker.instance(for: layout.count + $0)
|
||||
}
|
||||
a.append(copying: b.span)
|
||||
let c = layout.count + additions
|
||||
expectContainerContents(
|
||||
a, equivalentTo: 0 ..< c, by: { $0.payload == $1 })
|
||||
expectEqual(tracker.instances, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_append_copying_Container() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withEvery("additions", in: [0, 1, 10, 100]) { additions in
|
||||
withEvery("spanCount", in: 1 ... Swift.max(1, layout.capacity - layout.count)) { spanCount in
|
||||
withLifetimeTracking { tracker in
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
|
||||
let c = layout.count + additions
|
||||
let addition = (layout.count ..< c).map {
|
||||
tracker.instance(for: $0)
|
||||
}
|
||||
let b = StaccatoContainer(
|
||||
contents: RigidArray(copying: addition),
|
||||
spanCounts: [spanCount])
|
||||
a.append(copying: b)
|
||||
expectContainerContents(
|
||||
a, equivalentTo: 0 ..< c, by: { $0.payload == $1 })
|
||||
expectEqual(tracker.instances, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_insert_at() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withEvery("i", in: 0 ... layout.count) { i in
|
||||
withLifetimeTracking { tracker in
|
||||
var expected = Array(0 ..< layout.count)
|
||||
expected.insert(-1, at: i)
|
||||
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
a.insert(tracker.instance(for: -1), at: i)
|
||||
|
||||
expectContainerContents(
|
||||
a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
expectEqual(tracker.instances, layout.count + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_insert_copying_Collection() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withEvery("c", in: [0, 1, 10, 100]) { c in
|
||||
withEvery("i", in: 0 ... layout.count) { i in
|
||||
withLifetimeTracking { tracker in
|
||||
let addition = (layout.count ..< layout.count + c)
|
||||
|
||||
var expected = Array(0 ..< layout.count)
|
||||
expected.insert(contentsOf: addition, at: i)
|
||||
|
||||
let trackedAddition = addition.map { tracker.instance(for: $0) }
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
a.insert(copying: trackedAddition, at: i)
|
||||
|
||||
expectContainerContents(
|
||||
a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
expectEqual(tracker.instances, layout.count + c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_insert_copying_Span() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withEvery("i", in: 0 ... layout.count) { i in
|
||||
withEvery("c", in: [0, 1, 10, 100]) { c in
|
||||
withLifetimeTracking { tracker in
|
||||
let addition = Array(layout.count ..< layout.count + c)
|
||||
|
||||
var expected = Array(0 ..< layout.count)
|
||||
expected.insert(contentsOf: addition, at: i)
|
||||
|
||||
let rigidAddition = RigidArray(count: addition.count) {
|
||||
tracker.instance(for: addition[$0])
|
||||
}
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
a.insert(copying: rigidAddition.span, at: i)
|
||||
|
||||
expectContainerContents(
|
||||
a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
expectEqual(tracker.instances, layout.count + c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_insert_copying_Container() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withEvery("i", in: 0 ... layout.count) { i in
|
||||
withEvery("c", in: [0, 1, 10, 100]) { c in
|
||||
withEvery("spanCount", in: 1 ... Swift.max(1, layout.capacity - layout.count)) { spanCount in
|
||||
withLifetimeTracking { tracker in
|
||||
|
||||
var expected = Array(0 ..< layout.count)
|
||||
let addition = Array(layout.count ..< layout.count + c)
|
||||
expected.insert(contentsOf: addition, at: i)
|
||||
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
let rigidAddition = StaccatoContainer(
|
||||
contents: RigidArray(count: addition.count) {
|
||||
tracker.instance(for: addition[$0])
|
||||
},
|
||||
spanCounts: [spanCount])
|
||||
a.insert(copying: rigidAddition, at: i)
|
||||
|
||||
expectContainerContents(
|
||||
a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
expectEqual(tracker.instances, layout.count + c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_replaceSubrange_copying_Collection() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 5, 10]) { layout in
|
||||
withEveryRange("range", in: 0 ..< layout.count) { range in
|
||||
withEvery("c", in: [0, 1, 10, 100]) { c in
|
||||
withLifetimeTracking { tracker in
|
||||
var expected = Array(0 ..< layout.count)
|
||||
let addition = (0 ..< c).map { -100 - $0 }
|
||||
expected.replaceSubrange(range, with: addition)
|
||||
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
let trackedAddition = addition.map { tracker.instance(for: $0) }
|
||||
a.replaceSubrange(range, copying: trackedAddition)
|
||||
|
||||
expectContainerContents(
|
||||
a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
expectEqual(tracker.instances, layout.count - range.count + c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_replaceSubrange_copying_Span() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 5, 10]) { layout in
|
||||
withEveryRange("range", in: 0 ..< layout.count) { range in
|
||||
withEvery("c", in: [0, 1, 10, 100]) { c in
|
||||
withLifetimeTracking { tracker in
|
||||
var expected = Array(0 ..< layout.count)
|
||||
let addition = (0 ..< c).map { -100 - $0 }
|
||||
expected.replaceSubrange(range, with: addition)
|
||||
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
let trackedAddition = RigidArray(
|
||||
copying: addition.map { tracker.instance(for: $0) })
|
||||
a.replaceSubrange(range, copying: trackedAddition.span)
|
||||
|
||||
expectContainerContents(
|
||||
a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
expectEqual(tracker.instances, layout.count - range.count + c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_replaceSubrange_copying_Container() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 5, 10]) { layout in
|
||||
withEveryRange("range", in: 0 ..< layout.count) { range in
|
||||
withEvery("c", in: [0, 1, 10, 100]) { c in
|
||||
withEvery("spanCount", in: 1 ... Swift.max(1, layout.capacity - layout.count)) { spanCount in
|
||||
withLifetimeTracking { tracker in
|
||||
var expected = Array(0 ..< layout.count)
|
||||
let addition = (0 ..< c).map { -100 - $0 }
|
||||
expected.replaceSubrange(range, with: addition)
|
||||
|
||||
var a = tracker.dynamicArray(layout: layout)
|
||||
let trackedAddition = StaccatoContainer(
|
||||
contents: RigidArray(
|
||||
copying: addition.map { tracker.instance(for: $0) }),
|
||||
spanCounts: [spanCount])
|
||||
a.replaceSubrange(range, copying: trackedAddition)
|
||||
|
||||
expectContainerContents(
|
||||
a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
expectEqual(tracker.instances, layout.count - range.count + c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+87
-130
@@ -1,85 +1,18 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// RepeatingContainerTests.swift
|
||||
// swift-collections
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Created by Karoy Lorentey on 2025-04-21.
|
||||
// Copyright (c) 2025 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import _CollectionsTestSupport
|
||||
import Future
|
||||
|
||||
struct ArrayLayout {
|
||||
var capacity: Int
|
||||
var count: Int
|
||||
|
||||
init(capacity: Int, count: Int) {
|
||||
precondition(count >= 0 && count <= capacity)
|
||||
self.capacity = capacity
|
||||
self.count = count
|
||||
}
|
||||
}
|
||||
|
||||
extension RigidArray where Element: ~Copyable {
|
||||
init(layout: ArrayLayout, using generator: (Int) -> Element) {
|
||||
self.init(capacity: layout.capacity)
|
||||
for i in 0 ..< layout.count {
|
||||
self.append(generator(i))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension LifetimeTracker {
|
||||
func rigidArray(layout: ArrayLayout) -> RigidArray<LifetimeTracked<Int>> {
|
||||
rigidArray(layout: layout, using: { $0 })
|
||||
}
|
||||
|
||||
func rigidArray<Element>(
|
||||
layout: ArrayLayout,
|
||||
using generator: (Int) -> Element
|
||||
) -> RigidArray<LifetimeTracked<Element>> {
|
||||
RigidArray(layout: layout, using: { self.instance(for: generator($0)) })
|
||||
}
|
||||
}
|
||||
|
||||
func withSomeArrayLayouts<E: Error>(
|
||||
_ label: String,
|
||||
ofCapacities capacities: some Sequence<Int>,
|
||||
file: StaticString = #file,
|
||||
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 {
|
||||
let layout = ArrayLayout(capacity: capacity, count: count)
|
||||
let entry = context.push("\(label): \(layout)", file: file, line: line)
|
||||
|
||||
var done = false
|
||||
defer {
|
||||
context.pop(entry)
|
||||
if !done {
|
||||
print(context.currentTrace(title: "Throwing trace"))
|
||||
}
|
||||
}
|
||||
try body(layout)
|
||||
done = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 6.0, *)
|
||||
class RigidArrayTests: CollectionTestCase {
|
||||
func test_validate_Container() {
|
||||
@@ -145,16 +78,18 @@ class RigidArrayTests: CollectionTestCase {
|
||||
}
|
||||
|
||||
func test_init_copying_Collection() {
|
||||
withEvery("c", in: [0, 10, 100]) { c in
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
let a = RigidArray(copying: (0 ..< c).map { tracker.instance(for: $0) })
|
||||
expectEqual(tracker.instances, c)
|
||||
expectEqual(a.capacity, c)
|
||||
expectEqual(a.count, c)
|
||||
expectEqual(a.freeCapacity, 0)
|
||||
expectEqual(a.isEmpty, c == 0)
|
||||
expectTrue(a.isFull)
|
||||
for i in 0 ..< c {
|
||||
let a = RigidArray(
|
||||
capacity: layout.capacity,
|
||||
copying: (0 ..< layout.count).map { tracker.instance(for: $0) })
|
||||
expectEqual(tracker.instances, layout.count)
|
||||
expectEqual(a.capacity, layout.capacity)
|
||||
expectEqual(a.count, layout.count)
|
||||
expectEqual(a.freeCapacity, layout.capacity - layout.count)
|
||||
expectEqual(a.isEmpty, layout.count == 0)
|
||||
expectEqual(a.isFull, layout.count == layout.capacity)
|
||||
for i in 0 ..< layout.count {
|
||||
expectEqual(a[i].payload, i)
|
||||
}
|
||||
}
|
||||
@@ -162,7 +97,7 @@ class RigidArrayTests: CollectionTestCase {
|
||||
}
|
||||
|
||||
func test_init_copying_Container() {
|
||||
withEvery("c", in: [0, 10, 100]) { c in
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withEvery("spanCounts", in: [
|
||||
[1],
|
||||
[3, 5, 7],
|
||||
@@ -170,14 +105,15 @@ class RigidArrayTests: CollectionTestCase {
|
||||
] as [[Int]]) { spanCounts in
|
||||
withLifetimeTracking { tracker in
|
||||
let additions = StaccatoContainer(
|
||||
contents: RigidArray(copying: (0 ..< c).map { tracker.instance(for: $0) }),
|
||||
contents: RigidArray(
|
||||
copying: (0 ..< layout.count).map { tracker.instance(for: $0) }),
|
||||
spanCounts: spanCounts)
|
||||
|
||||
let array = RigidArray(copying: additions)
|
||||
expectEqual(tracker.instances, c)
|
||||
expectEqual(array.capacity, c)
|
||||
expectEqual(array.count, c)
|
||||
for i in 0 ..< c {
|
||||
let array = RigidArray(capacity: layout.capacity, copying: additions)
|
||||
expectEqual(tracker.instances, layout.count)
|
||||
expectEqual(array.capacity, layout.capacity)
|
||||
expectEqual(array.count, layout.count)
|
||||
for i in 0 ..< layout.count {
|
||||
expectEqual(array[i].payload, i)
|
||||
}
|
||||
}
|
||||
@@ -268,7 +204,6 @@ class RigidArrayTests: CollectionTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func test_index_properties() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
let a = RigidArray(layout: layout, using: { $0 })
|
||||
@@ -366,7 +301,7 @@ class RigidArrayTests: CollectionTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func test_setCapacity() {
|
||||
func test_reallocate() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withEvery(
|
||||
"newCapacity",
|
||||
@@ -374,11 +309,14 @@ class RigidArrayTests: CollectionTestCase {
|
||||
) { newCapacity in
|
||||
withLifetimeTracking { tracker in
|
||||
var a = tracker.rigidArray(layout: layout)
|
||||
a.setCapacity(newCapacity)
|
||||
expectEqual(a.count, layout.count)
|
||||
expectEqual(a.capacity, layout.capacity)
|
||||
a.reallocate(capacity: newCapacity)
|
||||
expectEqual(a.count, layout.count)
|
||||
expectEqual(a.capacity, newCapacity)
|
||||
expectEqual(tracker.instances, layout.count)
|
||||
expectContainerContents(a, equivalentTo: 0 ..< layout.count, by: { $0.payload == $1 })
|
||||
expectContainerContents(
|
||||
a, equivalentTo: 0 ..< layout.count, by: { $0.payload == $1 })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -389,16 +327,20 @@ class RigidArrayTests: CollectionTestCase {
|
||||
withEvery(
|
||||
"newCapacity",
|
||||
in: [
|
||||
0, layout.count - 1, layout.count, layout.count + 1, layout.capacity, layout.capacity + 1
|
||||
0, layout.count - 1, layout.count, layout.count + 1,
|
||||
layout.capacity, layout.capacity + 1
|
||||
] as Set
|
||||
) { newCapacity in
|
||||
withLifetimeTracking { tracker in
|
||||
var a = tracker.rigidArray(layout: layout)
|
||||
expectEqual(a.count, layout.count)
|
||||
expectEqual(a.capacity, layout.capacity)
|
||||
a.reserveCapacity(newCapacity)
|
||||
expectEqual(a.count, layout.count)
|
||||
expectEqual(a.capacity, Swift.max(layout.capacity, newCapacity))
|
||||
expectEqual(tracker.instances, layout.count)
|
||||
expectContainerContents(a, equivalentTo: 0 ..< layout.count, by: { $0.payload == $1 })
|
||||
expectContainerContents(
|
||||
a, equivalentTo: 0 ..< layout.count, by: { $0.payload == $1 })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -454,7 +396,8 @@ class RigidArrayTests: CollectionTestCase {
|
||||
withLifetimeTracking { tracker in
|
||||
var a = tracker.rigidArray(layout: layout)
|
||||
withEvery("i", in: 0 ..< layout.count) { i in
|
||||
a.removeLast()
|
||||
let old = a.removeLast()
|
||||
expectEqual(old.payload, layout.count - 1 - i)
|
||||
expectEqual(a.count, layout.count - 1 - i)
|
||||
expectEqual(a.capacity, layout.capacity)
|
||||
}
|
||||
@@ -474,7 +417,8 @@ class RigidArrayTests: CollectionTestCase {
|
||||
expectEqual(tracker.instances, layout.count)
|
||||
a.removeLast(k)
|
||||
expectEqual(tracker.instances, layout.count - k)
|
||||
expectContainerContents(a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
expectContainerContents(
|
||||
a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -488,7 +432,8 @@ class RigidArrayTests: CollectionTestCase {
|
||||
expected.remove(at: i)
|
||||
|
||||
var a = tracker.rigidArray(layout: layout)
|
||||
a.remove(at: i)
|
||||
let old = a.remove(at: i)
|
||||
expectEqual(old.payload, i)
|
||||
expectContainerContents(a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
}
|
||||
}
|
||||
@@ -518,7 +463,8 @@ class RigidArrayTests: CollectionTestCase {
|
||||
|
||||
var a = tracker.rigidArray(layout: layout)
|
||||
a.removeAll(where: { $0.payload.isMultiple(of: 2) })
|
||||
expectContainerContents(a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
expectContainerContents(
|
||||
a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -533,7 +479,8 @@ class RigidArrayTests: CollectionTestCase {
|
||||
let item = a.popLast()
|
||||
|
||||
expectEquivalent(item, expectedItem, by: { $0?.payload == $1 })
|
||||
expectContainerContents(a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
expectContainerContents(
|
||||
a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -553,16 +500,18 @@ class RigidArrayTests: CollectionTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func test_append_contentsOf() {
|
||||
func test_append_copying_MinimalSequence() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withLifetimeTracking { tracker in
|
||||
var a = tracker.rigidArray(layout: layout)
|
||||
a.append(contentsOf: MinimalSequence(
|
||||
elements: (layout.count ..< layout.capacity).map { tracker.instance(for: $0) },
|
||||
underestimatedCount: .half,
|
||||
isContiguous: false))
|
||||
expectTrue(a.isFull)
|
||||
expectEqual(tracker.instances, layout.capacity)
|
||||
withEvery("isContiguous", in: [false, true]) { isContiguous in
|
||||
withLifetimeTracking { tracker in
|
||||
var a = tracker.rigidArray(layout: layout)
|
||||
a.append(copying: MinimalSequence(
|
||||
elements: (layout.count ..< layout.capacity).map { tracker.instance(for: $0) },
|
||||
underestimatedCount: .half,
|
||||
isContiguous: isContiguous))
|
||||
expectTrue(a.isFull)
|
||||
expectEqual(tracker.instances, layout.capacity)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -621,20 +570,25 @@ class RigidArrayTests: CollectionTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func test_insert_contentsOf() {
|
||||
func test_insert_copying_Collection() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withEvery("i", in: 0 ... layout.count) { i in
|
||||
withLifetimeTracking { tracker in
|
||||
let addition = (layout.count ..< layout.capacity)
|
||||
withEvery("isContiguous", in: [false, true]) { isContiguous in
|
||||
withLifetimeTracking { tracker in
|
||||
let addition = (layout.count ..< layout.capacity)
|
||||
|
||||
var expected = Array(0 ..< layout.count)
|
||||
expected.insert(contentsOf: addition, at: i)
|
||||
var expected = Array(0 ..< layout.count)
|
||||
expected.insert(contentsOf: addition, at: i)
|
||||
|
||||
var a = tracker.rigidArray(layout: layout)
|
||||
a.insert(contentsOf: addition.map { tracker.instance(for: $0) }, at: i)
|
||||
let trackedAddition = MinimalCollection(
|
||||
addition.map { tracker.instance(for: $0) },
|
||||
isContiguous: isContiguous)
|
||||
var a = tracker.rigidArray(layout: layout)
|
||||
a.insert(copying: trackedAddition, at: i)
|
||||
|
||||
expectContainerContents(a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
expectEqual(tracker.instances, layout.capacity)
|
||||
expectContainerContents(a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
expectEqual(tracker.instances, layout.capacity)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -691,24 +645,27 @@ class RigidArrayTests: CollectionTestCase {
|
||||
func test_replaceSubrange_Collection() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 5, 10]) { layout in
|
||||
withEveryRange("range", in: 0 ..< layout.count) { range in
|
||||
withEvery("c", in: 0 ..< layout.capacity - layout.count + range.count) { c in
|
||||
withLifetimeTracking { tracker in
|
||||
var expected = Array(0 ..< layout.count)
|
||||
let addition = (0 ..< c).map { -100 - $0 }
|
||||
expected.replaceSubrange(range, with: addition)
|
||||
withEvery("isContiguous", in: [false, true]) { isContiguous in
|
||||
withEvery("c", in: 0 ..< layout.capacity - layout.count + range.count) { c in
|
||||
withLifetimeTracking { tracker in
|
||||
var expected = Array(0 ..< layout.count)
|
||||
let addition = (0 ..< c).map { -100 - $0 }
|
||||
expected.replaceSubrange(range, with: addition)
|
||||
|
||||
var a = tracker.rigidArray(layout: layout)
|
||||
let trackedAddition = addition.map { tracker.instance(for: $0) }
|
||||
a.replaceSubrange(range, with: trackedAddition)
|
||||
let trackedAddition = MinimalCollection(
|
||||
addition.map { tracker.instance(for: $0) },
|
||||
isContiguous: isContiguous)
|
||||
var a = tracker.rigidArray(layout: layout)
|
||||
a.replaceSubrange(range, copying: trackedAddition)
|
||||
|
||||
expectContainerContents(a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
expectEqual(tracker.instances, layout.count - range.count + c)
|
||||
expectContainerContents(a, equivalentTo: expected, by: { $0.payload == $1 })
|
||||
expectEqual(tracker.instances, layout.count - range.count + c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_replaceSubrange_Span() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 5, 10]) { layout in
|
||||
withEveryRange("range", in: 0 ..< layout.count) { range in
|
||||
@@ -1,3 +1,14 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2025 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import Future
|
||||
|
||||
|
||||
@@ -1,212 +0,0 @@
|
||||
import XCTest
|
||||
import _CollectionsTestSupport
|
||||
import Future
|
||||
import Synchronization
|
||||
|
||||
class DynamicArrayTests: CollectionTestCase {
|
||||
func test_validate_Container() {
|
||||
let c = 100
|
||||
|
||||
withLifetimeTracking { tracker in
|
||||
let expected = (0 ..< c).map { tracker.instance(for: $0) }
|
||||
let items = DynamicArray(count: c, initializedWith: { expected[$0] })
|
||||
checkContainer(items, expectedContents: expected)
|
||||
}
|
||||
}
|
||||
|
||||
func test_basics() {
|
||||
withLifetimeTracking { tracker in
|
||||
typealias Value = LifetimeTrackedStruct<Int>
|
||||
|
||||
var array = DynamicArray<Value>()
|
||||
expectTrue(array.isEmpty)
|
||||
expectEqual(array.count, 0)
|
||||
expectEqual(array.capacity, 0)
|
||||
expectEqual(tracker.instances, 0)
|
||||
|
||||
array.append(tracker.structInstance(for: 42))
|
||||
expectFalse(array.isEmpty)
|
||||
expectEqual(array.count, 1)
|
||||
expectEqual(array[0].payload, 42)
|
||||
expectEqual(tracker.instances, 1)
|
||||
|
||||
array.append(tracker.structInstance(for: 23))
|
||||
expectFalse(array.isEmpty)
|
||||
expectEqual(array.count, 2)
|
||||
expectEqual(array[0].payload, 42)
|
||||
expectEqual(array[1].payload, 23)
|
||||
expectEqual(tracker.instances, 2)
|
||||
|
||||
let old = array.remove(at: 0)
|
||||
expectEqual(old.payload, 42)
|
||||
expectFalse(array.isEmpty)
|
||||
expectEqual(array.count, 1)
|
||||
expectEqual(array[0].payload, 23)
|
||||
expectEqual(tracker.instances, 2)
|
||||
_ = consume old
|
||||
expectEqual(tracker.instances, 1)
|
||||
|
||||
let old2 = array.remove(at: 0)
|
||||
expectEqual(old2.payload, 23)
|
||||
expectEqual(array.count, 0)
|
||||
expectTrue(array.isEmpty)
|
||||
expectEqual(tracker.instances, 1)
|
||||
_ = consume old2
|
||||
expectEqual(tracker.instances, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func test_read_access() {
|
||||
withLifetimeTracking { tracker in
|
||||
typealias Value = LifetimeTrackedStruct<Int>
|
||||
|
||||
let c = 100
|
||||
let array = DynamicArray<Value>(count: c) { tracker.structInstance(for: $0) }
|
||||
|
||||
for i in 0 ..< c {
|
||||
expectEqual(array.borrowElement(at: i)[].payload, i)
|
||||
expectEqual(array[i].payload, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_update_access() {
|
||||
withLifetimeTracking { tracker in
|
||||
typealias Value = LifetimeTrackedStruct<Int>
|
||||
|
||||
let c = 100
|
||||
var array = DynamicArray<Value>(count: c) { tracker.structInstance(for: $0) }
|
||||
|
||||
for i in 0 ..< c {
|
||||
// FIXME: 'exclusive' or something instead of mutating subscript
|
||||
var me = array.mutateElement(at: i)
|
||||
me[].payload += 100
|
||||
array[i].payload += 100
|
||||
}
|
||||
|
||||
for i in 0 ..< c {
|
||||
expectEqual(array[i].payload, 200 + i)
|
||||
}
|
||||
|
||||
expectEqual(tracker.instances, c)
|
||||
_ = consume array
|
||||
expectEqual(tracker.instances, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func test_append() {
|
||||
withLifetimeTracking { tracker in
|
||||
typealias Value = LifetimeTrackedStruct<Int>
|
||||
|
||||
var array = DynamicArray<Value>()
|
||||
let c = 100
|
||||
for i in 0 ..< c {
|
||||
array.append(tracker.structInstance(for: 100 + i))
|
||||
}
|
||||
expectEqual(tracker.instances, c)
|
||||
expectEqual(array.count, c)
|
||||
|
||||
for i in 0 ..< c {
|
||||
expectEqual(array.borrowElement(at: i)[].payload, 100 + i)
|
||||
expectEqual(array[i].payload, 100 + i)
|
||||
}
|
||||
|
||||
_ = consume array
|
||||
expectEqual(tracker.instances, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func test_insert() {
|
||||
withLifetimeTracking { tracker in
|
||||
typealias Value = LifetimeTrackedStruct<Int>
|
||||
|
||||
var array = DynamicArray<Value>()
|
||||
let c = 100
|
||||
for i in 0 ..< c {
|
||||
array.insert(tracker.structInstance(for: 100 + i), at: 0)
|
||||
}
|
||||
expectEqual(tracker.instances, c)
|
||||
expectEqual(array.count, c)
|
||||
|
||||
for i in 0 ..< c {
|
||||
expectEqual(array.borrowElement(at: i)[].payload, c + 99 - i)
|
||||
expectEqual(array[i].payload, c + 99 - i)
|
||||
}
|
||||
|
||||
_ = consume array
|
||||
expectEqual(tracker.instances, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func test_remove() {
|
||||
withLifetimeTracking { tracker in
|
||||
typealias Value = LifetimeTrackedStruct<Int>
|
||||
|
||||
let c = 100
|
||||
var array = DynamicArray<Value>(count: c) { tracker.structInstance(for: 100 + $0) }
|
||||
expectEqual(tracker.instances, c)
|
||||
expectEqual(array.count, c)
|
||||
|
||||
for i in 0 ..< c {
|
||||
array.remove(at: 0)
|
||||
expectEqual(array.count, c - 1 - i)
|
||||
expectEqual(tracker.instances, c - 1 - i)
|
||||
}
|
||||
|
||||
expectTrue(array.isEmpty)
|
||||
expectEqual(tracker.instances, 0)
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftCompatibilitySpan 5.0, *)
|
||||
func test_iterate_full() {
|
||||
withLifetimeTracking { tracker in
|
||||
typealias Value = LifetimeTrackedStruct<Int>
|
||||
|
||||
let c = 100
|
||||
let array = DynamicArray<Value>(count: c) { tracker.structInstance(for: 100 + $0) }
|
||||
|
||||
var index = 0
|
||||
do {
|
||||
let span = array.nextSpan(after: &index)
|
||||
expectEqual(span.count, c)
|
||||
for i in 0 ..< span.count {
|
||||
expectEqual(span[i].payload, 100 + i)
|
||||
}
|
||||
}
|
||||
do {
|
||||
let span2 = array.nextSpan(after: &index)
|
||||
expectEqual(span2.count, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftCompatibilitySpan 5.0, *)
|
||||
func test_iterate_stepped() {
|
||||
withLifetimeTracking { tracker in
|
||||
typealias Value = LifetimeTrackedStruct<Int>
|
||||
|
||||
let c = 100
|
||||
let array = DynamicArray<Value>(count: c) { tracker.structInstance(for: $0) }
|
||||
|
||||
withEvery("stride", in: 1 ... c) { stride in
|
||||
var index = 0
|
||||
var i = 0
|
||||
while true {
|
||||
expectEqual(index, i)
|
||||
let span = array.nextSpan(after: &index, maximumCount: stride)
|
||||
expectEqual(index, i + span.count)
|
||||
if span.isEmpty { break }
|
||||
expectEqual(span.count, i + stride <= c ? stride : c % stride)
|
||||
for j in 0 ..< span.count {
|
||||
expectEqual(span[j].payload, i)
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
expectEqual(i, c)
|
||||
expectEqual(array.nextSpan(after: &index, maximumCount: Int.max).count, 0)
|
||||
expectEqual(index, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,13 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// RepeatingContainerTests.swift
|
||||
// swift-collections
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Created by Karoy Lorentey on 2025-04-21.
|
||||
// Copyright (c) 2025 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import _CollectionsTestSupport
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
|
||||
// Copyright (c) 2014 - 2025 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
|
||||
@@ -29,9 +29,14 @@ public struct MinimalBidirectionalCollection<Element> {
|
||||
public init<S: Sequence>(
|
||||
_ elements: S,
|
||||
context: TestContext = TestContext.current,
|
||||
isContiguous: Bool = false,
|
||||
underestimatedCount: UnderestimatedCountBehavior = .value(0)
|
||||
) where S.Element == Element {
|
||||
self._core = _MinimalCollectionCore(context: context, elements: elements, underestimatedCount: underestimatedCount)
|
||||
self._core = _MinimalCollectionCore(
|
||||
context: context,
|
||||
elements: elements,
|
||||
isContiguous: isContiguous,
|
||||
underestimatedCount: underestimatedCount)
|
||||
}
|
||||
|
||||
var _context: TestContext {
|
||||
@@ -51,6 +56,13 @@ extension MinimalBidirectionalCollection: Sequence {
|
||||
timesUnderestimatedCountCalled.increment()
|
||||
return _core.underestimatedCount
|
||||
}
|
||||
|
||||
public func withContiguousStorageIfAvailable<R>(
|
||||
_ body: (UnsafeBufferPointer<Element>) throws -> R
|
||||
) rethrows -> R? {
|
||||
guard _core.isContiguous else { return nil }
|
||||
return unsafe try _core.elements.withUnsafeBufferPointer(body)
|
||||
}
|
||||
}
|
||||
|
||||
extension MinimalBidirectionalCollection: BidirectionalCollection {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
|
||||
// Copyright (c) 2014 - 2025 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
|
||||
@@ -28,9 +28,14 @@ public struct MinimalCollection<Element> {
|
||||
public init<S: Sequence>(
|
||||
_ elements: S,
|
||||
context: TestContext = TestContext.current,
|
||||
isContiguous: Bool = false,
|
||||
underestimatedCount: UnderestimatedCountBehavior = .value(0)
|
||||
) where S.Element == Element {
|
||||
self._core = _MinimalCollectionCore(context: context, elements: elements, underestimatedCount: underestimatedCount)
|
||||
self._core = _MinimalCollectionCore(
|
||||
context: context,
|
||||
elements: elements,
|
||||
isContiguous: isContiguous,
|
||||
underestimatedCount: underestimatedCount)
|
||||
}
|
||||
|
||||
var _context: TestContext {
|
||||
@@ -50,6 +55,13 @@ extension MinimalCollection: Sequence {
|
||||
timesUnderestimatedCountCalled.increment()
|
||||
return _core.underestimatedCount
|
||||
}
|
||||
|
||||
public func withContiguousStorageIfAvailable<R>(
|
||||
_ body: (UnsafeBufferPointer<Element>) throws -> R
|
||||
) rethrows -> R? {
|
||||
guard _core.isContiguous else { return nil }
|
||||
return unsafe try _core.elements.withUnsafeBufferPointer(body)
|
||||
}
|
||||
}
|
||||
|
||||
extension MinimalCollection: Collection {
|
||||
|
||||
+14
-2
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
|
||||
// Copyright (c) 2014 - 2025 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
|
||||
@@ -30,9 +30,14 @@ public struct MinimalMutableRandomAccessCollection<Element> {
|
||||
public init<S: Sequence>(
|
||||
_ elements: S,
|
||||
context: TestContext = TestContext.current,
|
||||
isContiguous: Bool = false,
|
||||
underestimatedCount: UnderestimatedCountBehavior = .value(0)
|
||||
) where S.Element == Element {
|
||||
self._core = _MinimalCollectionCore(context: context, elements: elements, underestimatedCount: underestimatedCount)
|
||||
self._core = _MinimalCollectionCore(
|
||||
context: context,
|
||||
elements: elements,
|
||||
isContiguous: isContiguous,
|
||||
underestimatedCount: underestimatedCount)
|
||||
}
|
||||
|
||||
var _context: TestContext {
|
||||
@@ -52,6 +57,13 @@ extension MinimalMutableRandomAccessCollection: Sequence {
|
||||
timesUnderestimatedCountCalled.increment()
|
||||
return _core.underestimatedCount
|
||||
}
|
||||
|
||||
public func withContiguousStorageIfAvailable<R>(
|
||||
_ body: (UnsafeBufferPointer<Element>) throws -> R
|
||||
) rethrows -> R? {
|
||||
guard _core.isContiguous else { return nil }
|
||||
return unsafe try _core.elements.withUnsafeBufferPointer(body)
|
||||
}
|
||||
}
|
||||
|
||||
extension MinimalMutableRandomAccessCollection: RandomAccessCollection {
|
||||
|
||||
+14
-2
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
|
||||
// Copyright (c) 2014 - 2025 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
|
||||
@@ -30,9 +30,14 @@ public struct MinimalMutableRangeReplaceableRandomAccessCollection<Element> {
|
||||
public init<S: Sequence>(
|
||||
_ elements: S,
|
||||
context: TestContext = TestContext.current,
|
||||
isContiguous: Bool = false,
|
||||
underestimatedCount: UnderestimatedCountBehavior = .value(0)
|
||||
) where S.Element == Element {
|
||||
self._core = _MinimalCollectionCore(context: context, elements: elements, underestimatedCount: underestimatedCount)
|
||||
self._core = _MinimalCollectionCore(
|
||||
context: context,
|
||||
elements: elements,
|
||||
isContiguous: isContiguous,
|
||||
underestimatedCount: underestimatedCount)
|
||||
}
|
||||
|
||||
var _context: TestContext {
|
||||
@@ -52,6 +57,13 @@ extension MinimalMutableRangeReplaceableRandomAccessCollection: Sequence {
|
||||
timesUnderestimatedCountCalled.increment()
|
||||
return _core.underestimatedCount
|
||||
}
|
||||
|
||||
public func withContiguousStorageIfAvailable<R>(
|
||||
_ body: (UnsafeBufferPointer<Element>) throws -> R
|
||||
) rethrows -> R? {
|
||||
guard _core.isContiguous else { return nil }
|
||||
return unsafe try _core.elements.withUnsafeBufferPointer(body)
|
||||
}
|
||||
}
|
||||
|
||||
extension MinimalMutableRangeReplaceableRandomAccessCollection: RandomAccessCollection {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
|
||||
// Copyright (c) 2014 - 2025 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
|
||||
@@ -26,9 +26,14 @@ public struct MinimalRandomAccessCollection<Element> {
|
||||
public init<S: Sequence>(
|
||||
_ elements: S,
|
||||
context: TestContext = TestContext.current,
|
||||
isContiguous: Bool = false,
|
||||
underestimatedCount: UnderestimatedCountBehavior = .value(0)
|
||||
) where S.Element == Element {
|
||||
self._core = _MinimalCollectionCore(context: context, elements: elements, underestimatedCount: underestimatedCount)
|
||||
self._core = _MinimalCollectionCore(
|
||||
context: context,
|
||||
elements: elements,
|
||||
isContiguous: isContiguous,
|
||||
underestimatedCount: underestimatedCount)
|
||||
}
|
||||
|
||||
var _context: TestContext {
|
||||
@@ -48,6 +53,13 @@ extension MinimalRandomAccessCollection: Sequence {
|
||||
timesUnderestimatedCountCalled.increment()
|
||||
return _core.underestimatedCount
|
||||
}
|
||||
|
||||
public func withContiguousStorageIfAvailable<R>(
|
||||
_ body: (UnsafeBufferPointer<Element>) throws -> R
|
||||
) rethrows -> R? {
|
||||
guard _core.isContiguous else { return nil }
|
||||
return unsafe try _core.elements.withUnsafeBufferPointer(body)
|
||||
}
|
||||
}
|
||||
|
||||
extension MinimalRandomAccessCollection: RandomAccessCollection {
|
||||
|
||||
+14
-2
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
|
||||
// Copyright (c) 2014 - 2025 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
|
||||
@@ -30,9 +30,14 @@ public struct MinimalRangeReplaceableRandomAccessCollection<Element> {
|
||||
public init<S: Sequence>(
|
||||
_ elements: S,
|
||||
context: TestContext = TestContext.current,
|
||||
isContiguous: Bool = false,
|
||||
underestimatedCount: UnderestimatedCountBehavior = .value(0)
|
||||
) where S.Element == Element {
|
||||
self._core = _MinimalCollectionCore(context: context, elements: elements, underestimatedCount: underestimatedCount)
|
||||
self._core = _MinimalCollectionCore(
|
||||
context: context,
|
||||
elements: elements,
|
||||
isContiguous: isContiguous,
|
||||
underestimatedCount: underestimatedCount)
|
||||
}
|
||||
|
||||
var _context: TestContext {
|
||||
@@ -52,6 +57,13 @@ extension MinimalRangeReplaceableRandomAccessCollection: Sequence {
|
||||
timesUnderestimatedCountCalled.increment()
|
||||
return _core.underestimatedCount
|
||||
}
|
||||
|
||||
public func withContiguousStorageIfAvailable<R>(
|
||||
_ body: (UnsafeBufferPointer<Element>) throws -> R
|
||||
) rethrows -> R? {
|
||||
guard _core.isContiguous else { return nil }
|
||||
return unsafe try _core.elements.withUnsafeBufferPointer(body)
|
||||
}
|
||||
}
|
||||
|
||||
extension MinimalRangeReplaceableRandomAccessCollection: RandomAccessCollection {
|
||||
|
||||
@@ -44,17 +44,17 @@ public enum UnderestimatedCountBehavior {
|
||||
/// narrow way possible.
|
||||
///
|
||||
/// This sequence is consumed when its iterator is advanced.
|
||||
public struct MinimalSequence<T>: Sequence, CustomDebugStringConvertible {
|
||||
public struct MinimalSequence<Element>: Sequence, CustomDebugStringConvertible {
|
||||
public let timesMakeIteratorCalled = ResettableValue(0)
|
||||
|
||||
internal let _sharedState: _MinimalIteratorSharedState<T>
|
||||
internal let _sharedState: _MinimalIteratorSharedState<Element>
|
||||
internal let _isContiguous: Bool
|
||||
|
||||
public init<S: Sequence>(
|
||||
elements: S,
|
||||
underestimatedCount: UnderestimatedCountBehavior = .value(0),
|
||||
isContiguous: Bool = false
|
||||
) where S.Element == T {
|
||||
) where S.Element == Element {
|
||||
let data = Array(elements)
|
||||
self._sharedState = _MinimalIteratorSharedState(data)
|
||||
self._isContiguous = isContiguous
|
||||
@@ -73,7 +73,7 @@ public struct MinimalSequence<T>: Sequence, CustomDebugStringConvertible {
|
||||
}
|
||||
}
|
||||
|
||||
public func makeIterator() -> MinimalIterator<T> {
|
||||
public func makeIterator() -> MinimalIterator<Element> {
|
||||
timesMakeIteratorCalled.value += 1
|
||||
return MinimalIterator(_sharedState)
|
||||
}
|
||||
@@ -87,10 +87,10 @@ public struct MinimalSequence<T>: Sequence, CustomDebugStringConvertible {
|
||||
}
|
||||
|
||||
public func withContiguousStorageIfAvailable<R>(
|
||||
_ body: (UnsafeBufferPointer<T>) throws -> R
|
||||
_ body: (UnsafeBufferPointer<Element>) throws -> R
|
||||
) rethrows -> R? {
|
||||
guard _isContiguous else { return nil }
|
||||
return try _sharedState.data[_sharedState.i...]
|
||||
return unsafe try _sharedState.data[_sharedState.i...]
|
||||
.withContiguousStorageIfAvailable(body)
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
|
||||
// Copyright (c) 2014 - 2025 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
|
||||
@@ -13,23 +13,31 @@
|
||||
struct _MinimalCollectionCore<Element> {
|
||||
var state: _CollectionState
|
||||
var elements: [Element]
|
||||
let isContiguous: Bool
|
||||
var underestimatedCount: Int
|
||||
|
||||
public init<S: Sequence>(
|
||||
context: TestContext,
|
||||
elements: S,
|
||||
isContiguous: Bool,
|
||||
underestimatedCount: UnderestimatedCountBehavior? = nil
|
||||
) where S.Element == Element {
|
||||
self.init(context: context, elements: Array(elements), underestimatedCount: underestimatedCount)
|
||||
self.init(
|
||||
context: context,
|
||||
elements: Array(elements),
|
||||
isContiguous: isContiguous,
|
||||
underestimatedCount: underestimatedCount)
|
||||
}
|
||||
|
||||
public init(
|
||||
context: TestContext,
|
||||
elements: [Element],
|
||||
isContiguous: Bool,
|
||||
underestimatedCount: UnderestimatedCountBehavior? = nil
|
||||
) {
|
||||
self.state = _CollectionState(context: context, parent: nil, count: elements.count)
|
||||
self.elements = elements
|
||||
self.isContiguous = isContiguous
|
||||
self.underestimatedCount = underestimatedCount?.value(forCount: elements.count) ?? elements.count
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user