Skip to content

Commit ccdc4ca

Browse files
authored
Merge pull request #3273 from apple/stdlib-simplify-floating-point-test
2 parents 423c1f0 + 8507dec commit ccdc4ca

File tree

1 file changed

+53
-100
lines changed

1 file changed

+53
-100
lines changed

validation-test/stdlib/FloatingPointConversion.swift.gyb

Lines changed: 53 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import StdlibUnittest
1212

13-
1413
%{
1514

1615
floatNameToSignificandBits = { 'Float32':24, 'Float64':53, 'Float80':64 }
@@ -34,120 +33,74 @@ var FloatingPointConversionFailures = TestSuite("FloatingPointToFloatingPointCon
3433

3534
% if otherSignificandBits <= selfSignificandBits:
3635

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
36+
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion")
37+
.forEach(in: [
38+
${OtherFloat}.greatestFiniteMagnitude,
39+
-${OtherFloat}.greatestFiniteMagnitude,
40+
(1.0 as ${OtherFloat}).nextUp,
41+
(1.0 as ${OtherFloat}).nextDown,
42+
(-1.0 as ${OtherFloat}).nextUp,
43+
(-1.0 as ${OtherFloat}).nextDown,
44+
]) {
45+
input in
46+
// FIXME: we should have a stronger postcondition here.
4147
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!)
48+
let resultConvertedBack = ${OtherFloat}(result)
7249
expectEqual(input, resultConvertedBack)
7350
}
7451

7552
% else:
7653

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)
54+
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion")
55+
.forEach(in: [
56+
( ${OtherFloat}.greatestFiniteMagnitude, ${Self}.infinity),
57+
(-${OtherFloat}.greatestFiniteMagnitude, -${Self}.infinity),
58+
( (1.0 as ${OtherFloat}).nextUp, 1.0 as ${Self}),
59+
( (1.0 as ${OtherFloat}).nextDown, 1.0 as ${Self}),
60+
((-1.0 as ${OtherFloat}).nextUp, -1.0 as ${Self}),
61+
((-1.0 as ${OtherFloat}).nextDown, -1.0 as ${Self}),
62+
]) {
63+
(input, expectedResult) in
64+
expectEqual(expectedResult, ${Self}(input))
10965
}
11066

11167
% end
11268

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)
69+
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/special") {
70+
expectEqual( 1.0 as ${Self}, ${Self}(exactly: 1.0 as ${OtherFloat}))
71+
expectEqual(-1.0 as ${Self}, ${Self}(exactly: -1.0 as ${OtherFloat}))
72+
expectEqual( ${Self}.infinity, ${Self}( ${OtherFloat}.infinity))
73+
expectEqual(-${Self}.infinity, ${Self}(-${OtherFloat}.infinity))
74+
expectTrue(${Self}(${OtherFloat}.nan).isNaN)
11975
}
12076

121-
FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/dest=NaN") {
122-
let input = ${OtherFloat}.nan
123-
var result = ${Self}(exactly: input)
77+
FloatingPointConversionFailures.test("${OtherFloat}To${Self}FailableConversion")
78+
.forEach(in: [
79+
${OtherFloat}.greatestFiniteMagnitude,
80+
-${OtherFloat}.greatestFiniteMagnitude,
81+
(1.0 as ${OtherFloat}).nextUp,
82+
(1.0 as ${OtherFloat}).nextDown,
83+
(-1.0 as ${OtherFloat}).nextUp,
84+
(-1.0 as ${OtherFloat}).nextDown,
85+
]) {
86+
input in
87+
let result = ${Self}(exactly: input)
88+
% if otherSignificandBits <= selfSignificandBits:
89+
if let result = expectNotEmpty(result) {
90+
// FIXME: we should have a stronger postcondition here.
91+
expectEqual(input, ${OtherFloat}(result))
92+
}
93+
% else:
12494
expectEmpty(result)
95+
% end
12596
}
12697

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)
98+
FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/AlwaysSuccess") {
99+
expectEqual( 1.0 as ${Self}, ${Self}(exactly: 1.0 as ${OtherFloat}))
100+
expectEqual(-1.0 as ${Self}, ${Self}(exactly: -1.0 as ${OtherFloat}))
101+
expectEqual( ${Self}.infinity, ${Self}(exactly: ${OtherFloat}.infinity))
102+
expectEqual(-${Self}.infinity, ${Self}(exactly: -${OtherFloat}.infinity))
103+
expectEmpty(${Self}(exactly: ${OtherFloat}.nan))
151104
}
152105

153106
% if OtherFloat == 'Float80':

0 commit comments

Comments
 (0)