From 2f7e74da45a76449ef7820972c1ae8ee65a0984d Mon Sep 17 00:00:00 2001 From: yancy Date: Thu, 8 May 2025 16:21:24 -0500 Subject: [PATCH 1/2] Add MathOp helper methods It's helpful to be able to assert what type of Math error occurred. --- units/src/result.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/units/src/result.rs b/units/src/result.rs index a7bfe46d0..adb301930 100644 --- a/units/src/result.rs +++ b/units/src/result.rs @@ -254,6 +254,18 @@ impl MathOp { /// Returns `true` if this operation error'ed due to division by zero. pub fn is_div_by_zero(self) -> bool { !self.is_overflow() } + + /// Returns `true` if this operation error'ed due to addition. + pub fn is_addition(self) -> bool { self == MathOp::Add } + + /// Returns `true` if this operation error'ed due to subtraction. + pub fn is_subtraction(self) -> bool { self == MathOp::Sub } + + /// Returns `true` if this operation error'ed due to multiplication. + pub fn is_multiplication(self) -> bool { self == MathOp::Mul } + + /// Returns `true` if this operation error'ed due to negation. + pub fn is_negation(self) -> bool { self == MathOp::Neg } } impl fmt::Display for MathOp { From a7d059151eb47bf4202302604309c95c0d66371d Mon Sep 17 00:00:00 2001 From: yancy Date: Thu, 8 May 2025 16:21:39 -0500 Subject: [PATCH 2/2] Assert error type Improve test granularity by asserting the specific type of overflow returned. --- units/src/fee.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/units/src/fee.rs b/units/src/fee.rs index 6bf9fe1a9..f6085ff02 100644 --- a/units/src/fee.rs +++ b/units/src/fee.rs @@ -344,8 +344,11 @@ mod tests { #[test] fn fee_wu() { - let fee_overflow = FeeRate::from_sat_per_kwu(10).to_fee(Weight::MAX); - assert!(fee_overflow.is_error()); + let operation = FeeRate::from_sat_per_kwu(10) + .to_fee(Weight::MAX) + .unwrap_err() + .operation(); + assert!(operation.is_multiplication()); let fee_rate = FeeRate::from_sat_per_vb(2).unwrap(); let weight = Weight::from_vb(3).unwrap();