package net.sf.javaml.core; import static org.junit.Assert.*; import org.junit.Test; public class TestComplex { public static boolean apprEq(double d1, double d2){ final double accuracy = 0.000001; double diff = d1 - d2; return diff <= accuracy && diff >= -accuracy; } @Test public void test(){ Complex c = new Complex(); c = new Complex(0, 1); assertNotNull(c.toString()); assertTrue(apprEq(c.abs(), 1.0)); Complex res = Complex.multiply(c, Complex.I); assertTrue(apprEq(res.re, -1.0) && apprEq(res.im, 0.0)); res = Complex.multiply(c, 5.0); assertTrue(apprEq(res.re, 0) && apprEq(res.im, 5.0)); res = Complex.plus(c, Complex.I); assertTrue(apprEq(res.re, 0) && apprEq(res.im, 2.0)); assertTrue(apprEq(c.im, 1.0) && apprEq(c.re, 0.0)); c.conjugate(); assertTrue(apprEq(c.im, -1.0)); c.conjugate(); assertTrue(apprEq(c.im, 1.0)); c.times(0.0); assertTrue(apprEq(c.im, 0.0) && apprEq(c.re, 0.0)); c.re = 2.0; c.im = 2.0; c.times(new Complex(2.0, 2.0)); assertTrue(apprEq(c.re, 0.0) && apprEq(c.im, 8.0)); c.minus(c); assertTrue(apprEq(c.re, 0.0) && apprEq(c.im, 0.0)); c.re = 2.0; c.im = 2.0; c.plus(c); c.minus(c); c.minus(c); assertTrue(apprEq(c.re, 0.0) && apprEq(c.im, 0.0)); } }