-
Notifications
You must be signed in to change notification settings - Fork 10.5k
SE-0080 (2/5) - Failable initializers for Float->Float #2977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure |
||
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/dest=NaN") { | ||
let input = ${OtherFloat}.nan | ||
var result = ${Self}(input) | ||
var resultConvertedBack = ${OtherFloat}(result) | ||
expectTrue(input.isNaN) | ||
expectTrue(resultConvertedBack.isNaN) | ||
} | ||
|
||
FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/dest=NaN") { | ||
let input = ${OtherFloat}.nan | ||
var result = ${Self}(exactly: input) | ||
expectEmpty(result) | ||
} | ||
|
||
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/dest=inf") { | ||
let input = ${OtherFloat}.infinity | ||
var result = ${Self}(input) | ||
var resultConvertedBack = ${OtherFloat}(result) | ||
expectEqual(input, resultConvertedBack) | ||
} | ||
|
||
FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/dest=inf") { | ||
let input = ${OtherFloat}.infinity | ||
var result = ${Self}(exactly: input) | ||
expectEqual(${Self}.infinity, result) | ||
} | ||
|
||
FloatingPointConversionTruncations.test("${OtherFloat}To${Self}Conversion/dest=-inf") { | ||
let input = -${OtherFloat}.infinity | ||
var result = ${Self}(input) | ||
var resultConvertedBack = ${OtherFloat}(result) | ||
expectEqual(input, resultConvertedBack) | ||
} | ||
|
||
FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/dest=-inf") { | ||
let input = -${OtherFloat}.infinity | ||
var result = ${Self}(exactly: input) | ||
expectEqual(-${Self}.infinity, result) | ||
} | ||
|
||
% if OtherFloat == 'Float80': | ||
#endif | ||
% end | ||
|
||
% end # for in floatNameToSignificandBits (Other) | ||
|
||
% if Self == 'Float80': | ||
#endif | ||
% end | ||
|
||
% end # for in floatNameToSignificandBits (Self) | ||
|
||
runAllTests() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sholud the test name say
1.0.nextUp
instead ofgreatestFiniteMagnitude
?