use super::PolyOverZ;
use crate::macros::for_others::implement_for_owned;
use core::fmt;
use flint_sys::fmpz_poly::fmpz_poly_get_str;
use std::ffi::CStr;
impl From<&PolyOverZ> for String {
fn from(value: &PolyOverZ) -> Self {
value.to_string()
}
}
implement_for_owned!(PolyOverZ, String, From);
impl fmt::Display for PolyOverZ {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let c_str_ptr = unsafe { fmpz_poly_get_str(&self.poly) };
let return_str = unsafe { CStr::from_ptr(c_str_ptr).to_str().unwrap().to_owned() };
unsafe { libc::free(c_str_ptr as *mut libc::c_void) };
write!(f, "{return_str}")
}
}
#[cfg(test)]
mod test_to_string {
use super::PolyOverZ;
use std::str::FromStr;
#[test]
fn working_keeps_same_string() {
let cmp_str = "3 1 2 -3";
let cmp = PolyOverZ::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 -3";
let cmp = PolyOverZ::from_str(cmp_str).unwrap();
let cmp_str_2 = cmp.to_string();
assert!(PolyOverZ::from_str(&cmp_str_2).is_ok());
}
#[test]
fn large_entries() {
let cmp_str = format!("3 1 {} -{}", u64::MAX, u64::MAX);
let cmp = PolyOverZ::from_str(&cmp_str).unwrap();
let cmp_str_2 = cmp.to_string();
assert!(PolyOverZ::from_str(&cmp_str_2).is_ok());
}
#[test]
fn into_works_properly() {
let cmp = "2 6 1";
let poly = PolyOverZ::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);
}
}