-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathGenerator.swift
56 lines (47 loc) · 1.17 KB
/
Generator.swift
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
// RUN: %target-run-stdlib-swift
// REQUIRES: executable_test
import StdlibUnittest
import StdlibCollectionUnittest
var tests = TestSuite("Iterator")
// Check to make sure we are actually getting Optionals out of this
// IteratorProtocol
tests.test("Range") {
var w = (1..<2).makeIterator()
var maybe_one = w.next()
expectType(Optional<Int>.self, &maybe_one)
expectEqual(1, maybe_one)
expectNil(w.next())
}
tests.test("RangeIteratorConformsToSequence") {
for x in (1..<2).makeIterator() {
expectEqual(1, x)
}
}
// Test round-trip IteratorProtocol/IteratorProtocol adaptation
tests.test("IteratorSequence") {
var r = 1..<7
var x = MinimalIterator(Array(r))
var rangeIndex = r.lowerBound
for a in IteratorSequence(x) {
expectEqual(rangeIndex, a)
rangeIndex = r.index(after: rangeIndex)
}
expectEqual(rangeIndex, r.upperBound)
}
struct MyIterator : IteratorProtocol {
var i = 0
mutating func next() -> Int? {
if i >= 10 { return nil }
i += 1
return i-1
}
}
extension MyIterator : Sequence {}
tests.test("IteratorsModelSequenceByDeclaration") {
var n = 0
for i in MyIterator() {
expectEqual(n, i)
n += 1
}
}
runAllTests()