From fe2b083fae59da0e5eab7e0712929039c572ee79 Mon Sep 17 00:00:00 2001 From: Joseph D Cursio Date: Sun, 13 Sep 2026 13:55:10 -0500 Subject: [PATCH] [DequeModule] Add tests for Deque's storage allocation behavior #113 switched `append(contentsOf:)` to exponential capacity reservation without adding tests. This covers Deque's allocation behavior: - Implement the empty `test_reserveCapacity` stub: reservations grow storage linearly to exactly the requested capacity, never shrink it, and leave sufficiently large unique storage in place. - Initializers with a known element count allocate exactly that much. - Single-element insertions follow the same capacity sequence as `UniqueDeque`. - Repeated bulk insertions (append/prepend/insert/replaceSubrange, from contiguous arrays, collections and sequences) grow storage by a constant factor rather than reallocating on each call. - Reserved capacity is used without reallocating. - Inserting into shared storage keeps capacity when there is room and otherwise grows it by the same factor, leaving the original untouched. Resolves #129. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_016NTKVRe3aXMteHVrhbrTYE --- Tests/DequeTests/DequeAllocationTests.swift | 200 ++++++++++++++++++ .../RangeReplaceableCollectionTests.swift | 23 +- 2 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 Tests/DequeTests/DequeAllocationTests.swift diff --git a/Tests/DequeTests/DequeAllocationTests.swift b/Tests/DequeTests/DequeAllocationTests.swift new file mode 100644 index 00000000..66f66aba --- /dev/null +++ b/Tests/DequeTests/DequeAllocationTests.swift @@ -0,0 +1,200 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2026 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception +// +//===----------------------------------------------------------------------===// + +import XCTest +#if COLLECTIONS_SINGLE_MODULE +@_spi(Testing) import Collections +#else +import _CollectionsTestSupport +@_spi(Testing) import DequeModule +#endif + +/// Clear box tests for `Deque`'s storage allocation behavior, observed through +/// the `_capacity` testing SPI. +final class DequeAllocationTests: CollectionTestCase { + enum SingleInsertion: CaseIterable { + case append + case prepend + case insertInMiddle + + func apply(_ item: Int, to deque: inout Deque, model: inout [Int]) { + switch self { + case .append: + deque.append(item) + model.append(item) + case .prepend: + deque.prepend(item) + model.insert(item, at: 0) + case .insertInMiddle: + deque.insert(item, at: deque.count / 2) + model.insert(item, at: model.count / 2) + } + } + } + + enum BulkInsertion: CaseIterable { + case appendContiguousArray + case appendCollection + case appendSequence + case appendSequenceWithoutUnderestimatedCount + case prependContiguousArray + case prependCollection + case prependSequence + case insertCollectionInMiddle + case replaceEmptySubrangeInMiddle + + func apply(_ items: [Int], to deque: inout Deque, model: inout [Int]) { + switch self { + case .appendContiguousArray: + deque.append(contentsOf: ContiguousArray(items)) + model.append(contentsOf: items) + case .appendCollection: + deque.append(contentsOf: MinimalCollection(items)) + model.append(contentsOf: items) + case .appendSequence: + deque.append( + contentsOf: MinimalSequence(elements: items, underestimatedCount: .precise)) + model.append(contentsOf: items) + case .appendSequenceWithoutUnderestimatedCount: + deque.append(contentsOf: MinimalSequence(elements: items)) + model.append(contentsOf: items) + case .prependContiguousArray: + deque.prepend(contentsOf: ContiguousArray(items)) + model.insert(contentsOf: items, at: 0) + case .prependCollection: + deque.prepend(contentsOf: MinimalCollection(items)) + model.insert(contentsOf: items, at: 0) + case .prependSequence: + deque.prepend( + contentsOf: MinimalSequence(elements: items, underestimatedCount: .precise)) + model.insert(contentsOf: items, at: 0) + case .insertCollectionInMiddle: + deque.insert(contentsOf: MinimalCollection(items), at: deque.count / 2) + model.insert(contentsOf: items, at: model.count / 2) + case .replaceEmptySubrangeInMiddle: + let i = deque.count / 2 + deque.replaceSubrange(i ..< i, with: MinimalCollection(items)) + model.replaceSubrange(i ..< i, with: items) + } + } + } + + func test_initializers_allocateExactCapacity() { + withEvery("count", in: [0, 1, 2, 10, 100]) { count in + expectEqual(Deque(minimumCapacity: count)._capacity, count) + expectEqual(Deque(0 ..< count)._capacity, count) + expectEqual(Deque(MinimalCollection(0 ..< count))._capacity, count) + expectEqual( + Deque(MinimalSequence(elements: 0 ..< count, underestimatedCount: .precise))._capacity, + count) + expectEqual(Deque(repeating: 42, count: count)._capacity, count) + } + } + + func test_singleInsertions_growExponentially() { + let expectedCapacities = [ + 0, 1, 2, 3, 5, 8, 12, 18, 27, 41, 62, 93, 140, 210, 315 + ] + withEvery("operation", in: SingleInsertion.allCases) { operation in + var deque = Deque() + var contents: [Int] = [] + var capacities = [deque._capacity] + for i in 0 ..< 256 { + operation.apply(i, to: &deque, model: &contents) + if deque._capacity != capacities.last { + capacities.append(deque._capacity) + } + } + expectEqualElements(capacities, expectedCapacities) + expectEqualElements(deque, contents) + } + } + + func test_bulkInsertions_growExponentially() { + // https://github.com/apple/swift-collections/pull/113 + // Inserting many small batches must not grow storage linearly, reallocating + // on nearly every call. + withEvery("operation", in: BulkInsertion.allCases) { operation in + withEvery("batchSize", in: [1, 2, 3, 7]) { batchSize in + var deque = Deque() + var contents: [Int] = [] + var reallocationCount = 0 + for round in 0 ..< 100 { + let batch = Array(round * batchSize ..< (round + 1) * batchSize) + let oldCapacity = deque._capacity + operation.apply(batch, to: &deque, model: &contents) + let newCapacity = deque._capacity + guard newCapacity != oldCapacity else { continue } + reallocationCount += 1 + expectGreaterThanOrEqual(newCapacity, oldCapacity + oldCapacity / 2) + } + // Linear growth would reallocate in every round. + expectLessThan(reallocationCount, 20) + expectEqualElements(deque, contents) + } + } + } + + func test_reservedCapacity_isUsedWithoutReallocating() { + let reservedCapacity = 100 + withEvery("initialCount", in: [0, 1, 5]) { initialCount in + withEvery("operation", in: SingleInsertion.allCases) { operation in + var deque = Deque(0 ..< initialCount) + var contents = Array(0 ..< initialCount) + deque.reserveCapacity(reservedCapacity) + while deque.count < reservedCapacity { + operation.apply(deque.count, to: &deque, model: &contents) + expectEqual(deque._capacity, reservedCapacity) + } + expectEqualElements(deque, contents) + } + + withEvery("operation", in: BulkInsertion.allCases) { operation in + var deque = Deque(0 ..< initialCount) + var contents = Array(0 ..< initialCount) + deque.reserveCapacity(reservedCapacity) + while deque.count + 3 <= reservedCapacity { + let batch = Array(deque.count ..< deque.count + 3) + operation.apply(batch, to: &deque, model: &contents) + expectEqual(deque._capacity, reservedCapacity) + } + expectEqualElements(deque, contents) + } + } + } + + func test_insertingIntoSharedStorage() { + withEveryDeque("deque", ofCapacities: [1, 2, 3, 5, 10]) { layout in + withLifetimeTracking { tracker in + var (deque, contents) = tracker.deque(with: layout) + let copy = deque + let extra = tracker.instance(for: layout.count) + deque.append(extra) + contents.append(extra) + expectEqualElements(deque, contents) + if layout.count < layout.capacity { + // Making a unique copy preserves capacity when there is enough room. + expectEqual(deque._capacity, layout.capacity) + } else { + // Otherwise the copy grows by the same factor as unique storage. + expectGreaterThanOrEqual( + deque._capacity, layout.capacity + layout.capacity / 2) + } + // The original storage is left untouched. + expectEqual(copy._capacity, layout.capacity) + expectEqual(copy._startSlot, layout.startSlot) + expectEqual(copy.count, layout.count) + } + } + } +} diff --git a/Tests/DequeTests/RangeReplaceableCollectionTests.swift b/Tests/DequeTests/RangeReplaceableCollectionTests.swift index 1c1010aa..065febee 100644 --- a/Tests/DequeTests/RangeReplaceableCollectionTests.swift +++ b/Tests/DequeTests/RangeReplaceableCollectionTests.swift @@ -125,7 +125,28 @@ final class RangeReplaceableCollectionTests: CollectionTestCase { } func test_reserveCapacity() { - // FIXME: Implement + withEveryDeque("deque", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in + withEvery("minimumCapacity", in: 0 ..< 13) { minimumCapacity in + withEvery("isShared", in: [false, true]) { isShared in + withLifetimeTracking { tracker in + var deque: Deque> + let contents: [LifetimeTracked] + (deque, contents) = tracker.deque(with: layout) + withHiddenCopies(if: isShared, of: &deque) { deque in + deque.reserveCapacity(minimumCapacity) + // Reservations never shrink storage, and they grow it linearly, + // to exactly the requested capacity. + expectEqual(deque._capacity, Swift.max(layout.capacity, minimumCapacity)) + if !isShared && minimumCapacity <= layout.capacity { + // Unique storage that already has enough room is left in place. + expectEqual(deque._startSlot, layout.startSlot) + } + expectEqualElements(deque, contents) + } + } + } + } + } } func test_repeatingInitializer() {