From 20ad931bf337db2efd25e6571e322d31b5b83877 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Mon, 29 Apr 2013 16:02:43 +1000 Subject: [PATCH] Rename 'divisible_by' method to 'is_multiple_of', add tests for 'is_odd' and 'is_even' --- src/libcore/num/int-template.rs | 40 ++++++++++++++++++++++++++++++-- src/libcore/num/num.rs | 3 ++- src/libcore/num/uint-template.rs | 29 +++++++++++++++++++++-- src/libstd/num/bigint.rs | 4 ++-- 4 files changed, 69 insertions(+), 7 deletions(-) diff --git a/src/libcore/num/int-template.rs b/src/libcore/num/int-template.rs index 08df820a73d..ec38a32c039 100644 --- a/src/libcore/num/int-template.rs +++ b/src/libcore/num/int-template.rs @@ -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); diff --git a/src/libcore/num/num.rs b/src/libcore/num/num.rs index 6fbc2985952..3e43ebfef12 100644 --- a/src/libcore/num/num.rs +++ b/src/libcore/num/num.rs @@ -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; } diff --git a/src/libcore/num/uint-template.rs b/src/libcore/num/uint-template.rs index af64660ad0c..3dfdd22c42d 100644 --- a/src/libcore/num/uint-template.rs +++ b/src/libcore/num/uint-template.rs @@ -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))); diff --git a/src/libstd/num/bigint.rs b/src/libstd/num/bigint.rs index 3ea94eababb..e97b3b5eeec 100644 --- a/src/libstd/num/bigint.rs +++ b/src/libstd/num/bigint.rs @@ -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)]