From: Samuel Neves Date: Wed, 2 Jul 2014 03:58:23 +0000 (+0100) Subject: Fix rotate_{left, right} for multiple of bitsize rotation amounts X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=c0248c0839cfdf5b7030f4191ea7aed0981b9e4e;p=rust.git Fix rotate_{left, right} for multiple of bitsize rotation amounts Add additional rotation tests --- diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index b32e4167da1..1fae362471d 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -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] diff --git a/src/libcoretest/num/int_macros.rs b/src/libcoretest/num/int_macros.rs index 940b036ca90..d078b514085 100644 --- a/src/libcoretest/num/int_macros.rs +++ b/src/libcoretest/num/int_macros.rs @@ -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] diff --git a/src/libcoretest/num/uint_macros.rs b/src/libcoretest/num/uint_macros.rs index 2272af67daf..aefaa90520e 100644 --- a/src/libcoretest/num/uint_macros.rs +++ b/src/libcoretest/num/uint_macros.rs @@ -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]