2015-07-26 10:50:29 -05:00
|
|
|
#![allow(overflowing_literals)]
|
|
|
|
|
|
|
|
mod parse;
|
|
|
|
mod rawfp;
|
|
|
|
|
2015-10-07 17:11:25 -05:00
|
|
|
// Take a float literal, turn it into a string in various ways (that are all trusted
|
2015-07-26 10:50:29 -05:00
|
|
|
// to be correct) and see if those strings are parsed back to the value of the literal.
|
2018-11-26 20:59:49 -06:00
|
|
|
// Requires a *polymorphic literal*, i.e., one that can serve as f64 as well as f32.
|
2015-07-26 10:50:29 -05:00
|
|
|
macro_rules! test_literal {
|
2019-12-06 22:18:12 -06:00
|
|
|
($x: expr) => {{
|
2015-07-26 10:50:29 -05:00
|
|
|
let x32: f32 = $x;
|
|
|
|
let x64: f64 = $x;
|
|
|
|
let inputs = &[stringify!($x).into(), format!("{:?}", x64), format!("{:e}", x64)];
|
|
|
|
for input in inputs {
|
2016-02-04 06:51:18 -06:00
|
|
|
assert_eq!(input.parse(), Ok(x64));
|
|
|
|
assert_eq!(input.parse(), Ok(x32));
|
|
|
|
let neg_input = &format!("-{}", input);
|
|
|
|
assert_eq!(neg_input.parse(), Ok(-x64));
|
|
|
|
assert_eq!(neg_input.parse(), Ok(-x32));
|
2015-07-26 10:50:29 -05:00
|
|
|
}
|
2019-12-06 22:18:12 -06:00
|
|
|
}};
|
2015-07-26 10:50:29 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 08:46:54 -05:00
|
|
|
#[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
|
2015-07-26 10:50:29 -05:00
|
|
|
#[test]
|
|
|
|
fn ordinary() {
|
|
|
|
test_literal!(1.0);
|
|
|
|
test_literal!(3e-5);
|
|
|
|
test_literal!(0.1);
|
|
|
|
test_literal!(12345.);
|
|
|
|
test_literal!(0.9999999);
|
2019-12-07 05:47:18 -06:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
if cfg!(miri) {
|
|
|
|
// Miri is too slow
|
2019-12-07 05:47:18 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-26 10:50:29 -05:00
|
|
|
test_literal!(2.2250738585072014e-308);
|
|
|
|
}
|
|
|
|
|
2017-06-13 08:46:54 -05:00
|
|
|
#[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
|
2015-07-26 10:50:29 -05:00
|
|
|
#[test]
|
|
|
|
fn special_code_paths() {
|
|
|
|
test_literal!(36893488147419103229.0); // 2^65 - 3, triggers half-to-even with even significand
|
|
|
|
test_literal!(101e-33); // Triggers the tricky underflow case in AlgorithmM (for f32)
|
|
|
|
test_literal!(1e23); // Triggers AlgorithmR
|
|
|
|
test_literal!(2075e23); // Triggers another path through AlgorithmR
|
|
|
|
test_literal!(8713e-23); // ... and yet another.
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn large() {
|
|
|
|
test_literal!(1e300);
|
|
|
|
test_literal!(123456789.34567e250);
|
|
|
|
test_literal!(943794359898089732078308743689303290943794359843568973207830874368930329.);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-12-07 05:38:13 -06:00
|
|
|
#[cfg_attr(miri, ignore)] // Miri is too slow
|
2015-07-26 10:50:29 -05:00
|
|
|
fn subnormals() {
|
|
|
|
test_literal!(5e-324);
|
|
|
|
test_literal!(91e-324);
|
|
|
|
test_literal!(1e-322);
|
|
|
|
test_literal!(13245643e-320);
|
|
|
|
test_literal!(2.22507385851e-308);
|
|
|
|
test_literal!(2.1e-308);
|
|
|
|
test_literal!(4.9406564584124654e-324);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-12-07 05:38:13 -06:00
|
|
|
#[cfg_attr(miri, ignore)] // Miri is too slow
|
2015-07-26 10:50:29 -05:00
|
|
|
fn infinity() {
|
|
|
|
test_literal!(1e400);
|
|
|
|
test_literal!(1e309);
|
|
|
|
test_literal!(2e308);
|
|
|
|
test_literal!(1.7976931348624e308);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn zero() {
|
|
|
|
test_literal!(0.0);
|
|
|
|
test_literal!(1e-325);
|
2019-12-07 05:47:18 -06:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
if cfg!(miri) {
|
|
|
|
// Miri is too slow
|
2019-12-07 05:47:18 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-26 10:50:29 -05:00
|
|
|
test_literal!(1e-326);
|
|
|
|
test_literal!(1e-500);
|
|
|
|
}
|
|
|
|
|
2015-08-10 16:14:30 -05:00
|
|
|
#[test]
|
|
|
|
fn fast_path_correct() {
|
|
|
|
// This number triggers the fast path and is handled incorrectly when compiling on
|
|
|
|
// x86 without SSE2 (i.e., using the x87 FPU stack).
|
|
|
|
test_literal!(1.448997445238699);
|
|
|
|
}
|
|
|
|
|
2015-07-26 10:50:29 -05:00
|
|
|
#[test]
|
|
|
|
fn lonely_dot() {
|
2016-01-03 14:08:53 -06:00
|
|
|
assert!(".".parse::<f32>().is_err());
|
|
|
|
assert!(".".parse::<f64>().is_err());
|
2015-07-26 10:50:29 -05:00
|
|
|
}
|
|
|
|
|
2018-02-15 09:54:40 -06:00
|
|
|
#[test]
|
|
|
|
fn exponentiated_dot() {
|
|
|
|
assert!(".e0".parse::<f32>().is_err());
|
|
|
|
assert!(".e0".parse::<f64>().is_err());
|
|
|
|
}
|
|
|
|
|
2015-10-14 12:27:49 -05:00
|
|
|
#[test]
|
|
|
|
fn lonely_sign() {
|
|
|
|
assert!("+".parse::<f32>().is_err());
|
|
|
|
assert!("-".parse::<f64>().is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn whitespace() {
|
|
|
|
assert!(" 1.0".parse::<f32>().is_err());
|
|
|
|
assert!("1.0 ".parse::<f64>().is_err());
|
|
|
|
}
|
|
|
|
|
2015-07-26 10:50:29 -05:00
|
|
|
#[test]
|
|
|
|
fn nan() {
|
2015-08-17 16:16:41 -05:00
|
|
|
assert!("NaN".parse::<f32>().unwrap().is_nan());
|
|
|
|
assert!("NaN".parse::<f64>().unwrap().is_nan());
|
2015-07-26 10:50:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inf() {
|
2015-08-17 16:16:41 -05:00
|
|
|
assert_eq!("inf".parse(), Ok(f64::INFINITY));
|
|
|
|
assert_eq!("-inf".parse(), Ok(f64::NEG_INFINITY));
|
|
|
|
assert_eq!("inf".parse(), Ok(f32::INFINITY));
|
|
|
|
assert_eq!("-inf".parse(), Ok(f32::NEG_INFINITY));
|
2015-07-26 10:50:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn massive_exponent() {
|
|
|
|
let max = i64::MAX;
|
2015-08-17 16:16:41 -05:00
|
|
|
assert_eq!(format!("1e{}000", max).parse(), Ok(f64::INFINITY));
|
|
|
|
assert_eq!(format!("1e-{}000", max).parse(), Ok(0.0));
|
|
|
|
assert_eq!(format!("1e{}000", max).parse(), Ok(f64::INFINITY));
|
2015-07-26 10:50:29 -05:00
|
|
|
}
|
|
|
|
|
2016-01-26 15:10:21 -06:00
|
|
|
#[test]
|
|
|
|
fn borderline_overflow() {
|
|
|
|
let mut s = "0.".to_string();
|
|
|
|
for _ in 0..375 {
|
|
|
|
s.push('3');
|
|
|
|
}
|
|
|
|
// At the time of this writing, this returns Err(..), but this is a bug that should be fixed.
|
|
|
|
// It makes no sense to enshrine that in a test, the important part is that it doesn't panic.
|
|
|
|
let _ = s.parse::<f64>();
|
|
|
|
}
|