use imagnum::*;
#[test]
fn test_create_complex() {
let z = create_complex("3", "4");
assert_eq!(z.to_string(), "3.0 + 4.0i");
}
#[test]
fn test_complex_addition() {
let z1 = create_complex("3", "4");
let z2 = create_complex("1", "2");
let result = (z1 + z2).unwrap();
assert_eq!(result.to_string(), "4.0 + 6.0i");
}
#[test]
fn test_complex_subtraction() {
let z1 = create_complex("5", "7");
let z2 = create_complex("2", "3");
let result = (z1 - z2).unwrap();
assert_eq!(result.to_string(), "3.0 + 4.0i");
}
#[test]
fn test_complex_multiplication() {
let z1 = create_complex("3", "2");
let z2 = create_complex("1", "4");
let result = (z1 * z2).unwrap();
assert_eq!(result.to_string(), "-5.0 + 14.0i");
}
#[test]
fn test_complex_division() {
let z1 = create_complex("4", "2");
let z2 = create_complex("3", "-1");
let result = (z1 / z2).unwrap();
assert_eq!(result.to_string(), "1.0 + i");
}
#[test]
fn test_pure_imaginary() {
let z = create_complex("0", "5");
assert_eq!(z.to_string(), "5.0i");
}
#[test]
fn test_pure_real() {
let z = create_complex("7", "0");
assert_eq!(z.to_string(), "7.0");
}
#[test]
fn test_imaginary_unit() {
let i = create_imaginary();
assert_eq!(i.to_string(), "i");
}
#[test]
fn test_negative_imaginary() {
let z = create_complex("0", "-1");
assert_eq!(z.to_string(), "-i");
}
#[test]
fn test_complex_with_negative_imaginary() {
let z = create_complex("3", "-4");
assert_eq!(z.to_string(), "3.0 - 4.0i");
}
#[test]
fn test_complex_real_addition() {
let z = create_complex("3", "4");
let r = create_float("2");
let result = (z + r).unwrap();
assert_eq!(result.to_string(), "5.0 + 4.0i");
}
#[test]
fn test_real_complex_addition() {
let r = create_float("2");
let z = create_complex("3", "4");
let result = (r + z).unwrap();
assert_eq!(result.to_string(), "5.0 + 4.0i");
}
#[test]
fn test_complex_real_multiplication() {
let z = create_complex("3", "4");
let r = create_float("2");
let result = (z * r).unwrap();
assert_eq!(result.to_string(), "6.0 + 8.0i");
}
#[test]
fn test_complex_real_division() {
let z = create_complex("6", "8");
let r = create_float("2");
let result = (z / r).unwrap();
assert_eq!(result.to_string(), "3.0 + 4.0i");
}
#[test]
fn test_real_complex_division() {
let r = create_float("10");
let z = create_complex("3", "4");
let result = (r / z).unwrap();
assert_eq!(result.to_string(), "1.2 - 1.6i");
}
#[test]
fn test_i_squared() {
let i = create_imaginary();
let result = (i.clone() * i).unwrap();
assert_eq!(result.to_string(), "-1.0");
}
#[test]
fn test_complex_conjugate_multiplication() {
let z1 = create_complex("3", "4");
let z2 = create_complex("3", "-4");
let result = (z1 * z2).unwrap();
assert_eq!(result.to_string(), "25.0");
}
#[test]
fn test_complex_zero_addition() {
let z1 = create_complex("3", "4");
let z2 = create_complex("0", "0");
let result = (z1 + z2).unwrap();
assert_eq!(result.to_string(), "3.0 + 4.0i");
}
#[test]
fn test_complex_division_by_real() {
let z = create_complex("8", "6");
let r = create_float("2");
let result = (z / r).unwrap();
assert_eq!(result.to_string(), "4.0 + 3.0i");
}
#[test]
fn test_complex_abs() {
let z = create_complex("3", "4");
let result = z.abs();
assert_eq!(result.to_string(), "5.0");
}
#[test]
fn test_complex_sqrt() {
let z = create_complex("3", "4");
let result = z.sqrt().unwrap();
assert!(result.is_complex());
let s = result.to_string();
assert!(s.contains("2.0") && s.contains("i"));
}
#[test]
fn test_complex_exp() {
let pi = create_float("3.141592653589793");
let i = create_imaginary();
let pi_i = (pi * i).unwrap();
let result = pi_i.exp().unwrap();
assert!(result.is_complex());
let s = result.to_string();
assert!(s.contains("-1") || s.contains("-0.9"));
}
#[test]
fn test_complex_sin() {
let i = create_imaginary();
let result = i.sin().unwrap();
assert!(result.is_complex());
let s = result.to_string();
assert!(s.contains("i"));
}
#[test]
fn test_complex_cos() {
let i = create_imaginary();
let result = i.cos().unwrap();
assert!(result.is_complex());
let s = result.to_string();
assert!(s.contains("1.5") || s.contains("1.6"));
}
#[test]
fn test_complex_tan() {
let z = create_complex("1", "1");
let result = z.tan().unwrap();
assert!(result.is_complex());
}
#[test]
fn test_complex_ln() {
let e = create_float("2.718281828459045");
let result = e.ln().unwrap();
let val = result.to_f64().unwrap();
assert!((val - 1.0).abs() < 0.0001);
let i = create_imaginary();
let result_i = i.ln().unwrap();
assert!(result_i.is_complex());
}
#[test]
fn test_complex_log() {
let eight = create_float("8");
let two = create_float("2");
let result = eight.log(&two).unwrap();
let val = result.to_f64().unwrap();
assert!((val - 3.0).abs() < 0.0001);
let z = create_complex("2", "3");
let base = create_float("10");
let result = z.log(&base).unwrap();
assert!(result.is_complex());
}
#[test]
fn test_complex_log10() {
let hundred = create_float("100");
let result = hundred.log10().unwrap();
let val = result.to_f64().unwrap();
assert!((val - 2.0).abs() < 0.0001);
let z = create_complex("1", "1");
let result = z.log10().unwrap();
assert!(result.is_complex());
}
#[test]
fn test_complex_pow() {
let i = create_imaginary();
let two = create_float("2");
let result = i.pow(&two).unwrap();
assert!(result.is_complex());
let s = result.to_string();
assert!(s.contains("-1"));
let z = create_complex("1", "1");
let result2 = z.pow(&two).unwrap();
assert!(result2.is_complex());
let s = result2.to_string();
assert!(s.contains("2") && s.contains("i"));
}
#[test]
fn test_complex_round() {
let z = create_complex("3.456", "7.891");
let rounded = z.round(2);
assert!(rounded.is_complex());
let s = rounded.to_string();
assert!(s.contains("3.46") && s.contains("7.89"));
}
#[test]
fn test_complex_is_complex() {
let z = create_complex("1", "2");
assert!(z.is_complex());
let r = create_float("5");
assert!(!r.is_complex());
}
#[test]
fn test_complex_floor_ceil_error() {
let z = create_complex("3", "4");
assert!(z.floor().is_err());
assert!(z.ceil().is_err());
}
#[test]
fn test_complex_modulo_error() {
let z1 = create_complex("3", "4");
let z2 = create_complex("1", "2");
let result = z1 % z2;
assert!(result.is_err());
}