// 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::arithmetic::traits::Parity; use malachite_base::num::basic::floats::PrimitiveFloat; use malachite_base::num::basic::integers::PrimitiveInt; use malachite_base::num::basic::signeds::PrimitiveSigned; use malachite_base::num::basic::unsigneds::PrimitiveUnsigned; use malachite_base::num::float::NiceFloat; use malachite_base::test_util::generators::{ primitive_float_pair_gen, primitive_float_signed_pair_gen, signed_unsigned_pair_gen_var_15, unsigned_pair_gen_var_29, }; #[test] fn test_pow() { fn test_primitive_int(x: T, y: u64, out: T) { assert_eq!(x.pow(y), out); let mut x = x; x.pow_assign(y); assert_eq!(x, out); } test_primitive_int::(0, 0, 1); test_primitive_int::(123, 0, 1); test_primitive_int::(123, 1, 123); test_primitive_int::(0, 123, 0); test_primitive_int::(1, 123, 1); test_primitive_int::(-1, 123, -1); test_primitive_int::(-1, 124, 1); test_primitive_int::(3, 3, 27); test_primitive_int::(-10, 9, -1000000000); test_primitive_int::(-10, 8, 100000000); fn test_i64_primitive_float(x: T, y: i64, out: T) { assert_eq!(NiceFloat(x.pow(y)), NiceFloat(out)); let mut x = x; x.pow_assign(y); assert_eq!(NiceFloat(x), NiceFloat(out)); } test_i64_primitive_float::(2.0, 5, 32.0); fn test_primitive_float_primitive_float(x: T, y: T, out: T) { assert_eq!(NiceFloat(x.pow(y)), NiceFloat(out)); let mut x = x; x.pow_assign(y); assert_eq!(NiceFloat(x), NiceFloat(out)); } test_primitive_float_primitive_float::(2.0, 5.0, 32.0); } fn pow_assign_properties_helper_unsigned() { unsigned_pair_gen_var_29::().test_properties(|(x, y)| { let mut power = x; power.pow_assign(y); assert_eq!(power, x.pow(y)); if x > T::ONE { assert_eq!(power.checked_log_base(x), Some(y)); } if y != 0 { assert_eq!(power.checked_root(y), Some(x)); } }); } fn pow_assign_properties_helper_signed() { signed_unsigned_pair_gen_var_15::().test_properties(|(x, y)| { let mut power = x; power.pow_assign(y); assert_eq!(power, x.pow(y)); if y != 0 { assert_eq!( power.checked_root(y), Some(if y.even() { x.abs() } else { x }) ); } }); } fn pow_assign_properties_helper_primitive_float() { primitive_float_signed_pair_gen::().test_properties(|(x, y)| { let mut power = x; power.pow_assign(y); assert_eq!(NiceFloat(power), NiceFloat(x.pow(y))); }); primitive_float_pair_gen::().test_properties(|(x, y)| { let mut power = x; power.pow_assign(y); assert_eq!(NiceFloat(power), NiceFloat(x.pow(y))); }); } #[test] fn pow_assign_properties() { apply_fn_to_unsigneds!(pow_assign_properties_helper_unsigned); apply_fn_to_signeds!(pow_assign_properties_helper_signed); apply_fn_to_primitive_floats!(pow_assign_properties_helper_primitive_float); }