Skip to content

Commit 34f48d1

Browse files
bors[bot]cuviper
andauthored
Merge #240
240: Fix is_multiple_of with a 0 arg r=cuviper a=cuviper See also: rust-num/num-integer#47 Co-authored-by: Josh Stone <[email protected]>
2 parents 51eb448 + 016c04a commit 34f48d1

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/biguint.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ impl Integer for BigUint {
283283
/// Returns `true` if the number is a multiple of `other`.
284284
#[inline]
285285
fn is_multiple_of(&self, other: &BigUint) -> bool {
286+
if other.is_zero() {
287+
return self.is_zero();
288+
}
286289
(self % other).is_zero()
287290
}
288291

tests/bigint.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,18 @@ fn test_lcm() {
10361036
check(11, 5, 55);
10371037
}
10381038

1039+
#[test]
1040+
fn test_is_multiple_of() {
1041+
assert!(BigInt::from(0).is_multiple_of(&BigInt::from(0)));
1042+
assert!(BigInt::from(6).is_multiple_of(&BigInt::from(6)));
1043+
assert!(BigInt::from(6).is_multiple_of(&BigInt::from(3)));
1044+
assert!(BigInt::from(6).is_multiple_of(&BigInt::from(1)));
1045+
1046+
assert!(!BigInt::from(42).is_multiple_of(&BigInt::from(5)));
1047+
assert!(!BigInt::from(5).is_multiple_of(&BigInt::from(3)));
1048+
assert!(!BigInt::from(42).is_multiple_of(&BigInt::from(0)));
1049+
}
1050+
10391051
#[test]
10401052
fn test_next_multiple_of() {
10411053
assert_eq!(

tests/biguint.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,18 @@ fn test_lcm() {
10851085
check(99, 17, 1683);
10861086
}
10871087

1088+
#[test]
1089+
fn test_is_multiple_of() {
1090+
assert!(BigUint::from(0u32).is_multiple_of(&BigUint::from(0u32)));
1091+
assert!(BigUint::from(6u32).is_multiple_of(&BigUint::from(6u32)));
1092+
assert!(BigUint::from(6u32).is_multiple_of(&BigUint::from(3u32)));
1093+
assert!(BigUint::from(6u32).is_multiple_of(&BigUint::from(1u32)));
1094+
1095+
assert!(!BigUint::from(42u32).is_multiple_of(&BigUint::from(5u32)));
1096+
assert!(!BigUint::from(5u32).is_multiple_of(&BigUint::from(3u32)));
1097+
assert!(!BigUint::from(42u32).is_multiple_of(&BigUint::from(0u32)));
1098+
}
1099+
10881100
#[test]
10891101
fn test_next_multiple_of() {
10901102
assert_eq!(

0 commit comments

Comments
 (0)