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