qfall_math/utils/factorization/default.rs
1// Copyright © 2023 Marcel Luca Schmidt
2//
3// This file is part of qFALL-math.
4//
5// qFALL-math is free software: you can redistribute it and/or modify it under
6// the terms of the Mozilla Public License Version 2.0 as published by the
7// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
8
9//! Default values for a [`Factorization`].
10
11use super::Factorization;
12use flint_sys::fmpz_factor::fmpz_factor_init;
13use std::mem::MaybeUninit;
14
15impl Default for Factorization {
16 /// Returns an instantiation of [`Factorization`] with `1` as the only factor.
17 ///
18 /// # Examples
19 /// ```
20 /// use std::default::Default;
21 /// use qfall_math::utils::Factorization;
22 ///
23 /// let fac = Factorization::default();
24 /// ```
25 fn default() -> Self {
26 let mut factors = MaybeUninit::uninit();
27
28 unsafe {
29 fmpz_factor_init(factors.as_mut_ptr());
30
31 Self {
32 factors: factors.assume_init(),
33 }
34 }
35 }
36}
37
38#[cfg(test)]
39mod tests_init {
40 use crate::utils::Factorization;
41
42 /// Ensure that [`Default`] initializes a [`Factorization`] with `1`.
43 #[test]
44 fn init_default() {
45 let fac = Factorization::default();
46
47 assert_eq!("[(1, 1)]", fac.to_string());
48 }
49}