// 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::gcd::{gcd_binary, gcd_euclidean, gcd_fast_a, gcd_fast_b}; use malachite_base::num::basic::unsigneds::PrimitiveUnsigned; use malachite_base::test_util::generators::{ unsigned_gen, unsigned_pair_gen_var_27, unsigned_triple_gen_var_19, }; #[test] fn test_gcd() { fn test(x: T, y: T, out: T) { assert_eq!(x.gcd(y), out); let mut x = x; x.gcd_assign(y); assert_eq!(x, out); } test::(0, 0, 0); test::(0, 6, 6); test::(6, 0, 6); test::(1, 6, 1); test::(6, 1, 1); test::(8, 12, 4); test::(54, 24, 6); test::(42, 56, 14); test::(48, 18, 6); test::(3, 5, 1); test::(12, 60, 12); test::(12, 90, 6); } fn gcd_properties_helper() { unsigned_pair_gen_var_27::().test_properties(|(x, y)| { let gcd = x.gcd(y); assert_eq!(gcd_euclidean(x, y), gcd); assert_eq!(gcd_binary(x, y), gcd); assert_eq!(gcd_fast_a(x, y), gcd); assert_eq!(gcd_fast_b(x, y), gcd); let mut x_mut = x; x_mut.gcd_assign(y); assert_eq!(x_mut, gcd); assert_eq!(y.gcd(x), gcd); assert!(x.divisible_by(gcd)); assert!(y.divisible_by(gcd)); if gcd != T::ZERO { assert!((x.div_exact(gcd)).coprime_with(y.div_exact(gcd))); } assert_eq!(gcd == T::ZERO, x == T::ZERO && y == T::ZERO); }); unsigned_gen::().test_properties(|x| { assert_eq!(x.gcd(x), x); assert_eq!(x.gcd(T::ONE), T::ONE); assert_eq!(x.gcd(T::ZERO), x); }); unsigned_triple_gen_var_19::().test_properties(|(x, y, z)| { assert_eq!(x.gcd(y).gcd(z), x.gcd(y.gcd(z))); }); } #[test] fn gcd_properties() { apply_fn_to_unsigneds!(gcd_properties_helper); }