-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathFloatingPointConversion.swift.gyb
194 lines (154 loc) · 6.16 KB
/
FloatingPointConversion.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
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: %gyb %s -o %t/FloatingPointConversion.swift
// RUN: %line-directive %t/FloatingPointConversion.swift -- %target-build-swift %t/FloatingPointConversion.swift -Xfrontend -disable-access-control -o %t/a.out_Debug
// RUN: %line-directive %t/FloatingPointConversion.swift -- %target-build-swift %t/FloatingPointConversion.swift -Xfrontend -disable-access-control -o %t/a.out_Release -O
//
// RUN: %line-directive %t/FloatingPointConversion.swift -- %target-run %t/a.out_Debug
// RUN: %line-directive %t/FloatingPointConversion.swift -- %target-run %t/a.out_Release
// REQUIRES: executable_test
import StdlibUnittest
%{
import gyb
from SwiftFloatingPointTypes import all_floating_point_types, getFtoIBounds
from SwiftIntTypes import all_integer_types
}%
var FixedPointConversionTruncations = TestSuite("FixedPointToFloatingPointConversionTruncations")
var FixedPointConversionFailures = TestSuite("FixedPointToFloatingPointConversionFailures")
var FloatingPointConversionTruncations = TestSuite("FloatingPointToFloatingPointConversionTruncations")
var FloatingPointConversionFailures = TestSuite("FloatingPointToFloatingPointConversionFailures")
% for self_type in all_floating_point_types():
% SelfSignificandBits = self_type.bits
% Self = self_type.stdlib_name
% if Self == 'Float80':
#if !os(Windows) && (arch(i386) || arch(x86_64))
% end
% for other_type in all_floating_point_types():
% OtherSignificandBits = other_type.bits
% OtherFloat = other_type.stdlib_name
% if OtherFloat == 'Float80':
#if !os(Windows) && (arch(i386) || arch(x86_64))
% end
% if OtherSignificandBits <= SelfSignificandBits:
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion")
.forEach(in: [
${OtherFloat}.greatestFiniteMagnitude,
-${OtherFloat}.greatestFiniteMagnitude,
(1.0 as ${OtherFloat}).nextUp,
(1.0 as ${OtherFloat}).nextDown,
(-1.0 as ${OtherFloat}).nextUp,
(-1.0 as ${OtherFloat}).nextDown,
]) {
input in
// FIXME: we should have a stronger postcondition here.
let result = ${Self}(input)
let resultConvertedBack = ${OtherFloat}(result)
expectEqual(input, resultConvertedBack)
}
% else:
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion")
.forEach(in: [
( ${OtherFloat}.greatestFiniteMagnitude, ${Self}.infinity),
(-${OtherFloat}.greatestFiniteMagnitude, -${Self}.infinity),
( (1.0 as ${OtherFloat}).nextUp, 1.0 as ${Self}),
( (1.0 as ${OtherFloat}).nextDown, 1.0 as ${Self}),
((-1.0 as ${OtherFloat}).nextUp, -1.0 as ${Self}),
((-1.0 as ${OtherFloat}).nextDown, -1.0 as ${Self}),
]) {
(input, expectedResult) in
expectEqual(expectedResult, ${Self}(input))
}
% end
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/special") {
expectEqual( 1.0 as ${Self}, ${Self}(exactly: 1.0 as ${OtherFloat}))
expectEqual(-1.0 as ${Self}, ${Self}(exactly: -1.0 as ${OtherFloat}))
expectEqual( ${Self}.infinity, ${Self}( ${OtherFloat}.infinity))
expectEqual(-${Self}.infinity, ${Self}(-${OtherFloat}.infinity))
expectTrue(${Self}(${OtherFloat}.nan).isNaN)
}
FloatingPointConversionFailures.test("${OtherFloat}To${Self}FailableConversion")
.forEach(in: [
${OtherFloat}.greatestFiniteMagnitude,
-${OtherFloat}.greatestFiniteMagnitude,
(1.0 as ${OtherFloat}).nextUp,
(1.0 as ${OtherFloat}).nextDown,
(-1.0 as ${OtherFloat}).nextUp,
(-1.0 as ${OtherFloat}).nextDown,
]) {
input in
let result = ${Self}(exactly: input)
% if OtherSignificandBits <= SelfSignificandBits:
if let result = expectNotNil(result) {
// FIXME: we should have a stronger postcondition here.
expectEqual(input, ${OtherFloat}(result))
}
% else:
expectNil(result)
% end
}
FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/AlwaysSuccess") {
expectEqual( 1.0 as ${Self}, ${Self}(exactly: 1.0 as ${OtherFloat}))
expectEqual(-1.0 as ${Self}, ${Self}(exactly: -1.0 as ${OtherFloat}))
expectEqual( ${Self}.infinity, ${Self}(exactly: ${OtherFloat}.infinity))
expectEqual(-${Self}.infinity, ${Self}(exactly: -${OtherFloat}.infinity))
expectNil(${Self}(exactly: ${OtherFloat}.nan))
}
% if OtherFloat == 'Float80':
#endif
% end
% end # for in all_floating_point_types (Other)
%{
float_to_int_conversion_template = gyb.parse_template("float_to_int_conversion",
"""
% for int_ty in all_integer_types(word_bits):
% OtherInt = int_ty.stdlib_name
% OtherMin = int_ty.min
% OtherMax = int_ty.max
% (FloatMin, FloatMax) = getFtoIBounds(self_type.bits, int_ty.bits, int_ty.is_signed)
% for testValue in [0, FloatMin, FloatMax, FloatMin - 1, FloatMax + 1, OtherMin, OtherMax]:
% if testValue < OtherMin or testValue > OtherMax:
% # Can't construct `other` value, do nothing and continue.
% elif testValue >= FloatMin and testValue <= FloatMax:
FixedPointConversionTruncations.test("${OtherInt}to${Self}Conversion/${testValue}") {
expectEqual(${Self}(${testValue} as ${OtherInt}), ${testValue})
}
FixedPointConversionFailures.test("${OtherInt}to${Self}FailableConversion/${testValue}") {
expectEqual(${Self}(exactly: ${testValue} as ${OtherInt}), ${testValue})
}
% else:
FixedPointConversionTruncations.test("${OtherInt}to${Self}Truncation/${testValue}") {
let value: ${OtherInt} = ${testValue}
let result = ${Self}(value)
expectNotEqual(${OtherInt}(result), value)
}
FixedPointConversionFailures.test("${OtherInt}to${Self}Failure/${testValue}") {
let value: ${OtherInt} = ${testValue}
let result = ${Self}(exactly: value)
expectEqual(result, ${OtherMin} as ${Self})
expectEqual(${OtherInt}(result!), value)
}
% end
% end # testValue in testValues
% end # for in all_integer_types (Other)
""")
}%
#if arch(i386) || arch(arm)
${gyb.execute_template(
float_to_int_conversion_template,
word_bits=32,
**locals()
)}
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
${gyb.execute_template(
float_to_int_conversion_template,
word_bits=64,
**locals()
)}
#else
_UnimplementedError()
#endif
% if Self == 'Float80':
#endif
% end
% end # for in all_floating_point_types (Self)
runAllTests()