mirror of
https://github.com/allankoechke/rust.git
synced 2026-11-30 11:05:36 +00:00
76 lines
2.7 KiB
Rust
76 lines
2.7 KiB
Rust
use core::num::{Complex, Wrapping};
|
|
|
|
#[test]
|
|
fn complex_default() {
|
|
assert_eq!(Complex::<i32>::default(), Complex::new(0, 0));
|
|
assert_eq!(Complex::<f32>::default(), Complex::new(0.0, 0.0));
|
|
|
|
// The default is the additive unit.
|
|
let a = Complex::new(1, 2);
|
|
assert_eq!(a + Complex::<i32>::default(), a);
|
|
assert_eq!(Complex::<i32>::default() + a, a);
|
|
}
|
|
|
|
#[test]
|
|
fn complex_addition() {
|
|
let a = Complex::new(1, 2);
|
|
let b = Complex::new(3, 4);
|
|
assert_eq!(a + b, Complex::new(a.re + b.re, a.im + b.im));
|
|
assert_eq!(a + b, b + a);
|
|
assert_eq!(a + 8, Complex::new(a.re + 8, a.im));
|
|
|
|
let a = Complex::new(Wrapping(1u8), Wrapping(2));
|
|
let b = Complex::new(Wrapping(3u8), Wrapping(4));
|
|
assert_eq!(a + b, Complex::new(a.re + b.re, a.im + b.im));
|
|
assert_eq!(a + b, b + a);
|
|
let c = a + Wrapping(u8::MAX);
|
|
assert_eq!(c, Complex::new(a.re + Wrapping(u8::MAX), a.im));
|
|
assert_eq!(c.re.0, 1u8.wrapping_add(u8::MAX));
|
|
|
|
let a = Complex::new(1.0, 2.0);
|
|
let b = Complex::new(3.0, 4.0);
|
|
assert_eq!(a + b, Complex::new(a.re + b.re, a.im + b.im));
|
|
assert_eq!(a + b, b + a);
|
|
assert_eq!(a + 8.0, Complex::new(a.re + 8.0, a.im));
|
|
}
|
|
|
|
#[test]
|
|
fn complex_subtraction() {
|
|
let a = Complex::new(1, 2);
|
|
let b = Complex::new(3, 4);
|
|
assert_eq!(a - b, Complex::new(a.re - b.re, a.im - b.im));
|
|
assert_eq!(a - 8, Complex::new(a.re - 8, a.im));
|
|
|
|
let a = Complex::new(Wrapping(1u8), Wrapping(2));
|
|
let b = Complex::new(Wrapping(3u8), Wrapping(4));
|
|
assert_eq!(a - b, Complex::new(a.re - b.re, a.im - b.im));
|
|
let c = a - Wrapping(u8::MAX);
|
|
assert_eq!(c, Complex::new(a.re - Wrapping(u8::MAX), a.im));
|
|
assert_eq!(c.re.0, 1u8.wrapping_sub(u8::MAX));
|
|
|
|
let a = Complex::new(1.0, 2.0);
|
|
let b = Complex::new(3.0, 4.0);
|
|
assert_eq!(a - b, Complex::new(a.re - b.re, a.im - b.im));
|
|
assert_eq!(a - 8.0, Complex::new(a.re - 8.0, a.im));
|
|
}
|
|
|
|
#[test]
|
|
fn complex_conjugate() {
|
|
assert_eq!(Complex::new(1, 2).conjugate(), Complex::new(1, -2));
|
|
assert_eq!(Complex::new(1, -2).conjugate(), Complex::new(1, 2));
|
|
|
|
assert_eq!(Complex::new(1.0, 2.0).conjugate(), Complex::new(1.0, -2.0));
|
|
assert_eq!(Complex::new(1.0, -2.0).conjugate(), Complex::new(1.0, 2.0));
|
|
assert_eq!(Complex::new(1.0, f32::INFINITY).conjugate(), Complex::new(1.0, f32::NEG_INFINITY));
|
|
}
|
|
|
|
#[test]
|
|
fn complex_negation() {
|
|
assert_eq!(-Complex::new(1, 2), Complex::new(-1, -2));
|
|
assert_eq!(-Complex::new(1, -2), Complex::new(-1, 2));
|
|
|
|
assert_eq!(-Complex::new(1.0, 2.0), Complex::new(-1.0, -2.0));
|
|
assert_eq!(-Complex::new(1.0, -2.0), Complex::new(-1.0, 2.0));
|
|
assert_eq!(-Complex::new(1.0, f32::INFINITY), Complex::new(-1.0, f32::NEG_INFINITY),);
|
|
}
|