mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-10 14:57:14 +00:00
auto merge of #8610 : kballard/rust/mod_floor, r=alexcrichton
`mod_floor()` was incorrectly defined for uint types as `a / b` instead of `a % b`.
This commit is contained in:
commit
e66478193b
@ -238,9 +238,9 @@ impl Integer for $T {
|
|||||||
|
|
||||||
/// Unsigned integer modulo operation. Returns the same result as `rem` (`%`).
|
/// Unsigned integer modulo operation. Returns the same result as `rem` (`%`).
|
||||||
#[inline]
|
#[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]
|
#[inline]
|
||||||
fn div_mod_floor(&self, other: &$T) -> ($T,$T) {
|
fn div_mod_floor(&self, other: &$T) -> ($T,$T) {
|
||||||
(*self / *other, *self % *other)
|
(*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);
|
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]
|
#[test]
|
||||||
fn test_gcd() {
|
fn test_gcd() {
|
||||||
assert_eq!((10 as $T).gcd(&2), 2 as $T);
|
assert_eq!((10 as $T).gcd(&2), 2 as $T);
|
||||||
|
Loading…
Reference in New Issue
Block a user