@@ -33,89 +33,71 @@ var FloatingPointConversionFailures = TestSuite("FloatingPointToFloatingPointCon
33
33
34
34
% if otherSignificandBits <= selfSignificandBits:
35
35
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.
40
47
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)
71
49
expectEqual(input, resultConvertedBack)
72
50
}
73
51
74
52
% else:
75
53
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))
108
65
}
109
66
110
67
% end
111
68
112
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}))
113
72
expectEqual( ${Self}.infinity, ${Self}( ${OtherFloat}.infinity))
114
73
expectEqual(-${Self}.infinity, ${Self}(-${OtherFloat}.infinity))
115
74
expectTrue(${Self}(${OtherFloat}.nan).isNaN)
116
75
}
117
76
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}))
119
101
expectEqual( ${Self}.infinity, ${Self}(exactly: ${OtherFloat}.infinity))
120
102
expectEqual(-${Self}.infinity, ${Self}(exactly: -${OtherFloat}.infinity))
121
103
expectEmpty(${Self}(exactly: ${OtherFloat}.nan))
0 commit comments