ocpi-tariffs 0.54.0

OCPI tariff calculations
Documentation
use rust_decimal_macros::dec;

use super::RoundDecimal as _;

#[test]
fn should_round_to_nearest_even_below() {
    let d = dec!(13.951148000000);
    assert_eq!(d.round_to_ocpi_scale(), dec!(13.9511));
}

#[test]
fn should_round_to_nearest_even_above() {
    let d = dec!(13.951158000000);
    assert_eq!(d.round_to_ocpi_scale(), dec!(13.9512));
}

/// A tie is the only case where a rounding strategy is observable, and the case the OCPI
/// spec leaves open. Pinned here so the choice stated at `number::SCALE` cannot drift.
#[test]
fn should_round_a_tie_to_the_even_digit() {
    let cases = [
        (dec!(0.00005), dec!(0.0000)),
        (dec!(0.00015), dec!(0.0002)),
        (dec!(0.00025), dec!(0.0002)),
        (dec!(0.00035), dec!(0.0004)),
        (dec!(0.12345), dec!(0.1234)),
        (dec!(0.25005), dec!(0.2500)),
    ];

    for (value, expected) in cases {
        assert_eq!(value.round_to_ocpi_scale(), expected, "rounding {value}");
    }
}

/// The sign must not decide a tie: `-0.00025` and `0.00025` both land on the even digit.
#[test]
fn should_round_a_negative_tie_to_the_even_digit() {
    let cases = [
        (dec!(-0.00005), dec!(0.0000)),
        (dec!(-0.00015), dec!(-0.0002)),
        (dec!(-0.00025), dec!(-0.0002)),
        (dec!(-0.00035), dec!(-0.0004)),
        (dec!(-0.12345), dec!(-0.1234)),
        (dec!(-0.25005), dec!(-0.2500)),
    ];

    for (value, expected) in cases {
        assert_eq!(value.round_to_ocpi_scale(), expected, "rounding {value}");
    }
}