From e101fee7667ce5e35f80ce4f4e582b63b423489b Mon Sep 17 00:00:00 2001 From: donjuanplatinum Date: Sun, 11 Aug 2024 13:39:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?test(complex):=20=E6=B7=BB=E5=8A=A0Complex?= =?UTF-8?q?=E5=9B=9B=E5=88=99=E8=BF=90=E7=AE=97Test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/math/complex.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/src/math/complex.rs b/src/math/complex.rs index 85713fe..b40afa9 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,61 @@ 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); + } +} From bf08d4625a2e49375d01dc054d2cf8f208ea0690 Mon Sep 17 00:00:00 2001 From: donjuanplatinum Date: Sun, 11 Aug 2024 13:44:04 +0800 Subject: [PATCH 2/2] fmt --- src/math/complex.rs | 70 +++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/src/math/complex.rs b/src/math/complex.rs index b40afa9..7ecd960 100644 --- a/src/math/complex.rs +++ b/src/math/complex.rs @@ -51,7 +51,7 @@ macro_rules! impl_all { }; } -#[derive(Clone, Debug, PartialEq , Eq,Hash)] +#[derive(Clone, Debug, PartialEq, Eq, Hash)] #[repr(C)] /// Complex pub struct Complex { @@ -136,13 +136,15 @@ where } } -impl From<(T,T)> for Complex { - fn from(value: (T,T)) -> Self { - return Self{re:value.0,im:value.1}; +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; @@ -150,46 +152,52 @@ mod max_substring { use super::*; #[test] fn create_new_complex() { - let a = Complex::new(1,1); - assert_eq!(Complex{re:1,im:1},a); + 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); + 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); + 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); + 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); + 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); + 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); } }