Skip to content

Commit

Permalink
Merge pull request #1267 from CosmWasm/1186-add-methods-positive-test…
Browse files Browse the repository at this point in the history
…cases

Add more testcases to checked implementations in `Uint*` types
  • Loading branch information
ueco-jb authored Apr 4, 2022
2 parents 5ad8703 + ebb3d49 commit ad54fd1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/std/src/math/uint128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,26 +807,35 @@ mod tests {
Uint128::MAX.checked_add(Uint128(1)),
Err(OverflowError { .. })
));
assert!(matches!(Uint128(1).checked_add(Uint128(1)), Ok(Uint128(2))));
assert!(matches!(
Uint128(0).checked_sub(Uint128(1)),
Err(OverflowError { .. })
));
assert!(matches!(Uint128(2).checked_sub(Uint128(1)), Ok(Uint128(1))));
assert!(matches!(
Uint128::MAX.checked_mul(Uint128(2)),
Err(OverflowError { .. })
));
assert!(matches!(Uint128(2).checked_mul(Uint128(2)), Ok(Uint128(4))));
assert!(matches!(
Uint128::MAX.checked_pow(2u32),
Err(OverflowError { .. })
));
assert!(matches!(Uint128(2).checked_pow(3), Ok(Uint128(8))));
assert!(matches!(
Uint128::MAX.checked_div(Uint128(0)),
Err(DivideByZeroError { .. })
));
assert!(matches!(Uint128(6).checked_div(Uint128(2)), Ok(Uint128(3))));
assert!(matches!(
Uint128::MAX.checked_div_euclid(Uint128(0)),
Err(DivideByZeroError { .. })
));
assert!(matches!(
Uint128(6).checked_div_euclid(Uint128(2)),
Ok(Uint128(3)),
));
assert!(matches!(
Uint128::MAX.checked_rem(Uint128(0)),
Err(DivideByZeroError { .. })
Expand Down
20 changes: 20 additions & 0 deletions packages/std/src/math/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1344,22 +1344,42 @@ mod tests {
Uint256::MAX.checked_add(Uint256::from(1u32)),
Err(OverflowError { .. })
));
assert_eq!(
Uint256::from(1u32).checked_add(Uint256::from(1u32)),
Ok(Uint256::from(2u32)),
);
assert!(matches!(
Uint256::from(0u32).checked_sub(Uint256::from(1u32)),
Err(OverflowError { .. })
));
assert_eq!(
Uint256::from(2u32).checked_sub(Uint256::from(1u32)),
Ok(Uint256::from(1u32)),
);
assert!(matches!(
Uint256::MAX.checked_mul(Uint256::from(2u32)),
Err(OverflowError { .. })
));
assert_eq!(
Uint256::from(2u32).checked_mul(Uint256::from(2u32)),
Ok(Uint256::from(4u32)),
);
assert!(matches!(
Uint256::MAX.checked_pow(2u32),
Err(OverflowError { .. })
));
assert_eq!(
Uint256::from(2u32).checked_pow(3u32),
Ok(Uint256::from(8u32)),
);
assert!(matches!(
Uint256::MAX.checked_div(Uint256::from(0u32)),
Err(DivideByZeroError { .. })
));
assert_eq!(
Uint256::from(6u32).checked_div(Uint256::from(2u32)),
Ok(Uint256::from(3u32)),
);
assert!(matches!(
Uint256::MAX.checked_rem(Uint256::from(0u32)),
Err(DivideByZeroError { .. })
Expand Down
20 changes: 20 additions & 0 deletions packages/std/src/math/uint512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,22 +1003,42 @@ mod tests {
Uint512::MAX.checked_add(Uint512::from(1u32)),
Err(OverflowError { .. })
));
assert_eq!(
Uint512::from(1u32).checked_add(Uint512::from(1u32)),
Ok(Uint512::from(2u32)),
);
assert!(matches!(
Uint512::from(0u32).checked_sub(Uint512::from(1u32)),
Err(OverflowError { .. })
));
assert_eq!(
Uint512::from(2u32).checked_sub(Uint512::from(1u32)),
Ok(Uint512::from(1u32)),
);
assert!(matches!(
Uint512::MAX.checked_mul(Uint512::from(2u32)),
Err(OverflowError { .. })
));
assert_eq!(
Uint512::from(2u32).checked_mul(Uint512::from(2u32)),
Ok(Uint512::from(4u32)),
);
assert!(matches!(
Uint512::MAX.checked_pow(2u32),
Err(OverflowError { .. })
));
assert_eq!(
Uint512::from(2u32).checked_pow(3u32),
Ok(Uint512::from(8u32)),
);
assert!(matches!(
Uint512::MAX.checked_div(Uint512::from(0u32)),
Err(DivideByZeroError { .. })
));
assert_eq!(
Uint512::from(6u32).checked_div(Uint512::from(2u32)),
Ok(Uint512::from(3u32)),
);
assert!(matches!(
Uint512::MAX.checked_rem(Uint512::from(0u32)),
Err(DivideByZeroError { .. })
Expand Down
10 changes: 10 additions & 0 deletions packages/std/src/math/uint64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,30 +729,40 @@ mod tests {
Uint64::MAX.checked_add(Uint64(1)),
Err(OverflowError { .. })
));
assert!(matches!(Uint64(1).checked_add(Uint64(1)), Ok(Uint64(2))));
assert!(matches!(
Uint64(0).checked_sub(Uint64(1)),
Err(OverflowError { .. })
));
assert!(matches!(Uint64(2).checked_sub(Uint64(1)), Ok(Uint64(1))));
assert!(matches!(
Uint64::MAX.checked_mul(Uint64(2)),
Err(OverflowError { .. })
));
assert!(matches!(Uint64(2).checked_mul(Uint64(2)), Ok(Uint64(4))));
assert!(matches!(
Uint64::MAX.checked_pow(2u32),
Err(OverflowError { .. })
));
assert!(matches!(Uint64(2).checked_pow(3), Ok(Uint64(8))));
assert!(matches!(
Uint64::MAX.checked_div(Uint64(0)),
Err(DivideByZeroError { .. })
));
assert!(matches!(Uint64(6).checked_div(Uint64(2)), Ok(Uint64(3))));
assert!(matches!(
Uint64::MAX.checked_div_euclid(Uint64(0)),
Err(DivideByZeroError { .. })
));
assert!(matches!(
Uint64(6).checked_div_euclid(Uint64(2)),
Ok(Uint64(3)),
));
assert!(matches!(
Uint64::MAX.checked_rem(Uint64(0)),
Err(DivideByZeroError { .. })
));
assert!(matches!(Uint64(7).checked_rem(Uint64(2)), Ok(Uint64(1))));

// saturating_*
assert_eq!(Uint64::MAX.saturating_add(Uint64(1)), Uint64::MAX);
Expand Down

0 comments on commit ad54fd1

Please sign in to comment.