Skip to content

Commit d4f31b4

Browse files
Fixup tests to test both const-eval and runtime
1 parent 0dbce10 commit d4f31b4

File tree

1 file changed

+22
-99
lines changed

1 file changed

+22
-99
lines changed

src/test/ui/numbers-arithmetic/saturating-float-casts.rs

Lines changed: 22 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// run-pass
2+
// compile-flags:-Zmir-opt-level=0
23
// Tests saturating float->int casts. See u128-as-f32.rs for the opposite direction.
34
//
45
// Some of these tests come from a similar file in miri,
5-
// tests/run-pass/float.rs. They're just duplicated currently but we may want
6-
// to merge this in the future.
6+
// tests/run-pass/float.rs. Individual test cases are potentially duplicated
7+
// with the previously existing tests, but since this runs so quickly anyway,
8+
// we're not spending the time to figure out exactly which ones should be
9+
// merged.
710

811
#![feature(test, stmt_expr_attributes)]
912
#![feature(track_caller)]
@@ -21,31 +24,18 @@ macro_rules! test {
2124
// black_box disables constant evaluation to test run-time conversions:
2225
assert_eq!(black_box::<$src_ty>($val) as $dest_ty, $expected,
2326
"run-time {} -> {}", stringify!($src_ty), stringify!($dest_ty));
24-
);
25-
26-
($fval:expr, f* -> $ity:ident, $ival:expr) => (
27-
test!($fval, f32 -> $ity, $ival);
28-
test!($fval, f64 -> $ity, $ival);
29-
)
30-
}
3127

32-
// This macro tests const eval in addition to run-time evaluation.
33-
// If and when saturating casts are adopted, this macro should be merged with test!() to ensure
34-
// that run-time and const eval agree on inputs that currently trigger a const eval error.
35-
macro_rules! test_c {
36-
($val:expr, $src_ty:ident -> $dest_ty:ident, $expected:expr) => ({
37-
test!($val, $src_ty -> $dest_ty, $expected);
3828
{
3929
const X: $src_ty = $val;
4030
const Y: $dest_ty = X as $dest_ty;
4131
assert_eq!(Y, $expected,
4232
"const eval {} -> {}", stringify!($src_ty), stringify!($dest_ty));
4333
}
44-
});
34+
);
4535

4636
($fval:expr, f* -> $ity:ident, $ival:expr) => (
47-
test_c!($fval, f32 -> $ity, $ival);
48-
test_c!($fval, f64 -> $ity, $ival);
37+
test!($fval, f32 -> $ity, $ival);
38+
test!($fval, f64 -> $ity, $ival);
4939
)
5040
}
5141

@@ -59,11 +49,11 @@ macro_rules! common_fptoi_tests {
5949
// as well, the test is just slightly misplaced.
6050
test!($ity::MIN as $fty, $fty -> $ity, $ity::MIN);
6151
test!($ity::MAX as $fty, $fty -> $ity, $ity::MAX);
62-
test_c!(0., $fty -> $ity, 0);
63-
test_c!($fty::MIN_POSITIVE, $fty -> $ity, 0);
52+
test!(0., $fty -> $ity, 0);
53+
test!($fty::MIN_POSITIVE, $fty -> $ity, 0);
6454
test!(-0.9, $fty -> $ity, 0);
65-
test_c!(1., $fty -> $ity, 1);
66-
test_c!(42., $fty -> $ity, 42);
55+
test!(1., $fty -> $ity, 1);
56+
test!(42., $fty -> $ity, 42);
6757
)+ });
6858

