use std::str::FromStr;
use webylib::Amount;
#[test]
fn test_amount_from_str() {
assert_eq!(Amount::from_str("0").unwrap(), Amount::ZERO);
assert_eq!(Amount::from_str("0.0").unwrap(), Amount::ZERO);
assert_eq!(Amount::from_str("1").unwrap().wats, Amount::UNIT);
assert_eq!(Amount::from_str("100").unwrap().wats, 100 * Amount::UNIT);
assert_eq!(Amount::from_str("0.00000001").unwrap().wats, 1);
assert_eq!(Amount::from_str("1.00000000").unwrap().wats, Amount::UNIT);
assert_eq!(
Amount::from_str("1.5").unwrap().wats,
Amount::UNIT + Amount::UNIT / 2
);
assert!(Amount::from_str("e1.00000000").is_err());
if let Ok(amount) = Amount::from_str("₩1.00000000") {
assert_eq!(amount.wats, Amount::UNIT);
}
assert_eq!(Amount::from_str("1E-8").unwrap().wats, 1);
assert_eq!(Amount::from_str("1e-8").unwrap().wats, 1);
assert_eq!(Amount::from_str("1.5E-8").unwrap().wats, 1); assert_eq!(Amount::from_str("2E-8").unwrap().wats, 2);
assert_eq!(Amount::from_str("1E-7").unwrap().wats, 10);
assert_eq!(Amount::from_str("1E-6").unwrap().wats, 100);
assert_eq!(Amount::from_str("1E-5").unwrap().wats, 1000);
assert_eq!(Amount::from_str("1E-4").unwrap().wats, 10000);
assert_eq!(Amount::from_str("1E-3").unwrap().wats, 100000);
assert_eq!(Amount::from_str("1E-2").unwrap().wats, 1000000);
assert_eq!(Amount::from_str("1E-1").unwrap().wats, 10000000);
assert_eq!(Amount::from_str("1E0").unwrap().wats, Amount::UNIT);
assert_eq!(Amount::from_str("1E1").unwrap().wats, 10 * Amount::UNIT);
}
#[test]
fn test_amount_to_string() {
assert_eq!(format!("{}", Amount::ZERO), "0");
assert_eq!(format!("{}", Amount::from_sats(Amount::UNIT)), "1");
assert_eq!(
format!("{}", Amount::from_sats(Amount::UNIT + 1)),
"1.00000001"
);
assert_eq!(format!("{}", Amount::from_sats(Amount::UNIT / 2)), "0.5");
assert_eq!(format!("{}", Amount::from_sats(1)), "0.00000001");
}
#[test]
fn test_amount_arithmetic() {
let a = Amount::from_sats(100);
let b = Amount::from_sats(50);
assert_eq!((a + b).wats, 150);
assert_eq!((a - b).wats, 50);
assert_eq!((a * 2).wats, 200);
assert_eq!((a / 2).wats, 50);
}
#[test]
fn test_amount_validation() {
assert!(Amount::from_str("1.00000000").unwrap().is_valid());
assert!(Amount::ZERO.is_valid());
assert!(Amount::from_str("-1").is_err());
assert!(Amount::from_str("1.000000000").is_err()); assert!(Amount::from_str("1.2.3").is_err()); }
#[test]
fn test_edge_cases() {
let max_amount = Amount::from_sats(i64::MAX / Amount::UNIT * Amount::UNIT);
assert!(max_amount.is_valid());
assert_eq!(Amount::from_str("0.00000001").unwrap().wats, 1);
assert_eq!(Amount::from_str("0.00000000").unwrap(), Amount::ZERO);
}