Rename 'divisible_by' method to 'is_multiple_of', add tests for 'is_odd' and 'is_even'

This commit is contained in:
Brendan Zabarauskas 2013-04-29 16:02:43 +10:00
parent 8f63f9789b
commit 20ad931bf3
4 changed files with 69 additions and 7 deletions

View File

@ -406,11 +406,11 @@ impl Integer for T {
/// Returns `true` if the number can be divided by `other` without leaving a remainder
#[inline(always)]
fn divisible_by(&self, other: &T) -> bool { *self % *other == 0 }
fn is_multiple_of(&self, other: &T) -> bool { *self % *other == 0 }
/// Returns `true` if the number is divisible by `2`
#[inline(always)]
fn is_even(&self) -> bool { self.divisible_by(&2) }
fn is_even(&self) -> bool { self.is_multiple_of(&2) }
/// Returns `true` if the number is not divisible by `2`
#[inline(always)]
@ -682,6 +682,42 @@ mod tests {
assert_eq!(-(0b11 as T) - (1 as T), (0b11 as T).not());
}
#[test]
fn test_multiple_of() {
assert!((6 as T).is_multiple_of(&(6 as T)));
assert!((6 as T).is_multiple_of(&(3 as T)));
assert!((6 as T).is_multiple_of(&(1 as T)));
assert!((-8 as T).is_multiple_of(&(4 as T)));
assert!((8 as T).is_multiple_of(&(-1 as T)));
assert!((-8 as T).is_multiple_of(&(-2 as T)));
}
#[test]
fn test_even() {
assert_eq!((-4 as T).is_even(), true);
assert_eq!((-3 as T).is_even(), false);
assert_eq!((-2 as T).is_even(), true);
assert_eq!((-1 as T).is_even(), false);
assert_eq!((0 as T).is_even(), true);
assert_eq!((1 as T).is_even(), false);
assert_eq!((2 as T).is_even(), true);
assert_eq!((3 as T).is_even(), false);
assert_eq!((4 as T).is_even(), true);
}
#[test]
fn test_odd() {
assert_eq!((-4 as T).is_odd(), false);
assert_eq!((-3 as T).is_odd(), true);
assert_eq!((-2 as T).is_odd(), false);
assert_eq!((-1 as T).is_odd(), true);
assert_eq!((0 as T).is_odd(), false);
assert_eq!((1 as T).is_odd(), true);
assert_eq!((2 as T).is_odd(), false);
assert_eq!((3 as T).is_odd(), true);
assert_eq!((4 as T).is_odd(), false);
}
#[test]
fn test_bitcount() {
assert_eq!((0b010101 as T).population_count(), 3);

View File

@ -85,7 +85,8 @@ pub trait Integer: Num
fn gcd(&self, other: &Self) -> Self;
fn lcm(&self, other: &Self) -> Self;
fn divisible_by(&self, other: &Self) -> bool;
fn is_multiple_of(&self, other: &Self) -> bool;
fn is_even(&self) -> bool;
fn is_odd(&self) -> bool;
}

View File

@ -238,11 +238,11 @@ impl Integer for T {
/// Returns `true` if the number can be divided by `other` without leaving a remainder
#[inline(always)]
fn divisible_by(&self, other: &T) -> bool { *self % *other == 0 }
fn is_multiple_of(&self, other: &T) -> bool { *self % *other == 0 }
/// Returns `true` if the number is divisible by `2`
#[inline(always)]
fn is_even(&self) -> bool { self.divisible_by(&2) }
fn is_even(&self) -> bool { self.is_multiple_of(&2) }
/// Returns `true` if the number is not divisible by `2`
#[inline(always)]
@ -415,6 +415,31 @@ mod tests {
assert_eq!((99 as T).lcm(&17), 1683 as T);
}
#[test]
fn test_multiple_of() {
assert!((6 as T).is_multiple_of(&(6 as T)));
assert!((6 as T).is_multiple_of(&(3 as T)));
assert!((6 as T).is_multiple_of(&(1 as T)));
}
#[test]
fn test_even() {
assert_eq!((0 as T).is_even(), true);
assert_eq!((1 as T).is_even(), false);
assert_eq!((2 as T).is_even(), true);
assert_eq!((3 as T).is_even(), false);
assert_eq!((4 as T).is_even(), true);
}
#[test]
fn test_odd() {
assert_eq!((0 as T).is_odd(), false);
assert_eq!((1 as T).is_odd(), true);
assert_eq!((2 as T).is_odd(), false);
assert_eq!((3 as T).is_odd(), true);
assert_eq!((4 as T).is_odd(), false);
}
#[test]
fn test_bitwise() {
assert_eq!(0b1110 as T, (0b1100 as T).bitor(&(0b1010 as T)));

View File

@ -428,7 +428,7 @@ impl Integer for BigUint {
/// Returns `true` if the number can be divided by `other` without leaving a remainder
#[inline(always)]
fn divisible_by(&self, other: &BigUint) -> bool { (*self % *other).is_zero() }
fn is_multiple_of(&self, other: &BigUint) -> bool { (*self % *other).is_zero() }
/// Returns `true` if the number is divisible by `2`
#[inline(always)]
@ -973,7 +973,7 @@ impl Integer for BigInt {
/// Returns `true` if the number can be divided by `other` without leaving a remainder
#[inline(always)]
fn divisible_by(&self, other: &BigInt) -> bool { self.data.divisible_by(&other.data) }
fn is_multiple_of(&self, other: &BigInt) -> bool { self.data.is_multiple_of(&other.data) }
/// Returns `true` if the number is divisible by `2`
#[inline(always)]