-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathFixedPointArithmeticTraps.swift.gyb
239 lines (184 loc) · 6.02 KB
/
FixedPointArithmeticTraps.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// RUN: %empty-directory(%t)
// RUN: %gyb %s -o %t/FixedPointArithmeticTraps.swift
// RUN: %line-directive %t/FixedPointArithmeticTraps.swift -- %target-build-swift %t/FixedPointArithmeticTraps.swift -o %t/a.out_Debug -Onone
// RUN: %line-directive %t/FixedPointArithmeticTraps.swift -- %target-build-swift %t/FixedPointArithmeticTraps.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/FixedPointArithmeticTraps.swift -- %target-run %t/a.out_Debug
// RUN: %line-directive %t/FixedPointArithmeticTraps.swift -- %target-run %t/a.out_Release
// REQUIRES: executable_test
import StdlibUnittest
// Note: in this file, we need to go through opaque functions to load
// constants. This is to check runtime behavior and ensure the constant is
// not folded.
func expectOverflow<T>(
_ res: (partialValue: T, overflow: Bool),
//===--- TRACE boilerplate ----------------------------------------------===//
_ message: @autoclosure () -> String = "",
showFrame: Bool = true,
stackTrace: SourceLocStack = SourceLocStack(),
file: String = #file, line: UInt = #line
) {
expectTrue(
res.overflow, "expected overflow",
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line))
}
func expectNoOverflow<T>(
_ res: (partialValue: T, overflow: Bool),
//===--- TRACE boilerplate ----------------------------------------------===//
_ message: @autoclosure () -> String = "",
showFrame: Bool = true,
stackTrace: SourceLocStack = SourceLocStack(),
file: String = #file, line: UInt = #line
) {
expectFalse(
res.overflow, "expected no overflow",
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line))
}
%{
from SwiftIntTypes import all_integer_types
# Test cases are written in a way that they don't depend on the word size.
word_bits = 4
}%
var FixedPointArithmeticTraps = TestSuite("FixedPointArithmeticTraps")
% for self_ty in all_integer_types(word_bits):
% IntTy = self_ty.stdlib_name
//
// Test pre- and post-increment/decrement for ${IntTy}
//
FixedPointArithmeticTraps.test("PreDecrement/${IntTy}") {
var x = get${IntTy}(${IntTy}.min)
x += 1
x = get${IntTy}(${IntTy}.min)
expectCrashLater()
// IntTy.min -= 1
x -= 1
_blackHole(x)
}
FixedPointArithmeticTraps.test("PreIncrement/${IntTy}") {
var x = get${IntTy}(${IntTy}.max)
x -= 1
x = get${IntTy}(${IntTy}.max)
expectCrashLater()
// IntTy.max += 1
x += 1
_blackHole(x)
}
FixedPointArithmeticTraps.test("PostDecrement/${IntTy}") {
var x = get${IntTy}(${IntTy}.min)
x += 1
x = get${IntTy}(${IntTy}.min)
expectCrashLater()
// IntTy.min -= 1
x -= 1
_blackHole(x)
}
FixedPointArithmeticTraps.test("PostIncrement/${IntTy}") {
var x = get${IntTy}(${IntTy}.max)
x -= 1
x = get${IntTy}(${IntTy}.max)
expectCrashLater()
// IntTy.max += 1
x += 1
_blackHole(x)
}
//
// Test addition for ${IntTy}
//
FixedPointArithmeticTraps.test("Addition/${IntTy}") {
var a = get${IntTy}(${IntTy}.max / 3)
expectNoOverflow(a.addingReportingOverflow(get${IntTy}(${IntTy}.max / 3)))
a = a + get${IntTy}(${IntTy}.max / 3)
expectNoOverflow(a.addingReportingOverflow(get${IntTy}(${IntTy}.max / 3)))
a = a + get${IntTy}(${IntTy}.max / 3)
// Overflow in addition.
expectOverflow(a.addingReportingOverflow(get${IntTy}(${IntTy}.max / 3)))
expectCrashLater()
a = a + get${IntTy}(${IntTy}.max / 3)
_blackHole(a)
}
//
// Test subtraction for ${IntTy}
//
FixedPointArithmeticTraps.test("Subtraction/${IntTy}") {
var a = get${IntTy}(${IntTy}.min + get${IntTy}(${IntTy}.max / 3))
expectNoOverflow(a.subtractingReportingOverflow(get${IntTy}(${IntTy}.max / 3)))
a = a - get${IntTy}(${IntTy}.max / 3)
// Overflow in subtraction.
expectOverflow(a.subtractingReportingOverflow(get${IntTy}(${IntTy}.max / 3)))
expectCrashLater()
a = a - get${IntTy}(${IntTy}.max / 3)
_blackHole(a)
}
//
// Test multiplication for ${IntTy}
//
FixedPointArithmeticTraps.test("Multiplication/${IntTy}") {
var a = get${IntTy}(${IntTy}.max / 3)
expectNoOverflow(a.multipliedReportingOverflow(by: get${IntTy}(2)))
a = a * get${IntTy}(2)
// Overflow in multiplication.
expectOverflow(a.multipliedReportingOverflow(by: get${IntTy}(2)))
expectCrashLater()
a = a * get${IntTy}(2)
_blackHole(a)
}
//
// Test division for ${IntTy}
//
FixedPointArithmeticTraps.test("Division/${IntTy}") {
var a = get${IntTy}(${IntTy}.max / 3)
// x / 3
expectNoOverflow(a.dividedReportingOverflow(by: get${IntTy}(3)))
a = a / get${IntTy}(3)
// x / 0
expectOverflow(a.dividedReportingOverflow(by: get${IntTy}(0)))
expectCrashLater()
a = a / get${IntTy}(0)
}
% if self_ty.is_signed:
FixedPointArithmeticTraps.test("Division/${IntTy}.min-over-minus-one") {
var a = get${IntTy}(${IntTy}.min)
expectNoOverflow(a.dividedReportingOverflow(by: get${IntTy}(3)))
a = a / get${IntTy}(3)
a = get${IntTy}(${IntTy}.min)
expectOverflow(a.dividedReportingOverflow(by: get${IntTy}(-1)))
expectCrashLater()
a = a / get${IntTy}(-1)
// IntTy.min / -1
_blackHole(a)
}
% end
//
// Test remainder computation for ${IntTy}
//
FixedPointArithmeticTraps.test("Remainder/${IntTy}") {
var a = get${IntTy}(${IntTy}.max / 3)
// x % 3
expectNoOverflow(a.remainderReportingOverflow(dividingBy: get${IntTy}(3)))
a = a % get${IntTy}(3)
// x % 0
expectOverflow(a.remainderReportingOverflow(dividingBy: get${IntTy}(0)))
expectCrashLater()
a = a % get${IntTy}(0)
_blackHole(a)
}
% if self_ty.is_signed:
FixedPointArithmeticTraps.test("Remainder/${IntTy}.min-mod-minus-one") {
var a = get${IntTy}(${IntTy}.min)
// Int.min % 3
expectNoOverflow(a.remainderReportingOverflow(dividingBy: get${IntTy}(3)))
a = a % get${IntTy}(3)
// Int.min % -1
a = get${IntTy}(${IntTy}.min)
expectOverflow(a.remainderReportingOverflow(dividingBy: get${IntTy}(-1)))
expectCrashLater()
a = a % get${IntTy}(-1)
_blackHole(a)
}
% end
// This comment prevents gyb from miscompiling this file.
// <rdar://problem/17548877> gyb miscompiles a certain for loop
% end
runAllTests()