From 9b3bb7abd71da3109119135f54ac601777b4516e Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Sat, 14 Sep 2019 16:16:12 -0500 Subject: [PATCH] Test floats that are too big/small --- src/parse/number_literal.rs | 4 ++-- tests/test_parse.rs | 33 ++++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/parse/number_literal.rs b/src/parse/number_literal.rs index 6fd68f1daf..f82bb74a73 100644 --- a/src/parse/number_literal.rs +++ b/src/parse/number_literal.rs @@ -94,8 +94,8 @@ where } // At this point we have a number, and will definitely succeed. - // If the number is malformed (too large to fit), we'll succeed with - // an appropriate Expr which records that. + // If the number is malformed (outside the supported range), + // we'll succeed with an appropriate Expr which records that. let expr = if has_decimal_point { let mut f64_buf = String::with_capacity_in( before_decimal.len() diff --git a/tests/test_parse.rs b/tests/test_parse.rs index 86b8c46053..2b3f188eec 100644 --- a/tests/test_parse.rs +++ b/tests/test_parse.rs @@ -298,14 +298,14 @@ mod test_parser { fn positive_int() { assert_parses_to("1", Int(1)); assert_parses_to("42", Int(42)); - assert_parses_to(i64::MAX.to_string().as_str(), Int(std::i64::MAX)); + assert_parses_to(i64::MAX.to_string().as_str(), Int(i64::MAX)); } #[test] fn negative_int() { assert_parses_to("-1", Int(-1)); assert_parses_to("-42", Int(-42)); - assert_parses_to(i64::MIN.to_string().as_str(), Int(std::i64::MIN)); + assert_parses_to(i64::MIN.to_string().as_str(), Int(i64::MIN)); } #[quickcheck] @@ -342,8 +342,11 @@ mod test_parser { assert_parses_to("1.1", Float(1.1)); assert_parses_to("42.0", Float(42.0)); assert_parses_to("42.9", Float(42.9)); - assert_parses_to(&format!("{}.0", f64::MAX), Float(std::f64::MAX)); - panic!("TODO stress test maximum float *digits*, not just integer vals"); + } + + #[test] + fn highest_float() { + assert_parses_to(&format!("{}.0", f64::MAX), Float(f64::MAX)); } #[test] @@ -352,7 +355,11 @@ mod test_parser { assert_parses_to("-1.1", Float(-1.1)); assert_parses_to("-42.0", Float(-42.0)); assert_parses_to("-42.9", Float(-42.9)); - assert_parses_to(f64::MIN.to_string().as_str(), Float(std::f64::MIN)); + } + + #[test] + fn lowest_float() { + assert_parses_to(&format!("{}.0", f64::MIN), Float(f64::MIN)); } #[quickcheck] @@ -360,6 +367,22 @@ mod test_parser { assert_parses_to(num.to_string().as_str(), Float(num)); } + #[test] + fn float_too_large() { + assert_parses_to( + format!("{}1.0", f64::MAX).as_str(), + MalformedNumber(Problem::OutsideSupportedRange), + ); + } + + #[test] + fn float_too_small() { + assert_parses_to( + format!("{}1.0", f64::MIN).as_str(), + MalformedNumber(Problem::OutsideSupportedRange), + ); + } + // fn expect_parsed_float<'a>(expected: f64, actual: &str) { // assert_eq!( // Ok((Float(expected), "".to_string())),