mirror of
https://github.com/apple/swift-collections
synced 2026-11-25 04:37:48 +00:00
Range replacement operations generally need to invalidate all indices, and that means that we lose our position in the container after each insertion/removal, unless we happen to be using a container with looser index invalidation (such as arrays or deques). A linked list is an example for a container type where insertions/removals need to invalidate all indices — as we need to track index validation using a single version counter that is stored in the list’s head. However, it would not be acceptable to lose our place whenever we insert a new item to the list; therefore, to mitigate the validation issue, this commit changes all range-replacement operations to return a valid index range to the affected items in the container. This is a radical break from `RangeReplaceableCollection`. It fixes a long-standing defect with it, but it also makes it tricky to conform the same type to both protocol families, as the two require the “same” methods to return different types (`Void` vs `Range<Index>`). Given that the LSG has intentionally diverged `UniqueArray.removeAll()` from RangeReplaceableCollection semantics, I expect this isn’t going to be an issue. For example, types with existing collection conformances can expose container views, mitigating such semantic differences. To allow SubrangeConsumers to work without losing position, add a new protocol, `ContainerDrain` with a consuming `finalize` operation that returns a valid index in the resulting container state. While we’re here, apply the API tweaks from SE-0527: - Revert the `replace(removing:…:)` names, rolling back to `replaceSubrange(_:…:)` . - Similarly, rename `consume(_:)` → `consumeSubrange(_:)` - Rename `reallocate(capacity:)` → `setCapacity(_:)`.
1060 lines
34 KiB
Swift
1060 lines
34 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 _CollectionsTestSupport
|
|
import InternalCollectionsUtilities
|
|
import BasicContainers
|
|
#endif
|
|
|
|
#if compiler(>=6.2)
|
|
|
|
/// Check if `left` and `right` contain equal elements in the same order.
|
|
@available(SwiftStdlib 5.0, *)
|
|
public func expectUniqueArrayContents<
|
|
Element: Equatable,
|
|
C2: Collection<Element>,
|
|
>(
|
|
_ left: borrowing UniqueArray<Element>,
|
|
equalTo right: C2,
|
|
_ message: @autoclosure () -> String = "",
|
|
trapping: Bool = false,
|
|
file: StaticString = #filePath,
|
|
line: UInt = #line
|
|
) {
|
|
expectIterableContents(
|
|
left.span,
|
|
equalTo: right,
|
|
message(), trapping: trapping, file: file, line: line)
|
|
}
|
|
|
|
/// Check if `left` and `right` contain equal elements in the same order.
|
|
@available(SwiftStdlib 5.0, *)
|
|
public func expectUniqueArrayContents<
|
|
E1: ~Copyable,
|
|
C2: Collection,
|
|
>(
|
|
_ left: borrowing UniqueArray<E1>,
|
|
equivalentTo right: C2,
|
|
by areEquivalent: (borrowing E1, C2.Element) -> Bool,
|
|
printer: (borrowing E1) -> String,
|
|
_ message: @autoclosure () -> String = "",
|
|
trapping: Bool = false,
|
|
file: StaticString = #filePath,
|
|
line: UInt = #line
|
|
) {
|
|
expectIterableContents(
|
|
left.span,
|
|
equivalentTo: right,
|
|
by: areEquivalent,
|
|
printer: printer,
|
|
message(),
|
|
trapping: trapping,
|
|
file: file,
|
|
line: line)
|
|
}
|
|
|
|
@available(SwiftStdlib 6.2, *)
|
|
class UniqueArrayTests: CollectionTestCase {
|
|
func test_memory_layout() {
|
|
let word = MemoryLayout<Int>.size
|
|
expectEqual(MemoryLayout<UniqueArray<Int>>.stride, 3 * word)
|
|
expectEqual(MemoryLayout<UniqueArray<Int>?>.stride, 3 * word)
|
|
}
|
|
|
|
#if compiler(>=6.4) && UnstableContainersPreview
|
|
@available(SwiftStdlib 6.4, *)
|
|
func test_validate_Container() {
|
|
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
|
withLifetimeTracking { tracker in
|
|
let items = UniqueArray(consuming: tracker.rigidArray(layout: layout))
|
|
let expected = (0 ..< layout.count).map { tracker.instance(for: $0) }
|
|
expectEqual(tracker.instances, 2 * layout.count)
|
|
expectUniqueArrayContents(items, equalTo: expected)
|
|
checkContainer(items, expectedContents: expected)
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
func test_basics() {
|
|
withLifetimeTracking { tracker in
|
|
typealias Value = LifetimeTrackedStruct<Int>
|
|
|
|
var array = UniqueArray<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 = UniqueArray<Int>(minimumCapacity: 0)
|
|
expectEqual(a.capacity, 0)
|
|
expectEqual(a.count, 0)
|
|
expectEqual(a.freeCapacity, 0)
|
|
expectTrue(a.isEmpty)
|
|
}
|
|
|
|
do {
|
|
let a = UniqueArray<Int>(minimumCapacity: 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.uniqueArray(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)
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: 0 ..< layout.count,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
}
|
|
}
|
|
}
|
|
|
|
func test_init_repeating() {
|
|
withEvery("c", in: [0, 10, 100]) { c in
|
|
withLifetimeTracking { tracker in
|
|
let value = tracker.instance(for: 0)
|
|
let a = UniqueArray(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 = UniqueArray(
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#if UnstableContainersPreview
|
|
#if false // FIXME: Update
|
|
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 = UniqueArray(
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
#endif
|
|
|
|
func test_init_copying_Span() {
|
|
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
|
withLifetimeTracking { tracker in
|
|
let a = tracker.uniqueArray(layout: layout)
|
|
let b = UniqueArray(capacity: layout.capacity, copying: a.span)
|
|
expectEqual(tracker.instances, layout.count)
|
|
expectEqual(b.capacity, layout.capacity)
|
|
expectEqual(b.count, layout.count)
|
|
expectEqual(b.freeCapacity, layout.capacity - layout.count)
|
|
expectEqual(b.isEmpty, layout.count == 0)
|
|
for i in 0 ..< layout.count {
|
|
expectEqual(b[i].payload, i)
|
|
}
|
|
expectUniqueArrayContents(
|
|
b,
|
|
equivalentTo: 0 ..< layout.count,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
}
|
|
}
|
|
}
|
|
|
|
func test_span() {
|
|
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
|
withLifetimeTracking { tracker in
|
|
let a = tracker.uniqueArray(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.uniqueArray(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() {
|
|
// UniqueArray is expected to have exactly one span.
|
|
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
|
withLifetimeTracking { tracker in
|
|
let a = tracker.uniqueArray(layout: layout)
|
|
let whole = a.span
|
|
var i = 0
|
|
let first = a.span(after: &i)
|
|
expectEqual(i, layout.count)
|
|
expectTrue(first.isIdentical(to: whole))
|
|
let second = a.span(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.uniqueArray(layout: layout)
|
|
let whole = a.span
|
|
var i = layout.count
|
|
let first = a.span(before: &i)
|
|
expectEqual(i, 0)
|
|
expectTrue(first.isIdentical(to: whole))
|
|
let second = a.span(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.uniqueArray(layout: layout)
|
|
var i = 0
|
|
var span = a.mutableSpan(after: &i)
|
|
expectEqual(i, layout.count)
|
|
expectEqual(span.count, layout.count)
|
|
span = a.mutableSpan(after: &i)
|
|
expectEqual(i, layout.count)
|
|
expectTrue(span.isEmpty)
|
|
}
|
|
}
|
|
}
|
|
|
|
func test_index_properties() {
|
|
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
|
let a = UniqueArray(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.uniqueArray(layout: layout)
|
|
withEvery("i", in: 0 ..< layout.count / 2) { i in
|
|
a.swapAt(i, layout.count - 1 - i)
|
|
}
|
|
let expected = (0 ..< layout.count).reversed()
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: expected,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
expectEqual(tracker.instances, layout.count)
|
|
}
|
|
}
|
|
}
|
|
|
|
#if false // TODO
|
|
func test_borrowElement() {
|
|
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
|
withLifetimeTracking { tracker in
|
|
let a = tracker.uniqueArray(layout: layout)
|
|
for i in 0 ..< layout.count {
|
|
let item = a.borrowElement(at: i)
|
|
expectEqual(item[].payload, i)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#if false // TODO
|
|
func test_mutateElement() {
|
|
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
|
withLifetimeTracking { tracker in
|
|
var a = tracker.uniqueArray(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)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
func test_edit() {
|
|
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
|
withLifetimeTracking { tracker in
|
|
var a = tracker.uniqueArray(layout: layout)
|
|
a.edit { span in
|
|
expectEqual(span.capacity, layout.capacity)
|
|
expectEqual(span.count, layout.count)
|
|
if layout.capacity > 0 {
|
|
// FIXME: OutputSpan.removeAll crashes when empty in some 6.2 snapshots (rdar://158440246)
|
|
span.removeAll()
|
|
}
|
|
expectEqual(tracker.instances, 0)
|
|
}
|
|
expectEqual(a.count, 0)
|
|
|
|
a.edit { span in
|
|
expectEqual(span.capacity, layout.capacity)
|
|
expectEqual(span.count, 0)
|
|
for i in 0 ..< span.capacity {
|
|
span.append(tracker.instance(for: -i))
|
|
}
|
|
expectEqual(tracker.instances, layout.capacity)
|
|
}
|
|
expectEqual(a.count, layout.capacity)
|
|
|
|
struct TestError: Error {}
|
|
|
|
expectThrows {
|
|
try a.edit { span in
|
|
expectEqual(tracker.instances, layout.capacity)
|
|
while !span.isEmpty {
|
|
if span.count == layout.count { break }
|
|
let old = span.removeLast()
|
|
expectEqual(old.payload, -span.count)
|
|
}
|
|
throw TestError()
|
|
}
|
|
}
|
|
errorHandler: { error in
|
|
expectTrue(error is TestError)
|
|
}
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: (0 ..< layout.count).map { -$0 },
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
expectEqual(tracker.instances, layout.count)
|
|
}
|
|
}
|
|
}
|
|
|
|
@available(*, deprecated)
|
|
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.uniqueArray(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)
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: 0 ..< layout.count,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func test_setCapacity() {
|
|
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
|
withEvery(
|
|
"newCapacity",
|
|
in: [
|
|
layout.capacity, layout.count, layout.count + 1, layout.capacity + 1,
|
|
0, layout.count - 1
|
|
] as Set<Int>
|
|
) { newCapacity in
|
|
withLifetimeTracking { tracker in
|
|
var a = tracker.uniqueArray(layout: layout)
|
|
expectEqual(a.count, layout.count)
|
|
expectEqual(a.capacity, layout.capacity)
|
|
a.setCapacity(newCapacity)
|
|
expectEqual(a.count, layout.count)
|
|
expectEqual(a.capacity, Swift.max(newCapacity, layout.count))
|
|
expectEqual(tracker.instances, layout.count)
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: 0 ..< layout.count,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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.uniqueArray(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)
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: 0 ..< layout.count,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func test_removeAll() {
|
|
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
|
withLifetimeTracking { tracker in
|
|
var a = tracker.uniqueArray(layout: layout)
|
|
a.removeAll()
|
|
expectTrue(a.isEmpty)
|
|
expectEqual(a.capacity, layout.capacity)
|
|
expectEqual(tracker.instances, 0)
|
|
}
|
|
}
|
|
}
|
|
|
|
@available(*, deprecated)
|
|
func test_removeAll_keepingCapacity() {
|
|
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
|
withLifetimeTracking { tracker in
|
|
var a = tracker.uniqueArray(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.uniqueArray(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.uniqueArray(layout: layout)
|
|
expectEqual(tracker.instances, layout.count)
|
|
a.removeLast(k)
|
|
expectEqual(tracker.instances, layout.count - k)
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: expected,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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.uniqueArray(layout: layout)
|
|
let old = a.remove(at: i)
|
|
expectEqual(old.payload, i)
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: expected,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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.uniqueArray(layout: layout)
|
|
a.removeSubrange(range)
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: expected,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#if false // TODO
|
|
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.uniqueArray(layout: layout)
|
|
a.removeAll(where: { $0.payload.isMultiple(of: 2) })
|
|
expectUniqueArrayContents(
|
|
a, equivalentTo: expected, by: { $0.payload == $1 })
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
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.uniqueArray(layout: layout)
|
|
let item = a.popLast()
|
|
|
|
expectEquivalent(item, expectedItem, by: { $0?.payload == $1 })
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: expected,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
}
|
|
}
|
|
}
|
|
|
|
func test_append_geometric_growth() {
|
|
// This test depends on the precise growth curve of UniqueArray,
|
|
// 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 = UniqueArray<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, 3)
|
|
array.append(tracker.instance(for: 3))
|
|
expectEqual(array.capacity, 5)
|
|
array.append(tracker.instance(for: 4))
|
|
expectEqual(array.capacity, 5)
|
|
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, 12)
|
|
|
|
for i in 9 ..< 100 {
|
|
array.append(tracker.instance(for: i))
|
|
}
|
|
expectEqual(tracker.instances, 100)
|
|
expectEqual(array.count, 100)
|
|
expectEqual(array.capacity, 140)
|
|
|
|
do {
|
|
let additions = RigidArray(capacity: 300) { span in
|
|
for i in 0 ..< 300 {
|
|
span.append(tracker.instance(for: 100 + i))
|
|
}
|
|
}
|
|
array.append(copying: additions.span)
|
|
}
|
|
expectEqual(array.capacity, 400)
|
|
|
|
expectEqual(tracker.instances, 400)
|
|
for i in 0 ..< 400 {
|
|
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.uniqueArray(layout: layout)
|
|
for i in layout.count ..< c {
|
|
a.append(tracker.instance(for: i))
|
|
expectEqual(a.count, i + 1)
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: 0 ..< i + 1,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
}
|
|
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.uniqueArray(layout: layout)
|
|
a.append(copying: MinimalSequence(
|
|
elements: (layout.count ..< c).map { tracker.instance(for: $0) },
|
|
underestimatedCount: .half,
|
|
isContiguous: isContiguous))
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: 0 ..< c,
|
|
by: { $0.payload == $1},
|
|
printer: { "\($0.payload)" })
|
|
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.uniqueArray(layout: layout)
|
|
let b = RigidArray(capacity: additions) { span in
|
|
for i in 0 ..< additions {
|
|
span.append(tracker.instance(for: layout.count + i))
|
|
}
|
|
}
|
|
a.append(copying: b.span)
|
|
let c = layout.count + additions
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: 0 ..< c,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
expectEqual(tracker.instances, c)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#if UnstableContainersPreview
|
|
#if false // FIXME: Update
|
|
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.uniqueArray(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)
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: 0 ..< c,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
expectEqual(tracker.instances, c)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
#endif
|
|
|
|
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.uniqueArray(layout: layout)
|
|
a.insert(tracker.instance(for: -1), at: i)
|
|
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: expected,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
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.uniqueArray(layout: layout)
|
|
a.insert(copying: trackedAddition, at: i)
|
|
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: expected,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
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(
|
|
copying: (0 ..< addition.count).lazy.map { tracker.instance(for: addition[$0]) }
|
|
)
|
|
var a = tracker.uniqueArray(layout: layout)
|
|
a.insert(copying: rigidAddition.span, at: i)
|
|
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: expected,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
expectEqual(tracker.instances, layout.count + c)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#if UnstableContainersPreview
|
|
#if false // FIXME: Update
|
|
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.uniqueArray(layout: layout)
|
|
let rigidAddition = StaccatoContainer(
|
|
contents: RigidArray(capacity: addition.count) {
|
|
for item in addition {
|
|
$0.append(tracker.instance(for: item))
|
|
}
|
|
},
|
|
spanCounts: [spanCount])
|
|
a.insert(copying: rigidAddition, at: i)
|
|
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: expected,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
expectEqual(tracker.instances, layout.count + c)
|
|
|
|
if c <= range.count {
|
|
expectEqual(a.capacity, layout.capacity)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
#endif
|
|
|
|
func test_replace_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.uniqueArray(layout: layout)
|
|
let trackedAddition = addition.map { tracker.instance(for: $0) }
|
|
a.replaceSubrange(range, copying: trackedAddition)
|
|
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: expected,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
expectEqual(tracker.instances, layout.count - range.count + c)
|
|
|
|
if c <= range.count {
|
|
expectEqual(a.capacity, layout.capacity)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func test_replace_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.uniqueArray(layout: layout)
|
|
let trackedAddition = RigidArray(
|
|
copying: addition.map { tracker.instance(for: $0) })
|
|
a.replaceSubrange(range, copying: trackedAddition.span)
|
|
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: expected,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
expectEqual(tracker.instances, layout.count - range.count + c)
|
|
|
|
if c <= range.count {
|
|
expectEqual(a.capacity, layout.capacity)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func test_replace_copying_UnsafeBufferPointer() {
|
|
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)
|
|
|
|
let trackedAddition = RigidArray(
|
|
copying: addition.map { tracker.instance(for: $0) })
|
|
|
|
var a = tracker.uniqueArray(layout: layout)
|
|
trackedAddition.span.withUnsafeBufferPointer { buffer in
|
|
_ = a.replaceSubrange(range, copying: buffer)
|
|
}
|
|
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: expected,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
expectEqual(tracker.instances, layout.count - range.count + c)
|
|
|
|
if c <= range.count {
|
|
expectEqual(a.capacity, layout.capacity)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#if UnstableContainersPreview
|
|
#if false // FIXME: Update
|
|
func test_replace_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.uniqueArray(layout: layout)
|
|
let trackedAddition = StaccatoContainer(
|
|
contents: RigidArray(
|
|
copying: addition.map { tracker.instance(for: $0) }),
|
|
spanCounts: [spanCount])
|
|
a.replaceSubrange(range, copying: trackedAddition)
|
|
|
|
expectUniqueArrayContents(
|
|
a,
|
|
equivalentTo: expected,
|
|
by: { $0.payload == $1 },
|
|
printer: { "\($0.payload)" })
|
|
expectEqual(tracker.instances, layout.count - range.count + c)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
#endif
|
|
}
|
|
#endif
|