-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathArrayTraps.swift.gyb
192 lines (167 loc) · 5.54 KB
/
ArrayTraps.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// RUN: %empty-directory(%t)
// RUN: %gyb %s -o %t/ArrayTraps.swift
// RUN: %line-directive %t/ArrayTraps.swift -- %target-build-swift %t/ArrayTraps.swift -o %t/a.out_Debug -Onone
// RUN: %line-directive %t/ArrayTraps.swift -- %target-build-swift %t/ArrayTraps.swift -o %t/a.out_Release -O
//
// RUN: %target-codesign %t/a.out_Debug
// RUN: %target-codesign %t/a.out_Release
// RUN: %line-directive %t/ArrayTraps.swift -- %target-run %t/a.out_Debug
// RUN: %line-directive %t/ArrayTraps.swift -- %target-run %t/a.out_Release
// REQUIRES: executable_test
import StdlibUnittest
let testSuiteSuffix = _isDebugAssertConfiguration() ? "_debug" : "_release"
%{
# We test for bounds-checking traps for both reading and writing
# both single elements and slices of all three different array
# types.
array_types = ('Array', 'ContiguousArray', 'ArraySlice')
def bounds_trap(index, expr_to_write):
global io
return 'expectCrashLater()\n' + (
'let x = a[%s]' % index
if io == 'read' else
'a[%s] = %s' % (index, expr_to_write))
}%
% for ArrayTy in array_types:
var ${ArrayTy}Traps = TestSuite("${ArrayTy}Traps" + testSuiteSuffix)
var ${ArrayTy}LowerBoundOutOfRangeErrorMsg = (
% if ArrayTy in ['Array', 'ContiguousArray']:
"Negative ${ArrayTy} index is out of range"
% else:
"${ArrayTy} index is out of range (before startIndex)"
% end
)
var ${ArrayTy}BoundsErrorMsg = (
% if ArrayTy in ['Array', 'ContiguousArray']:
"Index out of range"
% else:
"Index out of bounds"
% end
)
% for io in ['read', 'write']:
${ArrayTy}Traps.test("bounds1/${io}")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.crashOutputMatches(_isDebugAssertConfiguration() ?
${ArrayTy}BoundsErrorMsg : "")
.code {
var a: ${ArrayTy}<Int> = []
${bounds_trap(index='0', expr_to_write='1')}
}
${ArrayTy}Traps.test("bounds2/${io}")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.crashOutputMatches(_isDebugAssertConfiguration() ?
${ArrayTy}BoundsErrorMsg : "")
.code {
var a: ${ArrayTy}<Int> = []
${bounds_trap(index='100', expr_to_write='1')}
}
${ArrayTy}Traps.test("bounds3/${io}")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.crashOutputMatches(_isDebugAssertConfiguration() ?
${ArrayTy}BoundsErrorMsg : "")
.code {
var a: ${ArrayTy} = [ 10, 20, 30 ]
${bounds_trap(index='3', expr_to_write='1')}
}
${ArrayTy}Traps.test("sliceBounds0/${io}")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.crashOutputMatches(_isDebugAssertConfiguration() ?
${ArrayTy}LowerBoundOutOfRangeErrorMsg : "")
.code {
var a: ${ArrayTy}<Int> = []
${bounds_trap(index='-1..<1', expr_to_write='ArraySlice()')}
}
${ArrayTy}Traps.test("sliceBounds1/${io}")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.crashOutputMatches(_isDebugAssertConfiguration() ?
${ArrayTy}LowerBoundOutOfRangeErrorMsg : "")
.code {
var a: ${ArrayTy} = [ 1 ]
${bounds_trap(index='-1..<1', expr_to_write='ArraySlice()')}
}
${ArrayTy}Traps.test("sliceBounds2/${io}")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.crashOutputMatches(_isDebugAssertConfiguration() ?
"${ArrayTy} index is out of range" : "")
.code {
var a: ${ArrayTy} = [ 1 ]
${bounds_trap(index='0..<2', expr_to_write='ArraySlice()')}
}
${ArrayTy}Traps.test("sliceBounds3/${io}")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.crashOutputMatches(_isDebugAssertConfiguration() ?
"${ArrayTy} index is out of range" : "")
.code {
var a: ${ArrayTy} = [ 1 ]
${bounds_trap(index='1..<2', expr_to_write='ArraySlice()')}
}
% end
${ArrayTy}Traps.test("PopFromEmpty")
.crashOutputMatches(_isDebugAssertConfiguration() ?
"Can't remove last element from an empty collection" : "")
.code {
var a: ${ArrayTy}<Int> = []
expectCrashLater()
a.removeLast()
}
% for index in -1, 2:
${ArrayTy}Traps.test("${'insert(_:at:)/%s' % index}")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.crashOutputMatches(_isDebugAssertConfiguration() ?
% if index < 0:
${ArrayTy}LowerBoundOutOfRangeErrorMsg
% else:
"${ArrayTy} index is out of range"
%end
: "")
.code {
var a: ${ArrayTy}<Int> = [42]
expectCrashLater()
a.insert(3, at: ${index})
}
% end
% for index in -1, 1, 2:
${ArrayTy}Traps.test("${'remove(at:)/%s' % index}")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.crashOutputMatches(_isDebugAssertConfiguration() ?
${ArrayTy}BoundsErrorMsg : "")
.code {
var a: ${ArrayTy}<Int> = [42]
expectCrashLater()
a.remove(at: ${index})
}
% end
% end
ArrayTraps.test("unsafeLength")
.skip(.custom(
{ !_isDebugAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -O or -Ounchecked"))
.crashOutputMatches(_isDebugAssertConfiguration() ?
"UnsafeBufferPointer with negative count" : "")
.code {
var a = [ 42, 77, 88 ]
expectEqual(42, a.withUnsafeBufferPointer({ $0[0] }))
expectCrashLater()
_ = a.withUnsafeBufferPointer {
UnsafeBufferPointer(start: $0.baseAddress, count: -1)
}
}
runAllTests()