lichtung-plot 0.3.0

Strongly typed scientific plotting with deterministic SVG and PNG output
Documentation
use super::*;

#[test]
fn exact_multiplier_composes_before_rounding_without_binary64_product_loss() {
    let format = crate::NumericFormat::new(
        NumericNotation::Scientific,
        crate::formatting::NumericPrecision::Significant(21),
    )
    .unwrap()
    .multiplier(10.0)
    .unwrap();
    certify_rational_cell(
        BigUint::from(3_602_879_701_896_397_u64) * 10_u8,
        BigUint::from(1_u8) << 55_usize,
        20,
        100_000_000_000_000_005_551,
    );
    assert_eq!(
        format.format_singleton(0.1, false).unwrap(),
        "1.00000000000000005551e0"
    );
    let large = crate::NumericFormat::new(
        NumericNotation::Scientific,
        crate::formatting::NumericPrecision::Significant(17),
    )
    .unwrap()
    .multiplier(2.0)
    .unwrap();
    assert_eq!(
        large.format_singleton(f64::MAX, false).unwrap(),
        "3.5953862697246314e308"
    );
    let tiny = crate::NumericFormat::default()
        .multiplier(f64::from_bits(2046_u64 << 52))
        .unwrap()
        .precision(crate::formatting::NumericPrecision::Fractional(20))
        .unwrap();
    assert_eq!(
        tiny.format_singleton(f64::from_bits(1), false).unwrap(),
        "0.00000000000000044409"
    );
    let negative = crate::NumericFormat::default().multiplier(-2.0).unwrap();
    assert_eq!(negative.format_singleton(1.25, false).unwrap(), "-2.50");
    let zero = negative.clone().multiplier(-0.0).unwrap();
    assert_eq!(zero, negative.multiplier(0.0).unwrap());
    assert_eq!(zero.format_singleton(f64::MAX, false).unwrap(), "0.00");
    for invalid in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
        assert!(crate::NumericFormat::default().multiplier(invalid).is_err());
    }
}

#[test]
fn rounding_preserves_signed_neighbors_and_both_tie_parities() {
    use NumericRounding::{
        AwayFromZero, Ceiling, Floor, NearestAway, NearestEven, NearestTowardZero, TowardZero,
    };
    // Exact dyadics straddle a half-cell and exercise its odd/even neighbors.
    for (rounding, expected) in [
        (
            NearestEven,
            [("1", "-1"), ("2", "-2"), ("2", "-2"), ("2", "-2")],
        ),
        (
            NearestAway,
            [("1", "-1"), ("2", "-2"), ("2", "-2"), ("3", "-3")],
        ),
        (
            NearestTowardZero,
            [("1", "-1"), ("1", "-1"), ("2", "-2"), ("2", "-2")],
        ),
        (
            TowardZero,
            [("1", "-1"), ("1", "-1"), ("1", "-1"), ("2", "-2")],
        ),
        (
            AwayFromZero,
            [("2", "-2"), ("2", "-2"), ("2", "-2"), ("3", "-3")],
        ),
        (Floor, [("1", "-2"), ("1", "-2"), ("1", "-2"), ("2", "-3")]),
        (
            Ceiling,
            [("2", "-1"), ("2", "-1"), ("2", "-1"), ("3", "-2")],
        ),
    ] {
        let format = crate::NumericFormat::new(
            NumericNotation::Decimal,
            crate::formatting::NumericPrecision::Fractional(0),
        )
        .unwrap()
        .rounding(rounding)
        .unwrap();
        for (value, (positive, negative)) in [1.125, 1.5, 1.875, 2.5].into_iter().zip(expected) {
            assert_eq!(format.format_singleton(value, false).unwrap(), positive);
            assert_eq!(format.format_singleton(-value, false).unwrap(), negative);
        }
    }
    let floor = ResolvedFormat {
        precision: ResolvedPrecision::Fractional(0),
        rounding: Floor,
        negative_zero: NegativeZero::Normalize,
        ..ResolvedFormat::default()
    };
    assert_eq!(format_finite(-f64::from_bits(1), &floor).unwrap(), "-1");
    assert_eq!(format_finite(-0.0, &floor).unwrap(), "0");
    let ceiling = ResolvedFormat {
        rounding: Ceiling,
        negative_zero: NegativeZero::Preserve,
        ..floor
    };
    assert_eq!(format_finite(-f64::from_bits(1), &ceiling).unwrap(), "-0");
}

