mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-04 01:42:54 +03:00
Fix rotate_{left, right} for multiple of bitsize rotation amounts
Add additional rotation tests
This commit is contained in:
@@ -586,14 +586,14 @@ fn trailing_zeros(self) -> $T { unsafe { $cttz(self) } }
|
||||
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]
|
||||
|
||||
@@ -114,6 +114,15 @@ fn test_rotate() {
|
||||
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]
|
||||
|
||||
@@ -74,6 +74,15 @@ fn test_rotate() {
|
||||
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]
|
||||
|
||||
Reference in New Issue
Block a user