diff --git a/src/math/complex.rs b/src/math/complex.rs index 85713fe..7ecd960 100644 --- a/src/math/complex.rs +++ b/src/math/complex.rs @@ -51,7 +51,7 @@ macro_rules! impl_all { }; } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq, Hash)] #[repr(C)] /// Complex pub struct Complex { @@ -135,3 +135,69 @@ where return write!(f, "({},{})", self.re, self.im); } } + +impl From<(T, T)> for Complex { + fn from(value: (T, T)) -> Self { + return Self { + re: value.0, + im: value.1, + }; + } +} + +#[cfg(test)] +mod max_substring { + use std::hash::Hasher; + + use super::*; + #[test] + fn create_new_complex() { + let a = Complex::new(1, 1); + assert_eq!(Complex { re: 1, im: 1 }, a); + } + #[test] + fn add() { + let a: Complex = (20, 30).into(); + let b: Complex = (992, 8770).into(); + assert_eq!(Complex { re: 1012, im: 8800 }, a + b); + } + #[test] + fn mul() { + let a: Complex = (20, 30).into(); + let b: Complex = (992, 8770).into(); + assert_eq!( + Complex { + re: -243260, + im: 205160 + }, + a * b + ); + } + #[test] + fn div() { + let a: Complex = (20, 30).into(); + let b: Complex = (992, 8770).into(); + assert_eq!(Complex { re: 217, im: 112 }, b / a); + } + #[test] + fn sub() { + let a: Complex = (20, 30).into(); + let b: Complex = (992, 8770).into(); + assert_eq!(Complex { re: 972, im: 8740 }, b - a); + } + #[test] + fn hash() { + use std::collections::hash_map::DefaultHasher; + use std::hash::Hash; + fn calculate_hash(t: &T) -> u64 { + let mut s = DefaultHasher::new(); + t.hash(&mut s); + s.finish() + } + let a: Complex = (20, 30).into(); + let b: Complex = (20, 40).into(); + let c: Complex = (20, 30).into(); + assert_eq!(calculate_hash(&a), calculate_hash(&c)); + assert_eq!(calculate_hash(&a) == calculate_hash(&b), false); + } +}