1 #![allow(overflowing_literals)]
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 {
13 let inputs = &[stringify!($x).into(), format!("{:?}", x64), format!("{:e}", x64)];
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));
29 test_literal!(12345.);
30 test_literal!(0.9999999);
37 test_literal!(2.2250738585072014e-308);
41 fn special_code_paths() {
42 test_literal!(36893488147419103229.0); // 2^65 - 3, triggers half-to-even with even significand
43 test_literal!(101e-33); // Triggers the tricky underflow case in AlgorithmM (for f32)
44 test_literal!(1e23); // Triggers AlgorithmR
45 test_literal!(2075e23); // Triggers another path through AlgorithmR
46 test_literal!(8713e-23); // ... and yet another.
52 test_literal!(123456789.34567e250);
53 test_literal!(943794359898089732078308743689303290943794359843568973207830874368930329.);
57 #[cfg_attr(miri, ignore)] // Miri is too slow
59 test_literal!(5e-324);
60 test_literal!(91e-324);
61 test_literal!(1e-322);
62 test_literal!(13245643e-320);
63 test_literal!(2.22507385851e-308);
64 test_literal!(2.1e-308);
65 test_literal!(4.9406564584124654e-324);
69 #[cfg_attr(miri, ignore)] // Miri is too slow
74 test_literal!(1.7976931348624e308);
80 test_literal!(1e-325);
87 test_literal!(1e-326);
88 test_literal!(1e-500);
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);
100 assert!(".".parse::<f32>().is_err());
101 assert!(".".parse::<f64>().is_err());
105 fn exponentiated_dot() {
106 assert!(".e0".parse::<f32>().is_err());
107 assert!(".e0".parse::<f64>().is_err());
112 assert!("+".parse::<f32>().is_err());
113 assert!("-".parse::<f64>().is_err());
118 assert!(" 1.0".parse::<f32>().is_err());
119 assert!("1.0 ".parse::<f64>().is_err());
124 assert!("NaN".parse::<f32>().unwrap().is_nan());
125 assert!("NaN".parse::<f64>().unwrap().is_nan());
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));
137 fn massive_exponent() {
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));
145 fn borderline_overflow() {
146 let mut s = "0.".to_string();
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>();