Skip to content

Commit

Permalink
Add more doctests for saturating_shl
Browse files Browse the repository at this point in the history
  • Loading branch information
kellerkindt committed Oct 23, 2022
1 parent 78c4b4f commit 3ad4b80
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,9 +1002,12 @@ macro_rules! int_impl {
///
/// ```
/// #![feature(saturating_bit_shifts)]
#[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".saturating_shl(0), 42);")]
#[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".saturating_shl(1), 0);")]
#[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS), 0);")]
#[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS - 2), 1_", stringify!($SelfT), " << ", stringify!($SelfT), "::BITS - 2);")]
#[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS - 1), ", stringify!($SelfT), "::MAX);")]
#[doc = concat!("assert_eq!(-42_", stringify!($SelfT), ".saturating_shl(0), -42);")]
#[doc = concat!("assert_eq!(-1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS - 2), -1_", stringify!($SelfT), " << ", stringify!($SelfT), "::BITS - 2);")]
#[doc = concat!("assert_eq!(-1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS - 1), ", stringify!($SelfT), "::MIN);")]
#[doc = concat!("assert_eq!(-1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS), ", stringify!($SelfT), "::MIN, \"-1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS), ", stringify!($SelfT), "::MIN\");")]
Expand All @@ -1018,11 +1021,11 @@ macro_rules! int_impl {
if rhs == 0 {
self
} else {
// leading zeros ignoring first bit (which indicates negative values)
// leading zeros/ones but ignoring first bit (which indicates negative values)
let leading_zeros = (self << 1).leading_zeros();
let leading_ones = (self << 1).leading_ones();

// would overflow => MIN / MAX depending on whether the value is negative or not
// would overflow? => MIN / MAX depending on whether the value is negative or not
if self > 0 && leading_zeros < rhs {
Self::MAX
} else if self < 0 && leading_ones < rhs {
Expand Down

0 comments on commit 3ad4b80

Please sign in to comment.