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