use super::PolyOverZq;
use crate::{integer::PolyOverZ, macros::for_others::implement_for_owned};
use flint_sys::fmpz_mod_poly::fmpz_mod_poly_get_fmpz_poly;
use std::fmt;
impl From<&PolyOverZq> for String {
fn from(value: &PolyOverZq) -> Self {
value.to_string()
}
}
implement_for_owned!(PolyOverZq, String, From);
impl fmt::Display for PolyOverZq {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut poly_over_z = PolyOverZ::default();
unsafe {
fmpz_mod_poly_get_fmpz_poly(
&mut poly_over_z.poly,
&self.poly,
self.modulus.get_fmpz_mod_ctx_struct(),
)
};
write!(f, "{poly_over_z} mod {}", self.modulus)
}
}
#[cfg(test)]
mod test_to_string {
use super::PolyOverZq;
use std::str::FromStr;
#[test]
fn working_keeps_same_string() {
let cmp_str = "3 1 2 2 mod 5";
let cmp = PolyOverZq::from_str(cmp_str).unwrap();
assert_eq!(cmp_str, cmp.to_string());
}
#[test]
fn working_use_result_of_to_string_as_input() {
let cmp_str = "3 1 2 2 mod 5";
let cmp = PolyOverZq::from_str(cmp_str).unwrap();
let cmp_str_2 = cmp.to_string();
assert!(PolyOverZq::from_str(&cmp_str_2).is_ok());
}
#[test]
fn initialized_neg() {
let cmp_str = "3 -1 -2 -3 mod 5";
let cmp = PolyOverZq::from_str(cmp_str).unwrap();
assert_eq!("3 4 3 2 mod 5", cmp.to_string());
}
#[test]
fn large_entries_modulus() {
let cmp_str = format!("3 1 2 {} mod 1{}", u64::MAX, u64::MAX);
let cmp = PolyOverZq::from_str(&cmp_str).unwrap();
assert_eq!(cmp_str, cmp.to_string());
}
#[test]
fn into_works_properly() {
let cmp = "2 2 1 mod 3";
let poly = PolyOverZq::from_str(cmp).unwrap();
let string: String = poly.clone().into();
let borrowed_string: String = (&poly).into();
assert_eq!(cmp, string);
assert_eq!(cmp, borrowed_string);
}
}