From e75f053e46ab6876c640c79709cfec170ce4c94b Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Mon, 20 Jul 2026 15:44:35 -0700 Subject: [PATCH] Update MutableContainer - Add `nextMutableSpan(after:)` and `nextMutableSpan(after:maxCount:limitedBy)`, replacing `nextMutableSpan(after:maxCount:)` as the core access primitives - Add `previousMutableSpan` default algorithms on bidirectional mutable containers --- .../RigidArray/RigidArray+Container.swift | 18 +- .../RigidArray/RigidArray+Deprecated.swift | 5 +- .../UniqueArray/UniqueArray+Container.swift | 4 +- .../UniqueArray/UniqueArray+Deprecated.swift | 5 +- .../Conformances/InputSpan+Container.swift | 16 +- .../Conformances/MutableSpan+Container.swift | 60 +++- .../Conformances/OutputSpan+Container.swift | 15 +- .../Container/MutableContainer.swift | 71 +++- .../RigidDeque/RigidDeque+Container.swift | 21 +- .../UniqueDeque/UniqueDeque+Container.swift | 12 +- Tests/DequeTests/RigidDequeCrashTests.swift | 8 +- Tests/DequeTests/RigidDequeTests.swift | 307 +++++++++--------- 12 files changed, 350 insertions(+), 192 deletions(-) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift index dafdc0cb..4eb17ccf 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift @@ -453,13 +453,23 @@ extension RigidArray where Element: ~Copyable { @inlinable @_lifetime(&self) public mutating func nextMutableSpan( - after index: inout Int, maxCount: Int + after index: inout Int ) -> MutableSpan { _checkValidIndex(index) + return _mutableSpan(in: Range(uncheckedBounds: (index, count))) + } + + @inlinable + @_lifetime(&self) + public mutating func nextMutableSpan( + after index: inout Int, maxCount: Int, limitedBy limit: Int + ) -> MutableSpan { + _checkValidIndex(index) + _checkValidIndex(limit) precondition(maxCount > 0, "maxCount must be positive") - let start = index - index = start &+ Swift.min(maxCount, _count &- start) - return _mutableSpan(in: Range(uncheckedBounds: (start, index))) + let end = index._clampedUp( + towards: count, maxDistance: maxCount, limitedBy: limit) + return _mutableSpan(in: Range(uncheckedBounds: (index, end))) } @_alwaysEmitIntoClient diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Deprecated.swift b/Sources/BasicContainers/RigidArray/RigidArray+Deprecated.swift index 0aadedd8..3719fbbe 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Deprecated.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Deprecated.swift @@ -33,7 +33,10 @@ extension RigidArray where Element: ~Copyable { public mutating func nextMutableSpan( after index: inout Int, maximumCount: Int ) -> MutableSpan { - self.nextMutableSpan(after: &index, maxCount: maximumCount) + self.nextMutableSpan( + after: &index, + maxCount: maximumCount, + limitedBy: self.endIndex) } @available(*, deprecated, renamed: "previousSpan(before:maxCount:)") diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift index 86f4e4c3..255e3e3b 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift @@ -403,9 +403,9 @@ extension UniqueArray where Element: ~Copyable { @inlinable @_lifetime(&self) public mutating func nextMutableSpan( - after index: inout Int, maxCount: Int + after index: inout Int, maxCount: Int, limitedBy limit: Int ) -> MutableSpan { - _storage.nextMutableSpan(after: &index, maxCount: maxCount) + _storage.nextMutableSpan(after: &index, maxCount: maxCount, limitedBy: limit) } @_alwaysEmitIntoClient diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Deprecated.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Deprecated.swift index 21a47b37..9142329a 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Deprecated.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Deprecated.swift @@ -33,7 +33,10 @@ extension UniqueArray where Element: ~Copyable { public mutating func nextMutableSpan( after index: inout Int, maximumCount: Int ) -> MutableSpan { - self.nextMutableSpan(after: &index, maxCount: maximumCount) + self.nextMutableSpan( + after: &index, + maxCount: maximumCount, + limitedBy: self.endIndex) } @available(*, deprecated, renamed: "previousSpan(before:maxCount:)") diff --git a/Sources/ContainersPreview/Conformances/InputSpan+Container.swift b/Sources/ContainersPreview/Conformances/InputSpan+Container.swift index 75453beb..51652cb7 100644 --- a/Sources/ContainersPreview/Conformances/InputSpan+Container.swift +++ b/Sources/ContainersPreview/Conformances/InputSpan+Container.swift @@ -62,13 +62,17 @@ where Element: ~Copyable @_alwaysEmitIntoClient @_lifetime(&self) public mutating func nextMutableSpan( - after index: inout Index, maxCount: Int + after index: inout Index ) -> MutableSpan { - precondition(index >= 0 && index <= count, "Index out of bounds") - precondition(maxCount > 0, "maxCount must be positive") - let c = Swift.min(count &- index, maxCount) - let start = capacity &- count &+ index - return _uncheckedMutableSpan(in: Range(uncheckedBounds: (start, start &+ c))) + self.mutableSpan._nextMutableSpan(after: &index) + } + + @_alwaysEmitIntoClient + @_lifetime(&self) + public mutating func nextMutableSpan( + after index: inout Index, maxCount: Int, limitedBy limit: Index + ) -> MutableSpan { + self.mutableSpan._nextMutableSpan(after: &index, maxCount: maxCount, limitedBy: limit) } @_alwaysEmitIntoClient diff --git a/Sources/ContainersPreview/Conformances/MutableSpan+Container.swift b/Sources/ContainersPreview/Conformances/MutableSpan+Container.swift index 950f6731..1a1292f5 100644 --- a/Sources/ContainersPreview/Conformances/MutableSpan+Container.swift +++ b/Sources/ContainersPreview/Conformances/MutableSpan+Container.swift @@ -88,14 +88,64 @@ extension MutableSpan: RandomAccessContainer where Element: ~Copyable { @available(SwiftStdlib 6.4, *) extension MutableSpan: MutableContainer where Element: ~Copyable { @_alwaysEmitIntoClient - @_lifetime(&self) - public mutating func nextMutableSpan( - after index: inout Int, maxCount: Int + @_lifetime(copy self) + public consuming func _nextMutableSpan( + after index: inout Int ) -> MutableSpan { precondition(index >= 0 && index <= count, "Index out of bounds") + let end = self.count + let r = self._consumingExtracting(unchecked: Range(uncheckedBounds: (index, end))) + index = end + return r + } + + @_alwaysEmitIntoClient + @_lifetime(&self) + public mutating func nextMutableSpan( + after index: inout Int + ) -> MutableSpan { + precondition(index >= 0 && index <= count, "Index out of bounds") + let end = self.count + let r = self._mutatingExtracting( + unchecked: Range(uncheckedBounds: (index, end))) + index = end + return r + } + + @_alwaysEmitIntoClient + @_lifetime(copy self) + public consuming func _nextMutableSpan( + after index: inout Int, maxCount: Int, limitedBy limit: Int + ) -> MutableSpan { + precondition(index >= 0 && index <= count, "Index out of bounds") + precondition(limit >= 0 && limit <= count, "Index out of bounds") precondition(maxCount > 0, "maxCount must be positive") - let limit = index &+ Swift.min(maxCount, count &- index) - return self._mutatingExtracting(unchecked: Range(uncheckedBounds: (index, limit))) + var end = index &+ Swift.min(maxCount, count &- index) + if limit >= index, limit < end { + end = limit + } + let r = self._consumingExtracting( + unchecked: Range(uncheckedBounds: (index, end))) + index = end + return r + } + + @_alwaysEmitIntoClient + @_lifetime(&self) + public mutating func nextMutableSpan( + after index: inout Int, maxCount: Int, limitedBy limit: Int + ) -> MutableSpan { + precondition(index >= 0 && index <= count, "Index out of bounds") + precondition(limit >= 0 && limit <= count, "Index out of bounds") + precondition(maxCount > 0, "maxCount must be positive") + var end = index &+ Swift.min(maxCount, count &- index) + if limit >= index, limit < end { + end = limit + } + let r = self._mutatingExtracting( + unchecked: Range(uncheckedBounds: (index, end))) + index = end + return r } } diff --git a/Sources/ContainersPreview/Conformances/OutputSpan+Container.swift b/Sources/ContainersPreview/Conformances/OutputSpan+Container.swift index 5ac50db2..4ce98bdf 100644 --- a/Sources/ContainersPreview/Conformances/OutputSpan+Container.swift +++ b/Sources/ContainersPreview/Conformances/OutputSpan+Container.swift @@ -61,12 +61,17 @@ where Element: ~Copyable @_alwaysEmitIntoClient @_lifetime(&self) public mutating func nextMutableSpan( - after index: inout Index, maxCount: Int + after index: inout Index ) -> MutableSpan { - precondition(index >= 0 && index <= count, "Index out of bounds") - precondition(maxCount > 0, "maxCount must be positive") - let limit = Swift.min(count &- index, maxCount) - return self.mutableSpan._consumingExtracting(Range(uncheckedBounds: (index, limit))) + self.mutableSpan._nextMutableSpan(after: &index) + } + + @_alwaysEmitIntoClient + @_lifetime(&self) + public mutating func nextMutableSpan( + after index: inout Index, maxCount: Int, limitedBy limit: Index + ) -> MutableSpan { + self.mutableSpan._nextMutableSpan(after: &index, maxCount: maxCount, limitedBy: limit) } @_alwaysEmitIntoClient diff --git a/Sources/ContainersPreview/Protocols/Container/MutableContainer.swift b/Sources/ContainersPreview/Protocols/Container/MutableContainer.swift index 2cc9b208..aa844857 100644 --- a/Sources/ContainersPreview/Protocols/Container/MutableContainer.swift +++ b/Sources/ContainersPreview/Protocols/Container/MutableContainer.swift @@ -27,7 +27,11 @@ where /// - Complexity: O(1) subscript(index: Index) -> Element { borrow mutate } - /// Return a mutable span over the container's storage that begins with the + @_alwaysEmitIntoClient + @_lifetime(&self) + mutating func nextMutableSpan(after index: inout Index) -> MutableSpan + + /// Return a mutable span over the container's storage that begins with the /// element at the given index, and extends to the end of the contiguous /// storage chunk that contains it, but no more than `maxCount` items. /// @@ -93,7 +97,8 @@ where @_lifetime(&self) mutating func nextMutableSpan( after index: inout Index, - maxCount: Int + maxCount: Int, + limitedBy limit: Index ) -> MutableSpan } @@ -103,10 +108,66 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable { @_alwaysEmitIntoClient @_lifetime(&self) - mutating func nextMutableSpan( - after index: inout Index, + public mutating func nextMutableSpan( + after index: inout Index ) -> MutableSpan { - nextMutableSpan(after: &index, maxCount: Int.max) + nextMutableSpan(after: &index, maxCount: Int.max, limitedBy: self.endIndex) + } + + @_alwaysEmitIntoClient + @_lifetime(&self) + public mutating func nextMutableSpan( + after index: inout Index, + limitedBy limit: Index + ) -> MutableSpan { + nextMutableSpan(after: &index, maxCount: Int.max, limitedBy: limit) + } + + @_alwaysEmitIntoClient + @_lifetime(&self) + public mutating func nextMutableSpan( + after index: inout Index, + maxCount: Int + ) -> MutableSpan { + nextMutableSpan(after: &index, maxCount: maxCount, limitedBy: self.endIndex) + } +} + +@available(SwiftStdlib 6.4, *) +extension MutableContainer +where + Self: BidirectionalContainer & ~Copyable & ~Escapable, + Element: ~Copyable +{ + @_alwaysEmitIntoClient + @_lifetime(&self) + public mutating func previousMutableSpan( + before index: inout Index + ) -> MutableSpan { + let start = self.spanBoundary(before: index).index + var i = start + let span = self.nextMutableSpan(after: &i, limitedBy: index) + precondition(i == index, "Invalid BidirectionalContainer") + return span + } + + @_alwaysEmitIntoClient + @_lifetime(&self) + public mutating func previousMutableSpan( + before index: inout Index, + maxCount: Int, + limitedBy limit: Index + ) -> MutableSpan { + let start = self.spanBoundary( + before: index, + maxDistance: maxCount, + limitedBy: limit + ).index + var i = start + let span = self.nextMutableSpan(after: &i, limitedBy: index) + precondition(i == index, "Invalid BidirectionalContainer") + index = start + return span } } diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift index c54ee4c4..c0f5750b 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift @@ -176,19 +176,30 @@ extension RigidDeque where Element: ~Copyable { @_lifetime(&self) public mutating func nextMutableSpan( - after index: inout Int, maxCount: Int + after index: inout Int ) -> MutableSpan { _checkValidIndex(index) - precondition(maxCount > 0, "maxCount must be positive") - let segment = self._handle - .nextSegment(after: index) - ._extracting(first: maxCount) + let segment = self._handle.nextSegment(after: index) index &+= segment.count return _overrideLifetime( MutableSpan(_unsafeElements: .init(mutating: segment)), mutating: &self) } + @_lifetime(&self) + public mutating func nextMutableSpan( + after index: inout Int, maxCount: Int, limitedBy limit: Int + ) -> MutableSpan { + _checkValidIndex(index) + _checkValidIndex(limit) + precondition(maxCount > 0, "maxCount must be positive") + let segment = self._handle.nextSegment( + after: &index, maxCount: maxCount, limitedBy: limit) + return _overrideLifetime( + MutableSpan(_unsafeElements: .init(mutating: segment)), + mutating: &self) + } + @_alwaysEmitIntoClient public func spanBoundary(before index: Index) -> (index: Index, distance: Int) { precondition(index >= 0 && index <= count, "Index out of bounds") diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift index 42b06bde..8ee86f4f 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift @@ -115,9 +115,17 @@ extension UniqueDeque where Element: ~Copyable { @_alwaysEmitIntoClient @_lifetime(&self) public mutating func nextMutableSpan( - after index: inout Int, maxCount: Int + after index: inout Int ) -> MutableSpan { - _storage.nextMutableSpan(after: &index, maxCount: maxCount) + _storage.nextMutableSpan(after: &index) + } + + @_alwaysEmitIntoClient + @_lifetime(&self) + public mutating func nextMutableSpan( + after index: inout Int, maxCount: Int, limitedBy limit: Int + ) -> MutableSpan { + _storage.nextMutableSpan(after: &index, maxCount: maxCount, limitedBy: limit) } @_alwaysEmitIntoClient diff --git a/Tests/DequeTests/RigidDequeCrashTests.swift b/Tests/DequeTests/RigidDequeCrashTests.swift index 95615425..2151213c 100644 --- a/Tests/DequeTests/RigidDequeCrashTests.swift +++ b/Tests/DequeTests/RigidDequeCrashTests.swift @@ -85,12 +85,12 @@ struct RigidDequeCrashTests { await #expect(processExitsWith: .failure) { var deque = RigidDeque(copying: [1, 2, 3]) var index = -1 - _ = deque.nextMutableSpan(after: &index, maxCount: 1) + _ = deque.nextMutableSpan(after: &index, maxCount: 1, limitedBy: deque.endIndex) } await #expect(processExitsWith: .failure) { var deque = RigidDeque(copying: [1, 2, 3]) var index = 4 // valid range is 0...3 (count == 3) - _ = deque.nextMutableSpan(after: &index, maxCount: 1) + _ = deque.nextMutableSpan(after: &index, maxCount: 1, limitedBy: deque.endIndex) } } @@ -99,12 +99,12 @@ struct RigidDequeCrashTests { await #expect(processExitsWith: .failure) { var deque = RigidDeque(copying: [1, 2, 3]) var index = 0 - _ = deque.nextMutableSpan(after: &index, maxCount: 0) + _ = deque.nextMutableSpan(after: &index, maxCount: 0, limitedBy: deque.endIndex) } await #expect(processExitsWith: .failure) { var deque = RigidDeque(copying: [1, 2, 3]) var index = 0 - _ = deque.nextMutableSpan(after: &index, maxCount: -1) + _ = deque.nextMutableSpan(after: &index, maxCount: -1, limitedBy: deque.endIndex) } } } diff --git a/Tests/DequeTests/RigidDequeTests.swift b/Tests/DequeTests/RigidDequeTests.swift index 0a3ce02d..24cab3bf 100644 --- a/Tests/DequeTests/RigidDequeTests.swift +++ b/Tests/DequeTests/RigidDequeTests.swift @@ -116,7 +116,7 @@ final class RigidDequeTests: CollectionTestCase { let value: Int init(_ value: Int) { self.value = value } } - + func test_testingSPIs() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withLifetimeTracking { tracker in @@ -132,10 +132,10 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_basicProperties() { var deque = RigidDeque(capacity: 10) - + // Empty deque expectEqual(deque.isEmpty, true) expectEqual(deque.isFull, false) @@ -145,12 +145,12 @@ final class RigidDequeTests: CollectionTestCase { expectEqual(deque.startIndex, 0) expectEqual(deque.endIndex, 0) expectEqual(deque.indices, 0 ..< 0) - + // Add some elements deque.append(1) deque.append(2) deque.append(3) - + expectEqual(deque.isEmpty, false) expectEqual(deque.isFull, false) expectEqual(deque.count, 3) @@ -159,19 +159,19 @@ final class RigidDequeTests: CollectionTestCase { expectEqual(deque.startIndex, 0) expectEqual(deque.endIndex, 3) expectEqual(deque.indices, 0 ..< 3) - + // Fill to capacity for i in 4...10 { deque.append(i) } - + expectEqual(deque.isEmpty, false) expectEqual(deque.isFull, true) expectEqual(deque.count, 10) expectEqual(deque.freeCapacity, 0) expectEqual(deque.indices, 0 ..< 10) } - + func test_subscriptBorrow() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withLifetimeTracking { tracker in @@ -182,7 +182,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_subscriptMutate() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery("i", in: 0 ..< layout.count) { i in @@ -196,7 +196,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_swapAt() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5]) { layout in withEvery("i", in: 0 ..< layout.count) { i in @@ -211,7 +211,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_reallocate() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery( @@ -221,7 +221,7 @@ final class RigidDequeTests: CollectionTestCase { withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) data.deque.reallocate(capacity: newCapacity) - + expectEqual(data.deque.capacity, newCapacity) expectEqual(data.deque.count, layout.count) expectEqual(tracker.instances, layout.count) @@ -230,7 +230,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_reserveCapacity() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery( @@ -240,7 +240,7 @@ final class RigidDequeTests: CollectionTestCase { withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) data.deque.reserveCapacity(newCapacity) - + expectEqual(data.deque.capacity, Swift.max(layout.capacity, newCapacity)) expectEqual(data.deque.count, layout.count) expectEqual(tracker.instances, layout.count) @@ -249,7 +249,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_initWithCapacity() { withEvery("capacity", in: [0, 1, 2, 3, 5, 10]) { capacity in let deque = RigidDeque(capacity: capacity) @@ -261,7 +261,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_initEmpty() { let deque = RigidDeque() expectEqual(deque.capacity, 0) @@ -269,7 +269,7 @@ final class RigidDequeTests: CollectionTestCase { expectEqual(deque.freeCapacity, 0) expectNil(deque._handle._buffer.baseAddress) // No allocations } - + func test_initWithClosure_Full() { withEvery("capacity", in: 0 ..< 10) { capacity in withLifetimeTracking { tracker in @@ -283,14 +283,14 @@ final class RigidDequeTests: CollectionTestCase { withEvery("i", in: 0 ..< capacity) { i in expectEqual(deque[i].payload, i) } - + expectEqual(tracker.instances, capacity) _ = consume deque expectEqual(tracker.instances, 0) } } } - + func test_initWithClosure_Partial() { withEvery("capacity", in: 0 ..< 10) { capacity in withEvery("count", in: [0, capacity / 2, max(capacity - 1, 0)] as Set) { count in @@ -305,7 +305,7 @@ final class RigidDequeTests: CollectionTestCase { withEvery("i", in: 0 ..< count) { i in expectEqual(deque[i].payload, i) } - + expectEqual(tracker.instances, count) _ = consume deque expectEqual(tracker.instances, 0) @@ -313,7 +313,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_initWithClosure_Failing() { withEvery("capacity", in: 1 ..< 10 as Range) { capacity -> Void in withEvery("count", in: [0, capacity / 2, max(capacity - 1, 0)] as Set) { count -> Void in @@ -337,7 +337,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_init_consumingUniqueDeque() { withLifetimeTracking { tracker in let ud = UniqueDeque(copying: tracker.instances(for: 0 ..< 10)) @@ -345,17 +345,17 @@ final class RigidDequeTests: CollectionTestCase { expectEqual(tracker.instances, 10) let buffer = ud._storage._handle._buffer expectNotNil(buffer.baseAddress) - + let rd = RigidDeque(consuming: ud) expectEqual(rd.count, 10) expectEqual(tracker.instances, 10) expectEqual(rd._handle._buffer.baseAddress, buffer.baseAddress) - + _ = consume rd expectEqual(tracker.instances, 0) } } - + #if compiler(>=6.4) && UnstableContainersPreview func test_initFromProducer() { withEvery("count", in: 0 ..< 10) { count in @@ -369,13 +369,13 @@ final class RigidDequeTests: CollectionTestCase { defer { invocations += 1 } return tracker.structInstance(for: invocations) } - + let deque = RigidDeque(capacity: capacity, from: &producer) expectEqual(invocations, count) expectEqual(deque.count, count) expectEqual(tracker.instances, count) expectEqual(invocations, count) - + for i in 0 ..< count { expectEqual(deque[i].payload, i) } @@ -383,7 +383,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_initFromThrowingProducer() { withEvery("count", in: 0 ..< 10) { (count: Int) in withLifetimeTracking { tracker in @@ -396,7 +396,7 @@ final class RigidDequeTests: CollectionTestCase { defer { invocations += 1 } return tracker.structInstance(for: invocations) } - + expectThrows { () throws(TestError) in let _ = try RigidDeque(capacity: count + 10, from: &producer) } @@ -408,7 +408,7 @@ final class RigidDequeTests: CollectionTestCase { } } #endif - + func test_initRepeatingValue() { withEvery("c", in: 0 ..< 10) { c in withLifetimeTracking { tracker in @@ -433,11 +433,11 @@ final class RigidDequeTests: CollectionTestCase { withLifetimeTracking { tracker in let values = RigidArray(copying: tracker.instances(for: 0 ..< c)) let deque = RigidDeque(capacity: capacity, copying: values) - + expectEqual(deque.count, c) expectEqual(deque.capacity, capacity) expectIterablePayloads(deque, equalTo: 0 ..< c) - + expectEqual(tracker.instances, c) _ = consume values expectEqual(tracker.instances, c) @@ -448,7 +448,7 @@ final class RigidDequeTests: CollectionTestCase { } } #endif - + func test_initCopyingSequence() { withEvery("c", in: 0 ..< 10) { c in withEvery("capacity", in: [c, c + 1]) { capacity in @@ -457,13 +457,13 @@ final class RigidDequeTests: CollectionTestCase { let contents = MinimalSequence( elements: values, underestimatedCount: .value(0)) - + let deque = RigidDeque(capacity: capacity, copying: contents) - + expectEqual(deque.count, c) expectEqual(deque.capacity, capacity) expectRigidDequeContents(deque, equalTo: values) - + expectEqual(tracker.instances, c) _ = consume values _ = consume contents @@ -474,20 +474,20 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_initCopyingCollection() { withEvery("c", in: 0 ..< 10) { c in withEvery("capacity", in: [c, c + 1]) { capacity in withLifetimeTracking { tracker in let values = tracker.instances(for: 0 ..< c) let contents = MinimalCollection(values) - + let deque = RigidDeque(capacity: capacity, copying: contents) - + expectEqual(deque.count, c) expectEqual(deque.capacity, capacity) expectRigidDequeContents(deque, equalTo: values) - + expectEqual(tracker.instances, c) _ = consume values _ = consume contents @@ -498,34 +498,34 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_initCopyingRange_classic() { // With a deployment target <6.2, this tests the Collection initializer withEvery("c", in: 0 ..< 10) { c in withEvery("capacity", in: [c, c + 1]) { capacity in let deque = RigidDeque(capacity: capacity, copying: 0 ..< c) - + expectEqual(deque.count, c) expectEqual(deque.capacity, capacity) expectRigidDequeContents(deque, equalTo: 0 ..< c) } } } - + @available(SwiftStdlib 6.2, *) func test_initCopyingRange_modern() { // With a deployment target >=6.2, this tests the BorrowingSequence initializer withEvery("c", in: 0 ..< 10) { c in withEvery("capacity", in: [c, c + 1]) { capacity in let deque = RigidDeque(capacity: capacity, copying: 0 ..< c) - + expectEqual(deque.count, c) expectEqual(deque.capacity, capacity) expectRigidDequeContents(deque, equalTo: 0 ..< c) } } } - + func test_popFirst() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withLifetimeTracking { tracker in @@ -549,7 +549,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_prepend() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withLifetimeTracking { tracker in @@ -562,7 +562,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_append() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withLifetimeTracking { tracker in @@ -575,15 +575,15 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_pushFirst() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) let extra = tracker.instance(for: layout.count) - + let result = data.deque.pushFirst(extra) - + if layout.count == layout.capacity { expectEqual(result, extra) expectRigidDequeContents(data.deque, equalTo: data.contents) @@ -594,15 +594,15 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_pushLast() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) let extra = tracker.instance(for: layout.count) - + let result = data.deque.pushLast(extra) - + if layout.count == layout.capacity { expectEqual(result, extra) expectRigidDequeContents(data.deque, equalTo: data.contents) @@ -613,17 +613,17 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_prepend_initializingWith_full() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery("prependCount", in: 0 ..< layout.freeCapacity) { prependCount in withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) - + let extras = tracker.instances(for: layout.count ..< layout.count + prependCount) - + data.contents.insert(contentsOf: extras, at: 0) - + var i = 0 data.deque.prepend(addingCount: prependCount) { target in while !target.isFull { @@ -631,23 +631,23 @@ final class RigidDequeTests: CollectionTestCase { i += 1 } } - + expectRigidDequeContents(data.deque, equalTo: data.contents) } } } } - + func test_append_initializingWith_full() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery("prependCount", in: 0 ..< layout.freeCapacity) { prependCount in withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) - + let extras = tracker.instances(for: layout.count ..< layout.count + prependCount) - + data.contents.append(contentsOf: extras) - + var i = 0 data.deque.append(addingCount: prependCount) { target in while !target.isFull { @@ -655,23 +655,23 @@ final class RigidDequeTests: CollectionTestCase { i += 1 } } - + expectRigidDequeContents(data.deque, equalTo: data.contents) } } } } - + func test_prepend_initializingWith_partial() { withEveryDeque("layout", ofCapacities: [1, 2, 3, 5, 10]) { layout in withEvery("prependCount", in: 0 ..< layout.freeCapacity) { prependCount in withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) - + let extras = tracker.instances(for: layout.count ..< layout.count + prependCount) - + data.contents.insert(contentsOf: extras, at: 0) - + var i = 0 data.deque.prepend(addingCount: layout.freeCapacity) { target in while !target.isFull, i < prependCount { @@ -685,17 +685,17 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_append_initializingWith_partial() { withEveryDeque("layout", ofCapacities: [1, 2, 3, 5, 10]) { layout in withEvery("prependCount", in: 0 ..< layout.freeCapacity) { prependCount in withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) - + let extras = tracker.instances(for: layout.count ..< layout.count + prependCount) - + data.contents.append(contentsOf: extras) - + var i = 0 data.deque.append(addingCount: layout.freeCapacity) { target in while !target.isFull, i < prependCount { @@ -709,18 +709,18 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_prepend_initializingWith_failing() { withEveryDeque("layout", ofCapacities: [1, 2, 3, 5, 10]) { layout in if layout.freeCapacity == 0 { return } withEvery("prependCount", in: 0 ..< layout.freeCapacity) { prependCount in withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) - + let extras = tracker.instances(for: layout.count ..< layout.count + prependCount) - + data.contents.insert(contentsOf: extras, at: 0) - + expectThrows { () throws(TestError) in var i = 0 try data.deque.prepend( @@ -743,19 +743,19 @@ final class RigidDequeTests: CollectionTestCase { } } } - - + + func test_append_initializingWith_failing() { withEveryDeque("layout", ofCapacities: [1, 2, 3, 5, 10]) { layout in if layout.freeCapacity == 0 { return } withEvery("prependCount", in: 0 ..< layout.freeCapacity) { prependCount in withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) - + let extras = tracker.instances(for: layout.count ..< layout.count + prependCount) - + data.contents.append(contentsOf: extras) - + expectThrows { () throws(TestError) in var i = 0 try data.deque.append( @@ -778,23 +778,23 @@ final class RigidDequeTests: CollectionTestCase { } } } - + #if compiler(>=6.4) && UnstableContainersPreview func test_prepend_Producer_full() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5]) { layout in withEvery("producerSize", in: 0 ..< 6) { producerSize in withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) - + var extras = tracker.instances(for: 0 ..< producerSize) - + data.contents.insert(contentsOf: extras.prefix(layout.freeCapacity), at: 0) - + var producer = CustomProducer, Never> { guard !extras.isEmpty else { return nil } return extras.removeFirst() } - + data.deque.prepend(from: &producer) expectEqual(extras.count, max(0, producerSize - layout.freeCapacity)) expectRigidDequeContents(data.deque, equalTo: data.contents) @@ -803,23 +803,23 @@ final class RigidDequeTests: CollectionTestCase { } } #endif - + #if compiler(>=6.4) && UnstableContainersPreview func test_append_Producer() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5]) { layout in withEvery("producerSize", in: 0 ..< layout.freeCapacity) { producerSize in withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) - + var extras = tracker.instances(for: 0 ..< producerSize) - + data.contents.append(contentsOf: extras) - + var producer = CustomProducer, Never> { guard !extras.isEmpty else { return nil } return extras.removeFirst() } - + data.deque.append(from: &producer) expectEqual(extras.count, max(0, producerSize - layout.freeCapacity)) expectRigidDequeContents(data.deque, equalTo: data.contents) @@ -828,7 +828,7 @@ final class RigidDequeTests: CollectionTestCase { } } #endif - + #if compiler(>=6.4) && UnstableContainersPreview func test_prepend_Producer_failing() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5]) { layout in @@ -836,16 +836,16 @@ final class RigidDequeTests: CollectionTestCase { withEvery("producerSize", in: 0 ..< layout.freeCapacity - 1) { producerSize in withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) - + var extras = tracker.instances(for: 0 ..< producerSize) - + data.contents.insert(contentsOf: extras, at: 0) - + var producer = CustomProducer, TestError> { () throws(TestError) in guard !extras.isEmpty else { throw TestError(23) } return extras.removeFirst() } - + expectThrows { () throws(TestError) in try data.deque.prepend(from: &producer) } @@ -859,7 +859,7 @@ final class RigidDequeTests: CollectionTestCase { } } #endif - + #if compiler(>=6.4) && UnstableContainersPreview func test_append_Producer_failing() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5]) { layout in @@ -867,16 +867,16 @@ final class RigidDequeTests: CollectionTestCase { withEvery("producerSize", in: 0 ..< layout.freeCapacity - 1) { producerSize in withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) - + var extras = tracker.instances(for: 0 ..< producerSize) - + data.contents.append(contentsOf: extras) - + var producer = CustomProducer, TestError> { () throws(TestError) in guard !extras.isEmpty else { throw TestError(23) } return extras.removeFirst() } - + expectThrows { () throws(TestError) in try data.deque.append(from: &producer) } @@ -890,7 +890,7 @@ final class RigidDequeTests: CollectionTestCase { } } #endif - + func test_prepend_MinimalSequence() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery("c", in: 0 ..< layout.freeCapacity) { c in @@ -907,7 +907,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_append_MinimalSequence() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery("c", in: 0 ..< layout.freeCapacity) { c in @@ -924,7 +924,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_prepend_MinimalCollection() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery("c", in: 0 ..< layout.freeCapacity) { c in @@ -939,7 +939,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_append_MinimalCollection() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery("c", in: 0 ..< layout.freeCapacity) { c in @@ -954,7 +954,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_prepend_ContiguousArray_asCollection() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery("c", in: 0 ..< layout.freeCapacity) { c in @@ -969,7 +969,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_append_ContiguousArray_asCollection() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery("c", in: 0 ..< layout.freeCapacity) { c in @@ -984,7 +984,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_prepend_ContiguousArray_asSequence() { // This calls the Sequence-based `Deque.prepend` overload, even if // `elements` happens to be of a Collection type. @@ -994,7 +994,7 @@ final class RigidDequeTests: CollectionTestCase { ) { deque.prepend(copying: elements) } - + withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery("c", in: 0 ..< layout.freeCapacity) { c in withLifetimeTracking { tracker in @@ -1008,7 +1008,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_append_ContiguousArray_asSequence() { // This calls the Sequence-based `Deque.append` overload, even if // `elements` happens to be of a Collection type. @@ -1018,7 +1018,7 @@ final class RigidDequeTests: CollectionTestCase { ) { deque.append(copying: elements) } - + withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery("c", in: 0 ..< layout.freeCapacity) { c in withLifetimeTracking { tracker in @@ -1032,7 +1032,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_prepend_BridgedArray() { // https://github.com/apple/swift-collections/issues/27 withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in @@ -1055,7 +1055,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_append_BridgedArray() { // https://github.com/apple/swift-collections/issues/27 withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in @@ -1078,7 +1078,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_insert() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery("offset", in: 0 ... layout.count) { offset in @@ -1100,11 +1100,11 @@ final class RigidDequeTests: CollectionTestCase { withEvery("c", in: 0 ..< layout.freeCapacity) { c in withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) - + let extras = tracker.instances(for: layout.count ..< layout.count + c) - + data.contents.insert(contentsOf: extras, at: i) - + var j = 0 data.deque.insert(addingCount: c, at: i) { target in expectLessThanOrEqual(target.count, extras.count - j) @@ -1121,7 +1121,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_insert_initializingWith_partial() { withEveryDeque("layout", ofCapacities: [1, 2, 3, 5, 10]) { layout in guard layout.freeCapacity > 0 else { return } @@ -1129,11 +1129,11 @@ final class RigidDequeTests: CollectionTestCase { withEvery("c", in: 0 ..< layout.freeCapacity - 1) { c in withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) - + let extras = tracker.instances(for: layout.count ..< layout.count + c) - + data.contents.insert(contentsOf: extras, at: i) - + var j = 0 data.deque.insert(addingCount: layout.freeCapacity, at: i) { target in while !target.isFull, j < c { @@ -1148,7 +1148,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_insert_initializingWith_failing() { withEveryDeque("layout", ofCapacities: [1, 2, 3, 5, 10]) { layout in if layout.freeCapacity == 0 { return } @@ -1156,11 +1156,11 @@ final class RigidDequeTests: CollectionTestCase { withEvery("c", in: 0 ..< layout.freeCapacity - 1) { c in withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) - + let extras = tracker.instances(for: layout.count ..< layout.count + c) - + data.contents.insert(contentsOf: extras, at: i) - + expectThrows { () throws(TestError) in var j = 0 try data.deque.insert( @@ -1193,18 +1193,18 @@ final class RigidDequeTests: CollectionTestCase { withEvery("c", in: 0 ..< 6) { c in withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) - + var extras = tracker.instances(for: 0 ..< c) - + data.contents.insert( contentsOf: extras.prefix(layout.freeCapacity), at: i) - + var producer = CustomProducer, Never> { guard !extras.isEmpty else { return nil } return extras.removeFirst() } - + data.deque.insert( addingCount: layout.freeCapacity, from: &producer, @@ -1217,7 +1217,7 @@ final class RigidDequeTests: CollectionTestCase { } } #endif - + #if compiler(>=6.4) && UnstableContainersPreview func test_insert_fromProducer_failing() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5]) { layout in @@ -1226,16 +1226,16 @@ final class RigidDequeTests: CollectionTestCase { withEvery("c", in: 0 ..< layout.freeCapacity - 1) { c in withLifetimeTracking { tracker in var data = tracker.rigidDeque(with: layout) - + var extras = tracker.instances(for: 0 ..< c) - + data.contents.insert(contentsOf: extras, at: i) - + var producer = CustomProducer, TestError> { () throws(TestError) in guard !extras.isEmpty else { throw TestError(23) } return extras.removeFirst() } - + expectThrows { () throws(TestError) in try data.deque.insert( addingCount: layout.freeCapacity, @@ -1304,7 +1304,7 @@ final class RigidDequeTests: CollectionTestCase { withEvery("c", in: 0 ..< subrange.count + layout.freeCapacity) { c in withLifetimeTracking { tracker in var a = tracker.rigidDeque(with: layout).consume() - + var i = 0 a.replace(removing: subrange, addingCount: c) { target in while !target.isFull { @@ -1313,12 +1313,12 @@ final class RigidDequeTests: CollectionTestCase { } } expectEqual(i, c) - + var expected = Array(0 ..< layout.count) expected.replaceSubrange( subrange, with: layout.count ..< layout.count + c) - + expectRigidDequeContents( a, equivalentTo: expected, @@ -1338,7 +1338,7 @@ final class RigidDequeTests: CollectionTestCase { withEvery("n", in: 0 ..< c - 1) { n in withLifetimeTracking { tracker in var a = tracker.rigidDeque(with: layout).consume() - + var i = 0 a.replace(removing: subrange, addingCount: c) { target in while !target.isFull, i < n { @@ -1347,12 +1347,12 @@ final class RigidDequeTests: CollectionTestCase { } } expectEqual(i, n) - + var expected = Array(0 ..< layout.count) expected.replaceSubrange( subrange, with: layout.count ..< layout.count + n) - + expectRigidDequeContents( a, equivalentTo: expected, @@ -1372,7 +1372,7 @@ final class RigidDequeTests: CollectionTestCase { withEvery("c", in: 0 ..< subrange.count + layout.freeCapacity) { c in withLifetimeTracking { tracker in var a = tracker.rigidDeque(with: layout).consume() - + var i = subrange.lowerBound var j = 0 a.replace( @@ -1398,7 +1398,7 @@ final class RigidDequeTests: CollectionTestCase { expected.replaceSubrange( subrange, with: layout.count ..< layout.count + c) - + expectRigidDequeContents( a, equivalentTo: expected, @@ -1420,7 +1420,7 @@ final class RigidDequeTests: CollectionTestCase { withEvery("n", in: 0 ..< c - 1) { n in withLifetimeTracking { tracker in var a = tracker.rigidDeque(with: layout).consume() - + var i = subrange.lowerBound var j = 0 a.replace( @@ -1441,12 +1441,12 @@ final class RigidDequeTests: CollectionTestCase { }) expectEqual(i, subrange.upperBound) expectEqual(j, n) - + var expected = Array(0 ..< layout.count) expected.replaceSubrange( subrange, with: layout.count ..< layout.count + n) - + expectRigidDequeContents( a, equivalentTo: expected, @@ -1473,7 +1473,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_removeSubrange() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEveryRange("range", in: 0 ..< layout.count) { range in @@ -1486,7 +1486,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_removeLast() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in guard layout.count > 0 else { return } @@ -1499,7 +1499,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_removeLast_many() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in guard layout.count > 0 else { return } @@ -1513,7 +1513,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_removeFirst_one() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in guard layout.count > 0 else { return } @@ -1526,7 +1526,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_removeFirst_many() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in guard layout.count > 0 else { return } @@ -1540,7 +1540,7 @@ final class RigidDequeTests: CollectionTestCase { } } } - + func test_removeAll_keepingCapacity() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery("isShared", in: [false, true]) { isShared in @@ -1575,7 +1575,7 @@ final class RigidDequeTests: CollectionTestCase { } } - func test_nextMutableSpan() { + func test_nextMutableSpan_maxCount() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery("maxCount", in: [1, 2, 3, 5, 100]) { maxCount in var deque = RigidDeque(layout: layout, contents: layout.valueRange) @@ -1583,7 +1583,10 @@ final class RigidDequeTests: CollectionTestCase { var totalSeen = 0 while index < deque.endIndex { let oldIndex = index - var span = deque.nextMutableSpan(after: &index, maxCount: maxCount) + var span = deque.nextMutableSpan( + after: &index, + maxCount: maxCount, + limitedBy: deque.endIndex) expectGreaterThan(span.count, 0) expectLessThanOrEqual(span.count, maxCount) expectEqual(index, oldIndex + span.count)