package at.priv.toastfreeware.jguitartuner.test;

import static org.junit.Assert.*;

import java.util.Vector;

import org.junit.Test;

import at.priv.toastfreeware.jguitartuner.Calculus;
import at.priv.toastfreeware.jguitartuner.Complex;

public class CalculusTest {
	
	@Test
	public void fft1() {
		Vector<Complex> x = new Vector<Complex>();
		x.add(new Complex(1));
		x.add(new Complex(0));
		x.add(new Complex(1));
		x.add(new Complex(0));
		x = Calculus.fft(x);
		double f = 10000;
		assertTrue(Math.round(x.get(0).real * f) == Math.round(2 * f) && Math.round(x.get(0).imag * f) == Math.round(0 * f));
		assertTrue(Math.round(x.get(1).real * f) == Math.round(0 * f) && Math.round(x.get(1).imag * f) == Math.round(0 * f));
		assertTrue(Math.round(x.get(2).real * f) == Math.round(2 * f) && Math.round(x.get(2).imag * f) == Math.round(0 * f));
		assertTrue(Math.round(x.get(3).real * f) == Math.round(0 * f) && Math.round(x.get(3).imag * f) == Math.round(0 * f));
	}
	
	
	@Test
	public void fft2() {
		Vector<Complex> x = new Vector<Complex>();
		x.add(new Complex(5, 9));
		x.add(new Complex(0, -17));
		x.add(new Complex(1, 33));
		x.add(new Complex(2, -20));
		x = Calculus.fft(x);
		double f = 10000;
		assertTrue(Math.round(x.get(0).real * f) == Math.round(8 * f) && Math.round(x.get(0).imag * f) == Math.round(5 * f));
		assertTrue(Math.round(x.get(1).real * f) == Math.round(7 * f) && Math.round(x.get(1).imag * f) == Math.round(-22 * f));
		assertTrue(Math.round(x.get(2).real * f) == Math.round(4 * f) && Math.round(x.get(2).imag * f) == Math.round(79 * f));
		assertTrue(Math.round(x.get(3).real * f) == Math.round(1 * f) && Math.round(x.get(3).imag * f) == Math.round(-26 * f));
	}
	
	
	@Test
	public void fft3() {
		// R: x = round(randu[2:21,1]*1000) + 1i*round(randu[2:21,2]*1000)
		Vector<Complex> x = new Vector<Complex>();
		x.add(new Complex(44, 156));
		x.add(new Complex(822, 873));
		x.add(new Complex(322, 649));
		x.add(new Complex(394, 827));
		x.add(new Complex(309, 927));
		x.add(new Complex(826, 309));
		x.add(new Complex(729, 742));
		x.add(new Complex(318, 393));
		x.add(new Complex(600, 846));
		x.add(new Complex(648, 282));
		x.add(new Complex(547, 949));
		x.add(new Complex(530, 348));
		x.add(new Complex(908, 13));
		x.add(new Complex(835, 815));
		x.add(new Complex(69, 276));
		x.add(new Complex(984, 928));
		x.add(new Complex(946, 690));
		x.add(new Complex(17, 166));
		x.add(new Complex(773, 282));
		x.add(new Complex(493, 944));
		// R: y<-round(fft(x))
		Vector<Complex> z = Calculus.fft(x);
		Vector<Complex> y = new Vector<Complex>();
		y.add(new Complex(11114, 11415));
		y.add(new Complex(-96, 1010));
		y.add(new Complex(-93, -487));
		y.add(new Complex(1351, -139));
		y.add(new Complex(-263, -141));
		y.add(new Complex(-628, -695));
		y.add(new Complex(-2781, 1733));
		y.add(new Complex(-1677, -3414));
		y.add(new Complex(1616, -2409));
		y.add(new Complex(-2232, -1843));
		y.add(new Complex(-620, -355));
		y.add(new Complex(1452, -1261));
		y.add(new Complex(-1594, 1336));
		y.add(new Complex(-1385, -990));
		y.add(new Complex(-1610, -1216));
		y.add(new Complex(1362, 163));
		y.add(new Complex(1132, 1508));
		y.add(new Complex(-1269, -657));
		y.add(new Complex(-991, -335));
		y.add(new Complex(-1908, -105));
		for(int i = 0; i != z.size(); ++i) {
			assertTrue(Math.round(z.get(i).real) == y.get(i).real && Math.round(z.get(i).imag) == y.get(i).imag);
		}
	}


}
