]> git.lizzy.rs Git - rust.git/blob - src/test/ui/numbers-arithmetic/u128-as-f32.rs
resolve: Remove an incorrect assert
[rust.git] / src / test / ui / numbers-arithmetic / u128-as-f32.rs
1 // run-pass
2
3 #![feature(test)]
4 #![deny(overflowing_literals)]
5 extern crate test;
6
7 use std::f32;
8 use std::u128;
9 use test::black_box;
10
11 macro_rules! test {
12     ($val:expr, $src_ty:ident -> $dest_ty:ident, $expected:expr) => ({
13         {
14             const X: $src_ty = $val;
15             const Y: $dest_ty = X as $dest_ty;
16             assert_eq!(Y, $expected,
17                         "const eval {} -> {}", stringify!($src_ty), stringify!($dest_ty));
18         }
19         // black_box disables constant evaluation to test run-time conversions:
20         assert_eq!(black_box::<$src_ty>($val) as $dest_ty, $expected,
21                     "run-time {} -> {}", stringify!($src_ty), stringify!($dest_ty));
22     });
23 }
24
25 pub fn main() {
26     // nextDown(f32::MAX) = 2^128 - 2 * 2^104
27     const SECOND_LARGEST_F32: f32 = 340282326356119256160033759537265639424.;
28
29     // f32::MAX - 0.5 ULP and smaller should be rounded down
30     test!(0xfffffe00000000000000000000000000, u128 -> f32, SECOND_LARGEST_F32);
31     test!(0xfffffe7fffffffffffffffffffffffff, u128 -> f32, SECOND_LARGEST_F32);
32     test!(0xfffffe80000000000000000000000000, u128 -> f32, SECOND_LARGEST_F32);
33     // numbers within < 0.5 ULP of f32::MAX it should be rounded to f32::MAX
34     test!(0xfffffe80000000000000000000000001, u128 -> f32, f32::MAX);
35     test!(0xfffffeffffffffffffffffffffffffff, u128 -> f32, f32::MAX);
36     test!(0xffffff00000000000000000000000000, u128 -> f32, f32::MAX);
37     test!(0xffffff00000000000000000000000001, u128 -> f32, f32::MAX);
38     test!(0xffffff7fffffffffffffffffffffffff, u128 -> f32, f32::MAX);
39     // f32::MAX + 0.5 ULP and greater should be rounded to infinity
40     test!(0xffffff80000000000000000000000000, u128 -> f32, f32::INFINITY);
41     test!(0xffffff80000000f00000000000000000, u128 -> f32, f32::INFINITY);
42     test!(0xffffff87ffffffffffffffff00000001, u128 -> f32, f32::INFINITY);
43
44     // u128->f64 should not be affected by the u128->f32 checks
45     test!(0xffffff80000000000000000000000000, u128 -> f64,
46           340282356779733661637539395458142568448.0);
47     test!(u128::MAX, u128 -> f64, 340282366920938463463374607431768211455.0);
48 }