Merge rust-bitcoin/rust-bitcoin#1690: Add tests for the FeeRate type

e3f95ee22b Add tests for the FeeRate type (yancy)

Pull request description:

  Adds some tests for the `FeeRate` type.

ACKs for top commit:
  apoelstra:
    ACK e3f95ee22b
  tcharding:
    ACK e3f95ee22b
  Kixunil:
    ACK e3f95ee22b

Tree-SHA512: 74d6597c747d5aa62a6510bf9fa8de971f89ad56f571aadd496f6487e80cc88bb2b5a1c6bcfed825d09d18ca2310b2bfd6fdbe330f2760369d167a653d26bef8
This commit is contained in:
Andrew Poelstra 2023-03-08 00:44:07 +00:00
commit d6134248df
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 65 additions and 0 deletions

View File

@ -136,3 +136,68 @@ impl Div<Weight> for Amount {
}
crate::parse::impl_parse_str_from_int_infallible!(FeeRate, u64, from_sat_per_kwu);
#[cfg(test)]
mod tests {
use super::*;
use std::u64;
#[test]
fn fee_rate_const_test() {
assert_eq!(0, FeeRate::ZERO.to_sat_per_kwu());
assert_eq!(u64::min_value(), FeeRate::MIN.to_sat_per_kwu());
assert_eq!(u64::max_value(), FeeRate::MAX.to_sat_per_kwu());
assert_eq!(250, FeeRate::BROADCAST_MIN.to_sat_per_kwu());
assert_eq!(750, FeeRate::DUST.to_sat_per_kwu());
}
#[test]
fn fee_rate_from_sat_per_vb_test() {
let fee_rate = FeeRate::from_sat_per_vb(10).expect("expected feerate in sat/kwu");
assert_eq!(FeeRate(2500), fee_rate);
}
#[test]
fn fee_rate_from_sat_per_vb_overflow_test() {
let fee_rate = FeeRate::from_sat_per_vb(u64::MAX);
assert!(fee_rate.is_none());
}
#[test]
fn from_sat_per_vb_unchecked_test() {
let fee_rate = FeeRate::from_sat_per_vb_unchecked(10);
assert_eq!(FeeRate(2500), fee_rate);
}
#[test]
#[should_panic]
fn from_sat_per_vb_unchecked_panic_test() {
FeeRate::from_sat_per_vb_unchecked(u64::MAX);
}
#[test]
fn raw_feerate_test() {
let fee_rate = FeeRate(333);
assert_eq!(333, fee_rate.to_sat_per_kwu());
assert_eq!(1, fee_rate.to_sat_per_vb_floor());
assert_eq!(2, fee_rate.to_sat_per_vb_ceil());
}
#[test]
fn checked_mul_test() {
let fee_rate = FeeRate(10).checked_mul(10).expect("expected feerate in sat/kwu");
assert_eq!(FeeRate(100), fee_rate);
let fee_rate = FeeRate(10).checked_mul(u64::MAX);
assert!(fee_rate.is_none());
}
#[test]
fn checked_div_test() {
let fee_rate = FeeRate(10).checked_div(10).expect("expected feerate in sat/kwu");
assert_eq!(FeeRate(1), fee_rate);
let fee_rate = FeeRate(10).checked_div(0);
assert!(fee_rate.is_none());
}
}