-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathExistentialCollection.swift.gyb
417 lines (358 loc) · 11.7 KB
/
ExistentialCollection.swift.gyb
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//===--- ExistentialCollection.swift.gyb ----------------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// RUN: %target-run-simple-swiftgyb
// REQUIRES: executable_test
// With a non-optimized stdlib the test takes very long.
// REQUIRES: optimized_stdlib
%{
from gyb_stdlib_support import (
TRAVERSALS,
collectionForTraversal
)
}%
import StdlibUnittest
import StdlibCollectionUnittest
// Check that the generic parameter is called 'Element'.
protocol TestProtocol1 {}
extension AnyIterator where Element : TestProtocol1 {
var _elementIsTestProtocol1: Bool {
fatalError("not implemented")
}
}
extension AnySequence where Element : TestProtocol1 {
var _elementIsTestProtocol1: Bool {
fatalError("not implemented")
}
}
extension AnyCollection where Element : TestProtocol1 {
var _elementIsTestProtocol1: Bool {
fatalError("not implemented")
}
}
extension AnyBidirectionalCollection where Element : TestProtocol1 {
var _elementIsTestProtocol1: Bool {
fatalError("not implemented")
}
}
extension AnyRandomAccessCollection where Element : TestProtocol1 {
var _elementIsTestProtocol1: Bool {
fatalError("not implemented")
}
}
func storesSameUnderlyingCollection<
L: _AnyCollectionProtocol, R: _AnyCollectionProtocol
>(_ lhs: L, _ rhs: R) -> Bool {
return lhs._boxID == rhs._boxID
}
var tests = TestSuite("ExistentialCollection")
tests.test("AnyIterator") {
func digits() -> AnyIterator<OpaqueValue<Int>> {
let integers: CountableRange = 0..<5
let lazyStrings = integers.lazy.map { OpaqueValue($0) }
// This is a really complicated type of no interest to our
// clients.
let iterator: LazyMapSequence<
CountableRange<Int>, OpaqueValue<Int>
>.Iterator = lazyStrings.makeIterator()
return AnyIterator(iterator)
}
expectEqual([0, 1, 2, 3, 4], Array(digits()).map { $0.value })
var x = 7
let iterator = AnyIterator<Int> {
if x >= 15 { return nil }
x += 1
return x-1
}
expectEqual([ 7, 8, 9, 10, 11, 12, 13, 14 ], Array(iterator))
}
tests.test("AnySequence.init(Sequence)") {
do {
let base = MinimalSequence<OpaqueValue<Int>>(elements: [])
var s = AnySequence(base)
expectType(AnySequence<OpaqueValue<Int>>.self, &s)
checkSequence([], s, resiliencyChecks: .none) { $0.value == $1.value }
}
do {
let intData = [ 1, 2, 3, 5, 8, 13, 21 ]
let data = intData.map(OpaqueValue.init)
let base = MinimalSequence(elements: data)
var s = AnySequence(base)
expectType(AnySequence<OpaqueValue<Int>>.self, &s)
checkSequence(data, s, resiliencyChecks: .none) { $0.value == $1.value }
}
}
tests.test("AnySequence.init(() -> Generator)") {
do {
var s = AnySequence {
return MinimalIterator<OpaqueValue<Int>>([])
}
expectType(AnySequence<OpaqueValue<Int>>.self, &s)
checkSequence([], s, resiliencyChecks: .none) { $0.value == $1.value }
}
do {
let intData = [ 1, 2, 3, 5, 8, 13, 21 ]
let data = intData.map(OpaqueValue.init)
var s = AnySequence {
return MinimalIterator(data)
}
expectType(AnySequence<OpaqueValue<Int>>.self, &s)
checkSequence(data, s, resiliencyChecks: .none) { $0.value == $1.value }
}
}
% for Traversal in ['Sequence'] + TRAVERSALS:
% if Traversal == 'Sequence':
% TestedType = 'AnySequence'
% LoggingWrapper = 'LoggingSequence'
% MinimalBase = 'MinimalSequence'
% else:
% TestedType = 'Any' + collectionForTraversal(Traversal)
% LoggingWrapper = 'Logging' + collectionForTraversal(Traversal)
% MinimalBase = 'Minimal' + collectionForTraversal(Traversal)
% end
tests.test("${TestedType}: dispatch to wrapped, SequenceLog") {
typealias Base = ${LoggingWrapper}<${MinimalBase}<OpaqueValue<Int>>>
typealias Log = SequenceLog
let base = Base(wrapping:
${MinimalBase}(elements: (0..<10).map(OpaqueValue.init)))
var s = ${TestedType}(base)
expectType(${TestedType}<OpaqueValue<Int>>.self, &s)
Log.makeIterator.expectIncrement(Base.self) {
_ = s.makeIterator()
}
Log.underestimatedCount.expectIncrement(Base.self) {
_ = s.underestimatedCount
}
Log._customContainsEquatableElement.expectIncrement(Base.self) {
_ = s._customContainsEquatableElement(OpaqueValue(42))
}
Log._copyToContiguousArray.expectIncrement(Base.self) {
_ = s._copyToContiguousArray()
}
do {
var result = Array(repeating: OpaqueValue(0), count: 10)
Log._copyContents.expectIncrement(Base.self) {
result.withUnsafeMutableBufferPointer {
_ = s._copyContents(initializing: $0)
}
}
}
}
% if Traversal != 'Sequence':
tests.test("${TestedType}: dispatch to wrapped, CollectionLog") {
typealias Base = ${LoggingWrapper}<${MinimalBase}<OpaqueValue<Int>>>
typealias Log = CollectionLog
let base = Base(wrapping:
${MinimalBase}(elements: (0..<10).map(OpaqueValue.init)))
var c = ${TestedType}(base)
expectType(${TestedType}<OpaqueValue<Int>>.self, &c)
do {
var i = c.startIndex
// `startIndex` is cached in the type erased wrapper, so first mutation
// makes a unique copy.
Log.successor.expectIncrement(Base.self) {
Log.formSuccessor.expectUnchanged(Base.self) {
c.formIndex(after: &i)
}
}
Log.successor.expectUnchanged(Base.self) {
Log.formSuccessor.expectIncrement(Base.self) {
c.formIndex(after: &i)
}
}
}
% if Traversal in ['Bidirectional', 'RandomAccess']:
do {
typealias Log = BidirectionalCollectionLog
var i = c.endIndex
// `startIndex` is cached in the type erased wrapper, so first mutation
// makes a unique copy.
Log.predecessor.expectIncrement(Base.self) {
Log.formPredecessor.expectUnchanged(Base.self) {
c.formIndex(before: &i)
}
}
Log.predecessor.expectUnchanged(Base.self) {
Log.formPredecessor.expectIncrement(Base.self) {
c.formIndex(before: &i)
}
}
}
% end
do {
let startIndex = c.startIndex
var badBounds = c.index(c.startIndex, offsetBy: 3)..<c.endIndex
Log.subscriptIndex.expectIncrement(Base.self) {
Log.subscriptRange.expectUnchanged(Base.self) {
_ = c[startIndex]
}
}
Log.subscriptIndex.expectUnchanged(Base.self) {
Log.subscriptRange.expectIncrement(Base.self) {
_ = c[startIndex..<startIndex]
}
}
Log._failEarlyRangeCheckIndex.expectUnchanged(Base.self) {
Log._failEarlyRangeCheckRange.expectUnchanged(Base.self) {
// Early range checks are not forwarded for performance reasons.
_ = c._failEarlyRangeCheck(startIndex, bounds: badBounds)
_ = c._failEarlyRangeCheck(startIndex..<startIndex, bounds: badBounds)
}
}
}
do {
var i = c.startIndex
Log.successor.expectIncrement(Base.self) {
Log.formSuccessor.expectUnchanged(Base.self) {
i = c.index(after: i)
}
}
Log.successor.expectUnchanged(Base.self) {
Log.formSuccessor.expectIncrement(Base.self) {
c.formIndex(after: &i)
}
}
var x = i
Log.successor.expectIncrement(Base.self) {
Log.formSuccessor.expectUnchanged(Base.self) {
i = c.index(after: i)
}
}
_blackHole(x)
}
% if Traversal in ['Bidirectional', 'RandomAccess']:
do {
typealias Log = BidirectionalCollectionLog
var i = c.endIndex
Log.predecessor.expectIncrement(Base.self) {
Log.formPredecessor.expectUnchanged(Base.self) {
i = c.index(before: i)
}
}
Log.predecessor.expectUnchanged(Base.self) {
Log.formPredecessor.expectIncrement(Base.self) {
c.formIndex(before: &i)
}
}
var x = i
Log.predecessor.expectIncrement(Base.self) {
Log.formPredecessor.expectUnchanged(Base.self) {
i = c.index(before: i)
}
}
_blackHole(x)
}
% end
}
% end
% end
tests.test("ForwardCollection") {
let a0: ContiguousArray = [1, 2, 3, 5, 8, 13, 21]
let fc0 = AnyCollection(a0)
let a1 = ContiguousArray(fc0)
expectEqual(a0, a1)
for e in a0 {
let i = fc0.firstIndex(of: e)
expectNotNil(i)
expectEqual(e, fc0[i!])
}
for i in fc0.indices {
expectNotEqual(fc0.endIndex, i)
expectEqual(1, fc0.indices.filter { $0 == i }.count)
}
}
tests.test("BidirectionalCollection") {
let a0: ContiguousArray = [1, 2, 3, 5, 8, 13, 21]
let fc0 = AnyCollection(a0.lazy.reversed())
let bc0_ = AnyBidirectionalCollection(fc0) // upgrade!
expectNotNil(bc0_)
let bc0 = bc0_!
expectTrue(storesSameUnderlyingCollection(fc0, bc0))
let fc1 = AnyCollection(a0.lazy.reversed()) // new collection
expectFalse(storesSameUnderlyingCollection(fc1, fc0))
let fc2 = AnyCollection(bc0) // downgrade
expectTrue(storesSameUnderlyingCollection(fc2, bc0))
let a1 = ContiguousArray(bc0.lazy.reversed())
expectEqual(a0, a1)
for e in a0 {
let i = bc0.firstIndex(of: e)
expectNotNil(i)
expectEqual(e, bc0[i!])
}
for i in bc0.indices {
expectNotEqual(bc0.endIndex, i)
expectEqual(1, bc0.indices.filter { $0 == i }.count)
}
// Can't upgrade a non-random-access collection to random access
let s0 = "Hello, World"
let bc1 = AnyBidirectionalCollection(s0)
let fc3 = AnyCollection(bc1)
expectTrue(storesSameUnderlyingCollection(fc3, bc1))
expectNil(AnyRandomAccessCollection(bc1))
expectNil(AnyRandomAccessCollection(fc3))
}
tests.test("RandomAccessCollection") {
let a0: ContiguousArray = [1, 2, 3, 5, 8, 13, 21]
let fc0 = AnyCollection(a0.lazy.reversed())
let rc0_ = AnyRandomAccessCollection(fc0) // upgrade!
expectNotNil(rc0_)
let rc0 = rc0_!
expectTrue(storesSameUnderlyingCollection(rc0, fc0))
let bc1 = AnyBidirectionalCollection(rc0) // downgrade
expectTrue(storesSameUnderlyingCollection(bc1, rc0))
let fc1 = AnyBidirectionalCollection(rc0) // downgrade
expectTrue(storesSameUnderlyingCollection(fc1, rc0))
let a1 = ContiguousArray(rc0.lazy.reversed())
expectEqual(a0, a1)
for e in a0 {
let i = rc0.firstIndex(of: e)
expectNotNil(i)
expectEqual(e, rc0[i!])
}
for i in rc0.indices {
expectNotEqual(rc0.endIndex, i)
expectEqual(1, rc0.indices.filter { $0 == i }.count)
}
}
% for Traversal in TRAVERSALS:
% AnyCollection = 'Any' + collectionForTraversal(Traversal)
% Base = 'Minimal' + collectionForTraversal(Traversal)
do {
var AnyCollectionTests = TestSuite("${AnyCollection}")
func makeCollection(elements: [OpaqueValue<Int>])
-> ${AnyCollection}<OpaqueValue<Int>> {
let base = ${Base}(elements: elements)
return ${AnyCollection}(base)
}
func makeCollectionOfEquatable(elements: [MinimalEquatableValue])
-> ${AnyCollection}<MinimalEquatableValue> {
let base = ${Base}(elements: elements)
return ${AnyCollection}(base)
}
func makeCollectionOfComparable(elements: [MinimalComparableValue])
-> ${AnyCollection}<MinimalComparableValue> {
let base = ${Base}(elements: elements)
return ${AnyCollection}(base)
}
AnyCollectionTests.add${collectionForTraversal(Traversal)}Tests(
"tests.",
makeCollection: makeCollection,
wrapValue: identity,
extractValue: identity,
makeCollectionOfEquatable: makeCollectionOfEquatable,
wrapValueIntoEquatable: identityEq,
extractValueFromEquatable: identityEq,
resiliencyChecks: CollectionMisuseResiliencyChecks.all,
collectionIsBidirectional: ${'false' if Traversal == 'Forward' else 'true'}
)
}
% end
runAllTests()