From ac3bc9cfcc6ac884311f9bed6853e36fd40868f4 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Sun, 18 Aug 2013 23:23:29 -0700 Subject: [PATCH] Fix mod_floor() for uint primitive types --- src/libstd/num/uint_macros.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/libstd/num/uint_macros.rs b/src/libstd/num/uint_macros.rs index 86b5b4ddfc0..f296c7fc92a 100644 --- a/src/libstd/num/uint_macros.rs +++ b/src/libstd/num/uint_macros.rs @@ -227,9 +227,9 @@ impl Integer for $T { /// Unsigned integer modulo operation. Returns the same result as `rem` (`%`). #[inline] - fn mod_floor(&self, other: &$T) -> $T { *self / *other } + fn mod_floor(&self, other: &$T) -> $T { *self % *other } - /// Calculates `div_floor` and `modulo_floor` simultaneously + /// Calculates `div_floor` and `mod_floor` simultaneously #[inline] fn div_mod_floor(&self, other: &$T) -> ($T,$T) { (*self / *other, *self % *other) @@ -458,6 +458,19 @@ mod tests { assert_eq!((3 as $T).clamp(&(2 as $T), &(4 as $T)), 3 as $T); } + #[test] + fn test_div_mod_floor() { + assert_eq!((10 as $T).div_floor(&(3 as $T)), 3 as $T); + assert_eq!((10 as $T).mod_floor(&(3 as $T)), 1 as $T); + assert_eq!((10 as $T).div_mod_floor(&(3 as $T)), (3 as $T, 1 as $T)); + assert_eq!((5 as $T).div_floor(&(5 as $T)), 1 as $T); + assert_eq!((5 as $T).mod_floor(&(5 as $T)), 0 as $T); + assert_eq!((5 as $T).div_mod_floor(&(5 as $T)), (1 as $T, 0 as $T)); + assert_eq!((3 as $T).div_floor(&(7 as $T)), 0 as $T); + assert_eq!((3 as $T).mod_floor(&(7 as $T)), 3 as $T); + assert_eq!((3 as $T).div_mod_floor(&(7 as $T)), (0 as $T, 3 as $T)); + } + #[test] fn test_gcd() { assert_eq!((10 as $T).gcd(&2), 2 as $T);