]> git.lizzy.rs Git - rust.git/blob - src/libcore/tests/num/dec2flt/mod.rs
17b2f59cd4df2814e47e1175eab92b011b3ddd99
[rust.git] / src / libcore / tests / 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.
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 #![allow(overflowing_literals)]
12
13 use std::{i64, f32, f64};
14
15 mod parse;
16 mod rawfp;
17
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 {
22     ($x: expr) => ({
23         let x32: f32 = $x;
24         let x64: f64 = $x;
25         let inputs = &[stringify!($x).into(), format!("{:?}", x64), format!("{:e}", x64)];
26         for input in inputs {
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));
32         }
33     })
34 }
35
36 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
37 #[test]
38 fn ordinary() {
39     test_literal!(1.0);
40     test_literal!(3e-5);
41     test_literal!(0.1);
42     test_literal!(12345.);
43     test_literal!(0.9999999);
44     test_literal!(2.2250738585072014e-308);
45 }
46
47 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
48 #[test]
49 fn special_code_paths() {
50     test_literal!(36893488147419103229.0); // 2^65 - 3, triggers half-to-even with even significand
51     test_literal!(101e-33); // Triggers the tricky underflow case in AlgorithmM (for f32)
52     test_literal!(1e23); // Triggers AlgorithmR
53     test_literal!(2075e23); // Triggers another path through AlgorithmR
54     test_literal!(8713e-23); // ... and yet another.
55 }
56
57 #[test]
58 fn large() {
59     test_literal!(1e300);
60     test_literal!(123456789.34567e250);
61     test_literal!(943794359898089732078308743689303290943794359843568973207830874368930329.);
62 }
63
64 #[test]
65 fn subnormals() {
66     test_literal!(5e-324);
67     test_literal!(91e-324);
68     test_literal!(1e-322);
69     test_literal!(13245643e-320);
70     test_literal!(2.22507385851e-308);
71     test_literal!(2.1e-308);
72     test_literal!(4.9406564584124654e-324);
73 }
74
75 #[test]
76 fn infinity() {
77     test_literal!(1e400);
78     test_literal!(1e309);
79     test_literal!(2e308);
80     test_literal!(1.7976931348624e308);
81 }
82
83 #[test]
84 fn zero() {
85     test_literal!(0.0);
86     test_literal!(1e-325);
87     test_literal!(1e-326);
88     test_literal!(1e-500);
89 }
90
91 #[test]
92 fn fast_path_correct() {
93     // This number triggers the fast path and is handled incorrectly when compiling on
94     // x86 without SSE2 (i.e., using the x87 FPU stack).
95     test_literal!(1.448997445238699);
96 }
97
98 #[test]
99 fn lonely_dot() {
100     assert!(".".parse::<f32>().is_err());
101     assert!(".".parse::<f64>().is_err());
102 }
103
104 #[test]
105 fn exponentiated_dot() {
106     assert!(".e0".parse::<f32>().is_err());
107     assert!(".e0".parse::<f64>().is_err());
108 }
109
110 #[test]
111 fn lonely_sign() {
112     assert!("+".parse::<f32>().is_err());
113     assert!("-".parse::<f64>().is_err());
114 }
115
116 #[test]
117 fn whitespace() {
118     assert!(" 1.0".parse::<f32>().is_err());
119     assert!("1.0 ".parse::<f64>().is_err());
120 }
121
122 #[test]
123 fn nan() {
124     assert!("NaN".parse::<f32>().unwrap().is_nan());
125     assert!("NaN".parse::<f64>().unwrap().is_nan());
126 }
127
128 #[test]
129 fn inf() {
130     assert_eq!("inf".parse(), Ok(f64::INFINITY));
131     assert_eq!("-inf".parse(), Ok(f64::NEG_INFINITY));
132     assert_eq!("inf".parse(), Ok(f32::INFINITY));
133     assert_eq!("-inf".parse(), Ok(f32::NEG_INFINITY));
134 }
135
136 #[test]
137 fn massive_exponent() {
138     let max = i64::MAX;
139     assert_eq!(format!("1e{}000", max).parse(), Ok(f64::INFINITY));
140     assert_eq!(format!("1e-{}000", max).parse(), Ok(0.0));
141     assert_eq!(format!("1e{}000", max).parse(), Ok(f64::INFINITY));
142 }
143
144 #[test]
145 fn borderline_overflow() {
146     let mut s = "0.".to_string();
147     for _ in 0..375 {
148         s.push('3');
149     }
150     // At the time of this writing, this returns Err(..), but this is a bug that should be fixed.
151     // It makes no sense to enshrine that in a test, the important part is that it doesn't panic.
152     let _ = s.parse::<f64>();
153 }