-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathSlice.swift.gyb
208 lines (176 loc) · 6.25 KB
/
Slice.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
// -*- swift -*-
// RUN: rm -rf %t ; mkdir -p %t
// RUN: %S/../../utils/gyb %s -o %t/Slice.swift
// RUN: %S/../../utils/line-directive %t/Slice.swift -- %target-build-swift %t/Slice.swift -o %t/a.out
// RUN: %S/../../utils/line-directive %t/Slice.swift -- %target-run %t/a.out
// REQUIRES: executable_test
%{
from gyb_stdlib_support import (
TRAVERSALS,
sliceTypeName,
defaultIndicesForTraversal
)
}%
import StdlibUnittest
import StdlibCollectionUnittest
import SwiftPrivate
// Also import modules which are used by StdlibUnittest internally. This
// workaround is needed to link all required libraries in case we compile
// StdlibUnittest with -sil-serialize-all.
#if _runtime(_ObjC)
import ObjectiveC
#endif
var SliceTests = TestSuite("Collection")
//===----------------------------------------------------------------------===//
// Slice<Base>
//===----------------------------------------------------------------------===//
% for Traversal in TRAVERSALS:
% for Mutable in [ False, True ]:
% for RangeReplaceable in [ False, True ]:
% Slice = sliceTypeName(traversal=Traversal, mutable=Mutable, rangeReplaceable=RangeReplaceable)
% Collection = 'Minimal' + Slice.replace('Slice', 'Collection')
SliceTests.test("${Slice}/AssociatedTypes") {
do {
typealias Collection = ${Collection}<OpaqueValue<Int>>
typealias CollectionSlice = ${Slice}<Collection>
expectSliceType(CollectionSlice.self)
expectCollectionAssociatedTypes(
collectionType: CollectionSlice.self,
iteratorType: IndexingIterator<CollectionSlice>.self,
subSequenceType: CollectionSlice.self,
indexType: MinimalIndex.self,
indexDistanceType: Int.self,
indicesType: ${defaultIndicesForTraversal(Traversal)}<CollectionSlice>.self)
}
func checkStaticTypealiases1<Base : Collection>(_: Base) {
expectEqualType(Base.Index.self, Slice<Base>.Index.self)
}
func checkStaticTypealiases2<
Base : MutableCollection
>(_: Base) {
expectEqualType(Base.Index.self, MutableSlice<Base>.Index.self)
}
}
SliceTests.test("${Slice}/init(base:bounds:)") {
for test in subscriptRangeTests {
let base = ${Collection}(elements: test.collection)
var slice = ${Slice}(base: base, bounds: test.bounds(in: base))
expectType(${Slice}<${Collection}<OpaqueValue<Int>>>.self, &slice)
checkCollection(
slice,
expected: test.expected,
stackTrace: SourceLocStack().with(test.loc))
{ $0.value == $1.value }
}
}
SliceTests.test("${Slice}.{startIndex,endIndex}") {
for test in subscriptRangeTests {
let c = ${Collection}(elements: test.collection)
let bounds = test.bounds(in: c)
var slice = ${Slice}(base: c, bounds: bounds)
expectType(${Slice}<${Collection}<OpaqueValue<Int>>>.self, &slice)
expectEqual(bounds.lowerBound, slice.startIndex)
expectEqual(bounds.upperBound, slice.endIndex)
}
}
% end
% end
% end
SliceTests.test("Slice.subscript(_: Range<Index>)") {
for test in subscriptRangeTests {
typealias Base = MinimalCollection<OpaqueValue<Int>>
Base.Index.trapOnRangeCheckFailure.value = false
let base = Base(elements: test.collection)
var slice = Slice(base: base, bounds: base.startIndex..<base.endIndex)
expectType(Slice<Base>.self, &slice)
var result = slice[test.bounds(in: slice)]
expectType(Slice<Base>.self, &result)
checkCollection(result, expected: test.expected) {
$0.value == $1.value
}
if test.bounds == test.collection.startIndex..<test.collection.endIndex {
let reSliced = result[base.startIndex..<base.endIndex]
checkCollection(
reSliced,
expected: test.collection,
stackTrace: SourceLocStack().with(test.loc))
{ $0.value == $1.value }
} else {
// `Slice` disallows out-of-bounds slicing when the underlying index type
// can perform a range check.
expectFailure {
_blackHole(result[base.startIndex..<base.endIndex])
}
}
}
}
//===----------------------------------------------------------------------===//
// MutableSlice<Base>
//===----------------------------------------------------------------------===//
/*
FIXME: uncomment this test when the following bug is fixed:
<rdar://problem/21935030> Recast Slice and MutableSlice in terms of Collection
and MutableCollection
extension MutableSlice {
func _checkBaseSubSequenceElementIsElement() {
Element.self,
Iterator.Element.self)
expectEqualType(
Element.self,
Iterator.Element.self,
Base.Iterator.Element.self)
expectEqualType(
Element.self,
Iterator.Element.self,
Base.SubSequence.Iterator.Element.self)
}
}
*/
SliceTests.test("MutableSlice.subscript(_: Index)/{get,set}") {
for test in subscriptRangeTests {
typealias Base = MinimalMutableCollection<OpaqueValue<Int>>
Base.Index.trapOnRangeCheckFailure.value = false
let base = Base(elements: test.collection)
let bounds = test.bounds(in: base)
var slice = MutableSlice(base: base, bounds: bounds)
expectType(MutableSlice<Base>.self, &slice)
for i in base[bounds].indices {
// Test getter.
var element = slice[i]
expectType(OpaqueValue<Int>.self, &element)
expectEqual(base[i].value, element.value)
}
do {
var sliceForSetter = slice
for i in base[bounds].indices {
// Test setter.
sliceForSetter[i].value += 1
expectEqual(base[i].value + 1, sliceForSetter[i].value)
}
}
var sliceForSetter = slice
for (i, index) in base.indices.enumerated() {
if test.bounds.contains(i) {
// Test getter.
expectEqual(base[index].value, slice[index].value)
// Test setter.
sliceForSetter[index].value += 1
expectEqual(base[index].value + 1, sliceForSetter[index].value)
} else {
// `MutableSlice` disallows out-of-bounds indices when the underlying
// index type can perform a range check.
// Test getter.
expectFailure {
_blackHole(slice[index])
}
// Test setter.
expectFailure {
sliceForSetter[index].value += 1
}
}
}
// Check that none of the mutations above affected the original collection.
expectEqualSequence(test.collection, base) { $0.value == $1.value }
}
}
runAllTests()