Skip to content

Commit 025a699

Browse files
ultramiraculousChris Williams
authored andcommitted
Float->Float Failable Initializer Tests
1 parent 26623fd commit 025a699

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
// RUN: %gyb %s -o %t/FloatingPointConversion.swift
4+
// RUN: %line-directive %t/FloatingPointConversion.swift -- %target-build-swift %t/FloatingPointConversion.swift -Xfrontend -disable-access-control -o %t/a.out_Debug
5+
// RUN: %line-directive %t/FloatingPointConversion.swift -- %target-build-swift %t/FloatingPointConversion.swift -Xfrontend -disable-access-control -o %t/a.out_Release -O
6+
//
7+
// RUN: %line-directive %t/FloatingPointConversion.swift -- %target-run %t/a.out_Debug
8+
// RUN: %line-directive %t/FloatingPointConversion.swift -- %target-run %t/a.out_Release
9+
// REQUIRES: executable_test
10+
11+
import StdlibUnittest
12+
13+
14+
%{
15+
16+
floatNameToSignificandBits = { 'Float32':24, 'Float64':53, 'Float80':64 }
17+
18+
}%
19+
20+
var FloatingPointConversionTruncations = TestSuite("FloatingPointToFloatingPointConversionTruncations")
21+
var FloatingPointConversionFailures = TestSuite("FloatingPointToFloatingPointConversionFailures")
22+
23+
% for Self, selfSignificandBits in floatNameToSignificandBits.iteritems():
24+
25+
% if Self == 'Float80':
26+
#if arch(i386) || arch(x86_64)
27+
% end
28+
29+
% for OtherFloat, otherSignificandBits in floatNameToSignificandBits.iteritems():
30+
31+
% if OtherFloat == 'Float80':
32+
#if arch(i386) || arch(x86_64)
33+
% end
34+
35+
% if otherSignificandBits <= selfSignificandBits:
36+
37+
/// Always-safe conversion from ${OtherFloat}.greatestFiniteMagnitude to ${Self}.
38+
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/dest=${OtherFloat}.greatestFiniteMagnitude") {
39+
// Test that we don't truncate floats that shouldn't be truncated.
40+
let input = ${OtherFloat}.greatestFiniteMagnitude
41+
let result = ${Self}(input)
42+
var resultConvertedBack = ${OtherFloat}(result)
43+
expectEqual(input, resultConvertedBack)
44+
}
45+
46+
/// Never-truncating conversion from ${OtherFloat}.nextUp to ${Self}.
47+
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/dest=${OtherFloat}.1.0.nextUp") {
48+
// Test that we don't truncate floats that shouldn't be truncated.
49+
let input = (1.0 as ${OtherFloat}).nextUp
50+
var result = ${Self}(input)
51+
var resultConvertedBack = ${OtherFloat}(result)
52+
expectEqual(input, resultConvertedBack)
53+
}
54+
55+
/// Never-nil failable conversion from ${OtherFloat}.greatestFiniteMagnitude to ${Self}.
56+
FloatingPointConversionFailures.test("${OtherFloat}To${Self}FailableConversion/dest=${OtherFloat}.greatestFiniteMagnitude") {
57+
// Test that nothing interesting happens and we end up with a non-nil, identical result.
58+
let input = ${OtherFloat}.greatestFiniteMagnitude
59+
var result = ${Self}(exactly: input)
60+
expectNotEmpty(result)
61+
var resultConvertedBack = ${OtherFloat}(result!)
62+
expectEqual(input, resultConvertedBack)
63+
}
64+
65+
/// Never-nil conversion from ${OtherFloat}.nextUp to ${Self}.
66+
FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/dest=${OtherFloat}.1.0.nextUp") {
67+
// Test that nothing interesting happens and we end up with a non-nil, identical result.
68+
let input = (1.0 as ${OtherFloat}).nextUp
69+
var result = ${Self}(exactly: input)
70+
expectNotEmpty(result)
71+
var resultConvertedBack = ${OtherFloat}(result!)
72+
expectEqual(input, resultConvertedBack)
73+
}
74+
75+
% else:
76+
77+
/// Always-succeeding, but out-of-range conversion from ${OtherFloat}.greatestFiniteMagnitude to ${Self}.
78+
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/dest=${OtherFloat}.greatestFiniteMagnitude") {
79+
// Test that we check if we truncate floats not representable by another type.
80+
let input = ${OtherFloat}.greatestFiniteMagnitude
81+
var result = ${Self}(input)
82+
var resultConvertedBack = ${OtherFloat}(result)
83+
expectEqual(${OtherFloat}.infinity, resultConvertedBack)
84+
}
85+
86+
/// Always-truncating conversion from ${OtherFloat}.nextUp to ${Self}.
87+
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/dest=${OtherFloat}.1.0.nextUp") {
88+
// Test that we check if we truncate floats not representable by another type.
89+
let input = (1.0 as ${OtherFloat}).nextUp
90+
var result = ${Self}(input)
91+
var resultConvertedBack = ${OtherFloat}(result)
92+
expectEqual(1.0, resultConvertedBack)
93+
}
94+
95+
/// Always-nil failable conversion from ${OtherFloat}.greatestFiniteMagnitude to ${Self}.
96+
FloatingPointConversionFailures.test("${OtherFloat}To${Self}FailableConversion/dest=${OtherFloat}.greatestFiniteMagnitude") {
97+
// Test that we check if we return nil when a float would be truncated in conversion.
98+
let input = ${OtherFloat}.greatestFiniteMagnitude
99+
var result = ${Self}(exactly: input)
100+
expectEmpty(result)
101+
}
102+
103+
/// Always-nil failable conversion from ${OtherFloat}.nextUp to ${Self}.
104+
FloatingPointConversionFailures.test("${OtherFloat}To${Self}FailableConversion/dest=${OtherFloat}.1.0.nextUp") {
105+
// Test that we check if we return nil when a float would be truncated in conversion.
106+
let input = (1.0 as ${OtherFloat}).nextUp
107+
var result = ${Self}(exactly: input)
108+
expectEmpty(result)
109+
}
110+
111+
% end
112+
113+
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/dest=NaN") {
114+
let input = ${OtherFloat}.nan
115+
var result = ${Self}(input)
116+
var resultConvertedBack = ${OtherFloat}(result)
117+
expectTrue(input.isNaN)
118+
expectTrue(resultConvertedBack.isNaN)
119+
}
120+
121+
FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/dest=NaN") {
122+
let input = ${OtherFloat}.nan
123+
var result = ${Self}(exactly: input)
124+
expectEmpty(result)
125+
}
126+
127+
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/dest=inf") {
128+
let input = ${OtherFloat}.infinity
129+
var result = ${Self}(input)
130+
var resultConvertedBack = ${OtherFloat}(result)
131+
expectEqual(input, resultConvertedBack)
132+
}
133+
134+
FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/dest=inf") {
135+
let input = ${OtherFloat}.infinity
136+
var result = ${Self}(exactly: input)
137+
expectEqual(${Self}.infinity, result)
138+
}
139+
140+
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/dest=-inf") {
141+
let input = -${OtherFloat}.infinity
142+
var result = ${Self}(input)
143+
var resultConvertedBack = ${OtherFloat}(result)
144+
expectEqual(input, resultConvertedBack)
145+
}
146+
147+
FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/dest=-inf") {
148+
let input = -${OtherFloat}.infinity
149+
var result = ${Self}(exactly: input)
150+
expectEqual(-${Self}.infinity, result)
151+
}
152+
153+
% if OtherFloat == 'Float80':
154+
#endif
155+
% end
156+
157+
% end # for in floatNameToSignificandBits (Other)
158+
159+
% if Self == 'Float80':
160+
#endif
161+
% end
162+
163+
% end # for in floatNameToSignificandBits (Self)
164+
165+
runAllTests()

0 commit comments

Comments
 (0)