Skip to content

Commit a9e9a29

Browse files
committed
Adding validation tests for LazyFilterCollection and LazyMapCollection
1 parent 6bd5a97 commit a9e9a29

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// -*- swift -*-
2+
3+
// RUN: rm -rf %t
4+
// RUN: mkdir -p %t
5+
// RUN: %S/../../../utils/gyb %s -o %t/main.swift
6+
// RUN: %S/../../../utils/line-directive %t/main.swift -- %target-build-swift %t/main.swift -o %t/LazyFilterCollection.swift.a.out
7+
// RUN: %S/../../../utils/line-directive %t/main.swift -- %target-run %t/LazyFilterCollection.swift.a.out
8+
// REQUIRES: executable_test
9+
10+
import StdlibUnittest
11+
import StdlibCollectionUnittest
12+
13+
var CollectionTests = TestSuite("Collection")
14+
15+
%{
16+
variations = [('', 'Sequence'), ('', 'Collection'), ('Bidirectional', 'Collection')]
17+
}%
18+
19+
// Test collections using value types as elements.
20+
% for (traversal, kind) in variations:
21+
CollectionTests.add${traversal}${kind}Tests(
22+
make${kind}: { (elements: [OpaqueValue<Int>]) -> LazyFilter${kind}<Minimal${traversal}${kind}<OpaqueValue<Int>>> in
23+
// FIXME: create a better sequence and filter
24+
Minimal${traversal}${kind}(elements: elements).lazy.filter { _ in return true }
25+
},
26+
wrapValue: identity,
27+
extractValue: identity,
28+
make${kind}OfEquatable: { (elements: [MinimalEquatableValue]) -> LazyFilter${kind}<Minimal${traversal}${kind}<MinimalEquatableValue>> in
29+
// FIXME: create a better sequence and filter
30+
Minimal${traversal}${kind}(elements: elements).lazy.filter { _ in return true }
31+
},
32+
wrapValueIntoEquatable: identityEq,
33+
extractValueFromEquatable: identityEq
34+
)
35+
% end
36+
37+
// Test collections using reference types as elements.
38+
% for (traversal, kind) in variations:
39+
CollectionTests.add${traversal}${kind}Tests(
40+
make${kind}: { (elements: [LifetimeTracked]) -> LazyFilter${kind}<Minimal${traversal}${kind}<LifetimeTracked]>> in
41+
// FIXME: create a better sequence and filter
42+
Minimal${traversal}${kind}(elements: elements).lazy.filter { _ in return true }
43+
},
44+
wrapValue: { (element: OpaqueValue<Int>) in
45+
LifetimeTracked(element.value, identity: element.identity)
46+
},
47+
extractValue: { (element: LifetimeTracked) in
48+
OpaqueValue(element.value, identity: element.identity)
49+
},
50+
make${kind}OfEquatable: { (elements: [LifetimeTracked]) -> LazyFilter${kind}<Minimal${traversal}${kind}<LifetimeTracked>> in
51+
// FIXME: create a better sequence and filter
52+
Minimal${traversal}${kind}(elements: elements).lazy.filter { _ in return true }
53+
},
54+
wrapValueIntoEquatable: { (element: MinimalEquatableValue) in
55+
LifetimeTracked(element.value, identity: element.identity)
56+
},
57+
extractValueFromEquatable: { (element: LifetimeTracked) in
58+
MinimalEquatableValue(element.value, identity: element.identity)
59+
}
60+
)
61+
% end
62+
63+
// Test collection instances and iterators.
64+
CollectionTests.test("LazyFilterCollection instances") {
65+
% for (traversal, kind) in variations:
66+
do {
67+
let expected : [String] = []
68+
let base = ["apple", "orange", "banana", "grapefruit", "lychee"]
69+
% if kind == 'Sequence':
70+
checkSequence(expected, MinimalSequence(elements: base).lazy.filter { _ in return false })
71+
% elif traversal == '' and kind == 'Collection':
72+
checkForwardCollection(expected, MinimalCollection(elements: base).lazy.filter { _ in return false }, sameValue: { $0 == $1 })
73+
% else:
74+
check${traversal}${kind}(expected, Minimal${traversal}${kind}(elements: base).lazy.filter { _ in return false }, sameValue: { $0 == $1 })
75+
% end
76+
}
77+
do {
78+
let expected = ["apple", "orange", "banana", "grapefruit", "lychee"]
79+
let base = ["apple", "orange", "banana", "grapefruit", "lychee"]
80+
% if kind == 'Sequence':
81+
checkSequence(expected, MinimalSequence(elements: base).lazy.filter { _ in return true })
82+
% elif traversal == '' and kind == 'Collection':
83+
checkForwardCollection(expected, MinimalCollection(elements: base).lazy.filter { _ in return true }, sameValue: { $0 == $1 })
84+
% else:
85+
check${traversal}${kind}(expected, Minimal${traversal}${kind}(elements: base).lazy.filter { _ in return true }, sameValue: { $0 == $1 })
86+
% end
87+
}
88+
do {
89+
let expected = [2, 4, 6, 8, 10, 12, 14, 16]
90+
let base = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
91+
% if kind == 'Sequence':
92+
checkSequence(expected, MinimalSequence(elements: base).lazy.filter { $0 % 2 == 0 })
93+
% elif traversal == '' and kind == 'Collection':
94+
checkForwardCollection(expected, MinimalCollection(elements: base).lazy.filter { $0 % 2 == 0 }, sameValue: { $0 == $1 })
95+
% else:
96+
check${traversal}${kind}(expected, Minimal${traversal}${kind}(elements: base).lazy.filter { $0 % 2 == 0 }, sameValue: { $0 == $1 })
97+
}
98+
% end
99+
}
100+
101+
runAllTests()
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// -*- swift -*-
2+
3+
// RUN: rm -rf %t
4+
// RUN: mkdir -p %t
5+
// RUN: %S/../../../utils/gyb %s -o %t/main.swift
6+
// RUN: %S/../../../utils/line-directive %t/main.swift -- %target-build-swift %t/main.swift -o %t/LazyMapCollection.swift.a.out
7+
// RUN: %S/../../../utils/line-directive %t/main.swift -- %target-run %t/LazyMapCollection.swift.a.out
8+
// REQUIRES: executable_test
9+
10+
import StdlibUnittest
11+
import StdlibCollectionUnittest
12+
13+
var CollectionTests = TestSuite("Collection")
14+
15+
%{
16+
variations = [('', 'Sequence'), ('', 'Collection'), ('Bidirectional', 'Collection'), ('RandomAccess', 'Collection')]
17+
}%
18+
19+
// Test collections using value types as elements.
20+
% for (traversal, kind) in variations:
21+
CollectionTests.add${traversal}${kind}Tests(
22+
make${kind}: { (elements: [OpaqueValue<Int>]) -> LazyMap${traversal}${kind}<Minimal${traversal}${kind}<OpaqueValue<Int>>, OpaqueValue<Int>> in
23+
Minimal${traversal}${kind}(elements: elements).lazy.map(identity)
24+
},
25+
wrapValue: identity,
26+
extractValue: identity,
27+
make${kind}OfEquatable: { (elements: [MinimalEquatableValue]) -> LazyMap${traversal}${kind}<Minimal${traversal}${kind}<MinimalEquatableValue>, MinimalEquatableValue> in
28+
Minimal${traversal}${kind}(elements: elements).lazy.map(identityEq)
29+
},
30+
wrapValueIntoEquatable: identityEq,
31+
extractValueFromEquatable: identityEq
32+
)
33+
% end
34+
35+
// Test collections using reference types as elements.
36+
% for (traversal, kind) in variations:
37+
CollectionTests.add${traversal}${kind}Tests(
38+
make${kind}: { (elements: [LifetimeTracked]) -> LazyMap${traversal}${kind}<Minimal${traversal}${kind}<LifetimeTracked>, LifetimeTracked> in
39+
Minimal${traversal}${kind}(elements: elements).lazy.map { $0 }
40+
},
41+
wrapValue: { (element: OpaqueValue<Int>) in
42+
LifetimeTracked(element.value, identity: element.identity)
43+
},
44+
extractValue: { (element: LifetimeTracked) in
45+
OpaqueValue(element.value, identity: element.identity)
46+
},
47+
make${kind}OfEquatable: { (elements: [LifetimeTracked]) -> LazyMap${traversal}${kind}<Minimal${traversal}${kind}<LifetimeTracked>, LifetimeTracked> in
48+
Minimal${traversal}${kind}(elements: elements).lazy.map { $0 }
49+
},
50+
wrapValueIntoEquatable: { (element: MinimalEquatableValue) in
51+
LifetimeTracked(element.value, identity: element.identity)
52+
},
53+
extractValueFromEquatable: { (element: LifetimeTracked) in
54+
MinimalEquatableValue(element.value, identity: element.identity)
55+
}
56+
)
57+
% end
58+
59+
// Test sequence instances and iterators.
60+
CollectionTests.test("LazyMapCollection instances") {
61+
% for (traversal, kind) in variations:
62+
do {
63+
let expected = ["convent", "conform", "constrict", "condone"]
64+
let base = ["vent", "form", "strict", "done"]
65+
% if kind == 'Sequence':
66+
checkSequence(expected, MinimalSequence(elements: base).lazy.map { "con" + $0 })
67+
% elif traversal == '' and kind == 'Collection':
68+
checkForwardCollection(expected, MinimalCollection(elements: base).lazy.map { "con" + $0 }, sameValue: { $0 == $1 })
69+
% else:
70+
check${traversal}${kind}(expected, Minimal${traversal}${kind}(elements: base).lazy.map { "con" + $0 }, sameValue: { $0 == $1 })
71+
% end
72+
}
73+
do {
74+
let expected = [1, 4, 9, 16, 25, 36, 49, 64]
75+
let base = [1, 2, 3, 4, 5, 6, 7, 8]
76+
% if kind == 'Sequence':
77+
checkSequence(expected, MinimalSequence(elements: base).lazy.map { $0 * $0 })
78+
% elif traversal == '' and kind == 'Collection':
79+
checkForwardCollection(expected, MinimalCollection(elements: base).lazy.map { $0 * $0 }, sameValue: { $0 == $1 })
80+
% else:
81+
check${traversal}${kind}(expected, Minimal${traversal}${kind}(elements: base).lazy.map { $0 * $0 }, sameValue: { $0 == $1 })
82+
% end
83+
}
84+
% end
85+
}
86+
87+
runAllTests()

0 commit comments

Comments
 (0)