-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathRangeReplaceable.swift.gyb
177 lines (153 loc) · 6.47 KB
/
RangeReplaceable.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// -*- swift -*-
// RUN: rm -rf %t ; mkdir -p %t
// RUN: %S/../../utils/gyb %s -o %t/RangeReplaceable.swift
// RUN: %S/../../utils/line-directive %t/RangeReplaceable.swift -- %target-build-swift %t/RangeReplaceable.swift -o %t/a.out
// RUN: %S/../../utils/line-directive %t/RangeReplaceable.swift -- %target-run %t/a.out
// REQUIRES: executable_test
// This is a workaround to avoid the compiler generating too large code: rdar://problem/24330686
// FIXME: remove the following line when the compiler problem is fixed.
// REQUIRES: swift_test_mode_optimize_none
import StdlibUnittest
import StdlibCollectionUnittest
// Also import modules which are used by StdlibUnittest internally. This
// workaround is needed to link all required libraries in case we compile
// StdlibUnittest with -sil-serialize-all.
import SwiftPrivate
#if _runtime(_ObjC)
import ObjectiveC
#endif
var RangeReplaceableTestSuite = TestSuite("RangeReplaceable")
RangeReplaceableTestSuite.test("append/dispatch") {
var tester = RangeReplaceableCollectionLog.dispatchTester([ OpaqueValue(1) ])
tester.append(OpaqueValue(2))
expectCustomizable(tester, tester.log.append)
}
RangeReplaceableTestSuite.test("appendContentsOf/dispatch") {
var tester = RangeReplaceableCollectionLog.dispatchTester([ OpaqueValue(1) ])
tester.appendContentsOf([ 2, 3 ].map(OpaqueValue.init))
expectCustomizable(tester, tester.log.appendContentsOf)
}
RangeReplaceableTestSuite.test("insert/dispatch") {
var tester = RangeReplaceableCollectionLog.dispatchTester([ OpaqueValue(1) ])
tester.insert(OpaqueValue(2), at: tester.base.startIndex)
expectCustomizable(tester, tester.log.insert)
}
RangeReplaceableTestSuite.test("insertContentsOf/dispatch") {
var tester = RangeReplaceableCollectionLog.dispatchTester([ OpaqueValue(1) ])
tester.insertContentsOf(
[ 2, 3 ].map(OpaqueValue.init),
at: tester.base.endIndex)
expectCustomizable(tester, tester.log.insertContentsOf)
}
RangeReplaceableTestSuite.test("remove(at:)/dispatch") {
var tester = RangeReplaceableCollectionLog.dispatchTester([ OpaqueValue(1) ])
tester.remove(at: tester.base.startIndex)
expectCustomizable(tester, tester.log.removeAt)
}
RangeReplaceableTestSuite.test("removeLast/whereIndexIsBidirectional/dispatch") {
var tester = RangeReplaceableCollectionLog.dispatchTester([ OpaqueValue(1) ])
_ = tester.removeLast()
expectCustomizable(tester, tester.log._customRemoveLast)
}
RangeReplaceableTestSuite.test("removeLast(n: Int)/whereIndexIsBidirectional/dispatch") {
var tester = RangeReplaceableCollectionLog.dispatchTester([ OpaqueValue(1) ])
_ = tester.removeLast(1)
expectCustomizable(tester, tester.log._customRemoveLastN)
}
RangeReplaceableTestSuite.test("removeSubrange/dispatch") {
var tester = RangeReplaceableCollectionLog.dispatchTester(
[ 1, 2, 3 ].map(OpaqueValue.init))
tester.removeSubrange(tester.base.startIndex..<tester.base.endIndex)
expectCustomizable(tester, tester.log.removeSubrange)
}
RangeReplaceableTestSuite.test("removeFirst/dispatch") {
var tester = RangeReplaceableCollectionLog.dispatchTester([ OpaqueValue(1) ])
tester.removeFirst()
expectCustomizable(tester, tester.log.removeFirst)
}
RangeReplaceableTestSuite.test("removeFirst(n)/dispatch") {
var tester = RangeReplaceableCollectionLog.dispatchTester([ OpaqueValue(1) ])
tester.removeFirst(1)
expectCustomizable(tester, tester.log.removeFirstN)
}
RangeReplaceableTestSuite.test("removeAll/dispatch") {
var tester = RangeReplaceableCollectionLog.dispatchTester([ OpaqueValue(1) ])
tester.removeAll(keepingCapacity: false)
expectCustomizable(tester, tester.log.removeAll)
}
RangeReplaceableTestSuite.test("reserveCapacity/dispatch") {
var tester = RangeReplaceableCollectionLog.dispatchTester(Array<Int>())
tester.reserveCapacity(10)
expectCustomizable(tester, tester.log.reserveCapacity)
}
func identity(element: OpaqueValue<Int>) -> OpaqueValue<Int> {
return element
}
func identityEq(element: MinimalEquatableValue) -> MinimalEquatableValue {
return element
}
% from itertools import product
% traversals = [ 'Forward', 'Bidirectional', 'RandomAccess' ]
% base_kinds = [ 'Defaulted', 'Minimal' ]
% collections = [
% ('{}{}RangeReplaceableCollection'.format(base_kind, traversal), traversal)
% for base_kind, traversal in product(base_kinds, traversals)
% ] + [
% ('Defaulted{}RangeReplaceableSlice'.format(traversal), traversal)
% for traversal in traversals
% ]
% other_array_types = [ 'Array', 'ArraySlice', 'ContiguousArray' ]
% collections += [(array, 'RandomAccess') for array in other_array_types]
% for Base, traversal in collections:
% oob_behavior = '.None' if Base in other_array_types else '.ExpectationFailure'
% collection_or_slice = 'Slice' if 'Slice' in Base else 'Collection'
if true {
var resiliencyChecks = CollectionMisuseResiliencyChecks.all
// `Array`, `ArraySlice`, and `ContiguousArrayBuffer` have no expectation of
// failure for advancing their indexes "out of bounds", because they are just
// `Int`.
resiliencyChecks.creatingOutOfBoundsIndicesBehavior = ${oob_behavior}
RangeReplaceableTestSuite.add${traversal}RangeReplaceable${collection_or_slice}Tests(
makeCollection: { (elements: [OpaqueValue<Int>]) in
% if Base in other_array_types:
return ${Base}(elements)
% else:
return ${Base}(elements: elements)
% end
},
wrapValue: identity,
extractValue: identity,
makeCollectionOfEquatable: { (elements: [MinimalEquatableValue]) in
% if Base in other_array_types:
return ${Base}(elements)
% else:
return ${Base}(elements: elements)
% end
},
wrapValueIntoEquatable: identityEq,
extractValueFromEquatable: identityEq,
resiliencyChecks: resiliencyChecks)
RangeReplaceableTestSuite.add${traversal}RangeReplaceable${collection_or_slice}Tests(
makeCollection: { (elements: [LifetimeTracked]) in
% if Base in other_array_types:
return ${Base}(elements)
% else:
return ${Base}(elements: elements)
% end
},
wrapValue: { (element: OpaqueValue<Int>) in LifetimeTracked(element.value) },
extractValue: { (element: LifetimeTracked) in OpaqueValue(element.value) },
makeCollectionOfEquatable: { (elements: [MinimalEquatableValue]) in
// FIXME: use LifetimeTracked.
% if Base in other_array_types:
return ${Base}(elements)
% else:
return ${Base}(elements: elements)
% end
},
wrapValueIntoEquatable: identityEq,
extractValueFromEquatable: identityEq,
resiliencyChecks: resiliencyChecks)
}
% end
runAllTests()