Skip to content

Commit

Permalink
Merge branch 'james/mainline/checked-amounts' (#748) into main
Browse files Browse the repository at this point in the history
Checked arithmetic was also brought in by other branches, but this one
is the only one with the tests.

* james/mainline/checked-amounts:
  [ci] wasm checksums update
  Add changelog
  Add tests for Amount checked arithmetic
  Add Amount::checked_add method
  • Loading branch information
juped committed Feb 7, 2023
2 parents 7210274 + 0fafa0a commit 1ac7e7b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/748-checked-amounts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Complete checked arithmetic for Amount type
([#748](https://github.com/anoma/namada/issues/748))
31 changes: 31 additions & 0 deletions core/src/types/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,37 @@ mod tests {
let zero = Amount::from(0);
assert_eq!("0", zero.to_string());
}

#[test]
fn test_amount_checked_sub() {
let max = Amount::from(u64::MAX);
let one = Amount::from(1);
let zero = Amount::from(0);

assert_eq!(zero.checked_sub(zero), Some(zero));
assert_eq!(zero.checked_sub(one), None);
assert_eq!(zero.checked_sub(max), None);

assert_eq!(max.checked_sub(zero), Some(max));
assert_eq!(max.checked_sub(one), Some(max - one));
assert_eq!(max.checked_sub(max), Some(zero));
}

#[test]
fn test_amount_checked_add() {
let max = Amount::from(u64::MAX);
let one = Amount::from(1);
let zero = Amount::from(0);

assert_eq!(zero.checked_add(zero), Some(zero));
assert_eq!(zero.checked_add(one), Some(one));
assert_eq!(zero.checked_add(max - one), Some(max - one));
assert_eq!(zero.checked_add(max), Some(max));

assert_eq!(max.checked_add(zero), Some(max));
assert_eq!(max.checked_add(one), None);
assert_eq!(max.checked_add(max), None);
}
}

/// Helpers for testing with addresses.
Expand Down

0 comments on commit 1ac7e7b

Please sign in to comment.