Skip to content

Commit

Permalink
Merge branch 'test/complex_operations' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
chikatetsu committed Nov 6, 2023
2 parents 4a3dcf1 + db51152 commit ffc021c
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions complex/src/complex_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use shared::types::complex::Complex;
pub trait ComplexOperations {
fn new(re: f64, im: f64) -> Self;
fn add(&self, other: &Self) -> Self;
fn sub(&self, other: &Self) -> Self;
fn mul(&self, other: &Self) -> Self;
fn square(&self) -> Self;
fn magnitude_squared(&self) -> f64;
Expand All @@ -18,6 +19,10 @@ impl ComplexOperations for Complex {
Complex::new(self.re + other.re, self.im + other.im)
}

fn sub(&self, other: &Self) -> Self {
Complex::new(self.re - other.re, self.im - other.im)
}

fn mul(&self, other: &Self) -> Self {
Complex::new(
self.re * other.re - self.im * other.im,
Expand All @@ -44,12 +49,32 @@ mod complex_tests {

#[test]
fn test_add() {
let a = Complex::new(1.0, 2.0);
let b = Complex::new(2.0, 3.0);
let a = Complex::new(-2.0, 5.0);
let b = Complex::new(1.0, -3.0);
let result = a.add(&b);

assert_eq!(result.re, -1.0);
assert_eq!(result.im, 2.0);
}

#[test]
fn test_add_with_wikipedia_example() {
let a = Complex::new(-2.0, 5.0);
let b = Complex::new(1.0, -3.0);
let result = a.add(&b);

assert_eq!(result.re, 3.0);
assert_eq!(result.im, 5.0);
assert_eq!(result.re, -1.0);
assert_eq!(result.im, 2.0);
}

#[test]
fn test_sub_with_wikipedia_example() {
let a = Complex::new(-2.0, 5.0);
let b = Complex::new(1.0, -3.0);
let result = a.sub(&b);

assert_eq!(result.re, -3.0);
assert_eq!(result.im, 8.0);
}

#[test]
Expand All @@ -62,6 +87,16 @@ mod complex_tests {
assert_eq!(result.im, 7.0);
}

#[test]
fn test_mul_with_wikipedia_example() {
let a = Complex::new(-2.0, 5.0);
let b = Complex::new(1.0, -3.0);
let result = a.mul(&b);

assert_eq!(result.re, 13.0);
assert_eq!(result.im, 11.0);
}

#[test]
fn test_square() {
let a = Complex::new(1.0, 2.0);
Expand Down

0 comments on commit ffc021c

Please sign in to comment.