1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
// 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 <https://www.gnu.org/licenses/>.
use malachite_base::num::basic::floats::PrimitiveFloat;
use malachite_base::num::basic::signeds::PrimitiveSigned;
use malachite_base::num::float::NiceFloat;
use malachite_base::test_util::generators::{primitive_float_gen, signed_gen_var_1};
fn neg_assign_helper_signed<T: PrimitiveSigned>() {
let test = |n: T, out| {
let mut n = n;
n.neg_assign();
assert_eq!(n, out);
};
test(T::ZERO, T::ZERO);
test(T::ONE, T::NEGATIVE_ONE);
test(T::exact_from(100), T::exact_from(-100));
test(T::NEGATIVE_ONE, T::ONE);
test(T::exact_from(-100), T::exact_from(100));
}
fn neg_assign_helper_primitive_float<T: PrimitiveFloat>() {
let test = |x: T, out| {
let mut x = x;
x.neg_assign();
assert_eq!(NiceFloat(x), NiceFloat(out));
};
test(T::ZERO, T::NEGATIVE_ZERO);
test(T::NEGATIVE_ZERO, T::ZERO);
test(T::INFINITY, T::NEGATIVE_INFINITY);
test(T::NEGATIVE_INFINITY, T::INFINITY);
test(T::NAN, T::NAN);
test(T::ONE, T::NEGATIVE_ONE);
test(T::from(100.0), T::from(-100.0));
test(T::NEGATIVE_ONE, T::ONE);
test(T::from(-100.0), T::from(100.0));
}
#[test]
fn test_neg_assign() {
apply_fn_to_signeds!(neg_assign_helper_signed);
apply_fn_to_primitive_floats!(neg_assign_helper_primitive_float);
}
fn neg_assign_properties_helper_signed<T: PrimitiveSigned>() {
signed_gen_var_1::<T>().test_properties(|n| {
let mut neg = n;
neg.neg_assign();
assert_eq!(neg, -n);
assert_eq!(-neg, n);
assert_eq!(neg == n, n == T::ZERO);
assert_eq!(n + neg, T::ZERO);
});
}
fn neg_assign_properties_helper_primitive_float<T: PrimitiveFloat>() {
primitive_float_gen::<T>().test_properties(|x| {
let mut neg = x;
neg.neg_assign();
assert_eq!(NiceFloat(neg), NiceFloat(-x));
assert_eq!(NiceFloat(-neg), NiceFloat(x));
assert_eq!(NiceFloat(neg) == NiceFloat(x), x.is_nan());
if x.is_finite() {
assert_eq!(x + neg, T::ZERO);
}
});
}
#[test]
fn neg_assign_properties() {
apply_fn_to_signeds!(neg_assign_properties_helper_signed);
apply_fn_to_primitive_floats!(neg_assign_properties_helper_primitive_float);
}
|