// Copyright © 2024 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See .
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::basic::signeds::PrimitiveSigned;
use malachite_base::num::basic::unsigneds::PrimitiveUnsigned;
use malachite_base::test_util::generators::{
signed_gen, signed_gen_var_6, signed_pair_gen, signed_pair_gen_var_3, signed_pair_gen_var_5,
unsigned_gen, unsigned_gen_var_1, unsigned_pair_gen_var_11, unsigned_pair_gen_var_13,
unsigned_pair_gen_var_27,
};
#[test]
fn test_divisible_by() {
fn test(x: T, y: T, out: bool) {
assert_eq!(x.divisible_by(y), out);
}
test::(0, 0, true);
test::(1, 0, false);
test::(1000000000000, 0, false);
test::(0, 1, true);
test::(0, 123, true);
test::(1, 1, true);
test::(123, 1, true);
test::(123, 123, true);
test::(123, 456, false);
test::(456, 123, false);
test::(369, 123, true);
test::(0xffffffff, 1, true);
test::(u32::MAX, u32::MAX, true);
test::(1000000000000, 1, true);
test::(1000000000000, 3, false);
test::(1000000000002, 3, true);
test::(1000000000000, 123, false);
test::(1000000000000, 0xffffffff, false);
test::(1000000000000000000000000, 1, true);
test::(1000000000000000000000000, 3, false);
test::(1000000000000000000000002, 3, true);
test::(1000000000000000000000000, 123, false);
test::(1000000000000000000000000, 0xffffffff, false);
test::(1000000000000000000000000, 1000000000000, true);
test::(1000000000000000000000000, 1000000000001, false);
test::(1000000000000, 0, false);
test::(0, -1, true);
test::(0, -123, true);
test::(1, -1, true);
test::(123, -1, true);
test::(123, -123, true);
test::(123, -456, false);
test::(456, -123, false);
test::(369, -123, true);
test::(0xffffffff, -1, true);
test::(0xffffffff, -0xffffffff, true);
test::(1000000000000, -1, true);
test::(1000000000000, -3, false);
test::(1000000000002, -3, true);
test::(1000000000000, -123, false);
test::(1000000000000, -0xffffffff, false);
test::(1000000000000000000000000, -1, true);
test::(1000000000000000000000000, -3, false);
test::(1000000000000000000000002, -3, true);
test::(1000000000000000000000000, -123, false);
test::(1000000000000000000000000, -0xffffffff, false);
test::(1000000000000000000000000, -1000000000000, true);
test::(1000000000000000000000000, -1000000000001, false);
test::(-1, 0, false);
test::(-1000000000000, 0, false);
test::(-1, 1, true);
test::(-123, 1, true);
test::(-123, 123, true);
test::(-123, 456, false);
test::(-456, 123, false);
test::(-369, 123, true);
test::(-0xffffffff, 1, true);
test::(-0xffffffff, 0xffffffff, true);
test::(-1000000000000, 1, true);
test::(-1000000000000, 3, false);
test::(-1000000000002, 3, true);
test::(-1000000000000, 123, false);
test::(-1000000000000, 0xffffffff, false);
test::(-1000000000000000000000000, 1, true);
test::(-1000000000000000000000000, 3, false);
test::(-1000000000000000000000002, 3, true);
test::(-1000000000000000000000000, 123, false);
test::(-1000000000000000000000000, 0xffffffff, false);
test::(-1000000000000000000000000, 1000000000000, true);
test::(-1000000000000000000000000, 1000000000001, false);
test::(-1, -1, true);
test::(-123, -1, true);
test::(-123, -123, true);
test::(-123, -456, false);
test::(-456, -123, false);
test::(-369, -123, true);
test::(-0xffffffff, -1, true);
test::(-0xffffffff, -0xffffffff, true);
test::(-1000000000000, -1, true);
test::(-1000000000000, -3, false);
test::(-1000000000002, -3, true);
test::(-1000000000000, -123, false);
test::(-1000000000000, -0xffffffff, false);
test::(-1000000000000000000000000, -1, true);
test::(-1000000000000000000000000, -3, false);
test::(-1000000000000000000000002, -3, true);
test::(-1000000000000000000000000, -123, false);
test::(-1000000000000000000000000, -0xffffffff, false);
test::(-1000000000000000000000000, -1000000000000, true);
test::(-1000000000000000000000000, -1000000000001, false);
}
fn divisible_by_properties_helper_unsigned() {
unsigned_pair_gen_var_27::().test_properties(|(x, y)| {
let divisible = x.divisible_by(y);
assert_eq!(x == T::ZERO || y != T::ZERO && x % y == T::ZERO, divisible);
});
unsigned_pair_gen_var_11::().test_properties(|(x, y)| {
assert!(x.divisible_by(y));
assert!(x == T::ZERO || y != T::ZERO && x % y == T::ZERO);
});
unsigned_pair_gen_var_13::().test_properties(|(x, y)| {
assert!(!x.divisible_by(y));
assert!(x != T::ZERO && (y == T::ZERO || x % y != T::ZERO));
});
unsigned_gen::().test_properties(|n| {
assert!(n.divisible_by(T::ONE));
});
unsigned_gen_var_1::().test_properties(|n| {
assert!(!n.divisible_by(T::ZERO));
assert!(T::ZERO.divisible_by(n));
if n > T::ONE {
assert!(!T::ONE.divisible_by(n));
}
assert!(n.divisible_by(n));
});
}
fn divisible_by_properties_helper_signed() {
signed_pair_gen::().test_properties(|(x, y)| {
let divisible = x.divisible_by(y);
assert_eq!(
x == T::ZERO || x == T::MIN && y == T::NEGATIVE_ONE || y != T::ZERO && x % y == T::ZERO,
divisible
);
if x != T::MIN {
assert_eq!((-x).divisible_by(y), divisible);
}
if y != T::MIN {
assert_eq!(x.divisible_by(-y), divisible);
}
});
signed_pair_gen_var_3::().test_properties(|(x, y)| {
assert!(x.divisible_by(y));
assert!(
x == T::ZERO || x == T::MIN && y == T::NEGATIVE_ONE || y != T::ZERO && x % y == T::ZERO
);
});
signed_pair_gen_var_5::().test_properties(|(x, y)| {
assert!(!x.divisible_by(y));
assert!(
x != T::ZERO
&& (x != T::MIN || y != T::NEGATIVE_ONE)
&& (y == T::ZERO || x % y != T::ZERO)
);
});
signed_gen::().test_properties(|n| {
assert!(n.divisible_by(T::ONE));
assert!(n.divisible_by(T::NEGATIVE_ONE));
});
signed_gen_var_6::().test_properties(|n| {
assert!(!n.divisible_by(T::ZERO));
assert!(T::ZERO.divisible_by(n));
if n > T::ONE {
assert!(!T::ONE.divisible_by(n));
}
assert!(n.divisible_by(n));
});
}
#[test]
fn divisible_by_properties() {
apply_fn_to_unsigneds!(divisible_by_properties_helper_unsigned);
apply_fn_to_signeds!(divisible_by_properties_helper_signed);
}