1
Fork 0

Fix rotate_{left, right} for multiple of bitsize rotation amounts

Add additional rotation tests
This commit is contained in:
Samuel Neves 2014-07-02 04:58:23 +01:00
parent 380657557c
commit c0248c0839
3 changed files with 20 additions and 2 deletions

View file

@ -586,14 +586,14 @@ macro_rules! int_impl {
fn rotate_left(self, n: uint) -> $T {
// Protect against undefined behaviour for over-long bit shifts
let n = n % $BITS;
(self << n) | (self >> ($BITS - n))
(self << n) | (self >> (($BITS - n) % $BITS))
}
#[inline]
fn rotate_right(self, n: uint) -> $T {
// Protect against undefined behaviour for over-long bit shifts
let n = n % $BITS;
(self >> n) | (self << ($BITS - n))
(self >> n) | (self << (($BITS - n) % $BITS))
}
#[inline]

View file

@ -114,6 +114,15 @@ mod tests {
assert_eq!(_1.rotate_left(124), _1);
assert_eq!(_0.rotate_right(124), _0);
assert_eq!(_1.rotate_right(124), _1);
// Rotating by 0 should have no effect
assert_eq!(A.rotate_left(0), A);
assert_eq!(B.rotate_left(0), B);
assert_eq!(C.rotate_left(0), C);
// Rotating by a multiple of word size should also have no effect
assert_eq!(A.rotate_left(64), A);
assert_eq!(B.rotate_left(64), B);
assert_eq!(C.rotate_left(64), C);
}
#[test]

View file

@ -74,6 +74,15 @@ mod tests {
assert_eq!(_1.rotate_left(124), _1);
assert_eq!(_0.rotate_right(124), _0);
assert_eq!(_1.rotate_right(124), _1);
// Rotating by 0 should have no effect
assert_eq!(A.rotate_left(0), A);
assert_eq!(B.rotate_left(0), B);
assert_eq!(C.rotate_left(0), C);
// Rotating by a multiple of word size should also have no effect
assert_eq!(A.rotate_left(64), A);
assert_eq!(B.rotate_left(64), B);
assert_eq!(C.rotate_left(64), C);
}
#[test]