use malachite_base::num::arithmetic::traits::PowerOf2;
use malachite_base::num::basic::traits::{
Infinity, NaN, NegativeInfinity, NegativeOne, NegativeZero, One, Zero,
};
use malachite_base::strings::typst::ToTypst;
use malachite_float::test_util::generators::float_gen;
use malachite_float::{ComparableFloat, ComparableFloatRef, Float};
fn undo_power_of_ten(s: &str) -> String {
const TIMES: &str = " times 10^";
s.find(TIMES).map_or_else(
|| s.to_string(),
|i| {
let mantissa = &s[..i];
let exponent = &s[i + TIMES.len()..];
let exponent = exponent
.strip_prefix('(')
.and_then(|e| e.strip_suffix(')'))
.unwrap_or(exponent);
format!("{mantissa}e{exponent}")
},
)
}
#[test]
fn test_float_to_typst() {
let test = |x: Float, out: &str| assert_eq!(x.to_typst_string(), out);
test(Float::NAN, r#""NaN""#);
test(Float::INFINITY, "infinity");
test(Float::NEGATIVE_INFINITY, "-infinity");
test(Float::ZERO, "0.0");
test(Float::NEGATIVE_ZERO, "-0.0");
test(Float::ONE, "1.0");
test(Float::NEGATIVE_ONE, "-1.0");
test(Float::from(1.5), "1.5");
test(Float::from(255), "255.0");
test(Float::power_of_2(100u64), "1.3 times 10^(30)");
test(Float::power_of_2(-100i64), "7.9 times 10^(-31)");
test(Float::one_prec(100), "1.0000000000000000000000000000000");
}
#[test]
fn float_to_typst_properties() {
float_gen().test_properties(|x| {
let s = x.to_typst_string();
assert!(!s.is_empty());
if !x.is_nan() && !x.is_infinite() {
assert_eq!(undo_power_of_ten(&s), x.to_string());
assert!(
!s.as_bytes()
.windows(2)
.any(|w| w[0].is_ascii_digit() && w[1] == b'e')
);
}
assert_eq!(ComparableFloat(x.clone()).to_typst_string(), s);
assert_eq!(ComparableFloatRef(&x).to_typst_string(), s);
});
}