|
11 | 11 | import StdlibUnittest
|
12 | 12 |
|
13 | 13 | %{
|
14 |
| -from SwiftFloatingPointTypes import all_floating_point_types |
| 14 | +import gyb |
| 15 | +from SwiftFloatingPointTypes import all_floating_point_types, getFtoIBounds |
| 16 | +from SwiftIntTypes import all_integer_types |
15 | 17 | }%
|
16 | 18 |
|
| 19 | +var FixedPointConversionTruncations = TestSuite("FixedPointToFloatingPointConversionTruncations") |
| 20 | +var FixedPointConversionFailures = TestSuite("FixedPointToFloatingPointConversionFailures") |
| 21 | + |
17 | 22 | var FloatingPointConversionTruncations = TestSuite("FloatingPointToFloatingPointConversionTruncations")
|
18 | 23 | var FloatingPointConversionFailures = TestSuite("FloatingPointToFloatingPointConversionFailures")
|
19 | 24 |
|
@@ -111,6 +116,75 @@ FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/AlwaysSuc
|
111 | 116 |
|
112 | 117 | % end # for in all_floating_point_types (Other)
|
113 | 118 |
|
| 119 | +%{ |
| 120 | + |
| 121 | +float_to_int_conversion_template = gyb.parse_template("float_to_int_conversion", |
| 122 | +""" |
| 123 | +% for int_ty in all_integer_types(word_bits): |
| 124 | +% OtherInt = int_ty.stdlib_name |
| 125 | +% OtherMin = int_ty.min |
| 126 | +% OtherMax = int_ty.max |
| 127 | +% (FloatMin, FloatMax) = getFtoIBounds(self_type.bits, int_ty.bits, int_ty.is_signed) |
| 128 | + |
| 129 | +% for testValue in [0, FloatMin, FloatMax, FloatMin - 1, FloatMax + 1, OtherMin, OtherMax]: |
| 130 | + |
| 131 | +% if testValue < OtherMin or testValue > OtherMax: |
| 132 | +% # Can't construct `other` value, do nothing and continue. |
| 133 | + |
| 134 | +% elif testValue >= FloatMin and testValue <= FloatMax: |
| 135 | + |
| 136 | +FixedPointConversionTruncations.test("${OtherInt}to${Self}Conversion/${testValue}") { |
| 137 | + expectEqual(${Self}(${testValue} as ${OtherInt}), ${testValue}) |
| 138 | +} |
| 139 | + |
| 140 | +FixedPointConversionFailures.test("${OtherInt}to${Self}FailableConversion/${testValue}") { |
| 141 | + expectEqual(${Self}(exactly: ${testValue} as ${OtherInt}), ${testValue}) |
| 142 | +} |
| 143 | + |
| 144 | +% else: |
| 145 | + |
| 146 | +FixedPointConversionTruncations.test("${OtherInt}to${Self}Truncation/${testValue}") { |
| 147 | + let value: ${OtherInt} = ${testValue} |
| 148 | + let result = ${Self}(value) |
| 149 | + expectNotEqual(${OtherInt}(result), value) |
| 150 | +} |
| 151 | + |
| 152 | +FixedPointConversionFailures.test("${OtherInt}to${Self}Failure/${testValue}") { |
| 153 | + let value: ${OtherInt} = ${testValue} |
| 154 | + let result = ${Self}(exactly: value) |
| 155 | + expectEqual(result, ${OtherMin} as ${Self}) |
| 156 | + expectEqual(${OtherInt}(result!), value) |
| 157 | +} |
| 158 | + |
| 159 | +% end |
| 160 | + |
| 161 | +% end # testValue in testValues |
| 162 | +% end # for in all_integer_types (Other) |
| 163 | +""") |
| 164 | +}% |
| 165 | + |
| 166 | +#if arch(i386) || arch(arm) |
| 167 | + |
| 168 | + ${gyb.execute_template( |
| 169 | + float_to_int_conversion_template, |
| 170 | + word_bits=32, |
| 171 | + **locals() |
| 172 | + )} |
| 173 | + |
| 174 | +#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) |
| 175 | + |
| 176 | + ${gyb.execute_template( |
| 177 | + float_to_int_conversion_template, |
| 178 | + word_bits=64, |
| 179 | + **locals() |
| 180 | + )} |
| 181 | + |
| 182 | +#else |
| 183 | + |
| 184 | +_UnimplementedError() |
| 185 | + |
| 186 | +#endif |
| 187 | + |
114 | 188 | % if Self == 'Float80':
|
115 | 189 | #endif
|
116 | 190 | % end
|
|
0 commit comments