Files
swift-mirror/validation-test/stdlib/FloatingPointConversion.swift.gyb

137 lines
5.5 KiB
Plaintext

// 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:
/// Always-safe conversion from ${OtherFloat}.greatestFiniteMagnitude to ${Self}.
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/dest=${OtherFloat}.greatestFiniteMagnitude") {
// Test that we don't truncate floats that shouldn't be truncated.
let input = ${OtherFloat}.greatestFiniteMagnitude
let result = ${Self}(input)
var resultConvertedBack = ${OtherFloat}(result)
expectEqual(input, resultConvertedBack)
}
/// Never-truncating conversion from ${OtherFloat}.nextUp to ${Self}.
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/dest=${OtherFloat}.1.0.nextUp") {
// Test that we don't truncate floats that shouldn't be truncated.
let input = (1.0 as ${OtherFloat}).nextUp
var result = ${Self}(input)
var resultConvertedBack = ${OtherFloat}(result)
expectEqual(input, resultConvertedBack)
}
/// Never-nil failable conversion from ${OtherFloat}.greatestFiniteMagnitude to ${Self}.
FloatingPointConversionFailures.test("${OtherFloat}To${Self}FailableConversion/dest=${OtherFloat}.greatestFiniteMagnitude") {
// Test that nothing interesting happens and we end up with a non-nil, identical result.
let input = ${OtherFloat}.greatestFiniteMagnitude
var result = ${Self}(exactly: input)
expectNotEmpty(result)
var resultConvertedBack = ${OtherFloat}(result!)
expectEqual(input, resultConvertedBack)
}
/// Never-nil conversion from ${OtherFloat}.nextUp to ${Self}.
FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/dest=${OtherFloat}.1.0.nextUp") {
// Test that nothing interesting happens and we end up with a non-nil, identical result.
let input = (1.0 as ${OtherFloat}).nextUp
var result = ${Self}(exactly: input)
expectNotEmpty(result)
var resultConvertedBack = ${OtherFloat}(result!)
expectEqual(input, resultConvertedBack)
}
% else:
/// Always-succeeding, but out-of-range conversion from ${OtherFloat}.greatestFiniteMagnitude to ${Self}.
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/dest=${OtherFloat}.greatestFiniteMagnitude") {
// Test that we check if we truncate floats not representable by another type.
let input = ${OtherFloat}.greatestFiniteMagnitude
var result = ${Self}(input)
var resultConvertedBack = ${OtherFloat}(result)
expectEqual(${OtherFloat}.infinity, resultConvertedBack)
}
/// Always-truncating conversion from ${OtherFloat}.nextUp to ${Self}.
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/dest=${OtherFloat}.1.0.nextUp") {
// Test that we check if we truncate floats not representable by another type.
let input = (1.0 as ${OtherFloat}).nextUp
var result = ${Self}(input)
var resultConvertedBack = ${OtherFloat}(result)
expectEqual(1.0, resultConvertedBack)
}
/// Always-nil failable conversion from ${OtherFloat}.greatestFiniteMagnitude to ${Self}.
FloatingPointConversionFailures.test("${OtherFloat}To${Self}FailableConversion/dest=${OtherFloat}.greatestFiniteMagnitude") {
// Test that we check if we return nil when a float would be truncated in conversion.
let input = ${OtherFloat}.greatestFiniteMagnitude
var result = ${Self}(exactly: input)
expectEmpty(result)
}
/// Always-nil failable conversion from ${OtherFloat}.nextUp to ${Self}.
FloatingPointConversionFailures.test("${OtherFloat}To${Self}FailableConversion/dest=${OtherFloat}.1.0.nextUp") {
// Test that we check if we return nil when a float would be truncated in conversion.
let input = (1.0 as ${OtherFloat}).nextUp
var result = ${Self}(exactly: input)
expectEmpty(result)
}
% end
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/special") {
expectEqual( ${Self}.infinity, ${Self}( ${OtherFloat}.infinity))
expectEqual(-${Self}.infinity, ${Self}(-${OtherFloat}.infinity))
expectTrue(${Self}(${OtherFloat}.nan).isNaN)
}
FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/special") {
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()