use super::super::{
complex_fn, imabs_fn, imaginary_fn, imargument_fn, imconjugate_fn, imdiv_fn, imexp_fn,
imln_fn, impower_fn, improduct_fn, imreal_fn, imsqrt_fn, imsub_fn, imsum_fn,
};
use crate::types::Value;
fn approx_eq(a: f64, b: f64, tol: f64) -> bool {
(a - b).abs() < tol
}
fn text(v: &Value) -> &str {
match v {
Value::Text(s) => s.as_str(),
other => panic!("expected Text, got {:?}", other),
}
}
fn num(v: &Value) -> f64 {
match v {
Value::Number(n) => *n,
other => panic!("expected Number, got {:?}", other),
}
}
fn t(s: &str) -> Value {
Value::Text(s.to_string())
}
#[test]
fn complex_3_4i() {
assert_eq!(
complex_fn(&[Value::Number(3.0), Value::Number(4.0)]),
Value::Text("3+4i".to_string())
);
}
#[test]
fn complex_3_neg4i() {
assert_eq!(
complex_fn(&[Value::Number(3.0), Value::Number(-4.0)]),
Value::Text("3-4i".to_string())
);
}
#[test]
fn complex_0_1() {
assert_eq!(
complex_fn(&[Value::Number(0.0), Value::Number(1.0)]),
Value::Text("i".to_string())
);
}
#[test]
fn complex_pure_real() {
assert_eq!(
complex_fn(&[Value::Number(5.0), Value::Number(0.0)]),
Value::Number(5.0)
);
}
#[test]
fn complex_with_j_suffix() {
assert_eq!(
complex_fn(&[Value::Number(3.0), Value::Number(4.0), t("j")]),
Value::Text("3+4j".to_string())
);
}
#[test]
fn imabs_3_4i() {
assert_eq!(imabs_fn(&[t("3+4i")]), Value::Number(5.0));
}
#[test]
fn imabs_zero() {
assert_eq!(imabs_fn(&[t("0")]), Value::Number(0.0));
}
#[test]
fn imabs_pure_real() {
assert_eq!(imabs_fn(&[Value::Number(5.0)]), Value::Number(5.0));
}
#[test]
fn imreal_3_4i() {
assert_eq!(imreal_fn(&[t("3+4i")]), Value::Number(3.0));
}
#[test]
fn imaginary_3_4i() {
assert_eq!(imaginary_fn(&[t("3+4i")]), Value::Number(4.0));
}
#[test]
fn imreal_pure_real() {
assert_eq!(imreal_fn(&[Value::Number(5.0)]), Value::Number(5.0));
}
#[test]
fn imaginary_pure_imag() {
assert_eq!(imaginary_fn(&[t("3i")]), Value::Number(3.0));
}
#[test]
fn imaginary_pure_real() {
assert_eq!(imaginary_fn(&[Value::Number(5.0)]), Value::Number(0.0));
}
#[test]
fn imargument_1_i() {
let result = num(&imargument_fn(&[t("1+i")]));
assert!(
approx_eq(result, std::f64::consts::FRAC_PI_4, 1e-10),
"imargument(1+i) = {}",
result
);
}
#[test]
fn imconjugate_3_4i() {
assert_eq!(
imconjugate_fn(&[t("3+4i")]),
Value::Text("3-4i".to_string())
);
}
#[test]
fn imdiv_basic() {
let result = imdiv_fn(&[t("4+2i"), t("1+i")]);
assert_eq!(result, Value::Text("3-i".to_string()));
}
#[test]
fn imexp_zero() {
assert_eq!(imexp_fn(&[Value::Number(0.0)]), Value::Number(1.0));
}
#[test]
fn imln_one() {
assert_eq!(imln_fn(&[Value::Number(1.0)]), Value::Number(0.0));
}
#[test]
fn impower_square() {
let result = impower_fn(&[t("1+i"), Value::Number(2.0)]);
assert_eq!(result, Value::Text("2i".to_string()));
}
#[test]
fn improduct_rotation_90() {
assert_eq!(
improduct_fn(&[t("1+0i"), t("0+1i")]),
t("i")
);
}
#[test]
fn improduct_1_2i_times_3_4i() {
let result = improduct_fn(&[t("1+2i"), t("3+4i")]);
assert_eq!(result, Value::Text("-5+10i".to_string()));
}
#[test]
fn imsqrt_neg1() {
let result = imsqrt_fn(&[Value::Number(-1.0)]);
assert_eq!(result, Value::Text("i".to_string()));
}
#[test]
fn imsub_basic() {
let result = imsub_fn(&[t("3+4i"), t("1+2i")]);
assert_eq!(result, Value::Text("2+2i".to_string()));
}
#[test]
fn imsum_basic() {
let result = imsum_fn(&[t("1+2i"), t("3+4i")]);
assert_eq!(result, Value::Text("4+6i".to_string()));
}
#[test]
fn imsum_single() {
let result = imsum_fn(&[t("3+4i")]);
assert_eq!(result, Value::Text("3+4i".to_string()));
}
#[test]
fn text_function_returns_text() {
let result = complex_fn(&[Value::Number(3.0), Value::Number(4.0)]);
assert!(matches!(result, Value::Text(_)));
assert_eq!(text(&result), "3+4i");
}