#[test]
fn decimal_emission_obeys_notation_precision_and_zero_occupancy() {
    use NumericNotation::{Decimal, Engineering, Scientific};
    use ResolvedPrecision::{Fractional, Significant};

    // Handwritten dyadic/integer results, including single-round carries.
    for (value, notation, precision, expected) in [
        (1.25, Decimal, Fractional(1), "1.2"),
        (1.75, Decimal, Fractional(1), "1.8"),
        (100.5, Decimal, Fractional(0), "100"),
        (101.5, Decimal, Fractional(0), "102"),
        (1.0, Decimal, Significant(3), "1.00"),
        (0.125, Decimal, Significant(5), "0.12500"),
        (0.001_953_125, Decimal, Significant(3), "0.00195"),
        (12345.0, Decimal, Significant(3), "12300"),
        (999.5, Decimal, Significant(3), "1000"),
        (9.5, Scientific, Fractional(0), "1e1"),
        (0.125, Scientific, Fractional(2), "1.25e-1"),
        (12345.0, Scientific, Fractional(2), "1.23e4"),
        (0.125, Engineering, Fractional(2), "125.00e-3"),
        (12345.0, Engineering, Fractional(2), "12.34e3"),
        (999.5, Engineering, Significant(3), "1.00e3"),
        (999.5, Engineering, Fractional(0), "1e3"),
        (999.5, Engineering, Significant(2), "1.0e3"),
        (999.5, Engineering, Significant(1), "1e3"),
        (12345.0, Engineering, Significant(1), "10e3"),
        (0.0, Decimal, Significant(3), "0.00"),
        (0.0, Scientific, Significant(3), "0.00e0"),
        (0.0, Engineering, Significant(3), "0.00e0"),
    ] {
        let format = ResolvedFormat {
            notation,
            precision,
            ..ResolvedFormat::default()
        };
        assert_eq!(format_finite(value, &format).unwrap(), expected);
        assert_eq!(
            format_finite(-value, &format).unwrap(),
            format!("-{expected}")
        );
    }

    for notation in [Decimal, Scientific, Engineering] {
        let format = ResolvedFormat {
            notation,
            precision: Significant(3),
            prefix: Some("  μ["),
            suffix: Some("\u{0301} units00 "),
            negative_zero: NegativeZero::Normalize,
            trailing_zeros: TrailingZeros::Trim,
            ..ResolvedFormat::default()
        };
        let expected = if notation == Decimal {
            "  μ[0\u{0301} units00 "
        } else {
            "  μ[0e0\u{0301} units00 "
        };
        assert_eq!(format_finite(-0.0, &format).unwrap(), expected);
    }
    let format = ResolvedFormat {
        prefix: Some("["),
        suffix: Some("]"),
        ..ResolvedFormat::default()
    };
    assert_eq!(format_finite(-0.003_906_25, &format).unwrap(), "[-0.00]");
    assert_eq!(
        format_finite(
            -0.003_906_25,
            &ResolvedFormat {
                negative_zero: NegativeZero::Normalize,
                ..format
            },
        )
        .unwrap(),
        "[0.00]"
    );
    let percent = ResolvedFormat {
        precision: Fractional(1),
        decimal_scale: 2,
        suffix: Some("%"),
        ..ResolvedFormat::default()
    };
    assert_eq!(format_finite(0.125, &percent).unwrap(), "12.5%");
    assert_eq!(
        format_finite(
            0.125,
            &ResolvedFormat {
                decimal_scale: 0,
                ..percent
            }
        )
        .unwrap(),
        "0.1%"
    );
    assert_eq!(
        format_finite(
            1000.0,
            &ResolvedFormat {
                trailing_zeros: TrailingZeros::Trim,
                ..format
            }
        )
        .unwrap(),
        "[1000]"
    );
}

