complex: Add tests for OpAssign
Add tests by expanding the current a + b tests to include a += b, and so on.
This commit is contained in:
parent
3ec194bafb
commit
686411f44e
|
@ -870,6 +870,7 @@ mod test {
|
||||||
pub const _neg1_1i : Complex64 = Complex { re: -1.0, im: 1.0 };
|
pub const _neg1_1i : Complex64 = Complex { re: -1.0, im: 1.0 };
|
||||||
pub const _05_05i : Complex64 = Complex { re: 0.5, im: 0.5 };
|
pub const _05_05i : Complex64 = Complex { re: 0.5, im: 0.5 };
|
||||||
pub const all_consts : [Complex64; 5] = [_0_0i, _1_0i, _1_1i, _neg1_1i, _05_05i];
|
pub const all_consts : [Complex64; 5] = [_0_0i, _1_0i, _1_1i, _neg1_1i, _05_05i];
|
||||||
|
pub const _4_2i : Complex64 = Complex { re: 4.0, im: 2.0 };
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_consts() {
|
fn test_consts() {
|
||||||
|
@ -1299,55 +1300,83 @@ mod test {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test both a + b and a += b
|
||||||
|
macro_rules! test_a_op_b {
|
||||||
|
($a:ident + $b:expr, $answer:expr) => {
|
||||||
|
assert_eq!($a + $b, $answer);
|
||||||
|
assert_eq!({ let mut x = $a; x += $b; x}, $answer);
|
||||||
|
};
|
||||||
|
($a:ident - $b:expr, $answer:expr) => {
|
||||||
|
assert_eq!($a - $b, $answer);
|
||||||
|
assert_eq!({ let mut x = $a; x -= $b; x}, $answer);
|
||||||
|
};
|
||||||
|
($a:ident * $b:expr, $answer:expr) => {
|
||||||
|
assert_eq!($a * $b, $answer);
|
||||||
|
assert_eq!({ let mut x = $a; x *= $b; x}, $answer);
|
||||||
|
};
|
||||||
|
($a:ident / $b:expr, $answer:expr) => {
|
||||||
|
assert_eq!($a / $b, $answer);
|
||||||
|
assert_eq!({ let mut x = $a; x /= $b; x}, $answer);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test both a + b and a + &b
|
||||||
|
macro_rules! test_op {
|
||||||
|
($a:ident $op:tt $b:expr, $answer:expr) => {
|
||||||
|
test_a_op_b!($a $op $b, $answer);
|
||||||
|
test_a_op_b!($a $op &$b, $answer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mod complex_arithmetic {
|
mod complex_arithmetic {
|
||||||
use super::{_0_0i, _1_0i, _1_1i, _0_1i, _neg1_1i, _05_05i, all_consts};
|
use super::{_0_0i, _1_0i, _1_1i, _0_1i, _neg1_1i, _05_05i, all_consts};
|
||||||
use traits::Zero;
|
use traits::Zero;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_add() {
|
fn test_add() {
|
||||||
assert_eq!(_05_05i + _05_05i, _1_1i);
|
test_op!(_05_05i + _05_05i, _1_1i);
|
||||||
assert_eq!(_0_1i + _1_0i, _1_1i);
|
test_op!(_0_1i + _1_0i, _1_1i);
|
||||||
assert_eq!(_1_0i + _neg1_1i, _0_1i);
|
test_op!(_1_0i + _neg1_1i, _0_1i);
|
||||||
|
|
||||||
for &c in all_consts.iter() {
|
for &c in all_consts.iter() {
|
||||||
assert_eq!(_0_0i + c, c);
|
test_op!(_0_0i + c, c);
|
||||||
assert_eq!(c + _0_0i, c);
|
test_op!(c + _0_0i, c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_sub() {
|
fn test_sub() {
|
||||||
assert_eq!(_05_05i - _05_05i, _0_0i);
|
test_op!(_05_05i - _05_05i, _0_0i);
|
||||||
assert_eq!(_0_1i - _1_0i, _neg1_1i);
|
test_op!(_0_1i - _1_0i, _neg1_1i);
|
||||||
assert_eq!(_0_1i - _neg1_1i, _1_0i);
|
test_op!(_0_1i - _neg1_1i, _1_0i);
|
||||||
|
|
||||||
for &c in all_consts.iter() {
|
for &c in all_consts.iter() {
|
||||||
assert_eq!(c - _0_0i, c);
|
test_op!(c - _0_0i, c);
|
||||||
assert_eq!(c - c, _0_0i);
|
test_op!(c - c, _0_0i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_mul() {
|
fn test_mul() {
|
||||||
assert_eq!(_05_05i * _05_05i, _0_1i.unscale(2.0));
|
test_op!(_05_05i * _05_05i, _0_1i.unscale(2.0));
|
||||||
assert_eq!(_1_1i * _0_1i, _neg1_1i);
|
test_op!(_1_1i * _0_1i, _neg1_1i);
|
||||||
|
|
||||||
// i^2 & i^4
|
// i^2 & i^4
|
||||||
assert_eq!(_0_1i * _0_1i, -_1_0i);
|
test_op!(_0_1i * _0_1i, -_1_0i);
|
||||||
assert_eq!(_0_1i * _0_1i * _0_1i * _0_1i, _1_0i);
|
assert_eq!(_0_1i * _0_1i * _0_1i * _0_1i, _1_0i);
|
||||||
|
|
||||||
for &c in all_consts.iter() {
|
for &c in all_consts.iter() {
|
||||||
assert_eq!(c * _1_0i, c);
|
test_op!(c * _1_0i, c);
|
||||||
assert_eq!(_1_0i * c, c);
|
test_op!(_1_0i * c, c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_div() {
|
fn test_div() {
|
||||||
assert_eq!(_neg1_1i / _0_1i, _1_1i);
|
test_op!(_neg1_1i / _0_1i, _1_1i);
|
||||||
for &c in all_consts.iter() {
|
for &c in all_consts.iter() {
|
||||||
if c != Zero::zero() {
|
if c != Zero::zero() {
|
||||||
assert_eq!(c / c, _1_0i);
|
test_op!(c / c, _1_0i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1364,29 +1393,30 @@ mod test {
|
||||||
|
|
||||||
mod real_arithmetic {
|
mod real_arithmetic {
|
||||||
use super::super::Complex;
|
use super::super::Complex;
|
||||||
|
use super::_4_2i;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_add() {
|
fn test_add() {
|
||||||
assert_eq!(Complex::new(4.0, 2.0) + 0.5, Complex::new(4.5, 2.0));
|
test_op!(_4_2i + 0.5, Complex::new(4.5, 2.0));
|
||||||
assert_eq!(0.5 + Complex::new(4.0, 2.0), Complex::new(4.5, 2.0));
|
assert_eq!(0.5 + _4_2i, Complex::new(4.5, 2.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_sub() {
|
fn test_sub() {
|
||||||
assert_eq!(Complex::new(4.0, 2.0) - 0.5, Complex::new(3.5, 2.0));
|
test_op!(_4_2i - 0.5, Complex::new(3.5, 2.0));
|
||||||
assert_eq!(0.5 - Complex::new(4.0, 2.0), Complex::new(-3.5, -2.0));
|
assert_eq!(0.5 - _4_2i, Complex::new(-3.5, -2.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_mul() {
|
fn test_mul() {
|
||||||
assert_eq!(Complex::new(4.0, 2.0) * 0.5, Complex::new(2.0, 1.0));
|
assert_eq!(_4_2i * 0.5, Complex::new(2.0, 1.0));
|
||||||
assert_eq!(0.5 * Complex::new(4.0, 2.0), Complex::new(2.0, 1.0));
|
assert_eq!(0.5 * _4_2i, Complex::new(2.0, 1.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_div() {
|
fn test_div() {
|
||||||
assert_eq!(Complex::new(4.0, 2.0) / 0.5, Complex::new(8.0, 4.0));
|
assert_eq!(_4_2i / 0.5, Complex::new(8.0, 4.0));
|
||||||
assert_eq!(0.5 / Complex::new(4.0, 2.0), Complex::new(0.1, -0.05));
|
assert_eq!(0.5 / _4_2i, Complex::new(0.1, -0.05));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue