#![cfg(feature = "eval")]
use rlx_tensor::Tensor;
fn approx(a: &[f32], b: &[f32]) {
assert_eq!(a.len(), b.len(), "length mismatch: {a:?} vs {b:?}");
for (x, y) in a.iter().zip(b) {
assert!((x - y).abs() < 1e-4, "{a:?} != {b:?}");
}
}
#[test]
fn dft_n2() {
let x = Tensor::from_vec(vec![1.0, 2.0, 0.0, 0.0], [4]);
approx(&x.fft().to_vec(), &[3.0, -1.0, 0.0, 0.0]);
}
#[test]
fn dft_n4_impulse() {
let x = Tensor::from_vec(vec![1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [8]);
let y = x.fft().to_vec();
approx(&y[0..4], &[1.0, 1.0, 1.0, 1.0]); approx(&y[4..8], &[0.0, 0.0, 0.0, 0.0]); }
#[test]
fn ifft_of_fft_is_n_times_input() {
let re = [0.5_f32, -1.0, 2.0, 0.25];
let mut data = re.to_vec();
data.extend_from_slice(&[0.0; 4]); let x = Tensor::from_vec(data, [8]);
let round = x.fft().ifft().to_vec();
let expect: Vec<f32> = re
.iter()
.map(|v| v * 4.0)
.chain(std::iter::repeat_n(0.0, 4))
.collect();
approx(&round, &expect);
}