Skip to content

Commit

Permalink
Add tests for Amount checked arithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
james-chf committed Jan 24, 2023
1 parent 222cdfe commit ea82573
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions core/src/types/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,39 @@ 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));
assert_eq!(max.checked_sub(max + one), None);
}

#[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!(zero.checked_add(max + one), None);

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 ea82573

Please sign in to comment.