Skip to content

Commit ae140e6

Browse files
ultramiraculousChris Williams
authored andcommitted
Tests for Float->Int Initializers
1 parent c3ac97d commit ae140e6

File tree

2 files changed

+135
-122
lines changed

2 files changed

+135
-122
lines changed

validation-test/stdlib/FixedPointConversion.swift.gyb

Lines changed: 135 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,40 @@ import StdlibUnittest
1717
var FixedPointConversionTraps = TestSuite("FixedPointToFixedPointConversionTraps")
1818
var FixedPointConversionFailure = TestSuite("FixedPointToFixedPointConversionFailures")
1919

20+
var FloatingPointConversionTruncations = TestSuite("FloatingPointToFixedPointConversionTruncations")
21+
var FloatingPointConversionTraps = TestSuite("FloatingPointConversionTraps")
22+
var FloatingPointConversionFailures = TestSuite("FloatingPointToFixedPointConversionFailures")
23+
24+
func getInfiniteOrNaNMessage() -> String {
25+
if _isDebugAssertConfiguration() {
26+
return "either infinite or NaN"
27+
}
28+
return ""
29+
}
30+
31+
func getTooSmallMessage() -> String {
32+
if _isDebugAssertConfiguration() {
33+
return "would be less than"
34+
}
35+
return ""
36+
}
37+
38+
func getTooLargeMessage() -> String {
39+
if _isDebugAssertConfiguration() {
40+
return "would be greater than"
41+
}
42+
return ""
43+
}
44+
2045
%{
2146

2247
int_to_int_conversion_template = gyb.parse_template("int_to_int_conversion",
2348
"""
2449
%{
2550
from SwiftIntTypes import all_integer_types
2651

52+
floatNameToSignificandBits = { 'Float32':24, 'Float64':53, 'Float80':64 }
53+
2754
def intMax(bits, signed):
2855
bits = bits - 1 if signed else bits
2956
return (1 << bits) - 1
@@ -60,17 +87,14 @@ FixedPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}")
6087
let input = get${Other}(${testValue})
6188
let result = ${Self}(input)
6289
expectEqual(${testValue}, result)
63-
_blackHole(result)
6490
}
6591

6692
/// Never-nil failable conversion from ${Other}(${testValue}) to ${Self}.
6793
FixedPointConversionFailure.test("${Other}To${Self}FailableConversion/dest=${testValue}") {
6894
// Test that nothing interesting happens and we end up with a non-nil, identical result.
6995
let input = get${Other}(${testValue})
7096
var result = ${Self}(exactly: input)
71-
expectNotEqual(result, nil)
7297
expectEqual(${testValue}, result)
73-
_blackHole(result)
7498
}
7599

76100
% else:
@@ -88,15 +112,120 @@ FixedPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}")
88112
FixedPointConversionFailure.test("${Other}To${Self}Conversion/dest=${testValue}") {
89113
// Test that we check if we return nil when an integer would be truncated in conversion.
90114
let input = get${Other}(${testValue})
91-
var result = ${Self}(exactly: input)
92-
expectEqual(nil, result)
93-
_blackHole(result)
115+
expectEmpty(${Self}(exactly: input))
94116
}
95117
% end
96118

97119
% end # for testValue in ...
98120
% end # for in all_integer_types (Other)
121+
122+
% for Other, otherSignificandBits in floatNameToSignificandBits.iteritems():
123+
% otherMin = intMin(otherSignificandBits, False)
124+
% otherMax = intMax(otherSignificandBits, False)
125+
126+
% if Other == 'Float80':
127+
#if !os(Windows) && (arch(i386) || arch(x86_64))
128+
% end
129+
130+
% for testValue in [repr(value) for value in [selfMin, selfMax, selfMin - 0.1, selfMax + 0.1, otherMin, otherMax, 0.0, -0.0, 0.1, -0.1]]:
131+
132+
% if testValue < otherMin or testValue > otherMax:
133+
% # Can't construct `other` value to test from, do nothing and continue.
134+
135+
% elif testValue >= selfMin and testValue <= selfMax and testValue % 1 == 0:
136+
137+
FloatingPointConversionTruncations.test("${Other}To${Self}Conversion/dest=${testValue}") {
138+
let input = get${Other}(${testValue})
139+
let result = ${Self}(input)
140+
var resultConvertedBack = ${Other}(result)
141+
expectEqual(${testValue}, resultConvertedBack)
142+
}
143+
144+
FloatingPointConversionFailures.test("${Other}To${Self}FailableConversion/dest=${testValue}") {
145+
let input = get${Other}(${testValue})
146+
expectEmpty(${Self}(exactly: input))
147+
}
148+
149+
% else:
150+
151+
% if testValue > selfMax:
152+
FloatingPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}").crashOutputMatches(getTooLargeMessage()).code {
153+
expectCrashLater()
154+
% elif testValue < selfMin:
155+
FloatingPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}").crashOutputMatches(getTooSmallMessage()).code {
156+
expectCrashLater()
157+
% else:
158+
FloatingPointConversionTruncations.test("${Other}To${Self}Conversion/dest=${testValue}") {
159+
% end
160+
let input = get${Other}(${testValue})
161+
var result = ${Self}(input)
162+
var resultConvertedBack = ${Other}(result)
163+
expectNotEqual(input, resultConvertedBack)
164+
}
165+
166+
FloatingPointConversionFailures.test("${Other}To${Self}Conversion/dest=${testValue}") {
167+
let input = get${Other}(${testValue})
168+
expectEmpty(${Self}(exactly: input))
169+
}
170+
% end
171+
% end # for in testValues
172+
173+
// Test Always-Trapping conversions.
174+
175+
% if not selfSigned:
176+
177+
FloatingPointConversionTraps.test("${Self}/${Other}/negative")
178+
.crashOutputMatches(getTooSmallMessage()).code {
179+
expectCrashLater()
180+
_blackHole(${Self}(get${Other}(-123.0)))
181+
}
182+
183+
FloatingPointConversionFailures.test("${Self}/${Other}/negative")
184+
expectEmpty(${Self}(exactly: get${Other}(-123.0)))
185+
}
186+
187+
188+
% end
189+
190+
FloatingPointConversionTraps.test("${Self}/${Other}/+inf")
191+
.crashOutputMatches(getInfiniteOrNaNMessage()).code {
192+
expectCrashLater()
193+
_blackHole(${Self}(get${Other}(${Other}.infinity)))
194+
}
195+
196+
FloatingPointConversionFailures.test("${Self}/${Other}/+inf") {
197+
expectEmpty(${Self}(exactly: get${Other}(${Other}.infinity)))
198+
}
199+
200+
FloatingPointConversionTraps.test("${Self}/${Other}/-inf")
201+
.crashOutputMatches(getInfiniteOrNaNMessage()).code {
202+
expectCrashLater()
203+
_blackHole(${Self}(get${Other}(-${Other}.infinity)))
204+
}
205+
206+
FloatingPointConversionFailures.test("${Self}/${Other}/-inf") {
207+
let result = ${Self}(exactly: get${Other}(-${Other}.infinity))
208+
expectEmpty(result)
209+
}
210+
211+
FloatingPointConversionTraps.test("${Self}/${Other}/NaN")
212+
.crashOutputMatches(getInfiniteOrNaNMessage()).code {
213+
expectCrashLater()
214+
_blackHole(${Self}(get${Other}(${Other}.nan)))
215+
}
216+
217+
FloatingPointConversionFailures.test("${Self}/${Other}/NaN") {
218+
let result = ${Self}(exactly: get${Other}(${Other}.nan))
219+
expectEmpty(result)
220+
}
221+
222+
% if Other == 'Float80':
223+
#endif
224+
% end
225+
226+
% end # for in floatNameToSignificandBits (Other)
99227
% end # for in all_integer_types (Self)
228+
100229
""")
101230

102231
}%

validation-test/stdlib/FloatingPointConversionTraps.swift.gyb

Lines changed: 0 additions & 116 deletions
This file was deleted.

0 commit comments

Comments
 (0)