1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
import XCTest
#if COLLECTIONS_SINGLE_MODULE
@_spi(Testing) import Collections
#else
import _CollectionsTestSupport
@_spi(Testing) import DequeModule
#endif
final class DequeTests: CollectionTestCase {
func test_testingSPIs() {
let deque = Deque(_capacity: 5, startSlot: 2, contents: [10, 20, 30, 40])
expectEqual(deque.count, 4)
expectEqual(deque._capacity, 5)
expectEqual(deque._startSlot, 2)
expectEqualElements(deque, [10, 20, 30, 40])
}
func test_CollectionConformance() {
checkBidirectionalCollection(Deque<Int>(), expectedContents: [])
checkBidirectionalCollection(Deque([1]), expectedContents: [1])
checkBidirectionalCollection(Deque([1, 2, 3]), expectedContents: [1, 2, 3])
checkBidirectionalCollection(Deque(0 ..< 10), expectedContents: 0 ..< 10)
// Exhaustive tests for all deque layouts of various capacities
withEveryDeque("deque", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in
withLifetimeTracking { tracker in
let (deque, contents) = tracker.deque(with: layout)
checkBidirectionalCollection(deque, expectedContents: contents)
}
}
}
func test_ExpressibleByArrayLiteral() {
let deque: Deque<Int> = [1, 2, 3, 4]
expectEqual(Array(deque), [1, 2, 3, 4])
}
func test_description() {
expectEqual("\([] as Deque<Int>)", "[]")
expectEqual("\([1, 2, 3] as Deque<Int>)", "[1, 2, 3]")
expectEqual("\([1, 2, nil, 3] as Deque<Int?>)", "[Optional(1), Optional(2), nil, Optional(3)]")
let deque: Deque<StringConvertibleValue> = [1, 2, 3]
expectEqual("\(deque)", "[debugDescription(1), debugDescription(2), debugDescription(3)]")
}
func test_debugDescription() {
expectEqual(String(reflecting: [] as Deque<Int>), "[]")
expectEqual(String(reflecting: [1, 2, 3] as Deque<Int>), "[1, 2, 3]")
expectEqual(
String(reflecting: [1, 2, nil, 3] as Deque<Int?>),
"[Optional(1), Optional(2), nil, Optional(3)]")
let deque: Deque<StringConvertibleValue> = [1, 2, 3]
expectEqual(String(reflecting: deque), "[debugDescription(1), debugDescription(2), debugDescription(3)]")
}
func test_customMirror() {
let deque: Deque<Int> = [1, 2, 3]
let mirror = Mirror(reflecting: deque)
expectEqual(mirror.displayStyle, .collection)
expectNil(mirror.superclassMirror)
expectTrue(mirror.children.compactMap { $0.label }.isEmpty) // No label
expectEqualElements(mirror.children.map { $0.value as? Int }, deque.map { $0 })
}
func test_Equatable_Hashable() {
let c1 = [1, 2, 3, 4]
let c2 = [1, 2]
let equivalenceClasses: [[Deque<Int>]] = [
[
Deque(_capacity: 4, startSlot: 0, contents: c1),
Deque(_capacity: 6, startSlot: 0, contents: c1),
Deque(_capacity: 4, startSlot: 2, contents: c1),
],
[
Deque(_capacity: 2, startSlot: 0, contents: c2),
Deque(_capacity: 6, startSlot: 0, contents: c2),
Deque(_capacity: 2, startSlot: 1, contents: c2),
],
[
Deque(),
Deque(_capacity: 0, startSlot: 0, contents: []),
Deque(_capacity: 6, startSlot: 0, contents: []),
Deque(_capacity: 2, startSlot: 1, contents: []),
],
]
checkHashable(equivalenceClasses: equivalenceClasses)
}
func test_Encodable() throws {
let d1: Deque<Int> = []
let v1: MinimalEncoder.Value = .array([])
expectEqual(try MinimalEncoder.encode(d1), v1)
let d2: Deque<Int> = [0, 1, 2, 3]
let v2: MinimalEncoder.Value = .array([.int(0), .int(1), .int(2), .int(3)])
expectEqual(try MinimalEncoder.encode(d2), v2)
try withEveryDeque("deque", ofCapacities: [0, 1, 3]) { layout in
try withLifetimeTracking { tracker in
let (deque, contents) = tracker.deque(with: layout)
let encoded = try MinimalEncoder.encode(deque)
expectEqual(encoded, .array(contents.map { .int($0.payload) }))
}
}
}
func test_Decodable() throws {
let d1: Deque<Int> = []
let v1: MinimalEncoder.Value = .array([])
expectEqual(try MinimalDecoder.decode(v1, as: Deque<Int>.self), d1)
let d2: Deque<Int> = [0, 1, 2, 3]
let v2: MinimalEncoder.Value = .array([.int(0), .int(1), .int(2), .int(3)])
expectEqual(try MinimalDecoder.decode(v2, as: Deque<Int>.self), d2)
try withEveryDeque("deque", ofCapacities: [0, 1, 3]) { layout in
let contents = Array(0 ..< layout.count)
let deque = Deque(layout: layout, contents: contents)
let input: MinimalDecoder.Value = .array(contents.map { .int($0) })
let decoded = try MinimalDecoder.decode(input, as: Deque<Int>.self)
expectEqual(decoded, deque)
}
}
func test_copyToContiguousArray() {
withEveryDeque("deque", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in
withLifetimeTracking { tracker in
let (deque, contents) = tracker.deque(with: layout)
let actual = deque._copyToContiguousArray()
expectEqualElements(actual, contents)
}
}
}
func test_partial_copyContents() {
// `Deque` supports `_copyContents` invocations with buffer sizes below
// `underestimatedCount`. This isn't a requirement, so the `Collection`
// checker doesn't cover this case.
withEveryDeque("deque", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in
withEvery("prefix", in: 0 ... layout.count) { prefix in
withLifetimeTracking { tracker in
let (deque, contents) = tracker.deque(with: layout)
var it: Deque<LifetimeTracked<Int>>.Iterator?
let head = Array<LifetimeTracked<Int>>(
unsafeUninitializedCapacity: prefix
) { buffer, count in
(it, count) = deque._copyContents(initializing: buffer)
}
let tail = Array(IteratorSequence(it!))
expectEqualElements(head, contents.prefix(upTo: prefix))
expectEqualElements(tail, contents.suffix(from: prefix))
}
}
}
}
func test_unsafeUninitializedInitializer_nothrow() {
withEvery("capacity", in: 0 ..< 100) { cap in
withEvery("count", in: [0, cap / 3, cap / 2, 2 * cap / 3, cap] as Set) { count in
withLifetimeTracking { tracker in
let contents = tracker.instances(for: 0 ..< count)
let d1 = Deque<LifetimeTracked<Int>>(
unsafeUninitializedCapacity: cap,
initializingWith: { target, c in
expectNotNil(target.baseAddress)
expectEqual(target.count, cap)
expectEqual(c, 0)
contents.withUnsafeBufferPointer { source in
precondition(source.count <= target.count)
target.baseAddress!.initialize(
from: source.baseAddress!,
count: source.count)
}
c = count
})
expectEqualElements(d1, contents)
}
}
}
}
struct TestError: Error, Equatable {
let value: Int
init(_ value: Int) { self.value = value }
}
func test_unsafeUninitializedInitializer_throw() {
func workaroundSR14134(cap: Int, count: Int, tracker: LifetimeTracker) {
// This function works around https://bugs.swift.org/browse/SR-14134
let contents = tracker.instances(for: 0 ..< count)
expectThrows(
try Deque<LifetimeTracked<Int>>(
unsafeUninitializedCapacity: cap,
initializingWith: { target, c in
expectNotNil(target.baseAddress)
expectEqual(target.count, cap)
expectEqual(c, 0)
contents.withUnsafeBufferPointer { source in
precondition(source.count <= target.count)
target.baseAddress!.initialize(
from: source.baseAddress!,
count: source.count)
}
c = count
throw TestError(count)
})
) { error in
expectEqual(error as? TestError, TestError(count))
}
}
withEvery("capacity", in: 0 ..< 100) { cap in
withEvery("count", in: [0, cap / 3, cap / 2, 2 * cap / 3, cap] as Set<Int>) { count in
withLifetimeTracking { tracker in
workaroundSR14134(cap: cap, count: count, tracker: tracker)
}
}
}
}
func test_popFirst() {
withEveryDeque("deque", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in
withEvery("isShared", in: [false, true]) { isShared in
withLifetimeTracking { tracker in
var (deque, contents) = tracker.deque(with: layout)
withHiddenCopies(if: isShared, of: &deque) { deque in
let expected = contents[...].popFirst()
let actual = deque.popFirst()
expectEqual(actual, expected)
expectEqualElements(deque, contents)
}
}
}
}
}
func test_prependOne() {
withEveryDeque("deque", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in
withEvery("isShared", in: [false, true]) { isShared in
withLifetimeTracking { tracker in
var (deque, contents) = tracker.deque(with: layout)
let extra = tracker.instance(for: layout.count)
withHiddenCopies(if: isShared, of: &deque) { deque in
contents.insert(extra, at: 0)
deque.prepend(extra)
expectEqualElements(deque, contents)
}
}
}
}
}
func test_prependManyFromMinimalSequence() {
withEveryDeque("deque", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in
withEvery("prependCount", in: 0 ..< 10) { prependCount in
withEvery("underestimatedCount", in: [UnderestimatedCountBehavior.precise, .half, .value(min(1, prependCount))]) { underestimatedCount in
withEvery("isShared", in: [false, true]) { isShared in
withLifetimeTracking { tracker in
var (deque, contents) = tracker.deque(with: layout)
let extras = tracker.instances(for: layout.count ..< layout.count + prependCount)
let sequence = MinimalSequence(elements: extras, underestimatedCount: underestimatedCount)
withHiddenCopies(if: isShared, of: &deque) { deque in
contents.insert(contentsOf: extras, at: 0)
deque.prepend(contentsOf: sequence)
expectEqualElements(deque, contents)
}
}
}
}
}
}
}
func test_prependManyFromMinimalCollection() {
withEveryDeque("deque", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in
withEvery("prependCount", in: 0 ..< 10) { prependCount in
withEvery("isShared", in: [false, true]) { isShared in
withLifetimeTracking { tracker in
var (deque, contents) = tracker.deque(with: layout)
let extra = tracker.instances(for: layout.count ..< layout.count + prependCount)
let minimal = MinimalCollection(extra)
withHiddenCopies(if: isShared, of: &deque) { deque in
contents.insert(contentsOf: extra, at: 0)
deque.prepend(contentsOf: minimal)
expectEqualElements(deque, contents)
}
}
}
}
}
}
func test_prependManyFromContiguousArray_asCollection() {
withEveryDeque("deque", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in
withEvery("prependCount", in: 0 ..< 10) { prependCount in
withEvery("isShared", in: [false, true]) { isShared in
withLifetimeTracking { tracker in
var (deque, contents) = tracker.deque(with: layout)
let extraRange = layout.count ..< layout.count + prependCount
let extra = ContiguousArray(tracker.instances(for: extraRange))
withHiddenCopies(if: isShared, of: &deque) { deque in
contents.insert(contentsOf: extra, at: 0)
deque.prepend(contentsOf: extra)
expectEqualElements(deque, contents)
}
}
}
}
}
}
func test_prependManyFromContiguousArray_asSequence() {
// This calls the Sequence-based `Deque.prepend` overload, even if
// `elements` happens to be of a Collection type.
func prependSequence<S: Sequence>(
contentsOf elements: S,
to deque: inout Deque<S.Element>
) {
deque.prepend(contentsOf: elements)
}
withEveryDeque("deque", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in
withEvery("prependCount", in: 0 ..< 10) { prependCount in
withEvery("isShared", in: [false, true]) { isShared in
withLifetimeTracking { tracker in
var (deque, contents) = tracker.deque(with: layout)
let extraRange = layout.count ..< layout.count + prependCount
let extra = ContiguousArray(tracker.instances(for: extraRange))
withHiddenCopies(if: isShared, of: &deque) { deque in
contents.insert(contentsOf: extra, at: 0)
prependSequence(contentsOf: extra, to: &deque)
expectEqualElements(deque, contents)
}
}
}
}
}
}
func test_prependManyFromBridgedArray() {
// https://github.com/apple/swift-collections/issues/27
withEveryDeque("deque", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in
withEvery("appendCount", in: 0 ..< 10) { appendCount in
withEvery("isShared", in: [false, true]) { isShared in
var contents: [NSObject] = (0 ..< layout.count).map { _ in NSObject() }
var deque = Deque(layout: layout, contents: contents)
let extra: [NSObject] = (0 ..< appendCount)
.map { _ in NSObject() }
.withUnsafeBufferPointer { buffer in
NSArray(objects: buffer.baseAddress, count: buffer.count) as! [NSObject]
}
withHiddenCopies(if: isShared, of: &deque) { deque in
contents.insert(contentsOf: extra, at: 0)
deque.prepend(contentsOf: extra)
expectEquivalentElements(deque, contents, by: ===)
}
}
}
}
}
}
|