Skip to content

Instantly share code, notes, and snippets.

@ssrlive
Last active February 18, 2025 05:05
Show Gist options
  • Save ssrlive/a03d9825a693ba85843ed35a91117839 to your computer and use it in GitHub Desktop.
Save ssrlive/a03d9825a693ba85843ed35a91117839 to your computer and use it in GitHub Desktop.
Rust digit_overflow
#[test]
fn digit_overflow() {
assert_eq!(u16::MAX.wrapping_add(9), 8);
assert_eq!(u16::MAX.saturating_add(9), u16::MAX);
assert_eq!(u16::MAX.checked_add(9), None);
assert_eq!(u16::MAX.overflowing_add(9), (8, true));
}
#[test]
fn digit_overflow_mul() {
assert_eq!(u16::MAX.wrapping_mul(9), u16::MAX - 8);
assert_eq!(u16::MAX.saturating_mul(9), u16::MAX);
assert_eq!(u16::MAX.checked_mul(9), None);
assert_eq!(u16::MAX.overflowing_mul(9), (u16::MAX - 8, true));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment