13
13
import StdlibUnittest
14
14
15
15
// A minimal RRC conformance, to test default implementations with
16
- struct NaiveRRC : RangeReplaceableCollection , ExpressibleByArrayLiteral {
16
+ public struct NaiveRRC : RangeReplaceableCollection , ExpressibleByArrayLiteral {
17
17
18
18
// we're trying to move away from calling reserveCapacity inside most mutating
19
19
// methods, this will let us verify that we don't
20
20
internal var allowReserveCapacity = true
21
- var storage : [ UInt8 ] = [ ]
21
+ var storage : [ Int ] = [ ]
22
22
23
- init ( ) { }
23
+ public init ( ) { }
24
24
25
- init ( arrayLiteral elements: Element ... ) {
25
+ public init ( arrayLiteral elements: Element ... ) {
26
26
storage. append ( contentsOf: elements)
27
27
}
28
28
29
- func index( after i: Int ) -> Int {
29
+ public func index( after i: Int ) -> Int {
30
30
i + 1
31
31
}
32
32
33
- func index( before i: Int ) -> Int {
33
+ public func index( before i: Int ) -> Int {
34
34
i - 1
35
35
}
36
36
37
- var startIndex : Int {
37
+ public var startIndex : Int {
38
38
0
39
39
}
40
40
41
- var endIndex : Int {
41
+ public var endIndex : Int {
42
42
count
43
43
}
44
44
45
- var count : Int {
45
+ public var count : Int {
46
46
storage. count
47
47
}
48
48
49
- subscript( position: Int ) -> UInt8 {
49
+ public subscript( position: Int ) -> Int {
50
50
get {
51
51
storage [ position]
52
52
}
@@ -55,11 +55,11 @@ struct NaiveRRC : RangeReplaceableCollection, ExpressibleByArrayLiteral {
55
55
}
56
56
}
57
57
58
- mutating func replaceSubrange( _ subrange: Range < Int > , with newElements: some Collection < UInt8 > ) {
58
+ public mutating func replaceSubrange( _ subrange: Range < Int > , with newElements: some Collection < Int > ) {
59
59
storage. replaceSubrange ( subrange, with: newElements)
60
60
}
61
61
62
- mutating func reserveCapacity( _ n: Int ) {
62
+ public mutating func reserveCapacity( _ n: Int ) {
63
63
precondition ( allowReserveCapacity)
64
64
storage. reserveCapacity ( n)
65
65
}
@@ -81,17 +81,18 @@ internal enum IndexSelection {
81
81
}
82
82
}
83
83
84
- public struct ReplaceSubrangeTest {
84
+ public struct ReplaceSubrangeTest < C : RangeReplaceableCollection > where C . Element == Int {
85
85
public let collection : [ OpaqueValue < Int > ]
86
86
public let newElements : [ OpaqueValue < Int > ]
87
87
public let rangeSelection : RangeSelection
88
- public let expected : [ Int ]
89
- public let closedExpected : [ Int ] ? // Expected array for closed ranges
88
+ public let expected : C
89
+ public let closedExpected : C ? // Expected array for closed ranges
90
90
public let loc : SourceLoc
91
91
92
92
internal init (
93
- collection: [ Int ] , newElements: [ Int ] ,
94
- rangeSelection: RangeSelection , expected: [ Int ] , closedExpected: [ Int ] ? = nil ,
93
+ collection: some RangeReplaceableCollection < Int > ,
94
+ newElements: some Collection < Int > ,
95
+ rangeSelection: RangeSelection , expected: C , closedExpected: C ? = nil ,
95
96
file: String = #file, line: UInt = #line
96
97
) {
97
98
self . collection = collection. map ( OpaqueValue . init)
@@ -103,14 +104,15 @@ public struct ReplaceSubrangeTest {
103
104
}
104
105
}
105
106
106
- internal struct AppendTest {
107
+ internal struct AppendTest < C : RangeReplaceableCollection > where C . Element == Int {
107
108
let collection : [ OpaqueValue < Int > ]
108
109
let newElement : OpaqueValue < Int >
109
- let expected : [ Int ]
110
+ let expected : C
110
111
let loc : SourceLoc
111
112
112
113
internal init (
113
- collection: [ Int ] , newElement: Int , expected: [ Int ] ,
114
+ collection: some RangeReplaceableCollection < Int > ,
115
+ newElement: Int , expected: C ,
114
116
file: String = #file, line: UInt = #line
115
117
) {
116
118
self . collection = collection. map ( OpaqueValue . init)
@@ -120,14 +122,15 @@ internal struct AppendTest {
120
122
}
121
123
}
122
124
123
- internal struct AppendContentsOfTest {
125
+ internal struct AppendContentsOfTest < C : RangeReplaceableCollection > where C . Element == Int {
124
126
let collection : [ OpaqueValue < Int > ]
125
127
let newElements : [ OpaqueValue < Int > ]
126
- let expected : [ Int ]
128
+ let expected : C
127
129
let loc : SourceLoc
128
130
129
131
internal init (
130
- collection: [ Int ] , newElements: [ Int ] , expected: [ Int ] ,
132
+ collection: some RangeReplaceableCollection < Int > ,
133
+ newElements: some RangeReplaceableCollection < Int > , expected: C ,
131
134
file: String = #file, line: UInt = #line
132
135
) {
133
136
self . collection = collection. map ( OpaqueValue . init)
@@ -137,16 +140,17 @@ internal struct AppendContentsOfTest {
137
140
}
138
141
}
139
142
140
- internal struct InsertTest {
143
+ internal struct InsertTest < C : RangeReplaceableCollection > where C . Element == Int {
141
144
let collection : [ OpaqueValue < Int > ]
142
145
let newElement : OpaqueValue < Int >
143
146
let indexSelection : IndexSelection
144
- let expected : [ Int ]
147
+ let expected : C
145
148
let loc : SourceLoc
146
149
147
150
internal init (
148
- collection: [ Int ] , newElement: Int , indexSelection: IndexSelection ,
149
- expected: [ Int ] , file: String = #file, line: UInt = #line
151
+ collection: some RangeReplaceableCollection < Int > ,
152
+ newElement: Int , indexSelection: IndexSelection ,
153
+ expected: C , file: String = #file, line: UInt = #line
150
154
) {
151
155
self . collection = collection. map ( OpaqueValue . init)
152
156
self . newElement = OpaqueValue ( newElement)
@@ -156,16 +160,18 @@ internal struct InsertTest {
156
160
}
157
161
}
158
162
159
- internal struct InsertContentsOfTest {
163
+ internal struct InsertContentsOfTest < C : RangeReplaceableCollection > where C . Element == Int {
160
164
let collection : [ OpaqueValue < Int > ]
161
165
let newElements : [ OpaqueValue < Int > ]
162
166
let indexSelection : IndexSelection
163
- let expected : [ Int ]
167
+ let expected : C
164
168
let loc : SourceLoc
165
169
166
170
internal init (
167
- collection: [ Int ] , newElements: [ Int ] , indexSelection: IndexSelection ,
168
- expected: [ Int ] , file: String = #file, line: UInt = #line
171
+ collection: some RangeReplaceableCollection < Int > ,
172
+ newElements: some RangeReplaceableCollection < Int > ,
173
+ indexSelection: IndexSelection ,
174
+ expected: C , file: String = #file, line: UInt = #line
169
175
) {
170
176
self . collection = collection. map ( OpaqueValue . init)
171
177
self . newElements = newElements. map ( OpaqueValue . init)
@@ -175,17 +181,17 @@ internal struct InsertContentsOfTest {
175
181
}
176
182
}
177
183
178
- internal struct RemoveAtIndexTest {
184
+ internal struct RemoveAtIndexTest < C : RangeReplaceableCollection > where C . Element == Int {
179
185
let collection : [ OpaqueValue < Int > ]
180
186
let indexSelection : IndexSelection
181
187
let expectedRemovedElement : Int
182
- let expectedCollection : [ Int ]
188
+ let expectedCollection : C
183
189
let loc : SourceLoc
184
190
185
191
internal init (
186
- collection: [ Int ] , indexSelection : IndexSelection ,
187
- expectedRemovedElement : Int , expectedCollection : [ Int ] ,
188
- file: String = #file, line: UInt = #line
192
+ collection: some RangeReplaceableCollection < Int > ,
193
+ indexSelection : IndexSelection , expectedRemovedElement : Int ,
194
+ expectedCollection : C , file: String = #file, line: UInt = #line
189
195
) {
190
196
self . collection = collection. map ( OpaqueValue . init)
191
197
self . indexSelection = indexSelection
@@ -195,14 +201,15 @@ internal struct RemoveAtIndexTest {
195
201
}
196
202
}
197
203
198
- internal struct RemoveLastNTest {
204
+ internal struct RemoveLastNTest < C : RangeReplaceableCollection > where C . Element == Int {
199
205
let collection : [ OpaqueValue < Int > ]
200
206
let numberToRemove : Int
201
- let expectedCollection : [ Int ]
207
+ let expectedCollection : C
202
208
let loc : SourceLoc
203
209
204
210
internal init (
205
- collection: [ Int ] , numberToRemove: Int , expectedCollection: [ Int ] ,
211
+ collection: some RangeReplaceableCollection < Int > ,
212
+ numberToRemove: Int , expectedCollection: C ,
206
213
file: String = #file, line: UInt = #line
207
214
) {
208
215
self . collection = collection. map ( OpaqueValue . init)
@@ -212,14 +219,15 @@ internal struct RemoveLastNTest {
212
219
}
213
220
}
214
221
215
- public struct RemoveSubrangeTest {
222
+ public struct RemoveSubrangeTest < C : RangeReplaceableCollection > where C . Element == Int {
216
223
public let collection : [ OpaqueValue < Int > ]
217
224
public let rangeSelection : RangeSelection
218
- public let expected : [ Int ]
225
+ public let expected : C
219
226
public let loc : SourceLoc
220
227
221
228
internal init (
222
- collection: [ Int ] , rangeSelection: RangeSelection , expected: [ Int ] ,
229
+ collection: some RangeReplaceableCollection < Int > ,
230
+ rangeSelection: RangeSelection , expected: C ,
223
231
file: String = #file, line: UInt = #line
224
232
) {
225
233
self . collection = collection. map ( OpaqueValue . init)
@@ -229,13 +237,13 @@ public struct RemoveSubrangeTest {
229
237
}
230
238
}
231
239
232
- internal struct RemoveAllTest {
240
+ internal struct RemoveAllTest < C : RangeReplaceableCollection > where C . Element == Int {
233
241
let collection : [ OpaqueValue < Int > ]
234
- let expected : [ Int ]
242
+ let expected : C
235
243
let loc : SourceLoc
236
244
237
245
internal init (
238
- collection: [ Int ] , expected: [ Int ] ,
246
+ collection: some RangeReplaceableCollection < Int > , expected: C ,
239
247
file: String = #file, line: UInt = #line
240
248
) {
241
249
self . collection = collection. map ( OpaqueValue . init)
@@ -250,7 +258,7 @@ internal struct ReserveCapacityTest {
250
258
let loc : SourceLoc
251
259
252
260
internal init (
253
- collection: [ Int ] , requestedCapacity: Int ,
261
+ collection: some RangeReplaceableCollection < Int > , requestedCapacity: Int ,
254
262
file: String = #file, line: UInt = #line
255
263
) {
256
264
self . collection = collection. map ( OpaqueValue . init)
@@ -259,14 +267,16 @@ internal struct ReserveCapacityTest {
259
267
}
260
268
}
261
269
262
- internal struct OperatorPlusTest {
270
+ internal struct OperatorPlusTest < C : RangeReplaceableCollection > where C . Element == Int {
263
271
let lhs : [ OpaqueValue < Int > ]
264
272
let rhs : [ OpaqueValue < Int > ]
265
- let expected : [ Int ]
273
+ let expected : C
266
274
let loc : SourceLoc
267
275
268
276
internal init (
269
- lhs: [ Int ] , rhs: [ Int ] , expected: [ Int ] ,
277
+ lhs: some RangeReplaceableCollection < Int > ,
278
+ rhs: some RangeReplaceableCollection < Int > ,
279
+ expected: C ,
270
280
file: String = #file, line: UInt = #line
271
281
) {
272
282
self . lhs = lhs. map ( OpaqueValue . init)
@@ -603,44 +613,44 @@ public let removeRangeTests: [RemoveSubrangeTest] = [
603
613
expected: [ 1010 , 6060 ] ) ,
604
614
605
615
RemoveSubrangeTest (
606
- collection: [ ] as NaiveRRC ) ,
616
+ collection: [ ] as NaiveRRC ,
607
617
rangeSelection: . emptyRange,
608
- expected : [ ] ) as NaiveRRC ) ,
618
+ expected: [ ] as NaiveRRC ) ,
609
619
610
620
RemoveSubrangeTest (
611
- collection: [ 1010 ] as NaiveRRC ) ,
621
+ collection: [ 1010 ] as NaiveRRC ,
612
622
rangeSelection: . middle,
613
- expected : [ ] as NaiveRRC ) ) ,
623
+ expected: [ ] as NaiveRRC ) ,
614
624
615
625
RemoveSubrangeTest (
616
- collection: [ 1010 , 2020 , 3030 , 4040 ] as NaiveRRC ) ,
626
+ collection: [ 1010 , 2020 , 3030 , 4040 ] as NaiveRRC ,
617
627
rangeSelection: . leftHalf,
618
- expected : [ 3030 , 4040 ] as NaiveRRC ) ) ,
628
+ expected: [ 3030 , 4040 ] as NaiveRRC ) ,
619
629
620
630
RemoveSubrangeTest (
621
- collection: [ 1010 , 2020 , 3030 , 4040 ] as NaiveRRC ) ,
631
+ collection: [ 1010 , 2020 , 3030 , 4040 ] as NaiveRRC ,
622
632
rangeSelection: . rightHalf,
623
- expected : [ 1010 , 2020 ] as NaiveRRC ) ) ,
633
+ expected: [ 1010 , 2020 ] as NaiveRRC ) ,
624
634
625
635
RemoveSubrangeTest (
626
- collection: [ 1010 , 2020 , 3030 , 4040 , 5050 ] as NaiveRRC ) ,
636
+ collection: [ 1010 , 2020 , 3030 , 4040 , 5050 ] as NaiveRRC ,
627
637
rangeSelection: . middle,
628
- expected : [ 1010 , 5050 ] as NaiveRRC ) ) ,
638
+ expected: [ 1010 , 5050 ] as NaiveRRC ) ,
629
639
630
640
RemoveSubrangeTest (
631
- collection: [ 1010 , 2020 , 3030 , 4040 , 5050 , 6060 ] as NaiveRRC ) ,
641
+ collection: [ 1010 , 2020 , 3030 , 4040 , 5050 , 6060 ] as NaiveRRC ,
632
642
rangeSelection: . leftHalf,
633
- expected : [ 4040 , 5050 , 6060 ] as NaiveRRC ) ) ,
643
+ expected: [ 4040 , 5050 , 6060 ] as NaiveRRC ) ,
634
644
635
645
RemoveSubrangeTest (
636
- collection: [ 1010 , 2020 , 3030 , 4040 , 5050 , 6060 ] as NaiveRRC ) ,
646
+ collection: [ 1010 , 2020 , 3030 , 4040 , 5050 , 6060 ] as NaiveRRC ,
637
647
rangeSelection: . rightHalf,
638
- expected : [ 1010 , 2020 , 3030 ] as NaiveRRC ) ) ,
648
+ expected: [ 1010 , 2020 , 3030 ] as NaiveRRC ) ,
639
649
640
650
RemoveSubrangeTest (
641
- collection: [ 1010 , 2020 , 3030 , 4040 , 5050 , 6060 ] as NaiveRRC ) ,
651
+ collection: [ 1010 , 2020 , 3030 , 4040 , 5050 , 6060 ] as NaiveRRC ,
642
652
rangeSelection: . middle,
643
- expected : [ 1010 , 6060 ] as NaiveRRC ) ) ,
653
+ expected: [ 1010 , 6060 ] as NaiveRRC ) ,
644
654
]
645
655
646
656
extension TestSuite {
0 commit comments