Skip to content

Commit

Permalink
test(neptune_coins): Fix ranges in proptest
Browse files Browse the repository at this point in the history
Fix a potential overflow problem in a test which caused this failed test
run:

https://github.com/Neptune-Crypto/neptune-core/actions/runs/12635124115/job/35204356806

The ranges could, when summed, exceeed the maximum allowed NeptuneCoins
value.

Also adds more tests of `checked_add`.
  • Loading branch information
Sword-Smith committed Jan 6, 2025
1 parent 8eb547c commit 7383c88
Showing 1 changed file with 57 additions and 4 deletions.
61 changes: 57 additions & 4 deletions src/models/blockchain/type_scripts/neptune_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,10 +786,63 @@ pub(crate) mod test {
}

#[proptest]
fn checked_add_proptest(lhs: i128, rhs: i128) {
let lhs = NeptuneCoins(lhs >> 1);
let rhs = NeptuneCoins(rhs >> 1);
prop_assert!(lhs.checked_add(&rhs).is_some());
fn checked_add_proptest_quarter_range(lhs: i128, rhs: i128) {
let lhs = lhs >> 2;
let rhs = rhs >> 2;
let expected = NeptuneCoins(lhs + rhs);
let lhs = NeptuneCoins(lhs);
let rhs = NeptuneCoins(rhs);

prop_assert_eq!(expected, lhs.checked_add(&rhs).unwrap());
}

#[proptest]
fn checked_add_proptest_full_range(
#[strategy(0..=NeptuneCoins::MAX_NAU >> 1)] lhs: i128,
#[strategy(0..=NeptuneCoins::MAX_NAU >> 1)] rhs: i128,
) {
let expected = NeptuneCoins(lhs + rhs);
let lhs = NeptuneCoins(lhs);
let rhs = NeptuneCoins(rhs);
prop_assert_eq!(expected, lhs.checked_add(&rhs).unwrap());
}

#[proptest]
fn checked_add_proptest_limit(#[strategy(0..=NeptuneCoins::MAX_NAU)] lhs: i128) {
let rhs = NeptuneCoins(NeptuneCoins::MAX_NAU - lhs);
prop_assert!(!rhs.is_negative());

let lhs = NeptuneCoins(lhs);
let expected = NeptuneCoins::max();
prop_assert_eq!(expected, lhs.checked_add(&rhs).unwrap());
}

#[test]
fn checked_add_unittest_limit() {
let lhs = NeptuneCoins(NeptuneCoins::MAX_NAU >> 1);
let rhs = NeptuneCoins(NeptuneCoins::MAX_NAU >> 1);

let expected = NeptuneCoins::max();
assert_eq!(expected, lhs.checked_add(&rhs).unwrap());
}

#[proptest]
fn checked_add_with_self_matches_scalar_mul_two(
#[strategy(0..=NeptuneCoins::MAX_NAU >> 1)] lhs: i128,
) {
let lhs = NeptuneCoins(lhs);
prop_assert_eq!(lhs.scalar_mul(2), lhs.checked_add(&lhs).unwrap());
}

#[proptest]
fn checked_add_with_self_and_self_matches_scalar_mul_three(
#[strategy(0..=NeptuneCoins::MAX_NAU >> 2)] lhs: i128,
) {
let lhs = NeptuneCoins(lhs);
prop_assert_eq!(
lhs.scalar_mul(3),
lhs.checked_add(&lhs).unwrap().checked_add(&lhs).unwrap()
);
}

#[proptest]
Expand Down

0 comments on commit 7383c88

Please sign in to comment.