Skip to content

Commit 8507dec

Browse files
committed
stdlib: simplify test organization in FloatingPointConversion.swift.gyb
1 parent 3658d14 commit 8507dec

File tree

1 file changed

+49
-67
lines changed

1 file changed

+49
-67
lines changed

validation-test/stdlib/FloatingPointConversion.swift.gyb

Lines changed: 49 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -33,89 +33,71 @@ var FloatingPointConversionFailures = TestSuite("FloatingPointToFloatingPointCon
3333

3434
% if otherSignificandBits <= selfSignificandBits:
3535

36-
/// Always-safe conversion from ${OtherFloat}.greatestFiniteMagnitude to ${Self}.
37-
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/dest=${OtherFloat}.greatestFiniteMagnitude") {
38-
// Test that we don't truncate floats that shouldn't be truncated.
39-
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.
4047
let result = ${Self}(input)
41-
var resultConvertedBack = ${OtherFloat}(result)
42-
expectEqual(input, resultConvertedBack)
43-
}
44-
45-
/// Never-truncating conversion from ${OtherFloat}.nextUp to ${Self}.
46-
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/dest=${OtherFloat}.1.0.nextUp") {
47-
// Test that we don't truncate floats that shouldn't be truncated.
48-
let input = (1.0 as ${OtherFloat}).nextUp
49-
var result = ${Self}(input)
50-
var resultConvertedBack = ${OtherFloat}(result)
51-
expectEqual(input, resultConvertedBack)
52-
}
53-
54-
/// Never-nil failable conversion from ${OtherFloat}.greatestFiniteMagnitude to ${Self}.
55-
FloatingPointConversionFailures.test("${OtherFloat}To${Self}FailableConversion/dest=${OtherFloat}.greatestFiniteMagnitude") {
56-
// Test that nothing interesting happens and we end up with a non-nil, identical result.
57-
let input = ${OtherFloat}.greatestFiniteMagnitude
58-
var result = ${Self}(exactly: input)
59-
expectNotEmpty(result)
60-
var resultConvertedBack = ${OtherFloat}(result!)
61-
expectEqual(input, resultConvertedBack)
62-
}
63-
64-
/// Never-nil conversion from ${OtherFloat}.nextUp to ${Self}.
65-
FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/dest=${OtherFloat}.1.0.nextUp") {
66-
// Test that nothing interesting happens and we end up with a non-nil, identical result.
67-
let input = (1.0 as ${OtherFloat}).nextUp
68-
var result = ${Self}(exactly: input)
69-
expectNotEmpty(result)
70-
var resultConvertedBack = ${OtherFloat}(result!)
48+
let resultConvertedBack = ${OtherFloat}(result)
7149
expectEqual(input, resultConvertedBack)
7250
}
7351

7452
% else:
7553

76-
/// Always-succeeding, but out-of-range conversion from ${OtherFloat}.greatestFiniteMagnitude to ${Self}.
77-
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/dest=${OtherFloat}.greatestFiniteMagnitude") {
78-
// Test that we check if we truncate floats not representable by another type.
79-
let input = ${OtherFloat}.greatestFiniteMagnitude
80-
var result = ${Self}(input)
81-
var resultConvertedBack = ${OtherFloat}(result)
82-
expectEqual(${OtherFloat}.infinity, resultConvertedBack)
83-
}
84-
85-
/// Always-truncating conversion from ${OtherFloat}.nextUp to ${Self}.
86-
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/dest=${OtherFloat}.1.0.nextUp") {
87-
// Test that we check if we truncate floats not representable by another type.
88-
let input = (1.0 as ${OtherFloat}).nextUp
89-
var result = ${Self}(input)
90-
var resultConvertedBack = ${OtherFloat}(result)
91-
expectEqual(1.0, resultConvertedBack)
92-
}
93-
94-
/// Always-nil failable conversion from ${OtherFloat}.greatestFiniteMagnitude to ${Self}.
95-
FloatingPointConversionFailures.test("${OtherFloat}To${Self}FailableConversion/dest=${OtherFloat}.greatestFiniteMagnitude") {
96-
// Test that we check if we return nil when a float would be truncated in conversion.
97-
let input = ${OtherFloat}.greatestFiniteMagnitude
98-
var result = ${Self}(exactly: input)
99-
expectEmpty(result)
100-
}
101-
102-
/// Always-nil failable conversion from ${OtherFloat}.nextUp to ${Self}.
103-
FloatingPointConversionFailures.test("${OtherFloat}To${Self}FailableConversion/dest=${OtherFloat}.1.0.nextUp") {
104-
// Test that we check if we return nil when a float would be truncated in conversion.
105-
let input = (1.0 as ${OtherFloat}).nextUp
106-
var result = ${Self}(exactly: input)
107-
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))
10865
}
10966

11067
% end
11168

11269
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}))
11372
expectEqual( ${Self}.infinity, ${Self}( ${OtherFloat}.infinity))
11473
expectEqual(-${Self}.infinity, ${Self}(-${OtherFloat}.infinity))
11574
expectTrue(${Self}(${OtherFloat}.nan).isNaN)
11675
}
11776

118-
FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/special") {
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:
94+
expectEmpty(result)
95+
% end
96+
}
97+
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}))
119101
expectEqual( ${Self}.infinity, ${Self}(exactly: ${OtherFloat}.infinity))
120102
expectEqual(-${Self}.infinity, ${Self}(exactly: -${OtherFloat}.infinity))
121103
expectEmpty(${Self}(exactly: ${OtherFloat}.nan))

0 commit comments

Comments
 (0)