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