about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/libcore/num/int-template.rs40
-rw-r--r--src/libcore/num/num.rs3
-rw-r--r--src/libcore/num/uint-template.rs29
-rw-r--r--src/libstd/num/bigint.rs4
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)]
@@ -683,6 +683,42 @@ mod tests {
     }
 
     #[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)]
@@ -416,6 +416,31 @@ mod tests {
     }
 
     #[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)));
         assert_eq!(0b1000 as T, (0b1100 as T).bitand(&(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)]