mirror of
https://github.com/apple/swift-collections
synced 2026-11-30 05:18:44 +00:00
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
This commit is contained in:
12 files changed
+350
-192
No files matched your search
@@ -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<Element> {
|
||||
_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<Element> {
|
||||
_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
|
||||
|
||||
@@ -33,7 +33,10 @@ extension RigidArray where Element: ~Copyable {
|
||||
public mutating func nextMutableSpan(
|
||||
after index: inout Int, maximumCount: Int
|
||||
) -> MutableSpan<Element> {
|
||||
self.nextMutableSpan(after: &index, maxCount: maximumCount)
|
||||
self.nextMutableSpan(
|
||||
after: &index,
|
||||
maxCount: maximumCount,
|
||||
limitedBy: self.endIndex)
|
||||
}
|
||||
|
||||
@available(*, deprecated, renamed: "previousSpan(before:maxCount:)")
|
||||
|
||||
@@ -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<Element> {
|
||||
_storage.nextMutableSpan(after: &index, maxCount: maxCount)
|
||||
_storage.nextMutableSpan(after: &index, maxCount: maxCount, limitedBy: limit)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
|
||||
@@ -33,7 +33,10 @@ extension UniqueArray where Element: ~Copyable {
|
||||
public mutating func nextMutableSpan(
|
||||
after index: inout Int, maximumCount: Int
|
||||
) -> MutableSpan<Element> {
|
||||
self.nextMutableSpan(after: &index, maxCount: maximumCount)
|
||||
self.nextMutableSpan(
|
||||
after: &index,
|
||||
maxCount: maximumCount,
|
||||
limitedBy: self.endIndex)
|
||||
}
|
||||
|
||||
@available(*, deprecated, renamed: "previousSpan(before:maxCount:)")
|
||||
|
||||
@@ -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<Element> {
|
||||
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<Element> {
|
||||
self.mutableSpan._nextMutableSpan(after: &index, maxCount: maxCount, limitedBy: limit)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
|
||||
@@ -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<Element> {
|
||||
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<Element> {
|
||||
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<Element> {
|
||||
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<Element> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Element> {
|
||||
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<Element> {
|
||||
self.mutableSpan._nextMutableSpan(after: &index, maxCount: maxCount, limitedBy: limit)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
|
||||
@@ -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<Element>
|
||||
|
||||
/// 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<Element>
|
||||
}
|
||||
|
||||
@@ -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<Element> {
|
||||
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<Element> {
|
||||
nextMutableSpan(after: &index, maxCount: Int.max, limitedBy: limit)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(&self)
|
||||
public mutating func nextMutableSpan(
|
||||
after index: inout Index,
|
||||
maxCount: Int
|
||||
) -> MutableSpan<Element> {
|
||||
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<Element> {
|
||||
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<Element> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Element> {
|
||||
_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<Element> {
|
||||
_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")
|
||||
|
||||
@@ -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<Element> {
|
||||
_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<Element> {
|
||||
_storage.nextMutableSpan(after: &index, maxCount: maxCount, limitedBy: limit)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Int>(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<Int>(capacity: capacity)
|
||||
@@ -261,7 +261,7 @@ final class RigidDequeTests: CollectionTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func test_initEmpty() {
|
||||
let deque = RigidDeque<Int>()
|
||||
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<Int>) { 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<LifetimeTracked<Int>, 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<LifetimeTracked<Int>, 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<LifetimeTracked<Int>, 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<LifetimeTracked<Int>, 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<LifetimeTracked<Int>, 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<LifetimeTracked<Int>, 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)
|
||||
|
||||
Reference in new issue
Block a user