use multiversx_sc::math::{
LinearInterpolationInvalidValuesError, linear_interpolation, weighted_average,
weighted_average_round_up,
};
#[test]
fn linear_interpolation_at_min_input() {
let result = linear_interpolation(0u32, 100u32, 0u32, 200u32, 400u32).unwrap();
assert_eq!(result, 200u32);
}
#[test]
fn linear_interpolation_at_max_input() {
let result = linear_interpolation(0u32, 100u32, 100u32, 200u32, 400u32).unwrap();
assert_eq!(result, 400u32);
}
#[test]
fn linear_interpolation_at_midpoint() {
let result = linear_interpolation(0u32, 100u32, 50u32, 0u32, 1000u32).unwrap();
assert_eq!(result, 500u32);
}
#[test]
fn linear_interpolation_at_one_quarter() {
let result = linear_interpolation(0u32, 100u32, 25u32, 0u32, 1000u32).unwrap();
assert_eq!(result, 250u32);
}
#[test]
fn linear_interpolation_non_zero_based_ranges() {
let result = linear_interpolation(10u32, 50u32, 30u32, 100u32, 200u32).unwrap();
assert_eq!(result, 150u32);
}
#[test]
fn linear_interpolation_reversed_output_range() {
let result = linear_interpolation(0u32, 100u32, 25u32, 1000u32, 0u32).unwrap();
assert_eq!(result, 750u32);
}
#[test]
fn linear_interpolation_below_range_returns_error() {
let result = linear_interpolation(10u32, 100u32, 5u32, 0u32, 1000u32);
assert!(matches!(result, Err(LinearInterpolationInvalidValuesError)));
}
#[test]
fn linear_interpolation_above_range_returns_error() {
let result = linear_interpolation(0u32, 100u32, 110u32, 0u32, 1000u32);
assert!(matches!(result, Err(LinearInterpolationInvalidValuesError)));
}
#[test]
fn weighted_average_equal_weights() {
let result = weighted_average(10u64, 1u64, 20u64, 1u64);
assert_eq!(result, 15);
}
#[test]
fn weighted_average_all_weight_on_first() {
let result = weighted_average(10u64, 5u64, 99u64, 0u64);
assert_eq!(result, 10);
}
#[test]
fn weighted_average_all_weight_on_second() {
let result = weighted_average(99u64, 0u64, 20u64, 5u64);
assert_eq!(result, 20);
}
#[test]
fn weighted_average_three_to_one() {
let result = weighted_average(0u64, 1u64, 60u64, 3u64);
assert_eq!(result, 45);
}
#[test]
fn weighted_average_round_up_exact_division() {
let result = weighted_average_round_up(10u64, 1u64, 20u64, 1u64);
assert_eq!(result, 15);
}
#[test]
fn weighted_average_round_up_truncates_vs_rounds() {
let floor_result = weighted_average(0u64, 1u64, 10u64, 3u64);
let ceil_result = weighted_average_round_up(0u64, 1u64, 10u64, 3u64);
assert_eq!(floor_result, 7);
assert_eq!(ceil_result, 8);
}
#[test]
fn weighted_average_round_up_no_change_when_exact() {
let result = weighted_average_round_up(0u64, 1u64, 20u64, 3u64);
assert_eq!(result, 15);
}