]>
git.proxmox.com Git - rustc.git/blob - src/libcoretest/num/dec2flt/mod.rs
1 // Copyright 2015 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.
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.
11 #![allow(overflowing_literals)]
13 use std
::{i64, f32, f64}
;
18 // Take a float literal, turn it into a string in various ways (that are all trusted
19 // to be correct) and see if those strings are parsed back to the value of the literal.
20 // Requires a *polymorphic literal*, i.e. one that can serve as f64 as well as f32.
21 macro_rules
! test_literal
{
25 let inputs
= &[stringify
!($x
).into(), format
!("{:?}", x64
), format
!("{:e}", x64
)];
27 assert_eq
!(input
.parse(), Ok(x64
));
28 assert_eq
!(input
.parse(), Ok(x32
));
29 let neg_input
= &format
!("-{}", input
);
30 assert_eq
!(neg_input
.parse(), Ok(-x64
));
31 assert_eq
!(neg_input
.parse(), Ok(-x32
));
41 test_literal
!(12345.);
42 test_literal
!(0.9999999);
43 test_literal
!(2.2250738585072014e-308);
47 fn special_code_paths() {
48 test_literal
!(36893488147419103229.0); // 2^65 - 3, triggers half-to-even with even significand
49 test_literal
!(101e-33); // Triggers the tricky underflow case in AlgorithmM (for f32)
50 test_literal
!(1e23
); // Triggers AlgorithmR
51 test_literal
!(2075e23
); // Triggers another path through AlgorithmR
52 test_literal
!(8713e-23); // ... and yet another.
58 test_literal
!(123456789.34567e250
);
59 test_literal
!(943794359898089732078308743689303290943794359843568973207830874368930329.);
64 test_literal
!(5e
-324);
65 test_literal
!(91e-324);
66 test_literal
!(1e
-322);
67 test_literal
!(13245643e-320);
68 test_literal
!(2.22507385851e-308);
69 test_literal
!(2.1e-308);
70 test_literal
!(4.9406564584124654e-324);
78 test_literal
!(1.7976931348624e308
);
84 test_literal
!(1e
-325);
85 test_literal
!(1e
-326);
86 test_literal
!(1e
-500);
90 fn fast_path_correct() {
91 // This number triggers the fast path and is handled incorrectly when compiling on
92 // x86 without SSE2 (i.e., using the x87 FPU stack).
93 test_literal
!(1.448997445238699);
98 assert
!(".".parse
::<f32>().is_err());
99 assert
!(".".parse
::<f64>().is_err());
104 assert
!("+".parse
::<f32>().is_err());
105 assert
!("-".parse
::<f64>().is_err());
110 assert
!(" 1.0".parse
::<f32>().is_err());
111 assert
!("1.0 ".parse
::<f64>().is_err());
116 assert
!("NaN".parse
::<f32>().unwrap().is_nan());
117 assert
!("NaN".parse
::<f64>().unwrap().is_nan());
122 assert_eq
!("inf".parse(), Ok(f64::INFINITY
));
123 assert_eq
!("-inf".parse(), Ok(f64::NEG_INFINITY
));
124 assert_eq
!("inf".parse(), Ok(f32::INFINITY
));
125 assert_eq
!("-inf".parse(), Ok(f32::NEG_INFINITY
));
129 fn massive_exponent() {
131 assert_eq
!(format
!("1e{}000", max
).parse(), Ok(f64::INFINITY
));
132 assert_eq
!(format
!("1e-{}000", max
).parse(), Ok(0.0));
133 assert_eq
!(format
!("1e{}000", max
).parse(), Ok(f64::INFINITY
));
137 fn borderline_overflow() {
138 let mut s
= "0.".to_string();
142 // At the time of this writing, this returns Err(..), but this is a bug that should be fixed.
143 // It makes no sense to enshrine that in a test, the important part is that it doesn't panic.
144 let _
= s
.parse
::<f64>();