use proptest::prelude::*;
use super::{Number, NumberReading};
#[cfg(all(feature = "convert", feature = "parse", feature = "record"))]
mod table;
fn bits(number: Option<Number>) -> Option<(char, u64)> {
Some(match number? {
Number::Int(n) => ('i', n.cast_unsigned()),
Number::UInt(n) => ('u', n),
Number::Float(n) => ('f', n.to_bits()),
})
}
fn same(a: Option<Number>, b: Option<Number>) -> bool {
bits(a) == bits(b)
}
#[test]
fn the_readings_differ_only_past_the_integer_range() {
use Number::{Float, Int, UInt};
let wide_digits = "9".repeat(400);
let cases: &[(&str, Option<Number>, Option<Number>)] = &[
("0", Some(Int(0)), Some(Int(0))),
("42", Some(Int(42)), Some(Int(42))),
("-7", Some(Int(-7)), Some(Int(-7))),
("-0", Some(Int(0)), Some(Float(-0.0))),
("+0", Some(Int(0)), Some(Int(0))),
("+5", Some(Int(5)), Some(Int(5))),
("007", Some(Int(7)), Some(Int(7))),
(
"9223372036854775807",
Some(Int(i64::MAX)),
Some(Int(i64::MAX)),
),
(
"9223372036854775808",
Some(UInt(1 << 63)),
Some(UInt(1 << 63)),
),
(
"18446744073709551615",
Some(UInt(u64::MAX)),
Some(UInt(u64::MAX)),
),
(
"000000000000000000000018446744073709551615",
Some(UInt(u64::MAX)),
Some(UInt(u64::MAX)),
),
(
"-9223372036854775808",
Some(Int(i64::MIN)),
Some(Int(i64::MIN)),
),
(
"18446744073709551616",
None,
Some(Float(18_446_744_073_709_551_616.0)),
),
(
"+18446744073709551616",
None,
Some(Float(18_446_744_073_709_551_616.0)),
),
(
"-9223372036854775809",
None,
Some(Float(-9_223_372_036_854_775_808.0)),
),
(
"9007199254740993",
Some(Int(9_007_199_254_740_993)),
Some(Int(9_007_199_254_740_993)),
),
("1.5", Some(Float(1.5)), Some(Float(1.5))),
("1.0", Some(Float(1.0)), Some(Float(1.0))),
("1e5", Some(Float(1e5)), Some(Float(1e5))),
("1E+3", Some(Float(1000.0)), Some(Float(1000.0))),
(".5", Some(Float(0.5)), Some(Float(0.5))),
("5.", Some(Float(5.0)), Some(Float(5.0))),
("-0.0", Some(Float(-0.0)), Some(Float(-0.0))),
("1e-9999", Some(Float(0.0)), Some(Float(0.0))),
("-1e-9999", Some(Float(-0.0)), Some(Float(-0.0))),
("1e309", None, None),
("-1e309", None, None),
(&wide_digits, None, None),
("", None, None),
("-", None, None),
("+", None, None),
(".", None, None),
("e5", None, None),
("1e", None, None),
("1e+", None, None),
("--1", None, None),
("1+2", None, None),
("1.5.2", None, None),
(" 1", None, None),
("1 ", None, None),
("1\n", None, None),
("0x10", None, None),
("1_000", None, None),
("1,5", None, None),
("inf", None, None),
("-inf", None, None),
("infinity", None, None),
("NaN", None, None),
("nan", None, None),
("\u{661}\u{662}", None, None),
];
for (text, exact, cel) in cases {
let got = NumberReading::Exact.read(text);
assert!(same(got, *exact), "Exact {text}: {got:?}, want {exact:?}");
let got = NumberReading::Cel.read(text);
assert!(same(got, *cel), "Cel {text}: {got:?}, want {cel:?}");
}
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(2_000))]
#[test]
fn every_i64_reads_the_same_under_both(n in any::<i64>()) {
let want = Some(Number::Int(n));
for reading in [NumberReading::Exact, NumberReading::Cel] {
prop_assert!(same(reading.read(&n.to_string()), want), "{:?} {}", reading, n);
}
}
#[test]
fn every_u64_reads_the_same_under_both(n in any::<u64>()) {
let want = Some(i64::try_from(n).map_or(Number::UInt(n), Number::Int));
for reading in [NumberReading::Exact, NumberReading::Cel] {
prop_assert!(same(reading.read(&n.to_string()), want), "{:?} {}", reading, n);
}
}
#[test]
#[allow(
clippy::cast_precision_loss,
reason = "rounding to the nearest double is the point"
)]
fn a_whole_number_past_u64_is_rounded_only_by_cel(
n in (u128::from(u64::MAX) + 1)..=u128::MAX,
negative in any::<bool>(),
) {
let text = if negative { format!("-{n}") } else { n.to_string() };
prop_assert_eq!(NumberReading::Exact.read(&text), None);
let rounded = if negative { -(n as f64) } else { n as f64 };
prop_assert!(
same(NumberReading::Cel.read(&text), Some(Number::Float(rounded))),
"{}",
text
);
}
#[test]
#[allow(
clippy::cast_precision_loss,
reason = "rounding to the nearest double is the point"
)]
fn a_whole_number_below_i64_min_is_rounded_only_by_cel(
n in (i128::from(i64::MIN) - (1_i128 << 100))..i128::from(i64::MIN),
) {
let text = n.to_string();
prop_assert_eq!(NumberReading::Exact.read(&text), None);
prop_assert!(
same(NumberReading::Cel.read(&text), Some(Number::Float(n as f64))),
"{}",
text
);
}
#[test]
fn every_finite_f64_reads_the_same_under_both(
x in any::<f64>().prop_filter("finite", |x| x.is_finite()),
) {
for text in [format!("{x}"), format!("{x:e}"), format!("{x:E}")] {
if !text.contains(['.', 'e', 'E']) {
continue;
}
for reading in [NumberReading::Exact, NumberReading::Cel] {
let got = reading.read(&text);
prop_assert!(
same(got, Some(Number::Float(x))),
"{:?} {}: {:?}",
reading,
text,
got
);
}
}
}
#[test]
fn any_text_reads_and_the_readings_differ_only_on_rounding(text in "[-+.eE0-9 xnaif_,]{0,24}") {
let exact = NumberReading::Exact.read(&text);
let cel = NumberReading::Cel.read(&text);
if !same(exact, cel) {
prop_assert!(matches!(cel, Some(Number::Float(_))), "{}", text);
let digits = text.strip_prefix(['+', '-']).unwrap_or(&text);
prop_assert!(
!digits.is_empty() && digits.bytes().all(|b| b.is_ascii_digit()),
"{}",
text
);
let zero = text.starts_with('-') && digits.bytes().all(|b| b == b'0');
prop_assert!(exact.is_none() || zero, "{}", text);
}
}
}