Skip to content

Commit 9925e5d

Browse files
author
Chris Williams
committed
Test initializers for FixedPoint numbers, testing trapping and failing initializers
1 parent af14925 commit 9925e5d

File tree

2 files changed

+159
-64
lines changed

2 files changed

+159
-64
lines changed

validation-test/stdlib/FixedPointArithmeticTraps.swift.gyb

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -299,69 +299,5 @@ FixedPointArithmeticTraps.test("${description}/${IntTy}/Max") {
299299

300300
% end
301301

302-
// FIXME: these tests should be more thorough, and test all combinations of
303-
// types and error conditions.
304-
var FixedPointTruncationTraps = TestSuite("FixedPointTruncationTraps")
305-
306-
FixedPointTruncationTraps.test("SignedToSignedTruncation/dest=sign-overflow") {
307-
// Test that we check if we overflow on the sign bit.
308-
var x = getInt16(128)
309-
expectCrashLater()
310-
var result = Int8(x)
311-
_blackHole(result)
312-
}
313-
314-
FixedPointTruncationTraps.test("SignedToUnsignedTruncation/src=-1") {
315-
var x = getInt32(-1)
316-
expectCrashLater()
317-
var result = UInt8(x)
318-
_blackHole(result)
319-
}
320-
321-
FixedPointTruncationTraps.test("SignedToUnsignedSameSize/src=min") {
322-
var x = getInt8(-128)
323-
expectCrashLater()
324-
var result = UInt16(x)
325-
_blackHole(result)
326-
}
327-
328-
329-
FixedPointTruncationTraps.test("SignedToUnsignedTruncation/src=max") {
330-
var x = getInt32(0xFFFFFFF)
331-
expectCrashLater()
332-
var result = UInt16(x)
333-
_blackHole(result)
334-
}
335-
336-
FixedPointTruncationTraps.test("UnsignedToSignedTruncation/dest=sign-overflow") {
337-
// Test that we check if we overflow on the sign bit.
338-
var x = getUInt16(128)
339-
expectCrashLater()
340-
var result = Int8(x)
341-
_blackHole(result)
342-
}
343-
344-
FixedPointTruncationTraps.test("UnsignedToUnsignedTruncation/src=max") {
345-
var x = getUInt32(0xFFFFFFFF)
346-
expectCrashLater()
347-
var result = UInt16(x)
348-
_blackHole(result)
349-
}
350-
351-
// Same size conversions.
352-
FixedPointTruncationTraps.test("SignedToUnsignedSameSize") {
353-
var x = getInt8(-2)
354-
expectCrashLater()
355-
var result = UInt8(x)
356-
_blackHole(result)
357-
}
358-
359-
FixedPointTruncationTraps.test("UnsignedToSignedSameSize") {
360-
var x = getUInt8(128)
361-
expectCrashLater()
362-
var result = Int8(x)
363-
_blackHole(result)
364-
}
365-
366302
runAllTests()
367303

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
// RUN: %S/../../utils/gyb %s -o %t/FixedPointConversion.swift
4+
// RUN: %S/../../utils/line-directive %t/FixedPointConversion.swift -- %target-build-swift %t/FixedPointConversion.swift -o %t/a.out_Debug
5+
// RUN: %S/../../utils/line-directive %t/FixedPointConversion.swift -- %target-build-swift %t/FixedPointConversion.swift -o %t/a.out_Release -O
6+
//
7+
// RUN: %S/../../utils/line-directive %t/FixedPointConversion.swift -- %target-run %t/a.out_Debug
8+
// RUN: %S/../../utils/line-directive %t/FixedPointConversion.swift -- %target-run %t/a.out_Release
9+
// REQUIRES: executable_test
10+
11+
%{
12+
13+
from SwiftIntTypes import all_integer_types
14+
15+
word_bits = 4
16+
17+
def intMax(bits, signed):
18+
bits = bits - 1 if signed else bits
19+
return (1 << bits) - 1
20+
21+
def intMin(bits, signed):
22+
return -1 * intMax(bits, signed) - 1 if signed else 0
23+
24+
floatNameToSignificandBits = { 'Float32':24, 'Float64':53, 'Float80':64 }
25+
26+
}%
27+
28+
import StdlibUnittest
29+
30+
var FixedPointConversionTraps = TestSuite("FixedPointToFixedPointConversionTraps")
31+
var FixedPointConversionFailure = TestSuite("FixedPointToFixedPointConversionFailures")
32+
33+
var FloatingPointConversionTruncations = TestSuite("FloatingPointToFixedPointConversionTruncations")
34+
var FloatingPointConversionFailure = TestSuite("FloatingPointToFixedPointConversionFailures")
35+
36+
% for self_ty in all_integer_types(word_bits):
37+
% selfBits = self_ty.bits
38+
% selfSigned = self_ty.is_signed
39+
% selfMin = intMin(selfBits, selfSigned)
40+
% selfMax = intMax(selfBits, selfSigned)
41+
% Self = self_ty.stdlib_name
42+
43+
% for other_ty in all_integer_types(word_bits):
44+
% otherBits = other_ty.bits
45+
% otherSigned = other_ty.is_signed
46+
% otherMin = intMin(otherBits, otherSigned)
47+
% otherMax = intMax(otherBits, otherSigned)
48+
% Other = other_ty.stdlib_name
49+
50+
% for testValue in [selfMin, selfMax, selfMin - 1, selfMax + 1, otherMin, otherMax]:
51+
52+
% if testValue < otherMin or testValue > otherMax:
53+
% # Do nothing/continue.
54+
55+
% elif testValue >= selfMin and testValue <= selfMax:
56+
57+
/// Always-safe conversion from ${Other}(${testValue}) to ${Self}.
58+
FixedPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}") {
59+
// Test that nothing interesting happens and we end up with the same result after converting.
60+
let input = get${Other}(${testValue})
61+
let result = ${Self}(input)
62+
expectEqual(${testValue}, result)
63+
_blackHole(result)
64+
}
65+
66+
/// Never-nil failable conversion from ${Other}(${testValue}) to ${Self}.
67+
FixedPointConversionFailure.test("${Other}To${Self}FailableConversion/dest=${testValue}") {
68+
// Test that nothing interesting happens and we end up with a non-nil, identical result.
69+
let input = get${Other}(${testValue})
70+
var result = ${Self}(exactly: input)
71+
expectNotEqual(result, nil)
72+
expectEqual(${testValue}, result)
73+
_blackHole(result)
74+
}
75+
76+
% else:
77+
78+
/// Always-failing conversion from ${Other}(${testValue}) to ${Self}.
79+
FixedPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}") {
80+
// Test that we check if we fail and crash when an integer would be truncated in conversion.
81+
let input = get${Other}(${testValue})
82+
expectCrashLater()
83+
var result = ${Self}(input)
84+
_blackHole(result)
85+
}
86+
87+
/// Always-nil failable conversion from ${Other}(${testValue}) to ${Self}.
88+
FixedPointConversionFailure.test("${Other}To${Self}Conversion/dest=${testValue}") {
89+
// Test that we check if we return nil when an integer would be truncated in conversion.
90+
let input = get${Other}(${testValue})
91+
var result = ${Self}(exactly: input)
92+
expectEqual(nil, result)
93+
_blackHole(result)
94+
}
95+
% end
96+
97+
% end # for testValue in ...
98+
% end # for in all_integer_types (Other)
99+
100+
% for Other, otherSignificandBits in floatNameToSignificandBits.iteritems():
101+
% otherMin = intMin(otherSignificandBits, False)
102+
% otherMax = intMax(otherSignificandBits, False)
103+
104+
% if Other == 'Float80':
105+
#if arch(i386) || arch(x86_64)
106+
% end
107+
108+
% for testValue in [selfMin, selfMax, selfMin - 1.0, selfMax + 1.0, otherMin, otherMax, 0.0, -0.0, 0.1, -0.1]:
109+
110+
% if testValue < otherMin or testValue > otherMax:
111+
% # Do nothing/continue.
112+
113+
% elif testValue >= selfMin and testValue <= selfMax and testValue % 1 == 0:
114+
115+
/// Always-safe conversion from ${Other}(${testValue}) to ${Self}.
116+
FloatingPointConversionTruncations.test("${Other}To${Self}Conversion/dest=${testValue}") {
117+
// Test that nothing interesting happens and we end up with the same result after converting.
118+
let input = get${Other}(${testValue})
119+
let result = ${Self}(input)
120+
var resultConvertedBack = ${Other}(result)
121+
expectEqual(${testValue}, resultConvertedBack)
122+
}
123+
124+
/// Never-nil failable conversion from ${Other}(${testValue}) to ${Self}.
125+
FloatingPointConversionFailure.test("${Other}To${Self}FailableConversion/dest=${testValue}") {
126+
// Test that nothing interesting happens and we end up with a non-nil, identical result.
127+
let input = get${Other}(${testValue})
128+
var result = ${Self}(exactly: input)
129+
expectNotEqual(result, nil)
130+
}
131+
132+
% else:
133+
134+
/// Always-truncating conversion from ${Other}(${testValue}) to ${Self}.
135+
FloatingPointConversionTruncations.test("${Other}To${Self}Conversion/dest=${testValue}") {
136+
// Test that we check if we fail and crash when an integer would be truncated in conversion.
137+
let input = get${Other}(${testValue})
138+
var result = ${Self}(input)
139+
var resultConvertedBack = ${Other}(result)
140+
expectNotEqual(input, resultConvertedBack)
141+
}
142+
143+
/// Always-nil failable conversion from ${Other}(${testValue}) to ${Self}.
144+
FloatingPointConversionFailure.test("${Other}To${Self}Conversion/dest=${testValue}") {
145+
// Test that we check if we return nil when an integer would be truncated in conversion.
146+
let input = get${Other}(${testValue})
147+
var result = ${Self}(exactly: input)
148+
expectEqual(nil, result)
149+
}
150+
% end
151+
152+
% end # for in testValues
153+
154+
% if Other == 'Float80':
155+
#endif
156+
% end
157+
158+
% end # for in floatNameToSignificandBits (Other)
159+
% end # for in all_integer_types (Self)

0 commit comments

Comments
 (0)