44 lines
2.5 KiB
Rust
44 lines
2.5 KiB
Rust
// file: crates/ksp-app-wallet-desk/unit_tests/wallet_balance.rs
|
|
// version: 3
|
|
|
|
//! Unit tests for exact Wallet balance presentation.
|
|
|
|
#[test]
|
|
fn lamports_formatting_is_exact_and_uses_nine_fractional_digits() {
|
|
assert_eq!(crate::format_lamports_as_sol(0), "0.000000000");
|
|
assert_eq!(crate::format_lamports_as_sol(1), "0.000000001");
|
|
assert_eq!(crate::format_lamports_as_sol(1_000_000_000), "1.000000000");
|
|
assert_eq!(crate::format_lamports_as_sol(1_234_567_890), "1.234567890");
|
|
assert_eq!(crate::format_lamports_as_sol(u64::MAX), "18446744073.709551615");
|
|
}
|
|
|
|
#[test]
|
|
fn sol_usd_average_is_deterministic_rounded_and_never_uses_f64() {
|
|
let values = ["103.96", "104.16", "104.07981394241013", "104.07595797523913", "103.92807280061784", "103.9"];
|
|
let prices = values.iter().filter_map(|value| return ksp_offchain_transport_lib::MarketPriceDecimal::parse(value).ok()).collect::<std::vec::Vec<_>>();
|
|
assert_eq!(prices.len(), values.len());
|
|
assert_eq!(crate::average_sol_usd_prices(prices.as_slice()), std::option::Option::Some("104.01730745".to_owned()));
|
|
}
|
|
|
|
#[test]
|
|
fn sol_usd_average_returns_none_without_observation_and_handles_integer_fraction() {
|
|
assert_eq!(crate::average_sol_usd_prices(&[]), std::option::Option::None);
|
|
let one = ksp_offchain_transport_lib::MarketPriceDecimal::parse("1");
|
|
let two = ksp_offchain_transport_lib::MarketPriceDecimal::parse("2");
|
|
assert!(one.is_ok());
|
|
assert!(two.is_ok());
|
|
if let (std::result::Result::Ok(one), std::result::Result::Ok(two)) = (one, two) {
|
|
assert_eq!(crate::average_sol_usd_prices(&[one, two]), std::option::Option::Some("1.5".to_owned()));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn sol_usd_equivalent_is_exact_rounded_and_unavailable_without_average() {
|
|
assert_eq!(crate::sol_usd_equivalent(0, std::option::Option::Some("104.01730745")), std::option::Option::Some("0".to_owned()));
|
|
assert_eq!(crate::sol_usd_equivalent(1_000_000_000, std::option::Option::Some("104.01730745")), std::option::Option::Some("104.01730745".to_owned()),);
|
|
assert_eq!(crate::sol_usd_equivalent(1_500_000_000, std::option::Option::Some("104.01730745")), std::option::Option::Some("156.02596118".to_owned()),);
|
|
assert_eq!(crate::sol_usd_equivalent(1, std::option::Option::Some("104.01730745")), std::option::Option::Some("0.0000001".to_owned()));
|
|
assert_eq!(crate::sol_usd_equivalent(1_000_000_000, std::option::Option::None), std::option::Option::None);
|
|
assert_eq!(crate::sol_usd_equivalent(1_000_000_000, std::option::Option::Some("invalid")), std::option::Option::None);
|
|
}
|