Skip to content

Commit 3e8ffae

Browse files
committed
New indexing model: update the test generator for Slice* tests
1 parent bd9e855 commit 3e8ffae

File tree

4 files changed

+285
-274
lines changed

4 files changed

+285
-274
lines changed

validation-test/stdlib/Slice.swift.gyb

+63-122
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
// RUN: %S/../../utils/line-directive %t/Slice.swift -- %target-run %t/a.out
66
// REQUIRES: executable_test
77

8+
%{
9+
from gyb_stdlib_support import (
10+
TRAVERSALS,
11+
sliceTypeName,
12+
defaultIndicesForTraversal
13+
)
14+
}%
15+
816
import StdlibUnittest
917
import StdlibCollectionUnittest
1018
import SwiftPrivate
@@ -22,109 +30,94 @@ var SliceTests = TestSuite("Collection")
2230
// Slice<Base>
2331
//===----------------------------------------------------------------------===//
2432

25-
SliceTests.test("Slice/AssociatedTypes") {
26-
%for traversal in [ 'Forward', 'Bidirectional', 'RandomAccess' ]:
27-
do {
28-
typealias Base = Minimal${traversal}Collection<OpaqueValue<Int>>
29-
typealias ${traversal}Slice = Slice<Base>
30-
expectSliceType(${traversal}Slice.self)
33+
% for Traversal in TRAVERSALS:
34+
% for Mutable in [ False, True ]:
35+
% for RangeReplaceable in [ False, True ]:
36+
% Slice = sliceTypeName(traversal=Traversal, mutable=Mutable, rangeReplaceable=RangeReplaceable)
37+
% Collection = 'Minimal' + Slice.replace('Slice', 'Collection')
38+
39+
SliceTests.test("${Slice}/AssociatedTypes") {
40+
do {
41+
typealias Collection = ${Collection}<OpaqueValue<Int>>
42+
typealias CollectionSlice = ${Slice}<Collection>
43+
expectSliceType(CollectionSlice.self)
3144
expectCollectionAssociatedTypes(
32-
collectionType: ${traversal}Slice.self,
33-
iteratorType: IndexingIterator<${traversal}Slice>.self,
34-
subSequenceType: ${traversal}Slice.self,
35-
indexType: Minimal${traversal}Index.self)
45+
collectionType: CollectionSlice.self,
46+
iteratorType: IndexingIterator<CollectionSlice>.self,
47+
subSequenceType: CollectionSlice.self,
48+
indexType: MinimalIndex.self,
49+
indexDistanceType: Int.self,
50+
indicesType: ${defaultIndicesForTraversal(Traversal)}<CollectionSlice>.self)
3651
}
37-
%end
3852

39-
func checkStaticTypealiases<Base : Collection>(_: Base) {
53+
func checkStaticTypealiases1<Base : Collection>(_: Base) {
4054
expectEqualType(Base.Index.self, Slice<Base>.Index.self)
4155
}
56+
57+
func checkStaticTypealiases2<
58+
Base : MutableCollection
59+
>(_: Base) {
60+
expectEqualType(Base.Index.self, MutableSlice<Base>.Index.self)
61+
}
4262
}
4363

44-
SliceTests.test("Slice/init(_base:bounds:)") {
64+
SliceTests.test("${Slice}/init(base:bounds:)") {
4565
for test in subscriptRangeTests {
46-
let base = MinimalForwardCollection(elements: test.collection)
47-
var result = Slice(base: base, bounds: test.boundsIn(base))
48-
expectType(
49-
Slice<MinimalForwardCollection<OpaqueValue<Int>>>.self,
50-
&result)
51-
52-
checkForwardCollection(
53-
test.expected,
54-
result,
66+
let base = ${Collection}(elements: test.collection)
67+
var slice = ${Slice}(base: base, bounds: test.bounds(in: base))
68+
expectType(${Slice}<${Collection}<OpaqueValue<Int>>>.self, &slice)
69+
70+
checkCollection(
71+
slice,
72+
expected: test.expected,
5573
stackTrace: SourceLocStack().with(test.loc))
5674
{ $0.value == $1.value }
5775
}
5876
}
5977

60-
SliceTests.test("Slice.{startIndex,endIndex}") {
78+
SliceTests.test("${Slice}.{startIndex,endIndex}") {
6179
for test in subscriptRangeTests {
62-
let base = MinimalForwardCollection(elements: test.collection)
63-
let bounds = test.boundsIn(base)
64-
var slice = Slice(base: base, bounds: bounds)
65-
expectType(
66-
Slice<MinimalForwardCollection<OpaqueValue<Int>>>.self,
67-
&slice)
80+
let c = ${Collection}(elements: test.collection)
81+
let bounds = test.bounds(in: c)
82+
var slice = ${Slice}(base: c, bounds: bounds)
83+
expectType(${Slice}<${Collection}<OpaqueValue<Int>>>.self, &slice)
6884

6985
expectEqual(bounds.lowerBound, slice.startIndex)
7086
expectEqual(bounds.upperBound, slice.endIndex)
7187
}
7288
}
7389

74-
SliceTests.test("Slice.subscript(_: Index)") {
75-
for test in subscriptRangeTests {
76-
typealias Base = MinimalForwardCollection<OpaqueValue<Int>>
77-
Base.Index.trapOnRangeCheckFailure.value = false
78-
let base = Base(elements: test.collection)
79-
let bounds = test.boundsIn(base)
80-
var slice = Slice(base: base, bounds: bounds)
81-
expectType(Slice<Base>.self, &slice)
82-
83-
for i in bounds {
84-
var element = slice[i]
85-
expectType(OpaqueValue<Int>.self, &element)
86-
expectEqual(base[i].value, element.value)
87-
}
88-
89-
for (i, index) in base.indices.enumerated() {
90-
if test.bounds.contains(i) {
91-
expectEqual(base[index].value, slice[index].value)
92-
} else {
93-
// `Slice` disallows out-of-bounds indices when the underlying index
94-
// type can perform a range check.
95-
expectFailure {
96-
_blackHole(slice[index])
97-
}
98-
}
99-
}
100-
}
101-
}
90+
% end
91+
% end
92+
% end
10293

10394
SliceTests.test("Slice.subscript(_: Range<Index>)") {
10495
for test in subscriptRangeTests {
105-
typealias Base = MinimalForwardCollection<OpaqueValue<Int>>
96+
typealias Base = MinimalCollection<OpaqueValue<Int>>
10697
Base.Index.trapOnRangeCheckFailure.value = false
10798
let base = Base(elements: test.collection)
108-
var slice = Slice(base: base, bounds: base.indices)
99+
var slice = Slice(base: base, bounds: base.startIndex..<base.endIndex)
109100
expectType(Slice<Base>.self, &slice)
110101

111-
var result = slice[test.boundsIn(slice)]
102+
var result = slice[test.bounds(in: slice)]
112103
expectType(Slice<Base>.self, &result)
113104

114-
checkForwardCollection(test.expected, result) { $0.value == $1.value }
105+
checkCollection(result, expected: test.expected) {
106+
$0.value == $1.value
107+
}
115108

116-
if test.bounds == test.collection.indices {
117-
let reSliced = result[base.indices]
118-
checkForwardCollection(
119-
test.collection,
109+
if test.bounds == test.collection.startIndex..<test.collection.endIndex {
110+
let reSliced = result[base.startIndex..<base.endIndex]
111+
checkCollection(
120112
reSliced,
113+
expected: test.collection,
121114
stackTrace: SourceLocStack().with(test.loc))
122115
{ $0.value == $1.value }
123116
} else {
124117
// `Slice` disallows out-of-bounds slicing when the underlying index type
125118
// can perform a range check.
126119
expectFailure {
127-
_blackHole(result[base.indices])
120+
_blackHole(result[base.startIndex..<base.endIndex])
128121
}
129122
}
130123
}
@@ -135,28 +128,6 @@ SliceTests.test("Slice.subscript(_: Range<Index>)") {
135128
// MutableSlice<Base>
136129
//===----------------------------------------------------------------------===//
137130

138-
SliceTests.test("MutableSlice/AssociatedTypes") {
139-
%for traversal in [ 'Forward', 'Bidirectional', 'RandomAccess' ]:
140-
do {
141-
typealias Base =
142-
Minimal${traversal}MutableCollection<OpaqueValue<Int>>
143-
typealias ${traversal}MutableSlice = MutableSlice<Base>
144-
expectMutableSliceType(${traversal}MutableSlice.self)
145-
expectCollectionAssociatedTypes(
146-
collectionType: ${traversal}MutableSlice.self,
147-
iteratorType: IndexingIterator<${traversal}MutableSlice>.self,
148-
subSequenceType: ${traversal}MutableSlice.self,
149-
indexType: Minimal${traversal}Index.self)
150-
}
151-
%end
152-
153-
func checkStaticTypealiases<
154-
Base : MutableCollection
155-
>(_: Base) {
156-
expectEqualType(Base.Index.self, MutableSlice<Base>.Index.self)
157-
}
158-
}
159-
160131
/*
161132
FIXME: uncomment this test when the following bug is fixed:
162133

@@ -179,46 +150,16 @@ extension MutableSlice {
179150
}
180151
*/
181152

182-
SliceTests.test("MutableSlice/init(_base:bounds:)") {
183-
for test in subscriptRangeTests {
184-
let c = MinimalForwardMutableCollection(elements: test.collection)
185-
var result = MutableSlice(base: c, bounds: test.boundsIn(c))
186-
expectType(
187-
MutableSlice<MinimalForwardMutableCollection<OpaqueValue<Int>>>.self,
188-
&result)
189-
190-
checkForwardCollection(
191-
test.expected,
192-
result,
193-
stackTrace: SourceLocStack().with(test.loc))
194-
{ $0.value == $1.value }
195-
}
196-
}
197-
198-
SliceTests.test("MutableSlice.{startIndex,endIndex}") {
199-
for test in subscriptRangeTests {
200-
let c = MinimalForwardMutableCollection(elements: test.collection)
201-
let bounds = test.boundsIn(c)
202-
var slice = MutableSlice(base: c, bounds: bounds)
203-
expectType(
204-
MutableSlice<MinimalForwardMutableCollection<OpaqueValue<Int>>>.self,
205-
&slice)
206-
207-
expectEqual(bounds.lowerBound, slice.startIndex)
208-
expectEqual(bounds.upperBound, slice.endIndex)
209-
}
210-
}
211-
212153
SliceTests.test("MutableSlice.subscript(_: Index)/{get,set}") {
213154
for test in subscriptRangeTests {
214-
typealias Base = MinimalForwardMutableCollection<OpaqueValue<Int>>
155+
typealias Base = MinimalMutableCollection<OpaqueValue<Int>>
215156
Base.Index.trapOnRangeCheckFailure.value = false
216157
let base = Base(elements: test.collection)
217-
let bounds = test.boundsIn(base)
158+
let bounds = test.bounds(in: base)
218159
var slice = MutableSlice(base: base, bounds: bounds)
219160
expectType(MutableSlice<Base>.self, &slice)
220161

221-
for i in bounds {
162+
for i in base[bounds].indices {
222163
// Test getter.
223164
var element = slice[i]
224165
expectType(OpaqueValue<Int>.self, &element)
@@ -227,7 +168,7 @@ SliceTests.test("MutableSlice.subscript(_: Index)/{get,set}") {
227168

228169
do {
229170
var sliceForSetter = slice
230-
for i in bounds {
171+
for i in base[bounds].indices {
231172
// Test setter.
232173
sliceForSetter[i].value += 1
233174
expectEqual(base[i].value + 1, sliceForSetter[i].value)
+34-70
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,37 @@
11
#!/usr/bin/env python
22

3-
import itertools
3+
from __future__ import print_function
4+
5+
import sys
6+
7+
def main():
8+
with open(sys.argv[1], 'r') as f:
9+
content = f.readlines()
10+
11+
while len(content) != 0:
12+
startIndex = -1
13+
endIndex = -1
14+
testFilename = ''
15+
for i, line in enumerate(content):
16+
if line.startswith('// Start of file: '):
17+
testFilename = line[18:].strip()
18+
startIndex = i
19+
break
20+
if startIndex == -1:
21+
return 0
22+
for i, line in enumerate(content):
23+
if line.startswith('// End of file: '):
24+
assert line[16:].strip() == testFilename
25+
endIndex = i
26+
break
27+
testContent = content[startIndex+1:endIndex]
28+
29+
with open(testFilename, 'w') as testFile:
30+
for line in testContent:
31+
testFile.write(line)
32+
33+
content = content[endIndex+1:]
34+
35+
if __name__ == "__main__":
36+
sys.exit(main())
437

5-
traversal_options = ['Forward', 'Bidirectional', 'RandomAccess']
6-
base_kind_options = ['Defaulted', 'Minimal']
7-
mutable_options = [False, True]
8-
for traversal, base_kind, mutable in itertools.product(traversal_options,
9-
base_kind_options,
10-
mutable_options):
11-
# Test Slice<Base> and MutableSlice<Base> of various collections using
12-
# value types as elements.
13-
wrapper_types = ['Slice', 'MutableSlice'] if mutable else ['Slice']
14-
for Wrapper in wrapper_types:
15-
for name, prefix, suffix in [
16-
('FullWidth', '[]', '[]'),
17-
('WithPrefix', '[-9999, -9998, -9997]', '[]'),
18-
('WithSuffix', '[]', '[ -9999, -9998, -9997]'),
19-
('WithPrefixAndSuffix', '[-9999, -9998, -9997, -9996, -9995]',
20-
'[-9994, -9993, -9992]')
21-
]:
22-
Base = '%s%s%sCollection' % (
23-
base_kind, traversal, 'Mutable' if mutable else '')
24-
testFilename = Wrapper + '_Of_' + Base + '_' + name + '.swift'
25-
with open(testFilename + '.gyb', 'w') as testFile:
26-
testFile.write("""
27-
//// Automatically Generated From \
28-
validation-test/stdlib/Inputs/GenerateSliceTests.py
29-
//////// Do Not Edit Directly!
30-
// -*- swift -*-
31-
// RUN: rm -rf %t ; mkdir -p %t
32-
// RUN: %S/../../../utils/gyb %s -o %t/{testFilename} -D test_path="%S"
33-
// RUN: %S/../../../utils/line-directive %t/{testFilename} -- \
34-
%target-build-swift %t/{testFilename} -o %t/{testFilename}.a.out
35-
// RUN: %S/../../../utils/line-directive %t/{testFilename} -- \
36-
%target-run %t/{testFilename}.a.out
37-
// REQUIRES: executable_test
38-
39-
// FIXME: the test is too slow when the standard library is not optimized.
40-
// REQUIRES: optimized_stdlib
41-
42-
import StdlibUnittest
43-
import StdlibCollectionUnittest
44-
45-
// Also import modules which are used by StdlibUnittest internally. This
46-
// workaround is needed to link all required libraries in case we compile
47-
// StdlibUnittest with -sil-serialize-all.
48-
import SwiftPrivate
49-
#if _runtime(_ObjC)
50-
import ObjectiveC
51-
#endif
52-
53-
var SliceTests = TestSuite("Collection")
54-
55-
% import gyb
56-
% TSliceTest = gyb.parse_template("{{}}/Inputs/slice.gyb".format(test_path))
57-
% SliceTest = gyb.execute_template(
58-
% TSliceTest,
59-
% traversal='{traversal}',
60-
% base_kind='{base_kind}',
61-
% mutable={mutable},
62-
% Wrapper='{Wrapper}',
63-
% name='{name}',
64-
% prefix={prefix},
65-
% suffix={suffix})
66-
${{SliceTest}}
67-
68-
runAllTests()
69-
""".format(
70-
testFilename=testFilename, traversal=traversal,
71-
base_kind=base_kind, mutable=mutable,
72-
Wrapper=Wrapper, name=name, prefix=prefix,
73-
suffix=suffix))

0 commit comments

Comments
 (0)