#[test]
fn finite_extremes_keep_exact_scaled_values_and_refuse_unrepresentable_output() {
    // Literal 17-significant coefficients, certified independently below against
    // binary rational magnitudes, not the converter's power-of-five decimal form.
    for (value, scale, expected, coefficient, quantum, numerator, denominator) in [
        (
            f64::MAX,
            324,
            "1.7976931348623157e632",
            17_976_931_348_623_157_u128,
            616_i32,
            (BigUint::from(1_u8) << 1024_usize) - (BigUint::from(1_u8) << 971_usize),
            BigUint::from(1_u8),
        ),
        (
            f64::MIN_POSITIVE,
            -324,
            "2.2250738585072014e-632",
            22_250_738_585_072_014,
            -648,
            BigUint::from(1_u8),
            BigUint::from(1_u8) << 1022_usize,
        ),
        (
            f64::from_bits(1),
            -324,
            "4.9406564584124654e-648",
            49_406_564_584_124_654,
            -664,
            BigUint::from(1_u8),
            BigUint::from(1_u8) << 1074_usize,
        ),
    ] {
        certify_rational_cell(
            numerator,
            denominator,
            i32::from(scale) - quantum,
            coefficient,
        );
        let format = ResolvedFormat {
            notation: NumericNotation::Scientific,
            precision: ResolvedPrecision::Significant(17),
            decimal_scale: scale,
            ..ResolvedFormat::default()
        };
        assert_eq!(format_finite(value, &format).unwrap(), expected);
        assert_eq!(
            format_finite(-value, &format).unwrap(),
            format!("-{expected}")
        );
    }
    let format = ResolvedFormat {
        precision: ResolvedPrecision::Significant(3),
        decimal_scale: -324,
        ..ResolvedFormat::default()
    };
    assert_eq!(
        format_finite(1.0, &format).unwrap(),
        format!("0.{}100", "0".repeat(323))
    );
    assert_eq!(
        format_finite(
            1.0,
            &ResolvedFormat {
                decimal_scale: 324,
                ..format
            }
        )
        .unwrap(),
        format!("1{}", "0".repeat(324))
    );

    for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
        assert!(matches!(format_finite(value, &format), Err(Error::Data(_))));
    }
    assert!(matches!(
        DecimalBuffer::reserve(usize::MAX, 0),
        Err(Error::Render(_))
    ));
    for invalid in [
        ResolvedFormat {
            decimal_scale: -325,
            ..format
        },
        ResolvedFormat {
            decimal_scale: 325,
            ..format
        },
    ] {
        assert!(matches!(format_finite(1.0, &invalid), Err(Error::Data(_))));
    }
    let huge = RoundedDecimal {
        negative: false,
        coefficient: BigUint::from(1_u8),
        exponent: BigInt::from(usize::MAX),
    };
    assert!(matches!(huge.emit(&format), Err(Error::Render(_))));
    assert!(matches!(
        RoundedDecimal {
            exponent: -huge.exponent,
            ..huge
        }
        .emit(&format),
        Err(Error::Render(_))
    ));
}

/// Strict rounding-cell inequalities certify these non-tie rational vectors.
/// This oracle uses their binary denominator, not production decimal decoding.
fn certify_rational_cell(numerator: BigUint, denominator: BigUint, shift: i32, coefficient: u128) {
    let (top, bottom) = if shift >= 0 {
        (
            numerator * BigUint::from(10_u8).pow(shift.unsigned_abs()),
            denominator,
        )
    } else {
        (
            numerator,
            denominator * BigUint::from(10_u8).pow(shift.unsigned_abs()),
        )
    };
    let twice = &top << 1_usize;
    assert!(twice > &bottom * (2 * coefficient - 1));
    assert!(twice < bottom * (2 * coefficient + 1));
}