use super::ModulusPolynomialRingZq;
use crate::macros::for_others::implement_for_owned;
use std::fmt::Display;
impl From<&ModulusPolynomialRingZq> for String {
fn from(value: &ModulusPolynomialRingZq) -> Self {
value.to_string()
}
}
implement_for_owned!(ModulusPolynomialRingZq, String, From);
impl Display for ModulusPolynomialRingZq {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let modulus = self.get_q_as_modulus();
let poly = self.modulus.get_representative_least_nonnegative_residue();
write!(f, "{poly} mod {modulus}")
}
}
#[cfg(test)]
mod test_to_string {
use crate::integer_mod_q::ModulusPolynomialRingZq;
use std::str::FromStr;
#[test]
fn working_keeps_same_string() {
let cmp_str = "3 1 2 1 mod 5";
let cmp = ModulusPolynomialRingZq::from_str(cmp_str).unwrap();
assert_eq!(cmp_str, cmp.to_string());
}
#[test]
fn working_use_result_of_to_string() {
let cmp_str = "3 1 2 1 mod 5";
let cmp = ModulusPolynomialRingZq::from_str(cmp_str).unwrap();
let str_1 = cmp.to_string();
assert!(ModulusPolynomialRingZq::from_str(&str_1).is_ok());
}
#[test]
fn into_works_properly() {
let cmp = "2 2 1 mod 3";
let modulus = ModulusPolynomialRingZq::from_str(cmp).unwrap();
let string: String = modulus.clone().into();
let borrowed_string: String = (&modulus).into();
assert_eq!(cmp, string);
assert_eq!(cmp, borrowed_string);
}
}