From 0565d73d5f5dad72b532d6d37b7b5fc2d23bdfc1 Mon Sep 17 00:00:00 2001 From: Alan Szepieniec Date: Mon, 6 Jan 2025 16:19:41 +0100 Subject: [PATCH] test(`NeptuneCoins`): Add `#filter` for edge cases Specifically: edge cases can be triggered without i128 overflow but with numbers exceeding the minimum or maximum number of nau. --- src/models/blockchain/type_scripts/neptune_coins.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/models/blockchain/type_scripts/neptune_coins.rs b/src/models/blockchain/type_scripts/neptune_coins.rs index 1ae3444b3..4e6568096 100644 --- a/src/models/blockchain/type_scripts/neptune_coins.rs +++ b/src/models/blockchain/type_scripts/neptune_coins.rs @@ -342,7 +342,8 @@ impl CheckedSub for NeptuneCoins { impl CheckedAdd for NeptuneCoins { /// Return Some(self+other) if (there is no i128-overflow and) the result is - /// smaller than the maximum number of nau. + /// smaller than or equal to the maximum number of nau, and larger than or + /// equal to its negation. fn checked_add(&self, v: &Self) -> Option { self.0.checked_add(v.0).and_then(|sum| { if !(-Self::MAX_NAU..=Self::MAX_NAU).contains(&sum) { @@ -785,8 +786,12 @@ pub(crate) mod test { a0.checked_add(&a1).unwrap(); } - #[proptest] - fn checked_add_proptest(lhs: i128, rhs: i128) { + #[proptest(cases = 10000)] + fn checked_add_proptest( + #[filter((#lhs >> 1) + (#rhs >> 1) <= NeptuneCoins::MAX_NAU && (#lhs >> 1) + (#rhs >> 1) >= -NeptuneCoins::MAX_NAU)] + lhs: i128, + rhs: i128, + ) { let lhs = NeptuneCoins(lhs >> 1); let rhs = NeptuneCoins(rhs >> 1); prop_assert!(lhs.checked_add(&rhs).is_some());