// 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, unsigned_gen};
#[test]
fn test_checked_square() {
fn test(x: T, out: Option) {
assert_eq!(x.checked_square(), out);
}
test::(0, Some(0));
test::(1, Some(1));
test::(2, Some(4));
test::(3, Some(9));
test::(10, Some(100));
test::(123, Some(15129));
test::(1000, Some(1000000));
test::(-1, Some(1));
test::(-2, Some(4));
test::(-3, Some(9));
test::(-10, Some(100));
test::(-123, Some(15129));
test::(-1000, Some(1000000));
test::(1000, None);
test::(-1000, None);
}
fn unsigned_checked_square_properties_helper() {
unsigned_gen::().test_properties(|x| {
let square = x.checked_square();
assert_eq!(square, x.checked_pow(2));
if let Some(square) = square {
assert_eq!(x.square(), square);
}
});
}
fn signed_checked_square_properties_helper() {
signed_gen::().test_properties(|x| {
let square = x.checked_square();
assert_eq!(square, x.checked_pow(2));
if let Some(square) = square {
assert_eq!(x.square(), square);
}
});
}
#[test]
fn checked_square_properties() {
apply_fn_to_unsigneds!(unsigned_checked_square_properties_helper);
apply_fn_to_signeds!(signed_checked_square_properties_helper);
}