-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathLazyFilterCollection.swift.gyb
132 lines (122 loc) · 4.46 KB
/
LazyFilterCollection.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
// -*- swift -*-
// RUN: %target-run-simple-swiftgyb
// REQUIRES: executable_test
// FIXME: the test is too slow when the standard library is not optimized.
// rdar://problem/46878013
// REQUIRES: optimized_stdlib
// REQUIRES: long_test
import SwiftPrivate
import StdlibUnittest
import StdlibCollectionUnittest
var CollectionTests = TestSuite("Collection")
%{
variations = [('', 'Sequence'), ('', 'Collection'), ('Bidirectional', 'Collection')]
}%
// Test collections using value types as elements.
% for (traversal, kind) in variations:
CollectionTests.add${traversal}${kind}Tests(
make${kind}: { (elements: [OpaqueValue<Int>]) -> LazyFilter${kind}<Minimal${traversal}${kind}<OpaqueValue<Int>>> in
Minimal${traversal}${kind}(elements: elements).lazy.filter { _ in return true }
},
wrapValue: identity,
extractValue: identity,
make${kind}OfEquatable: { (elements: [MinimalEquatableValue]) -> LazyFilter${kind}<Minimal${traversal}${kind}<MinimalEquatableValue>> in
Minimal${traversal}${kind}(elements: elements).lazy.filter { _ in return true }
},
wrapValueIntoEquatable: identityEq,
extractValueFromEquatable: identityEq
)
% end
// Test collections using reference types as elements.
% for (traversal, kind) in variations:
CollectionTests.add${traversal}${kind}Tests(
make${kind}: { (elements: [LifetimeTracked]) -> LazyFilter${kind}<Minimal${traversal}${kind}<LifetimeTracked>> in
// FIXME: create a better sequence and filter
Minimal${traversal}${kind}(elements: elements).lazy.filter { _ in return true }
},
wrapValue: { (element: OpaqueValue<Int>) in
LifetimeTracked(element.value, identity: element.identity)
},
extractValue: { (element: LifetimeTracked) in
OpaqueValue(element.value, identity: element.identity)
},
make${kind}OfEquatable: { (elements: [LifetimeTracked]) -> LazyFilter${kind}<Minimal${traversal}${kind}<LifetimeTracked>> in
// FIXME: create a better sequence and filter
Minimal${traversal}${kind}(elements: elements).lazy.filter { _ in return true }
},
wrapValueIntoEquatable: { (element: MinimalEquatableValue) in
LifetimeTracked(element.value, identity: element.identity)
},
extractValueFromEquatable: { (element: LifetimeTracked) in
MinimalEquatableValue(element.value, identity: element.identity)
}
)
% end
// Test collection instances and iterators.
% for (traversal, kind) in variations:
CollectionTests.test("LazyFilterCollection instances (${traversal}${kind})") {
do {
let expected : [String] = []
let base = ["apple", "orange", "banana", "grapefruit", "lychee"]
% if kind == 'Sequence':
checkSequence(
expected,
MinimalSequence(elements: base).lazy.filter { _ in return false })
% elif traversal == '' and kind == 'Collection':
checkForwardCollection(
expected,
MinimalCollection(elements: base).lazy.filter { _ in return false },
sameValue: { $0 == $1 })
% else:
check${traversal}${kind}(
expected,
Minimal${traversal}${kind}(elements: base).lazy.filter { _ in return false },
sameValue: { $0 == $1 })
% end
}
do {
let expected = ["apple", "orange", "banana", "grapefruit", "lychee"]
let base = ["apple", "orange", "banana", "grapefruit", "lychee"]
% if kind == 'Sequence':
checkSequence(
expected,
MinimalSequence(elements: base).lazy.filter { _ in return true })
% elif traversal == '' and kind == 'Collection':
checkForwardCollection(
expected,
MinimalCollection(elements: base).lazy.filter { _ in return true },
sameValue: { $0 == $1 })
% else:
check${traversal}${kind}(
expected,
Minimal${traversal}${kind}(elements: base).lazy.filter { _ in return true },
sameValue: { $0 == $1 })
% end
}
do {
let predicate: (Int) -> Bool = { $0 % 3 == 0 || $0 % 5 == 0 }
let count = 500
let base = (0 ..< count).map { _ in Int.random(in: .min ... .max) }
var expected : [Int] = []
for element in base where predicate(element) {
expected.append(element)
}
% if kind == 'Sequence':
checkSequence(
expected,
MinimalSequence(elements: base).lazy.filter(predicate))
% elif traversal == '' and kind == 'Collection':
checkOneLevelOfForwardCollection(
expected,
MinimalCollection(elements: base).lazy.filter(predicate),
sameValue: { $0 == $1 })
% else:
checkOneLevelOf${traversal}${kind}(
expected,
Minimal${traversal}${kind}(elements: base).lazy.filter(predicate),
sameValue: { $0 == $1 })
% end
}
}
% end
runAllTests()