Skip to content

Commit

Permalink
test(NeptuneCoins): Add #filter for edge cases
Browse files Browse the repository at this point in the history
Specifically: edge cases can be triggered without i128 overflow but
with numbers exceeding the minimum or maximum number of nau.
  • Loading branch information
aszepieniec committed Jan 6, 2025
1 parent 2f8fa32 commit 0565d73
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/models/blockchain/type_scripts/neptune_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
self.0.checked_add(v.0).and_then(|sum| {
if !(-Self::MAX_NAU..=Self::MAX_NAU).contains(&sum) {
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 0565d73

Please sign in to comment.