// 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::unsigneds::PrimitiveUnsigned; use malachite_base::num::conversion::traits::ExactFrom; use malachite_base::num::factorization::traits::Primes; use malachite_base::test_util::generators::{ unsigned_gen, unsigned_gen_var_27, unsigned_gen_var_28, }; use malachite_base::test_util::num::arithmetic::primorial::{ checked_primorial_naive, checked_product_of_first_n_primes_naive, }; use std::panic::catch_unwind; #[test] fn test_primorial() { fn test(n: u64, out: T) { assert_eq!(T::primorial(n), out); } test::(0, 1); test::(1, 1); test::(2, 2); test::(3, 6); test::(4, 6); test::(5, 30); test::(20, 9699690); } fn primorial_fail_helper() { assert_panic!(T::primorial(200)); } #[test] fn primorial_fail() { apply_fn_to_unsigneds!(primorial_fail_helper); } #[test] fn test_checked_primorial() { fn test(n: u64, out: Option) { assert_eq!(T::checked_primorial(n), out); assert_eq!(checked_primorial_naive(n), out); } test::(0, Some(1)); test::(1, Some(1)); test::(2, Some(2)); test::(3, Some(6)); test::(4, Some(6)); test::(5, Some(30)); test::(20, Some(9699690)); test::(11, None); test::(200, None); } #[test] fn test_product_of_first_n_primes() { fn test(n: u64, out: T) { assert_eq!(T::product_of_first_n_primes(n), out); } test::(0, 1); test::(1, 2); test::(2, 6); test::(3, 30); test::(4, 210); test::(9, 223092870); } fn product_of_first_n_primes_fail_helper() { assert_panic!(T::product_of_first_n_primes(100)); } #[test] fn product_of_first_n_primes_fail() { apply_fn_to_unsigneds!(product_of_first_n_primes_fail_helper); } #[test] fn test_checked_product_of_first_n_primes() { fn test(n: u64, out: Option) { assert_eq!(T::checked_product_of_first_n_primes(n), out); assert_eq!(checked_product_of_first_n_primes_naive(n), out); } test::(0, Some(1)); test::(1, Some(2)); test::(2, Some(6)); test::(3, Some(30)); test::(4, Some(210)); test::(9, Some(223092870)); test::(5, None); test::(100, None); } fn primorial_properties_helper() { unsigned_gen_var_27::().test_properties(|n| { let f = T::primorial(n); assert_eq!(T::checked_primorial(n), Some(f)); assert_ne!(f, T::ZERO); // TODO compare with primorial(n - 1) depending on whether n is prime }); } #[test] fn primorial_properties() { apply_fn_to_unsigneds!(primorial_properties_helper); } fn checked_primorial_properties_helper() { unsigned_gen().test_properties(|n| { let of = T::checked_primorial(n); assert_eq!(checked_primorial_naive(n), of); assert_ne!(of, Some(T::ZERO)); if let Some(f) = of { assert_eq!(T::primorial(n), f); } if n != u64::MAX && of.is_none() { assert!(T::checked_primorial(n + 1).is_none()); } }); } #[test] fn checked_primorial_properties() { apply_fn_to_unsigneds!(checked_primorial_properties_helper); } fn product_of_first_n_primes_properties_helper() { unsigned_gen_var_28::().test_properties(|n| { let f = T::product_of_first_n_primes(n); assert_eq!(T::checked_product_of_first_n_primes(n), Some(f)); assert_ne!(f, T::ZERO); if n != 0 { let p = u64::primes().nth(usize::exact_from(n) - 1).unwrap(); assert_eq!(T::primorial(p), f); assert_eq!(f / T::product_of_first_n_primes(n - 1), T::exact_from(p)); } }); } #[test] fn product_of_first_n_primes_properties() { apply_fn_to_unsigneds!(product_of_first_n_primes_properties_helper); } fn checked_product_of_first_n_primes_properties_helper() { unsigned_gen().test_properties(|n| { let of = T::checked_product_of_first_n_primes(n); assert_eq!(checked_product_of_first_n_primes_naive(n), of); assert_ne!(of, Some(T::ZERO)); if let Some(f) = of { assert_eq!(T::product_of_first_n_primes(n), f); } if n != u64::MAX && of.is_none() { assert!(T::checked_product_of_first_n_primes(n + 1).is_none()); } }); } #[test] fn checked_product_of_first_n_primes_properties() { apply_fn_to_unsigneds!(checked_product_of_first_n_primes_properties_helper); }