6959
(f* -> $($ity:ident)+) => ({
@@ -217,39 +207,6 @@ where
217207
assert_eq!(unsafe { x.cast_unchecked() }, y);
218208
}
219209

220-
fn basic() {
221-
// basic arithmetic
222-
assert_eq(6.0_f32 * 6.0_f32, 36.0_f32);
223-
assert_eq(6.0_f64 * 6.0_f64, 36.0_f64);
224-
assert_eq(-{ 5.0_f32 }, -5.0_f32);
225-
assert_eq(-{ 5.0_f64 }, -5.0_f64);
226-
// infinities, NaN
227-
assert!((5.0_f32 / 0.0).is_infinite());
228-
assert_ne!({ 5.0_f32 / 0.0 }, { -5.0_f32 / 0.0 });
229-
assert!((5.0_f64 / 0.0).is_infinite());
230-
assert_ne!({ 5.0_f64 / 0.0 }, { 5.0_f64 / -0.0 });
231-
assert!((-5.0_f32).sqrt().is_nan());
232-
assert!((-5.0_f64).sqrt().is_nan());
233-
assert_ne!(f32::NAN, f32::NAN);
234-
assert_ne!(f64::NAN, f64::NAN);
235-
// negative zero
236-
let posz = 0.0f32;
237-
let negz = -0.0f32;
238-
assert_eq(posz, negz);
239-
assert_ne!(posz.to_bits(), negz.to_bits());
240-
let posz = 0.0f64;
241-
let negz = -0.0f64;
242-
assert_eq(posz, negz);
243-
assert_ne!(posz.to_bits(), negz.to_bits());
244-
// byte-level transmute
245-
let x: u64 = unsafe { std::mem::transmute(42.0_f64) };
246-
let y: f64 = unsafe { std::mem::transmute(x) };
247-
assert_eq(y, 42.0_f64);
248-
let x: u32 = unsafe { std::mem::transmute(42.0_f32) };
249-
let y: f32 = unsafe { std::mem::transmute(x) };
250-
assert_eq(y, 42.0_f32);
251-
}
252-
253210
fn casts() {
254211
// f32 -> i8
255212
test_both_cast::<f32, i8>(127.99, 127);
@@ -500,42 +457,8 @@ fn casts() {
500457
assert_eq::<f32>(f64::NEG_INFINITY as f32, f32::NEG_INFINITY);
501458
}
502459

503-
fn ops() {
504-
// f32 min/max
505-
assert_eq((1.0 as f32).max(-1.0), 1.0);
506-
assert_eq((1.0 as f32).min(-1.0), -1.0);
507-
assert_eq(f32::NAN.min(9.0), 9.0);
508-
assert_eq(f32::NAN.max(-9.0), -9.0);
509-
assert_eq((9.0 as f32).min(f32::NAN), 9.0);
510-
assert_eq((-9.0 as f32).max(f32::NAN), -9.0);
511-
512-
// f64 min/max
513-
assert_eq((1.0 as f64).max(-1.0), 1.0);
514-
assert_eq((1.0 as f64).min(-1.0), -1.0);
515-
assert_eq(f64::NAN.min(9.0), 9.0);
516-
assert_eq(f64::NAN.max(-9.0), -9.0);
517-
assert_eq((9.0 as f64).min(f64::NAN), 9.0);
518-
assert_eq((-9.0 as f64).max(f64::NAN), -9.0);
519-
520-
// f32 copysign
521-
assert_eq(3.5_f32.copysign(0.42), 3.5_f32);
522-
assert_eq(3.5_f32.copysign(-0.42), -3.5_f32);
523-
assert_eq((-3.5_f32).copysign(0.42), 3.5_f32);
524-
assert_eq((-3.5_f32).copysign(-0.42), -3.5_f32);
525-
assert!(f32::NAN.copysign(1.0).is_nan());
526-
527-
// f64 copysign
528-
assert_eq(3.5_f64.copysign(0.42), 3.5_f64);
529-
assert_eq(3.5_f64.copysign(-0.42), -3.5_f64);
530-
assert_eq((-3.5_f64).copysign(0.42), 3.5_f64);
531-
assert_eq((-3.5_f64).copysign(-0.42), -3.5_f64);
532-
assert!(f64::NAN.copysign(1.0).is_nan());
533-
}
534-
535460
pub fn main() {
536-
basic();
537-
casts();
538-
ops();
461+
casts(); // from miri's tests
539462

540463
common_fptoi_tests!(f* -> i8 i16 i32 i64 u8 u16 u32 u64);
541464
fptoui_tests!(f* -> u8 u16 u32 u64);
@@ -549,39 +472,39 @@ pub fn main() {
549472
// The following tests cover edge cases for some integer types.
550473

551474
// # u8
552-
test_c!(254., f* -> u8, 254);
475+
test!(254., f* -> u8, 254);
553476
test!(256., f* -> u8, 255);
554477

555478
// # i8
556-
test_c!(-127., f* -> i8, -127);
479+
test!(-127., f* -> i8, -127);
557480
test!(-129., f* -> i8, -128);
558-
test_c!(126., f* -> i8, 126);
481+
test!(126., f* -> i8, 126);
559482
test!(128., f* -> i8, 127);
560483

561484
// # i32
562485
// -2147483648. is i32::MIN (exactly)
563-
test_c!(-2147483648., f* -> i32, i32::MIN);
486+
test!(-2147483648., f* -> i32, i32::MIN);
564487
// 2147483648. is i32::MAX rounded up
565488
test!(2147483648., f32 -> i32, 2147483647);
566489
// With 24 significand bits, floats with magnitude in [2^30 + 1, 2^31] are rounded to
567490
// multiples of 2^7. Therefore, nextDown(round(i32::MAX)) is 2^31 - 128:
568-
test_c!(2147483520., f32 -> i32, 2147483520);
491+
test!(2147483520., f32 -> i32, 2147483520);
569492
// Similarly, nextUp(i32::MIN) is i32::MIN + 2^8 and nextDown(i32::MIN) is i32::MIN - 2^7
570493
test!(-2147483904., f* -> i32, i32::MIN);
571-
test_c!(-2147483520., f* -> i32, -2147483520);
494+
test!(-2147483520., f* -> i32, -2147483520);
572495

573496
// # u32
574497
// round(MAX) and nextUp(round(MAX))
575-
test_c!(4294967040., f* -> u32, 4294967040);
498+
test!(4294967040., f* -> u32, 4294967040);
576499
test!(4294967296., f* -> u32, 4294967295);
577500

578501
// # u128
579502
#[cfg(not(target_os = "emscripten"))]
580503
{
581504
// float->int:
582-
test_c!(f32::MAX, f32 -> u128, 0xffffff00000000000000000000000000);
505+
test!(f32::MAX, f32 -> u128, 0xffffff00000000000000000000000000);
583506
// nextDown(f32::MAX) = 2^128 - 2 * 2^104
584507
const SECOND_LARGEST_F32: f32 = 340282326356119256160033759537265639424.;
585-
test_c!(SECOND_LARGEST_F32, f32 -> u128, 0xfffffe00000000000000000000000000);
508+
test!(SECOND_LARGEST_F32, f32 -> u128, 0xfffffe00000000000000000000000000);
586509
}
587510
}

0 commit comments

Comments
 (0)