Skip to content

Commit ac3bc9c

Browse files
committed
Fix mod_floor() for uint primitive types
1 parent 8fff3f4 commit ac3bc9c

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/libstd/num/uint_macros.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ impl Integer for $T {
227227

228228
/// Unsigned integer modulo operation. Returns the same result as `rem` (`%`).
229229
#[inline]
230-
fn mod_floor(&self, other: &$T) -> $T { *self / *other }
230+
fn mod_floor(&self, other: &$T) -> $T { *self % *other }
231231

232-
/// Calculates `div_floor` and `modulo_floor` simultaneously
232+
/// Calculates `div_floor` and `mod_floor` simultaneously
233233
#[inline]
234234
fn div_mod_floor(&self, other: &$T) -> ($T,$T) {
235235
(*self / *other, *self % *other)
@@ -458,6 +458,19 @@ mod tests {
458458
assert_eq!((3 as $T).clamp(&(2 as $T), &(4 as $T)), 3 as $T);
459459
}
460460

461+
#[test]
462+
fn test_div_mod_floor() {
463+
assert_eq!((10 as $T).div_floor(&(3 as $T)), 3 as $T);
464+
assert_eq!((10 as $T).mod_floor(&(3 as $T)), 1 as $T);
465+
assert_eq!((10 as $T).div_mod_floor(&(3 as $T)), (3 as $T, 1 as $T));
466+
assert_eq!((5 as $T).div_floor(&(5 as $T)), 1 as $T);
467+
assert_eq!((5 as $T).mod_floor(&(5 as $T)), 0 as $T);
468+
assert_eq!((5 as $T).div_mod_floor(&(5 as $T)), (1 as $T, 0 as $T));
469+
assert_eq!((3 as $T).div_floor(&(7 as $T)), 0 as $T);
470+
assert_eq!((3 as $T).mod_floor(&(7 as $T)), 3 as $T);
471+
assert_eq!((3 as $T).div_mod_floor(&(7 as $T)), (0 as $T, 3 as $T));
472+
}
473+
461474
#[test]
462475
fn test_gcd() {
463476
assert_eq!((10 as $T).gcd(&2), 2 as $T);

0 commit comments

Comments
 (0)