Skip to content

Commit ade42eb

Browse files
authored
Merge pull request #8041 from natecook1000/nc-check-nonint-collections
[test] Expand collection combinatorics to include index
2 parents b3020d8 + 3ef1dc2 commit ade42eb

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

test/stdlib/collection-combinatorics.swift.gyb

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,65 @@
66
// It should be possible to conform to any combination of
77
// (1 + RangeReplaceable) * (1 + Bidirectional + RandomAccess) * (1 + Mutable)
88
// Collection and get reasonable default implementations for slicing
9-
// operations from
10-
// the standard library.
9+
// operations from the standard library.
10+
11+
struct CustomIndex : Comparable {
12+
static func <(lhs: CustomIndex, rhs: CustomIndex) -> Bool {
13+
return false
14+
}
15+
static func ==(lhs: CustomIndex, rhs: CustomIndex) -> Bool {
16+
return false
17+
}
18+
static var min: CustomIndex { return CustomIndex() }
19+
static var max: CustomIndex { return CustomIndex() }
20+
}
1121

1222
% for mutable in ['', 'Mutable']:
1323
% for rangeReplaceable in ['', 'RangeReplaceable']:
1424
% for capability in ['', 'Bidirectional', 'RandomAccess']:
25+
% for index in ['Int', 'CustomIndex']:
26+
% indexLabel = 'Custom' if index == 'CustomIndex' else ''
1527

16-
struct ${mutable}${rangeReplaceable}${capability}Butt
28+
struct ${indexLabel}${mutable}${rangeReplaceable}${capability}Butt
1729
: ${mutable}Collection
18-
% if rangeReplaceable:
30+
% if rangeReplaceable:
1931
, ${rangeReplaceable}Collection
20-
% end
21-
% if capability:
32+
% end
33+
% if capability:
2234
, ${capability}Collection
23-
% end
35+
% end
2436
{
25-
subscript(i: Int) -> Int {
37+
subscript(i: ${index}) -> Int {
2638
get { return 0 }
27-
% if mutable:
39+
% if mutable:
2840
set { }
29-
% end
41+
% end
3042
}
3143

32-
% if capability:
33-
func index(before i: Int) -> Int {
34-
return i - 1
44+
% if capability:
45+
func index(before i: ${index}) -> ${index} {
46+
return i
3547
}
36-
% end
37-
func index(after i: Int) -> Int {
38-
return i + 1
48+
% end
49+
func index(after i: ${index}) -> ${index} {
50+
return i
3951
}
4052

41-
var startIndex: Int { return .min }
42-
var endIndex: Int { return .max }
53+
var startIndex: ${index} { return .min }
54+
var endIndex: ${index} { return .max }
4355

44-
% if rangeReplaceable:
56+
% if rangeReplaceable:
4557
init() {}
4658

47-
mutating func replaceSubrange<C: Collection>(_: Range<Int>, with: C)
59+
mutating func replaceSubrange<C: Collection>(_: Range<${index}>, with: C)
4860
where C.Iterator.Element == Int
4961
{}
50-
% end
62+
% end
63+
64+
% if capability == 'RandomAccess' and index == 'CustomIndex':
65+
// This type alias is required for some random-access collections with
66+
// a custom index type -- without it the default implementation for
67+
// `indices` doesn't attach.
68+
typealias Indices = DefaultRandomAccessIndices<${indexLabel}${mutable}${rangeReplaceable}${capability}Butt>
69+
% end
5170
}

0 commit comments

Comments
 (0)