]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/saturating-float-casts.rs
ci: Start running wasm32 tests on Travis
[rust.git] / src / test / run-pass / saturating-float-casts.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Tests saturating float->int casts. See u128-as-f32.rs for the opposite direction.
12 // compile-flags: -Z saturating-float-casts
13 // ignore-wasm32-bare FIXME(#46298) needs upstream llvm fixes
14
15 #![feature(test, i128, i128_type, stmt_expr_attributes)]
16 #![deny(overflowing_literals)]
17 extern crate test;
18
19 use std::{f32, f64};
20 use std::{u8, i8, u16, i16, u32, i32, u64, i64};
21 #[cfg(not(target_os="emscripten"))]
22 use std::{u128, i128};
23 use test::black_box;
24
25 macro_rules! test {
26     ($val:expr, $src_ty:ident -> $dest_ty:ident, $expected:expr) => (
27         // black_box disables constant evaluation to test run-time conversions:
28         assert_eq!(black_box::<$src_ty>($val) as $dest_ty, $expected,
29                     "run-time {} -> {}", stringify!($src_ty), stringify!($dest_ty));
30     );
31
32     ($fval:expr, f* -> $ity:ident, $ival:expr) => (
33         test!($fval, f32 -> $ity, $ival);
34         test!($fval, f64 -> $ity, $ival);
35     )
36 }
37
38 // This macro tests const eval in addition to run-time evaluation.
39 // If and when saturating casts are adopted, this macro should be merged with test!() to ensure
40 // that run-time and const eval agree on inputs that currently trigger a const eval error.
41 macro_rules! test_c {
42     ($val:expr, $src_ty:ident -> $dest_ty:ident, $expected:expr) => ({
43         test!($val, $src_ty -> $dest_ty, $expected);
44         {
45             const X: $src_ty = $val;
46             const Y: $dest_ty = X as $dest_ty;
47             assert_eq!(Y, $expected,
48                         "const eval {} -> {}", stringify!($src_ty), stringify!($dest_ty));
49         }
50     });
51
52     ($fval:expr, f* -> $ity:ident, $ival:expr) => (
53         test_c!($fval, f32 -> $ity, $ival);
54         test_c!($fval, f64 -> $ity, $ival);
55     )
56 }
57
58 macro_rules! common_fptoi_tests {
59     ($fty:ident -> $($ity:ident)+) => ({ $(
60         test!($fty::NAN, $fty -> $ity, 0);
61         test!($fty::INFINITY, $fty -> $ity, $ity::MAX);
62         test!($fty::NEG_INFINITY, $fty -> $ity, $ity::MIN);
63         // These two tests are not solely float->int tests, in particular the latter relies on
64         // `u128::MAX as f32` not being UB. But that's okay, since this file tests int->float
65         // as well, the test is just slightly misplaced.
66         test!($ity::MIN as $fty, $fty -> $ity, $ity::MIN);
67         test!($ity::MAX as $fty, $fty -> $ity, $ity::MAX);
68         test_c!(0., $fty -> $ity, 0);
69         test_c!($fty::MIN_POSITIVE, $fty -> $ity, 0);
70         test!(-0.9, $fty -> $ity, 0);
71         test_c!(1., $fty -> $ity, 1);
72         test_c!(42., $fty -> $ity, 42);
73     )+ });
74
75     (f* -> $($ity:ident)+) => ({
76         common_fptoi_tests!(f32 -> $($ity)+);
77         common_fptoi_tests!(f64 -> $($ity)+);
78     })
79 }
80
81 macro_rules! fptoui_tests {
82     ($fty: ident -> $($ity: ident)+) => ({ $(
83         test!(-0., $fty -> $ity, 0);
84         test!(-$fty::MIN_POSITIVE, $fty -> $ity, 0);
85         test!(-0.99999994, $fty -> $ity, 0);
86         test!(-1., $fty -> $ity, 0);
87         test!(-100., $fty -> $ity, 0);
88         test!(#[allow(overflowing_literals)] -1e50, $fty -> $ity, 0);
89         test!(#[allow(overflowing_literals)] -1e130, $fty -> $ity, 0);
90     )+ });
91
92     (f* -> $($ity:ident)+) => ({
93         fptoui_tests!(f32 -> $($ity)+);
94         fptoui_tests!(f64 -> $($ity)+);
95     })
96 }
97
98 pub fn main() {
99     common_fptoi_tests!(f* -> i8 i16 i32 i64 u8 u16 u32 u64);
100     fptoui_tests!(f* -> u8 u16 u32 u64);
101     // FIXME emscripten does not support i128
102     #[cfg(not(target_os="emscripten"))] {
103         common_fptoi_tests!(f* -> i128 u128);
104         fptoui_tests!(f* -> u128);
105     }
106
107     // The following tests cover edge cases for some integer types.
108
109     // # u8
110     test_c!(254., f* -> u8, 254);
111     test!(256., f* -> u8, 255);
112
113     // # i8
114     test_c!(-127., f* -> i8, -127);
115     test!(-129., f* -> i8, -128);
116     test_c!(126., f* -> i8, 126);
117     test!(128., f* -> i8, 127);
118
119     // # i32
120     // -2147483648. is i32::MIN (exactly)
121     test_c!(-2147483648., f* -> i32, i32::MIN);
122     // 2147483648. is i32::MAX rounded up
123     test!(2147483648., f32 -> i32, 2147483647);
124     // With 24 significand bits, floats with magnitude in [2^30 + 1, 2^31] are rounded to
125     // multiples of 2^7. Therefore, nextDown(round(i32::MAX)) is 2^31 - 128:
126     test_c!(2147483520., f32 -> i32, 2147483520);
127     // Similarly, nextUp(i32::MIN) is i32::MIN + 2^8 and nextDown(i32::MIN) is i32::MIN - 2^7
128     test!(-2147483904., f* -> i32, i32::MIN);
129     test_c!(-2147483520., f* -> i32, -2147483520);
130
131     // # u32
132     // round(MAX) and nextUp(round(MAX))
133     test_c!(4294967040., f* -> u32, 4294967040);
134     test!(4294967296., f* -> u32, 4294967295);
135
136     // # u128
137     #[cfg(not(target_os="emscripten"))]
138     {
139         // float->int:
140         test_c!(f32::MAX, f32 -> u128, 0xffffff00000000000000000000000000);
141         // nextDown(f32::MAX) = 2^128 - 2 * 2^104
142         const SECOND_LARGEST_F32: f32 = 340282326356119256160033759537265639424.;
143         test_c!(SECOND_LARGEST_F32, f32 -> u128, 0xfffffe00000000000000000000000000);
144     }
145 }