@@ -17,13 +17,40 @@ import StdlibUnittest
17
17
var FixedPointConversionTraps = TestSuite("FixedPointToFixedPointConversionTraps")
18
18
var FixedPointConversionFailure = TestSuite("FixedPointToFixedPointConversionFailures")
19
19
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
+
20
45
%{
21
46
22
47
int_to_int_conversion_template = gyb.parse_template("int_to_int_conversion",
23
48
"""
24
49
%{
25
50
from SwiftIntTypes import all_integer_types
26
51
52
+ floatNameToSignificandBits = { 'Float32':24, 'Float64':53, 'Float80':64 }
53
+
27
54
def intMax(bits, signed):
28
55
bits = bits - 1 if signed else bits
29
56
return (1 << bits) - 1
@@ -60,17 +87,14 @@ FixedPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}")
60
87
let input = get${Other}(${testValue})
61
88
let result = ${Self}(input)
62
89
expectEqual(${testValue}, result)
63
- _blackHole(result)
64
90
}
65
91
66
92
/// Never-nil failable conversion from ${Other}(${testValue}) to ${Self}.
67
93
FixedPointConversionFailure.test("${Other}To${Self}FailableConversion/dest=${testValue}") {
68
94
// Test that nothing interesting happens and we end up with a non-nil, identical result.
69
95
let input = get${Other}(${testValue})
70
96
var result = ${Self}(exactly: input)
71
- expectNotEqual(result, nil)
72
97
expectEqual(${testValue}, result)
73
- _blackHole(result)
74
98
}
75
99
76
100
% else:
@@ -88,15 +112,120 @@ FixedPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}")
88
112
FixedPointConversionFailure.test("${Other}To${Self}Conversion/dest=${testValue}") {
89
113
// Test that we check if we return nil when an integer would be truncated in conversion.
90
114
let input = get${Other}(${testValue})
91
- var result = ${Self}(exactly: input)
92
- expectEqual(nil, result)
93
- _blackHole(result)
115
+ expectEmpty(${Self}(exactly: input))
94
116
}
95
117
% end
96
118
97
119
% end # for testValue in ...
98
120
% 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)
99
227
% end # for in all_integer_types (Self)
228
+
100
229
""")
101
230
102
231
}%
0 commit comments