Skip to content

Commit 2b6ccd2

Browse files
committed
Add tests for traps in shift operations
Swift SVN r21575
1 parent 4ecfc2b commit 2b6ccd2

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

validation-test/stdlib/FixedPointArithmeticTraps.swift.gyb

+61
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,67 @@ FixedPointArithmeticTraps.test("Remainder/${IntTy}.min-mod-minus-one") {
213213

214214
% end
215215

216+
% for (description, operation) in [("RightShift", ">>"), ("LeftShift", "<<")]:
217+
218+
//
219+
// Test ${description} for ${IntTy}
220+
//
221+
222+
% if signedness == 'signed':
223+
224+
FixedPointArithmeticTraps.test("${description}/${IntTy}/Negative") {
225+
var a = get${IntTy}(${IntTy}.max / 3)
226+
227+
a = get${IntTy}(a ${operation} get${IntTy}(1))
228+
229+
let shiftAmount: ${IntTy} = -1
230+
231+
// Overflow in ${description}.
232+
expectCrashLater()
233+
a = a ${operation} get${IntTy}(shiftAmount)
234+
}
235+
236+
% end
237+
238+
FixedPointArithmeticTraps.test("${description}/${IntTy}/TypeSize") {
239+
var a = get${IntTy}(${IntTy}.max / 3)
240+
241+
a = get${IntTy}(a ${operation} get${IntTy}(0))
242+
a = get${IntTy}(a ${operation} get${IntTy}(1))
243+
244+
let shiftAmount = ${IntTy}(sizeof(${IntTy}.self) * 8)
245+
246+
// Overflow in ${description}.
247+
expectCrashLater()
248+
a = a ${operation} get${IntTy}(shiftAmount)
249+
}
250+
251+
FixedPointArithmeticTraps.test("${description}/${IntTy}/TypeSizePlusOne") {
252+
var a = get${IntTy}(${IntTy}.max / 3)
253+
254+
a = get${IntTy}(a ${operation} get${IntTy}(0))
255+
256+
let shiftAmount = ${IntTy}(sizeof(${IntTy}.self) * 8 + 1)
257+
258+
// Overflow in ${description}.
259+
expectCrashLater()
260+
a = a ${operation} get${IntTy}(shiftAmount)
261+
}
262+
263+
FixedPointArithmeticTraps.test("${description}/${IntTy}/Max") {
264+
var a = get${IntTy}(${IntTy}.max / 3)
265+
266+
a = get${IntTy}(a ${operation} get${IntTy}(0))
267+
268+
let shiftAmount: ${IntTy} = ${IntTy}.max
269+
270+
// Overflow in ${description}.
271+
expectCrashLater()
272+
a = a ${operation} get${IntTy}(shiftAmount)
273+
}
274+
275+
% end
276+
216277
// This comment prevents gyb from miscompiling this file.
217278
// <rdar://problem/17548877> gyb miscompiles a certain for loop
218279

0 commit comments

Comments
 (0)