-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathRange.swift.gyb
430 lines (371 loc) · 11.9 KB
/
Range.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
418
419
420
421
422
423
424
425
426
427
428
429
// RUN: rm -rf %t && mkdir -p %t && %S/../../utils/gyb %s -o %t/out.swift
// RUN: %S/../../utils/line-directive %t/out.swift -- %target-build-swift %t/out.swift -o %t/a.out
// RUN: %S/../../utils/line-directive %t/out.swift -- %target-run %t/a.out
// REQUIRES: executable_test
import StdlibUnittest
import StdlibCollectionUnittest
protocol TestProtocol1 {}
struct ContainsTest {
let lowerBound: Int
let upperBound: Int
let value: Int
let loc: SourceLoc
var containedInHalfOpen: Bool {
return lowerBound <= value && value < upperBound
}
var containedInClosed: Bool {
return lowerBound <= value && value <= upperBound
}
init(
lowerBound: Int,
upperBound: Int,
value: Int,
file: String = #file, line: UInt = #line
) {
self.lowerBound = lowerBound
self.upperBound = upperBound
self.value = value
self.loc = SourceLoc(file, line, comment: "test data")
}
}
func generateContainsTests() -> [ContainsTest] {
let bounds = [ Int.min, -30, -10, 0, 10, 20, Int.max ]
var result: [ContainsTest] = []
for lowerBound in bounds {
for upperBound in bounds {
if lowerBound > upperBound { continue }
for value in bounds {
result.append(
ContainsTest(
lowerBound: lowerBound, upperBound: upperBound,
value: value))
}
}
}
return result
}
let containsTests: [ContainsTest] = generateContainsTests()
infix operator ..<* { associativity none precedence 135 }
infix operator ...* { associativity none precedence 135 }
enum VariantRange {
case halfOpen(lowerBound: Int, upperBound: Int)
case closed(lowerBound: Int, upperBound: Int)
var isClosed: Bool {
switch self {
case .halfOpen:
return false
case .closed:
return true
}
}
var lowerBound: Int {
switch self {
case .halfOpen(let result, _):
return result
case .closed(let result, _):
return result
}
}
var upperBound: Int {
switch self {
case .halfOpen(_, let result):
return result
case .closed(_, let result):
return result
}
}
}
func ..<* (lhs: Int, rhs: Int) -> VariantRange {
return .halfOpen(lowerBound: lhs, upperBound: rhs)
}
func ...* (lhs: Int, rhs: Int) -> VariantRange {
return .closed(lowerBound: lhs, upperBound: rhs)
}
struct OverlapsTest {
let expected: Bool
let lhs: VariantRange
let rhs: VariantRange
let loc: SourceLoc
init(
expected: Bool,
lhs: VariantRange,
rhs: VariantRange,
file: String = #file, line: UInt = #line
) {
self.expected = expected
self.lhs = lhs
self.rhs = rhs
self.loc = SourceLoc(file, line, comment: "test data")
}
}
let overlapsTests: [OverlapsTest] = [
// 0-4, 5-10
OverlapsTest(expected: false, lhs: 0..<*4, rhs: 5..<*10),
OverlapsTest(expected: false, lhs: 0..<*4, rhs: 5...*10),
OverlapsTest(expected: false, lhs: 0...*4, rhs: 5..<*10),
OverlapsTest(expected: false, lhs: 0...*4, rhs: 5...*10),
// 0-5, 5-10
OverlapsTest(expected: false, lhs: 0..<*5, rhs: 5..<*10),
OverlapsTest(expected: false, lhs: 0..<*5, rhs: 5...*10),
OverlapsTest(expected: true, lhs: 0...*5, rhs: 5..<*10),
OverlapsTest(expected: true, lhs: 0...*5, rhs: 5...*10),
// 0-6, 5-10
OverlapsTest(expected: true, lhs: 0..<*6, rhs: 5..<*10),
OverlapsTest(expected: true, lhs: 0..<*6, rhs: 5...*10),
OverlapsTest(expected: true, lhs: 0...*6, rhs: 5..<*10),
OverlapsTest(expected: true, lhs: 0...*6, rhs: 5...*10),
// 0-20, 5-10
OverlapsTest(expected: true, lhs: 0..<*20, rhs: 5..<*10),
OverlapsTest(expected: true, lhs: 0..<*20, rhs: 5...*10),
OverlapsTest(expected: true, lhs: 0...*20, rhs: 5..<*10),
OverlapsTest(expected: true, lhs: 0...*20, rhs: 5...*10),
// 0-0, 0-5
OverlapsTest(expected: false, lhs: 0..<*0, rhs: 0..<*5),
OverlapsTest(expected: false, lhs: 0..<*0, rhs: 0...*5),
]
struct ClampedTest {
let expected: Range<Int>
let subject: Range<Int>
let limits: Range<Int>
let loc: SourceLoc
init(
expected: Range<Int>,
subject: Range<Int>,
limits: Range<Int>,
file: String = #file, line: UInt = #line
) {
self.expected = expected
self.subject = subject
self.limits = limits
self.loc = SourceLoc(file, line, comment: "test data")
}
}
let clampedTests: [ClampedTest] = [
ClampedTest(expected: 5..<5, subject: 0..<3, limits: 5..<10),
ClampedTest(expected: 5..<9, subject: 0..<9, limits: 5..<10),
ClampedTest(expected: 5..<10, subject: 0..<13, limits: 5..<10),
ClampedTest(expected: 7..<9, subject: 7..<9, limits: 5..<10),
ClampedTest(expected: 7..<10, subject: 7..<13, limits: 5..<10),
ClampedTest(expected: 10..<10, subject: 13..<15, limits: 5..<10),
]
%{
all_range_types = [
('Range', '..<', 'MinimalComparableValue'),
('CountableRange', '..<', 'MinimalStrideableValue'),
('ClosedRange', '...', 'MinimalComparableValue'),
('CountableClosedRange', '...', 'MinimalStrideableValue'),
]
}%
% for (Self, op, Bound) in all_range_types:
% TestSuite = Self + 'TestSuite'
// Check that the generic parameter is called 'Bound'.
extension ${Self} where Bound : TestProtocol1 {
var _elementIsTestProtocol1: Bool {
fatalError("not implemented")
}
}
var ${TestSuite} = TestSuite("${Self}")
${TestSuite}.test("init(uncheckedBounds:)") {
let _1 = ${Bound}(1)
let _2 = ${Bound}(2)
let r = ${Self}(uncheckedBounds: (lower: _2, upper: _1))
expectEqual(_2, r.lowerBound)
expectEqual(_1, r.upperBound)
}
${TestSuite}.test("lowerBound, upperBound") {
let _1 = ${Bound}(1, identity: 1010)
let _2 = ${Bound}(2, identity: 2020)
let range: ${Self}<${Bound}> = _1${op}_2
expectEqual(1, range.lowerBound.value)
expectEqual(1010, range.lowerBound.identity)
expectEqual(2, range.upperBound.value)
expectEqual(2020, range.upperBound.identity)
}
${TestSuite}.test("Equatable") {
let _1 = ${Bound}(1)
let _2 = ${Bound}(2)
let instances: [${Self}<${Bound}>] = [
_1${op}_1,
_1${op}_2,
_2${op}_2,
]
checkEquatable(instances, oracle: { $0 == $1 })
}
${TestSuite}.test("'${op}' traps when upperBound < lowerBound")
.crashOutputMatches("Can't form Range with upperBound < lowerBound")
.code {
let _1 = ${Bound}(1)
let _2 = ${Bound}(2)
expectCrashLater()
let range: ${Self}<${Bound}> = _2${op}_1
_blackHole(range)
}
${TestSuite}.test("contains(_:)/staticDispatch") {
let start = ${Bound}(10)
let end = ${Bound}(20)
let range: ${Self}<${Bound}> = start${op}end
expectEqual(1, ${Bound}.timesLessWasCalled.value)
for test in 0..<30 {
% if 'Closed' in Self:
let expected = test >= start.value && test <= end.value
% else:
let expected = test >= start.value && test < end.value
% end
expectEqual(
expected, range.contains(${Bound}(test)),
"test=\(test)")
}
expectEqual(51, ${Bound}.timesLessWasCalled.value)
}
${TestSuite}.test("~=/staticDispatch") {
let start = ${Bound}(10)
let end = ${Bound}(20)
let range: ${Self}<${Bound}> = start${op}end
expectEqual(1, ${Bound}.timesLessWasCalled.value)
for test in 0..<30 {
% if 'Closed' in Self:
let expected = test >= start.value && test <= end.value
% else:
let expected = test >= start.value && test < end.value
% end
expectEqual(
expected, range ~= ${Bound}(test),
"test=\(test)")
}
expectEqual(51, ${Bound}.timesLessWasCalled.value)
}
% if 'Countable' in Self:
${TestSuite}.test("contains(_:)/dynamicDispatch") {
let start = ${Bound}(10)
let end = ${Bound}(20)
let range: ${Self}<${Bound}> = start${op}end
let loggingRange = LoggingCollection(wrapping: range)
expectEqual(1, ${Bound}.timesLessWasCalled.value)
for test in 0..<30 {
% if 'Closed' in Self:
let expected = test >= start.value && test <= end.value
% else:
let expected = test >= start.value && test < end.value
% end
expectEqual(
expected, loggingRange.contains(${Bound}(test)),
"test=\(test)")
}
expectEqual(51, MinimalStrideableValue.timesLessWasCalled.value)
}
% end
${TestSuite}.test("contains(_:)/semantics, ~=/semantics")
.forEach(in: containsTests) {
(test) in
// Check both static and dynamic dispatch.
let range: ${Self}<${Bound}> = ${Bound}(test.lowerBound)${op}${Bound}(test.upperBound)
% if 'Countable' in Self:
let loggingRange = LoggingCollection(wrapping: range)
% else:
let loggingRange = range
% end
let value = ${Bound}(test.value)
% if 'Closed' in Self:
expectEqual(test.containedInClosed, range.contains(value))
expectEqual(test.containedInClosed, loggingRange.contains(value))
expectEqual(test.containedInClosed, range ~= value)
% else:
expectEqual(test.containedInHalfOpen, range.contains(value))
expectEqual(test.containedInHalfOpen, loggingRange.contains(value))
expectEqual(test.containedInHalfOpen, range ~= value)
% end
}
% for (OtherSelf, other_op, OtherBound) in all_range_types:
${TestSuite}.test("overlaps(${OtherSelf})/semantics")
.forEach(in: overlapsTests) {
(test) in
if test.lhs.isClosed != ${str('Closed' in Self).lower()} ||
test.rhs.isClosed != ${str('Closed' in OtherSelf).lower()} {
return
}
let lhs: ${Self}<${Bound}>
= ${Bound}(test.lhs.lowerBound)${op}${Bound}(test.lhs.upperBound)
let rhs: ${Self}<${Bound}>
= ${Bound}(test.rhs.lowerBound)${op}${Bound}(test.rhs.upperBound)
expectEqual(test.expected, lhs.overlaps(rhs))
expectEqual(test.expected, rhs.overlaps(lhs))
expectEqual(!lhs.isEmpty, lhs.overlaps(lhs))
expectEqual(!rhs.isEmpty, rhs.overlaps(rhs))
}
% end
${TestSuite}.test("clamped(to:)/semantics")
.forEach(in: clampedTests) {
(test) in
let subject: ${Self}<${Bound}>
= ${Bound}(test.subject.lowerBound)${op}${Bound}(test.subject.upperBound)
let limits: ${Self}<${Bound}>
= ${Bound}(test.limits.lowerBound)${op}${Bound}(test.limits.upperBound)
expectEqual(
${Bound}(test.expected.lowerBound)${op}${Bound}(test.expected.upperBound),
subject.clamped(to: limits))
}
${TestSuite}.test("isEmpty") {
let start = ${Bound}(10)
let end = ${Bound}(20)
let range1: ${Self}<${Bound}> = start${op}start
let range2: ${Self}<${Bound}> = start${op}end
expectEqual(2, ${Bound}.timesLessWasCalled.value)
% if 'Closed' in Self:
expectFalse(range1.isEmpty)
expectFalse(range2.isEmpty)
% else:
expectTrue(range1.isEmpty)
expectFalse(range2.isEmpty)
% end
expectEqual(2, ${Bound}.timesLessWasCalled.value)
}
${TestSuite}.test("CustomStringConvertible, CustomDebugStringConvertible, CustomReflectable") {
var r: ${Self}<CustomPrintableValue> =
CustomPrintableValue(1)${op}CustomPrintableValue(2)
expectPrinted("(value: 1).description${op}(value: 2).description", r)
expectDebugPrinted(
"${Self}(" +
"(value: 1).debugDescription${op}(value: 2).debugDescription" +
")",
r)
expectDumped(
"▿ ${Self}((value: 1).debugDescription${op}(value: 2).debugDescription)\n" +
" ▿ lowerBound: (value: 1).debugDescription\n" +
" - value: 1\n" +
" - identity: 0\n" +
" ▿ upperBound: (value: 2).debugDescription\n" +
" - value: 2\n" +
" - identity: 0\n",
r)
}
% end
var MiscTestSuite = TestSuite("Misc")
MiscTestSuite.test("map()") {
// <rdar://problem/17054014> map method should exist on ranges
var result = (1..<4).map { $0*2 }
expectType(Array<Int>.self, &result)
expectEqualSequence([ 2, 4, 6 ], result)
}
MiscTestSuite.test("reversed()") {
var result = (0..<10).lazy.reversed()
typealias Expected = LazyRandomAccessCollection<
ReversedRandomAccessCollection<CountableRange<Int>>>
expectType(Expected.self, &result)
expectEqualSequence(
[ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ],
result)
}
// FIXME: swift-3-indexing-model: check that CountableRange is a collection.
/*
func assertCollection<C: Collection>(_: C) {}
assertCollection(0..<10)
*/
// FIXME: swift-3-indexing-model: this test does not belong in this file.
MiscTestSuite.test("stride") {
var result = [Double]()
for i in stride(from: 1.4, through: 3.4, by: 1) {
result.append(i)
}
expectEqual([ 1.4, 2.4, 3.4 ], result)
}
runAllTests()