RootsOfUnity implements Serializable { public synchronized void computeRoots(int n) throws ZeroException { if (n == 0) { throw new ZeroException( LocalizedFormats.CANNOT_COMPUTE_0TH_ROOT_OF_UNITY); } isCounterClockWise = n > 0; final int absN = FastMath.abs(n); if (absN == omegaCount) { return; } final double t = 2.0 * FastMath.PI / absN; final double cosT = FastMath.cos(t); final double sinT = FastMath.sin(t); omegaReal = new double[absN]; omegaImaginaryCounterClockwise = new double[absN]; omegaImaginaryClockwise = new double[absN]; omegaReal[0] = 1.0; omegaImaginaryCounterClockwise[0] = 0.0; omegaImaginaryClockwise[0] = 0.0; for (int i = 1; i < absN; i++) { omegaReal[i] = omegaReal[i - 1] * cosT - omegaImaginaryCounterClockwise[i - 1] * sinT; omegaImaginaryCounterClockwise[i] = omegaReal[i - 1] * sinT + omegaImaginaryCounterClockwise[i - 1] * cosT; omegaImaginaryClockwise[i] = -omegaImaginaryCounterClockwise[i]; } omegaCount = absN; } RootsOfUnity(); synchronized boolean isCounterClockWise(); synchronized void computeRoots(int n); synchronized double getReal(int k); synchronized double getImaginary(int k); synchronized int getNumberOfRoots();  }
@Test(expected = ZeroException.class) public void testZeroNumberOfRoots() { final RootsOfUnity roots = new RootsOfUnity(); roots.computeRoots(0); }
@Test public void testComputeRoots() { final RootsOfUnity roots = new RootsOfUnity(); for (int n = -10; n < 11; n++) { if (n != 0) { roots.computeRoots(n); doTestComputeRoots(roots); roots.computeRoots(-n); doTestComputeRoots(roots); } } }