use super::PolyOverZq;
use flint3_sys::{fmpz_mod_poly_clear, fmpz_mod_poly_set};
impl Clone for PolyOverZq {
fn clone(&self) -> Self {
let mut out = PolyOverZq::from(&self.modulus);
unsafe {
fmpz_mod_poly_set(
&mut out.poly,
&self.poly,
self.modulus.get_fmpz_mod_ctx_struct(),
)
};
out
}
}
impl Drop for PolyOverZq {
fn drop(&mut self) {
unsafe {
fmpz_mod_poly_clear(&mut self.poly, self.modulus.get_fmpz_mod_ctx_struct());
}
}
}
#[cfg(test)]
mod test_clone {
use super::PolyOverZq;
use std::str::FromStr;
#[test]
fn same_reference() {
let a = PolyOverZq::from_str(&format!("4 {} 1 -2 3 mod {}", i64::MAX, u64::MAX)).unwrap();
let b = a.clone();
assert_eq!(
a.modulus.get_fmpz_mod_ctx_struct().n[0],
b.modulus.get_fmpz_mod_ctx_struct().n[0]
);
assert_ne!(unsafe { *a.poly.coeffs.offset(0) }, unsafe {
*b.poly.coeffs.offset(0)
}); assert_eq!(unsafe { *a.poly.coeffs.offset(1) }, unsafe {
*b.poly.coeffs.offset(1)
}); assert_ne!(unsafe { *a.poly.coeffs.offset(2) }, unsafe {
*b.poly.coeffs.offset(2)
}); assert_eq!(unsafe { *a.poly.coeffs.offset(3) }, unsafe {
*b.poly.coeffs.offset(3)
});
assert_eq!(a.poly.length, b.poly.length);
}
#[test]
fn zero_polynomial() {
let a = PolyOverZq::from_str("0 mod 17").unwrap();
let b = PolyOverZq::from_str("1 17 mod 17").unwrap();
assert_eq!(a, a.clone());
assert_eq!(b, b.clone());
assert_eq!(a.get_mod(), a.clone().get_mod());
}
#[test]
fn correct_value() {
let a = PolyOverZq::from_str(&format!("4 {} 1 -2 3 mod {}", i64::MAX, u64::MAX)).unwrap();
let b = a.clone();
assert_eq!(a, b);
assert_eq!(a.get_mod(), b.get_mod());
}
}
#[cfg(test)]
mod test_drop {
use super::PolyOverZq;
use std::{collections::HashSet, str::FromStr};
fn create_and_drop_modulus() -> (i64, i64) {
let a = PolyOverZq::from_str(&format!("2 {} -2 mod {}", i64::MAX, u64::MAX)).unwrap();
(unsafe { *a.poly.coeffs.offset(0) }, unsafe {
*a.poly.coeffs.offset(1)
})
}
#[test]
fn free_memory() {
let mut storage_addresses = HashSet::new();
for _i in 0..5 {
let (a, b) = create_and_drop_modulus();
storage_addresses.insert(a);
storage_addresses.insert(b);
}
assert!(storage_addresses.len() < 10);
}
}