Skip to content

Commit 20ad931

Browse files
committed
Rename 'divisible_by' method to 'is_multiple_of', add tests for 'is_odd' and 'is_even'
1 parent 8f63f97 commit 20ad931

File tree

4 files changed

+69
-7
lines changed

4 files changed

+69
-7
lines changed

src/libcore/num/int-template.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,11 @@ impl Integer for T {
406406
407407
/// Returns `true` if the number can be divided by `other` without leaving a remainder
408408
#[inline(always)]
409-
fn divisible_by(&self, other: &T) -> bool { *self % *other == 0 }
409+
fn is_multiple_of(&self, other: &T) -> bool { *self % *other == 0 }
410410
411411
/// Returns `true` if the number is divisible by `2`
412412
#[inline(always)]
413-
fn is_even(&self) -> bool { self.divisible_by(&2) }
413+
fn is_even(&self) -> bool { self.is_multiple_of(&2) }
414414
415415
/// Returns `true` if the number is not divisible by `2`
416416
#[inline(always)]
@@ -682,6 +682,42 @@ mod tests {
682682
assert_eq!(-(0b11 as T) - (1 as T), (0b11 as T).not());
683683
}
684684
685+
#[test]
686+
fn test_multiple_of() {
687+
assert!((6 as T).is_multiple_of(&(6 as T)));
688+
assert!((6 as T).is_multiple_of(&(3 as T)));
689+
assert!((6 as T).is_multiple_of(&(1 as T)));
690+
assert!((-8 as T).is_multiple_of(&(4 as T)));
691+
assert!((8 as T).is_multiple_of(&(-1 as T)));
692+
assert!((-8 as T).is_multiple_of(&(-2 as T)));
693+
}
694+
695+
#[test]
696+
fn test_even() {
697+
assert_eq!((-4 as T).is_even(), true);
698+
assert_eq!((-3 as T).is_even(), false);
699+
assert_eq!((-2 as T).is_even(), true);
700+
assert_eq!((-1 as T).is_even(), false);
701+
assert_eq!((0 as T).is_even(), true);
702+
assert_eq!((1 as T).is_even(), false);
703+
assert_eq!((2 as T).is_even(), true);
704+
assert_eq!((3 as T).is_even(), false);
705+
assert_eq!((4 as T).is_even(), true);
706+
}
707+
708+
#[test]
709+
fn test_odd() {
710+
assert_eq!((-4 as T).is_odd(), false);
711+
assert_eq!((-3 as T).is_odd(), true);
712+
assert_eq!((-2 as T).is_odd(), false);
713+
assert_eq!((-1 as T).is_odd(), true);
714+
assert_eq!((0 as T).is_odd(), false);
715+
assert_eq!((1 as T).is_odd(), true);
716+
assert_eq!((2 as T).is_odd(), false);
717+
assert_eq!((3 as T).is_odd(), true);
718+
assert_eq!((4 as T).is_odd(), false);
719+
}
720+
685721
#[test]
686722
fn test_bitcount() {
687723
assert_eq!((0b010101 as T).population_count(), 3);

src/libcore/num/num.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ pub trait Integer: Num
8585

8686
fn gcd(&self, other: &Self) -> Self;
8787
fn lcm(&self, other: &Self) -> Self;
88-
fn divisible_by(&self, other: &Self) -> bool;
88+
89+
fn is_multiple_of(&self, other: &Self) -> bool;
8990
fn is_even(&self) -> bool;
9091
fn is_odd(&self) -> bool;
9192
}

src/libcore/num/uint-template.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,11 @@ impl Integer for T {
238238
239239
/// Returns `true` if the number can be divided by `other` without leaving a remainder
240240
#[inline(always)]
241-
fn divisible_by(&self, other: &T) -> bool { *self % *other == 0 }
241+
fn is_multiple_of(&self, other: &T) -> bool { *self % *other == 0 }
242242
243243
/// Returns `true` if the number is divisible by `2`
244244
#[inline(always)]
245-
fn is_even(&self) -> bool { self.divisible_by(&2) }
245+
fn is_even(&self) -> bool { self.is_multiple_of(&2) }
246246
247247
/// Returns `true` if the number is not divisible by `2`
248248
#[inline(always)]
@@ -415,6 +415,31 @@ mod tests {
415415
assert_eq!((99 as T).lcm(&17), 1683 as T);
416416
}
417417
418+
#[test]
419+
fn test_multiple_of() {
420+
assert!((6 as T).is_multiple_of(&(6 as T)));
421+
assert!((6 as T).is_multiple_of(&(3 as T)));
422+
assert!((6 as T).is_multiple_of(&(1 as T)));
423+
}
424+
425+
#[test]
426+
fn test_even() {
427+
assert_eq!((0 as T).is_even(), true);
428+
assert_eq!((1 as T).is_even(), false);
429+
assert_eq!((2 as T).is_even(), true);
430+
assert_eq!((3 as T).is_even(), false);
431+
assert_eq!((4 as T).is_even(), true);
432+
}
433+
434+
#[test]
435+
fn test_odd() {
436+
assert_eq!((0 as T).is_odd(), false);
437+
assert_eq!((1 as T).is_odd(), true);
438+
assert_eq!((2 as T).is_odd(), false);
439+
assert_eq!((3 as T).is_odd(), true);
440+
assert_eq!((4 as T).is_odd(), false);
441+
}
442+
418443
#[test]
419444
fn test_bitwise() {
420445
assert_eq!(0b1110 as T, (0b1100 as T).bitor(&(0b1010 as T)));

src/libstd/num/bigint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ impl Integer for BigUint {
428428

429429
/// Returns `true` if the number can be divided by `other` without leaving a remainder
430430
#[inline(always)]
431-
fn divisible_by(&self, other: &BigUint) -> bool { (*self % *other).is_zero() }
431+
fn is_multiple_of(&self, other: &BigUint) -> bool { (*self % *other).is_zero() }
432432

433433
/// Returns `true` if the number is divisible by `2`
434434
#[inline(always)]
@@ -973,7 +973,7 @@ impl Integer for BigInt {
973973

974974
/// Returns `true` if the number can be divided by `other` without leaving a remainder
975975
#[inline(always)]
976-
fn divisible_by(&self, other: &BigInt) -> bool { self.data.divisible_by(&other.data) }
976+
fn is_multiple_of(&self, other: &BigInt) -> bool { self.data.is_multiple_of(&other.data) }
977977

978978
/// Returns `true` if the number is divisible by `2`
979979
#[inline(always)]

0 commit comments

Comments
 (0)