| // RUN: rm -rf %t |
| // RUN: mkdir -p %t |
| // RUN: %gyb %s -o %t/FloatingPointConversionTraps.swift |
| // RUN: %line-directive %t/FloatingPointConversionTraps.swift -- %target-build-swift %t/FloatingPointConversionTraps.swift -Xfrontend -disable-access-control -o %t/a.out_Debug |
| // RUN: %line-directive %t/FloatingPointConversionTraps.swift -- %target-build-swift %t/FloatingPointConversionTraps.swift -Xfrontend -disable-access-control -o %t/a.out_Release -O |
| // |
| // RUN: %line-directive %t/FloatingPointConversionTraps.swift -- %target-run %t/a.out_Debug |
| // RUN: %line-directive %t/FloatingPointConversionTraps.swift -- %target-run %t/a.out_Release |
| // REQUIRES: executable_test |
| |
| import StdlibUnittest |
| |
| |
| %{ |
| |
| from SwiftIntTypes import all_integer_types |
| |
| # Test cases are written in a way that they don't depend on the word size. |
| word_bits = 4 |
| |
| }% |
| |
| func getInfiniteOrNaNMessage(_ floatType: String) -> String { |
| if _isDebugAssertConfiguration() { |
| return "either infinite or NaN" |
| } |
| return "" |
| } |
| |
| func getTooSmallMessage() -> String { |
| if _isDebugAssertConfiguration() { |
| return "would be less than" |
| } |
| return "" |
| } |
| |
| func getTooLargeMessage() -> String { |
| if _isDebugAssertConfiguration() { |
| return "would be greater than" |
| } |
| return "" |
| } |
| |
| var FloatingPointConversion = TestSuite("FloatingPointConversion") |
| |
| % for FloatSelf in ['Float32', 'Float64', 'Float80']: |
| % for int_ty in all_integer_types(word_bits): |
| % IntSelf = int_ty.stdlib_name |
| |
| % if FloatSelf == 'Float80': |
| #if arch(i386) || arch(x86_64) |
| % end |
| |
| FloatingPointConversion.test("${FloatSelf}/${IntSelf}/+inf") |
| .crashOutputMatches(getInfiniteOrNaNMessage("${FloatSelf}")).code { |
| do { |
| var a = get${IntSelf}(${IntSelf}(get${FloatSelf}(0.0))) |
| expectEqual(0, a) |
| } |
| do { |
| var a = get${IntSelf}(${IntSelf}(get${FloatSelf}(123.0))) |
| expectEqual(123, a) |
| } |
| |
| expectCrashLater() |
| get${IntSelf}(${IntSelf}(get${FloatSelf}(+${FloatSelf}.infinity))) |
| } |
| |
| % if int_ty.is_signed: |
| |
| FloatingPointConversion.test("${FloatSelf}/${IntSelf}/-inf") |
| .crashOutputMatches(getInfiniteOrNaNMessage("${FloatSelf}")).code { |
| do { |
| var a = get${IntSelf}(${IntSelf}(get${FloatSelf}(-0.0))) |
| expectEqual(0, a) |
| } |
| do { |
| var a = get${IntSelf}(${IntSelf}(get${FloatSelf}(-123.0))) |
| expectEqual(-123, a) |
| } |
| |
| expectCrashLater() |
| get${IntSelf}(${IntSelf}(get${FloatSelf}(-${FloatSelf}.infinity))) |
| } |
| |
| % else: |
| |
| FloatingPointConversion.test("${FloatSelf}/${IntSelf}/negative") |
| .crashOutputMatches(getTooSmallMessage()).code { |
| expectCrashLater() |
| get${IntSelf}(${IntSelf}(get${FloatSelf}(-123.0))) |
| } |
| |
| FloatingPointConversion.test("${FloatSelf}/${IntSelf}/-inf") |
| .crashOutputMatches(getInfiniteOrNaNMessage("${FloatSelf}")).code { |
| expectCrashLater() |
| get${IntSelf}(${IntSelf}(get${FloatSelf}(-${FloatSelf}.infinity))) |
| } |
| |
| % end |
| |
| FloatingPointConversion.test("${FloatSelf}/${IntSelf}/NaN") |
| .crashOutputMatches(getInfiniteOrNaNMessage("${FloatSelf}")).code { |
| expectCrashLater() |
| get${IntSelf}(${IntSelf}(get${FloatSelf}(${FloatSelf}.nan))) |
| } |
| |
| % if FloatSelf == 'Float80': |
| #endif |
| % end |
| |
| % end |
| % end |
| |
| runAllTests() |
| |