copy some float cast tests from rustc
This commit is contained in:
parent
13f1b5f653
commit
da6846c8a9
1 changed files with 112 additions and 1 deletions
|
|
@ -1,4 +1,4 @@
|
|||
#![feature(track_caller)]
|
||||
#![feature(track_caller, stmt_expr_attributes)]
|
||||
use std::fmt::Debug;
|
||||
|
||||
// Helper function to avoid promotion so that this tests "run-time" casts, not CTFE.
|
||||
|
|
@ -78,6 +78,7 @@ fn test_both_cast<F, I>(x: F, y: I)
|
|||
fn main() {
|
||||
basic();
|
||||
casts();
|
||||
more_casts();
|
||||
ops();
|
||||
}
|
||||
|
||||
|
|
@ -334,3 +335,113 @@ fn ops() {
|
|||
assert_eq((-3.5_f64).copysign(-0.42), -3.5_f64);
|
||||
assert!(f64::NAN.copysign(1.0).is_nan());
|
||||
}
|
||||
|
||||
/// Tests taken from rustc test suite.
|
||||
///
|
||||
|
||||
// Poor-man's black-box
|
||||
#[inline(never)]
|
||||
fn black_box<T>(x: T) -> T { x }
|
||||
|
||||
macro_rules! test {
|
||||
($val:expr, $src_ty:ident -> $dest_ty:ident, $expected:expr) => (
|
||||
// black_box disables constant evaluation to test run-time conversions:
|
||||
assert_eq!(black_box::<$src_ty>($val) as $dest_ty, $expected,
|
||||
"run-time {} -> {}", stringify!($src_ty), stringify!($dest_ty));
|
||||
|
||||
{
|
||||
const X: $src_ty = $val;
|
||||
const Y: $dest_ty = X as $dest_ty;
|
||||
assert_eq!(Y, $expected,
|
||||
"const eval {} -> {}", stringify!($src_ty), stringify!($dest_ty));
|
||||
}
|
||||
);
|
||||
|
||||
($fval:expr, f* -> $ity:ident, $ival:expr) => (
|
||||
test!($fval, f32 -> $ity, $ival);
|
||||
test!($fval, f64 -> $ity, $ival);
|
||||
)
|
||||
}
|
||||
|
||||
macro_rules! common_fptoi_tests {
|
||||
($fty:ident -> $($ity:ident)+) => ({ $(
|
||||
test!($fty::NAN, $fty -> $ity, 0);
|
||||
test!($fty::INFINITY, $fty -> $ity, $ity::MAX);
|
||||
test!($fty::NEG_INFINITY, $fty -> $ity, $ity::MIN);
|
||||
// These two tests are not solely float->int tests, in particular the latter relies on
|
||||
// `u128::MAX as f32` not being UB. But that's okay, since this file tests int->float
|
||||
// as well, the test is just slightly misplaced.
|
||||
test!($ity::MIN as $fty, $fty -> $ity, $ity::MIN);
|
||||
test!($ity::MAX as $fty, $fty -> $ity, $ity::MAX);
|
||||
test!(0., $fty -> $ity, 0);
|
||||
test!($fty::MIN_POSITIVE, $fty -> $ity, 0);
|
||||
test!(-0.9, $fty -> $ity, 0);
|
||||
test!(1., $fty -> $ity, 1);
|
||||
test!(42., $fty -> $ity, 42);
|
||||
)+ });
|
||||
|
||||
(f* -> $($ity:ident)+) => ({
|
||||
common_fptoi_tests!(f32 -> $($ity)+);
|
||||
common_fptoi_tests!(f64 -> $($ity)+);
|
||||
})
|
||||
}
|
||||
|
||||
macro_rules! fptoui_tests {
|
||||
($fty: ident -> $($ity: ident)+) => ({ $(
|
||||
test!(-0., $fty -> $ity, 0);
|
||||
test!(-$fty::MIN_POSITIVE, $fty -> $ity, 0);
|
||||
test!(-0.99999994, $fty -> $ity, 0);
|
||||
test!(-1., $fty -> $ity, 0);
|
||||
test!(-100., $fty -> $ity, 0);
|
||||
test!(#[allow(overflowing_literals)] -1e50, $fty -> $ity, 0);
|
||||
test!(#[allow(overflowing_literals)] -1e130, $fty -> $ity, 0);
|
||||
)+ });
|
||||
|
||||
(f* -> $($ity:ident)+) => ({
|
||||
fptoui_tests!(f32 -> $($ity)+);
|
||||
fptoui_tests!(f64 -> $($ity)+);
|
||||
})
|
||||
}
|
||||
|
||||
fn more_casts() {
|
||||
common_fptoi_tests!(f* -> i8 i16 i32 i64 u8 u16 u32 u64);
|
||||
fptoui_tests!(f* -> u8 u16 u32 u64);
|
||||
common_fptoi_tests!(f* -> i128 u128);
|
||||
fptoui_tests!(f* -> u128);
|
||||
|
||||
// The following tests cover edge cases for some integer types.
|
||||
|
||||
// # u8
|
||||
test!(254., f* -> u8, 254);
|
||||
test!(256., f* -> u8, 255);
|
||||
|
||||
// # i8
|
||||
test!(-127., f* -> i8, -127);
|
||||
test!(-129., f* -> i8, -128);
|
||||
test!(126., f* -> i8, 126);
|
||||
test!(128., f* -> i8, 127);
|
||||
|
||||
// # i32
|
||||
// -2147483648. is i32::MIN (exactly)
|
||||
test!(-2147483648., f* -> i32, i32::MIN);
|
||||
// 2147483648. is i32::MAX rounded up
|
||||
test!(2147483648., f32 -> i32, 2147483647);
|
||||
// With 24 significand bits, floats with magnitude in [2^30 + 1, 2^31] are rounded to
|
||||
// multiples of 2^7. Therefore, nextDown(round(i32::MAX)) is 2^31 - 128:
|
||||
test!(2147483520., f32 -> i32, 2147483520);
|
||||
// Similarly, nextUp(i32::MIN) is i32::MIN + 2^8 and nextDown(i32::MIN) is i32::MIN - 2^7
|
||||
test!(-2147483904., f* -> i32, i32::MIN);
|
||||
test!(-2147483520., f* -> i32, -2147483520);
|
||||
|
||||
// # u32
|
||||
// round(MAX) and nextUp(round(MAX))
|
||||
test!(4294967040., f* -> u32, 4294967040);
|
||||
test!(4294967296., f* -> u32, 4294967295);
|
||||
|
||||
// # u128
|
||||
// float->int:
|
||||
test!(f32::MAX, f32 -> u128, 0xffffff00000000000000000000000000);
|
||||
// nextDown(f32::MAX) = 2^128 - 2 * 2^104
|
||||
const SECOND_LARGEST_F32: f32 = 340282326356119256160033759537265639424.;
|
||||
test!(SECOND_LARGEST_F32, f32 -> u128, 0xfffffe00000000000000000000000000);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue