Merge rust-bitcoin/rust-bitcoin#3914: units: Define constructor variable in amount tests

f01bb30121 Use sat variable for constructor (Tobin C. Harding)
34f846c074 Use ssat variable for constructor (Tobin C. Harding)

Pull request description:

  Add `sat` and `ssat` throughout the `amount::tests` module after new tests were added recently.

ACKs for top commit:
  jamillambert:
    ACK f01bb30121
  apoelstra:
    ACK f01bb30121ef5a59af069a55db727ee5a9ba71bb; successfully ran local tests

Tree-SHA512: 6a4cdc47000b22625132fe0d8faa7dbff59acfc0b6b4faffe091f885f8d0fd5f5ce15e2298c87e25bd4cc13a2611cecadb43b7d44ccd768a04cd150223577b6b
This commit is contained in:
merge-script 2025-01-19 16:31:10 +00:00
commit 3985333e23
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 33 additions and 25 deletions

View File

@ -18,28 +18,31 @@ use crate::{FeeRate, Weight};
#[test]
fn sanity_check() {
assert_eq!(SignedAmount::from_sat(-100).abs(), SignedAmount::from_sat(100));
let sat = Amount::from_sat;
let ssat = SignedAmount::from_sat;
assert_eq!(ssat(-100).abs(), ssat(100));
assert_eq!(
SignedAmount::from_sat(i64::MIN + 1).checked_abs().unwrap(),
SignedAmount::from_sat(i64::MAX)
ssat(i64::MIN + 1).checked_abs().unwrap(),
ssat(i64::MAX)
);
assert_eq!(SignedAmount::from_sat(-100).signum(), -1);
assert_eq!(SignedAmount::from_sat(0).signum(), 0);
assert_eq!(SignedAmount::from_sat(100).signum(), 1);
assert_eq!(SignedAmount::from(Amount::from_sat(100)), SignedAmount::from_sat(100));
assert!(SignedAmount::from_sat(i64::MIN).checked_abs().is_none());
assert!(!SignedAmount::from_sat(-100).is_positive());
assert!(SignedAmount::from_sat(100).is_positive());
assert_eq!(ssat(-100).signum(), -1);
assert_eq!(ssat(0).signum(), 0);
assert_eq!(ssat(100).signum(), 1);
assert_eq!(SignedAmount::from(sat(100)), ssat(100));
assert!(ssat(i64::MIN).checked_abs().is_none());
assert!(!ssat(-100).is_positive());
assert!(ssat(100).is_positive());
#[cfg(feature = "alloc")]
{
assert_eq!(
Amount::from_float_in(0_f64, Denomination::Bitcoin).unwrap(),
Amount::from_sat(0)
sat(0)
);
assert_eq!(
Amount::from_float_in(2_f64, Denomination::Bitcoin).unwrap(),
Amount::from_sat(200_000_000)
sat(200_000_000)
);
assert!(Amount::from_float_in(-100_f64, Denomination::Bitcoin).is_err());
}
@ -135,7 +138,7 @@ fn mul_div() {
a %= 3;
assert_eq!(a, sat(1));
a *= 3;
assert_eq!(a, Amount::from_sat(3));
assert_eq!(a, sat(3));
let mut b = ssat(30);
b /= 3;
@ -143,7 +146,7 @@ fn mul_div() {
b %= 3;
assert_eq!(b, ssat(1));
b *= 3;
assert_eq!(b, SignedAmount::from_sat(3));
assert_eq!(b, ssat(3));
}
#[test]
@ -179,27 +182,32 @@ fn checked_arithmetic() {
#[test]
#[allow(deprecated_in_future)]
fn unchecked_arithmetic() {
let sat = Amount::from_sat;
let ssat = SignedAmount::from_sat;
assert_eq!(
SignedAmount::from_sat(10).unchecked_add(SignedAmount::from_sat(20)),
SignedAmount::from_sat(30)
ssat(10).unchecked_add(ssat(20)),
ssat(30)
);
assert_eq!(
SignedAmount::from_sat(50).unchecked_sub(SignedAmount::from_sat(10)),
SignedAmount::from_sat(40)
ssat(50).unchecked_sub(ssat(10)),
ssat(40)
);
assert_eq!(Amount::from_sat(5).unchecked_add(Amount::from_sat(7)), Amount::from_sat(12));
assert_eq!(Amount::from_sat(10).unchecked_sub(Amount::from_sat(7)), Amount::from_sat(3));
assert_eq!(sat(5).unchecked_add(sat(7)), sat(12));
assert_eq!(sat(10).unchecked_sub(sat(7)), sat(3));
}
#[test]
fn positive_sub() {
let ssat = SignedAmount::from_sat;
assert_eq!(
SignedAmount::from_sat(10).positive_sub(SignedAmount::from_sat(7)).unwrap(),
SignedAmount::from_sat(3)
ssat(10).positive_sub(ssat(7)).unwrap(),
ssat(3)
);
assert!(SignedAmount::from_sat(-10).positive_sub(SignedAmount::from_sat(7)).is_none());
assert!(SignedAmount::from_sat(10).positive_sub(SignedAmount::from_sat(-7)).is_none());
assert!(SignedAmount::from_sat(10).positive_sub(SignedAmount::from_sat(11)).is_none());
assert!(ssat(-10).positive_sub(ssat(7)).is_none());
assert!(ssat(10).positive_sub(ssat(-7)).is_none());
assert!(ssat(10).positive_sub(ssat(11)).is_none());
}
#[cfg(feature = "alloc")]