5
5
// RUN: %S/../../utils/line-directive %t/Slice.swift -- %target-run %t/a.out
6
6
// REQUIRES: executable_test
7
7
8
+ % {
9
+ from gyb_stdlib_support import (
10
+ TRAVERSALS,
11
+ sliceTypeName,
12
+ defaultIndicesForTraversal
13
+ )
14
+ } %
15
+
8
16
import StdlibUnittest
9
17
import StdlibCollectionUnittest
10
18
import SwiftPrivate
@@ -22,109 +30,94 @@ var SliceTests = TestSuite("Collection")
22
30
// Slice<Base>
23
31
//===----------------------------------------------------------------------===//
24
32
25
- SliceTests . test ( " Slice/AssociatedTypes " ) {
26
- % for traversal in [ 'Forward', 'Bidirectional', 'RandomAccess' ] :
27
- do {
28
- typealias Base = Minimal ${ traversal} Collection< OpaqueValue< Int>>
29
- typealias ${ traversal} Slice = Slice< Base>
30
- expectSliceType ( ${ traversal} Slice. self)
33
+ % for Traversal in TRAVERSALS:
34
+ % for Mutable in [ False, True ] :
35
+ % for RangeReplaceable in [ False, True ] :
36
+ % Slice = sliceTypeName ( traversal= Traversal, mutable= Mutable, rangeReplaceable= RangeReplaceable)
37
+ % Collection = 'Minimal' + Slice. replace ( 'Slice', 'Collection')
38
+
39
+ SliceTests. test ( " ${Slice}/AssociatedTypes " ) {
40
+ do {
41
+ typealias Collection = ${ Collection} < OpaqueValue< Int>>
42
+ typealias CollectionSlice = ${ Slice} < Collection>
43
+ expectSliceType ( CollectionSlice . self)
31
44
expectCollectionAssociatedTypes (
32
- collectionType: ${ traversal} Slice. self,
33
- iteratorType: IndexingIterator < ${ traversal} Slice> . self ,
34
- subSequenceType: ${ traversal} Slice. self,
35
- indexType: Minimal ${ traversal} Index. self)
45
+ collectionType: CollectionSlice . self,
46
+ iteratorType: IndexingIterator< CollectionSlice> . self ,
47
+ subSequenceType: CollectionSlice . self,
48
+ indexType: MinimalIndex . self,
49
+ indexDistanceType: Int . self,
50
+ indicesType: ${ defaultIndicesForTraversal ( Traversal) } < CollectionSlice> . self )
36
51
}
37
- % end
38
52
39
- func checkStaticTypealiases < Base : Collection > ( _: Base ) {
53
+ func checkStaticTypealiases1 < Base : Collection > ( _: Base ) {
40
54
expectEqualType ( Base . Index. self, Slice < Base > . Index. self)
41
55
}
56
+
57
+ func checkStaticTypealiases2<
58
+ Base : MutableCollection
59
+ > ( _: Base ) {
60
+ expectEqualType ( Base . Index. self, MutableSlice < Base > . Index. self)
61
+ }
42
62
}
43
63
44
- SliceTests . test ( " Slice/init(_base :bounds:) " ) {
64
+ SliceTests. test ( " ${ Slice} /init(base :bounds:)" ) {
45
65
for test in subscriptRangeTests {
46
- let base = MinimalForwardCollection ( elements: test. collection)
47
- var result = Slice ( base: base, bounds: test. boundsIn ( base) )
48
- expectType (
49
- Slice< MinimalForwardCollection< OpaqueValue< Int>>> . self ,
50
- & result)
51
-
52
- checkForwardCollection (
53
- test. expected,
54
- result,
66
+ let base = ${ Collection} ( elements: test. collection)
67
+ var slice = ${ Slice} ( base: base, bounds: test. bounds ( in: base) )
68
+ expectType ( ${ Slice} < ${ Collection} < OpaqueValue< Int>>> . self , & slice)
69
+
70
+ checkCollection (
71
+ slice,
72
+ expected: test. expected,
55
73
stackTrace: SourceLocStack ( ) . with ( test. loc) )
56
74
{ $0. value == $1. value }
57
75
}
58
76
}
59
77
60
- SliceTests . test ( " Slice.{startIndex,endIndex} " ) {
78
+ SliceTests. test ( " ${ Slice} .{startIndex,endIndex}" ) {
61
79
for test in subscriptRangeTests {
62
- let base = MinimalForwardCollection ( elements: test. collection)
63
- let bounds = test. boundsIn ( base)
64
- var slice = Slice ( base: base, bounds: bounds)
65
- expectType (
66
- Slice< MinimalForwardCollection< OpaqueValue< Int>>> . self ,
67
- & slice)
80
+ let c = ${ Collection} ( elements: test. collection)
81
+ let bounds = test. bounds ( in: c)
82
+ var slice = ${ Slice} ( base: c, bounds: bounds)
83
+ expectType ( ${ Slice} < ${ Collection} < OpaqueValue< Int>>> . self , & slice)
68
84
69
85
expectEqual ( bounds. lowerBound, slice. startIndex)
70
86
expectEqual ( bounds. upperBound, slice. endIndex)
71
87
}
72
88
}
73
89
74
- SliceTests . test ( " Slice.subscript(_: Index) " ) {
75
- for test in subscriptRangeTests {
76
- typealias Base = MinimalForwardCollection < OpaqueValue < Int > >
77
- Base . Index. trapOnRangeCheckFailure. value = false
78
- let base = Base ( elements: test. collection)
79
- let bounds = test. boundsIn ( base)
80
- var slice = Slice ( base: base, bounds: bounds)
81
- expectType ( Slice< Base> . self , & slice)
82
-
83
- for i in bounds {
84
- var element = slice [ i]
85
- expectType ( OpaqueValue< Int> . self , & element)
86
- expectEqual ( base [ i] . value, element. value)
87
- }
88
-
89
- for (i, index) in base. indices. enumerated ( ) {
90
- if test. bounds. contains ( i) {
91
- expectEqual ( base [ index] . value, slice [ index] . value)
92
- } else {
93
- // `Slice` disallows out-of-bounds indices when the underlying index
94
- // type can perform a range check.
95
- expectFailure {
96
- _blackHole ( slice [ index] )
97
- }
98
- }
99
- }
100
- }
101
- }
90
+ % end
91
+ % end
92
+ % end
102
93
103
94
SliceTests. test( " Slice.subscript(_: Range<Index>) " ) {
104
95
for test in subscriptRangeTests {
105
- typealias Base = MinimalForwardCollection < OpaqueValue < Int > >
96
+ typealias Base = MinimalCollection < OpaqueValue < Int > >
106
97
Base . Index. trapOnRangeCheckFailure. value = false
107
98
let base = Base ( elements: test. collection)
108
- var slice = Slice ( base: base, bounds: base. indices )
99
+ var slice = Slice ( base: base, bounds: base. startIndex ..< base . endIndex )
109
100
expectType ( Slice< Base> . self , & slice)
110
101
111
- var result = slice [ test. boundsIn ( slice) ]
102
+ var result = slice [ test. bounds ( in : slice) ]
112
103
expectType ( Slice< Base> . self , & result)
113
104
114
- checkForwardCollection ( test. expected, result) { $0. value == $1. value }
105
+ checkCollection ( result, expected: test. expected) {
106
+ $0. value == $1. value
107
+ }
115
108
116
- if test. bounds == test. collection. indices {
117
- let reSliced = result [ base. indices]
118
- checkForwardCollection (
119
- test. collection,
109
+ if test. bounds == test. collection. startIndex..< test. collection. endIndex {
110
+ let reSliced = result [ base. startIndex..< base. endIndex]
111
+ checkCollection (
120
112
reSliced,
113
+ expected: test. collection,
121
114
stackTrace: SourceLocStack ( ) . with ( test. loc) )
122
115
{ $0. value == $1. value }
123
116
} else {
124
117
// `Slice` disallows out-of-bounds slicing when the underlying index type
125
118
// can perform a range check.
126
119
expectFailure {
127
- _blackHole ( result [ base. indices ] )
120
+ _blackHole ( result [ base. startIndex ..< base . endIndex ] )
128
121
}
129
122
}
130
123
}
@@ -135,28 +128,6 @@ SliceTests.test("Slice.subscript(_: Range<Index>)") {
135
128
// MutableSlice<Base>
136
129
//===----------------------------------------------------------------------===//
137
130
138
- SliceTests . test ( " MutableSlice/AssociatedTypes " ) {
139
- % for traversal in [ 'Forward', 'Bidirectional', 'RandomAccess' ] :
140
- do {
141
- typealias Base =
142
- Minimal ${ traversal} MutableCollection< OpaqueValue< Int>>
143
- typealias ${ traversal} MutableSlice = MutableSlice< Base>
144
- expectMutableSliceType ( ${ traversal} MutableSlice. self)
145
- expectCollectionAssociatedTypes (
146
- collectionType: ${ traversal} MutableSlice. self,
147
- iteratorType: IndexingIterator < ${ traversal} MutableSlice> . self ,
148
- subSequenceType: ${ traversal} MutableSlice. self,
149
- indexType: Minimal ${ traversal} Index. self)
150
- }
151
- % end
152
-
153
- func checkStaticTypealiases<
154
- Base : MutableCollection
155
- > ( _: Base ) {
156
- expectEqualType ( Base . Index. self, MutableSlice < Base > . Index. self)
157
- }
158
- }
159
-
160
131
/*
161
132
FIXME: uncomment this test when the following bug is fixed:
162
133
@@ -179,46 +150,16 @@ extension MutableSlice {
179
150
}
180
151
*/
181
152
182
- SliceTests . test ( " MutableSlice/init(_base:bounds:) " ) {
183
- for test in subscriptRangeTests {
184
- let c = MinimalForwardMutableCollection ( elements: test. collection)
185
- var result = MutableSlice ( base: c, bounds: test. boundsIn ( c) )
186
- expectType (
187
- MutableSlice< MinimalForwardMutableCollection< OpaqueValue< Int>>> . self ,
188
- & result)
189
-
190
- checkForwardCollection (
191
- test. expected,
192
- result,
193
- stackTrace: SourceLocStack ( ) . with ( test. loc) )
194
- { $0. value == $1. value }
195
- }
196
- }
197
-
198
- SliceTests . test ( " MutableSlice.{startIndex,endIndex} " ) {
199
- for test in subscriptRangeTests {
200
- let c = MinimalForwardMutableCollection ( elements: test. collection)
201
- let bounds = test. boundsIn ( c)
202
- var slice = MutableSlice ( base: c, bounds: bounds)
203
- expectType (
204
- MutableSlice< MinimalForwardMutableCollection< OpaqueValue< Int>>> . self ,
205
- & slice)
206
-
207
- expectEqual ( bounds. lowerBound, slice. startIndex)
208
- expectEqual ( bounds. upperBound, slice. endIndex)
209
- }
210
- }
211
-
212
153
SliceTests. test ( " MutableSlice.subscript(_: Index)/{get,set} " ) {
213
154
for test in subscriptRangeTests {
214
- typealias Base = MinimalForwardMutableCollection < OpaqueValue < Int > >
155
+ typealias Base = MinimalMutableCollection < OpaqueValue < Int > >
215
156
Base . Index. trapOnRangeCheckFailure. value = false
216
157
let base = Base ( elements: test. collection)
217
- let bounds = test. boundsIn ( base)
158
+ let bounds = test. bounds ( in : base)
218
159
var slice = MutableSlice ( base: base, bounds: bounds)
219
160
expectType ( MutableSlice< Base> . self , & slice)
220
161
221
- for i in bounds {
162
+ for i in base [ bounds] . indices {
222
163
// Test getter.
223
164
var element = slice [ i]
224
165
expectType ( OpaqueValue< Int> . self , & element)
@@ -227,7 +168,7 @@ SliceTests.test("MutableSlice.subscript(_: Index)/{get,set}") {
227
168
228
169
do {
229
170
var sliceForSetter = slice
230
- for i in bounds {
171
+ for i in base [ bounds] . indices {
231
172
// Test setter.
232
173
sliceForSetter [ i] . value += 1
233
174
expectEqual ( base [ i] . value + 1 , sliceForSetter [ i] . value)
0 commit comments