Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(complex): 添加Complex四则运算Test #44

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion src/math/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ macro_rules! impl_all {
};
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[repr(C)]
/// Complex
pub struct Complex<T> {
Expand Down Expand Up @@ -135,3 +135,69 @@ where
return write!(f, "({},{})", self.re, self.im);
}
}

impl<T> From<(T, T)> for Complex<T> {
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<i32> = (20, 30).into();
let b: Complex<i32> = (992, 8770).into();
assert_eq!(Complex { re: 1012, im: 8800 }, a + b);
}
#[test]
fn mul() {
let a: Complex<i32> = (20, 30).into();
let b: Complex<i32> = (992, 8770).into();
assert_eq!(
Complex {
re: -243260,
im: 205160
},
a * b
);
}
#[test]
fn div() {
let a: Complex<i32> = (20, 30).into();
let b: Complex<i32> = (992, 8770).into();
assert_eq!(Complex { re: 217, im: 112 }, b / a);
}
#[test]
fn sub() {
let a: Complex<i32> = (20, 30).into();
let b: Complex<i32> = (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: Hash>(t: &T) -> u64 {
let mut s = DefaultHasher::new();
t.hash(&mut s);
s.finish()
}
let a: Complex<i32> = (20, 30).into();
let b: Complex<i32> = (20, 40).into();
let c: Complex<i32> = (20, 30).into();
assert_eq!(calculate_hash(&a), calculate_hash(&c));
assert_eq!(calculate_hash(&a) == calculate_hash(&b), false);
}
}
Loading