| // RUN: rm -rf %t |
| // RUN: mkdir -p %t |
| // RUN: %gyb %s -o %t/FloatingPointConversion.swift |
| // RUN: %line-directive %t/FloatingPointConversion.swift -- %target-build-swift %t/FloatingPointConversion.swift -Xfrontend -disable-access-control -o %t/a.out_Debug |
| // RUN: %line-directive %t/FloatingPointConversion.swift -- %target-build-swift %t/FloatingPointConversion.swift -Xfrontend -disable-access-control -o %t/a.out_Release -O |
| // |
| // RUN: %line-directive %t/FloatingPointConversion.swift -- %target-run %t/a.out_Debug |
| // RUN: %line-directive %t/FloatingPointConversion.swift -- %target-run %t/a.out_Release |
| // REQUIRES: executable_test |
| |
| import StdlibUnittest |
| |
| %{ |
| |
| floatNameToSignificandBits = { 'Float32':24, 'Float64':53, 'Float80':64 } |
| |
| }% |
| |
| var FloatingPointConversionTruncations = TestSuite("FloatingPointToFloatingPointConversionTruncations") |
| var FloatingPointConversionFailures = TestSuite("FloatingPointToFloatingPointConversionFailures") |
| |
| % for Self, selfSignificandBits in floatNameToSignificandBits.iteritems(): |
| |
| % if Self == 'Float80': |
| #if arch(i386) || arch(x86_64) |
| % end |
| |
| % for OtherFloat, otherSignificandBits in floatNameToSignificandBits.iteritems(): |
| |
| % if OtherFloat == 'Float80': |
| #if arch(i386) || arch(x86_64) |
| % end |
| |
| % if otherSignificandBits <= selfSignificandBits: |
| |
| FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion") |
| .forEach(in: [ |
| ${OtherFloat}.greatestFiniteMagnitude, |
| -${OtherFloat}.greatestFiniteMagnitude, |
| (1.0 as ${OtherFloat}).nextUp, |
| (1.0 as ${OtherFloat}).nextDown, |
| (-1.0 as ${OtherFloat}).nextUp, |
| (-1.0 as ${OtherFloat}).nextDown, |
| ]) { |
| input in |
| // FIXME: we should have a stronger postcondition here. |
| let result = ${Self}(input) |
| let resultConvertedBack = ${OtherFloat}(result) |
| expectEqual(input, resultConvertedBack) |
| } |
| |
| % else: |
| |
| FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion") |
| .forEach(in: [ |
| ( ${OtherFloat}.greatestFiniteMagnitude, ${Self}.infinity), |
| (-${OtherFloat}.greatestFiniteMagnitude, -${Self}.infinity), |
| ( (1.0 as ${OtherFloat}).nextUp, 1.0 as ${Self}), |
| ( (1.0 as ${OtherFloat}).nextDown, 1.0 as ${Self}), |
| ((-1.0 as ${OtherFloat}).nextUp, -1.0 as ${Self}), |
| ((-1.0 as ${OtherFloat}).nextDown, -1.0 as ${Self}), |
| ]) { |
| (input, expectedResult) in |
| expectEqual(expectedResult, ${Self}(input)) |
| } |
| |
| % end |
| |
| FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/special") { |
| expectEqual( 1.0 as ${Self}, ${Self}(exactly: 1.0 as ${OtherFloat})) |
| expectEqual(-1.0 as ${Self}, ${Self}(exactly: -1.0 as ${OtherFloat})) |
| expectEqual( ${Self}.infinity, ${Self}( ${OtherFloat}.infinity)) |
| expectEqual(-${Self}.infinity, ${Self}(-${OtherFloat}.infinity)) |
| expectTrue(${Self}(${OtherFloat}.nan).isNaN) |
| } |
| |
| FloatingPointConversionFailures.test("${OtherFloat}To${Self}FailableConversion") |
| .forEach(in: [ |
| ${OtherFloat}.greatestFiniteMagnitude, |
| -${OtherFloat}.greatestFiniteMagnitude, |
| (1.0 as ${OtherFloat}).nextUp, |
| (1.0 as ${OtherFloat}).nextDown, |
| (-1.0 as ${OtherFloat}).nextUp, |
| (-1.0 as ${OtherFloat}).nextDown, |
| ]) { |
| input in |
| let result = ${Self}(exactly: input) |
| % if otherSignificandBits <= selfSignificandBits: |
| if let result = expectNotEmpty(result) { |
| // FIXME: we should have a stronger postcondition here. |
| expectEqual(input, ${OtherFloat}(result)) |
| } |
| % else: |
| expectEmpty(result) |
| % end |
| } |
| |
| FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/AlwaysSuccess") { |
| expectEqual( 1.0 as ${Self}, ${Self}(exactly: 1.0 as ${OtherFloat})) |
| expectEqual(-1.0 as ${Self}, ${Self}(exactly: -1.0 as ${OtherFloat})) |
| expectEqual( ${Self}.infinity, ${Self}(exactly: ${OtherFloat}.infinity)) |
| expectEqual(-${Self}.infinity, ${Self}(exactly: -${OtherFloat}.infinity)) |
| expectEmpty(${Self}(exactly: ${OtherFloat}.nan)) |
| } |
| |
| % if OtherFloat == 'Float80': |
| #endif |
| % end |
| |
| % end # for in floatNameToSignificandBits (Other) |
| |
| % if Self == 'Float80': |
| #endif |
| % end |
| |
| % end # for in floatNameToSignificandBits (Self) |
| |
| runAllTests() |