1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.complex;
23
24 import org.hipparchus.exception.MathIllegalArgumentException;
25 import org.hipparchus.exception.MathIllegalStateException;
26 import org.hipparchus.util.FastMath;
27 import org.junit.Assert;
28 import org.junit.Test;
29
30
31
32
33
34
35 public class RootsOfUnityTest {
36
37 @Test(expected = MathIllegalStateException.class)
38 public void testMathIllegalState1() {
39 final RootsOfUnity roots = new RootsOfUnity();
40 roots.getReal(0);
41 }
42
43 @Test(expected = MathIllegalStateException.class)
44 public void testMathIllegalState2() {
45 final RootsOfUnity roots = new RootsOfUnity();
46 roots.getImaginary(0);
47 }
48
49 @Test(expected = MathIllegalStateException.class)
50 public void testMathIllegalState3() {
51 final RootsOfUnity roots = new RootsOfUnity();
52 roots.isCounterClockWise();
53 }
54
55 @Test(expected = MathIllegalArgumentException.class)
56 public void testZeroNumberOfRoots() {
57 final RootsOfUnity roots = new RootsOfUnity();
58 roots.computeRoots(0);
59 }
60
61 @Test
62 public void testGetNumberOfRoots() {
63 final RootsOfUnity roots = new RootsOfUnity();
64 Assert.assertEquals("", 0, roots.getNumberOfRoots());
65 roots.computeRoots(5);
66 Assert.assertEquals("", 5, roots.getNumberOfRoots());
67
68
69
70
71 roots.computeRoots(-5);
72 Assert.assertEquals("", 5, roots.getNumberOfRoots());
73 roots.computeRoots(6);
74 Assert.assertEquals("", 6, roots.getNumberOfRoots());
75 }
76
77 @Test
78 public void testComputeRoots() {
79 final RootsOfUnity roots = new RootsOfUnity();
80 for (int n = -10; n < 11; n++) {
81
82
83
84
85 if (n != 0) {
86 roots.computeRoots(n);
87 doTestComputeRoots(roots);
88 roots.computeRoots(-n);
89 doTestComputeRoots(roots);
90 }
91 }
92 }
93
94 private void doTestComputeRoots(final RootsOfUnity roots) {
95 final int n = roots.isCounterClockWise() ? roots.getNumberOfRoots() :
96 -roots.getNumberOfRoots();
97 final double tol = 10 * Math.ulp(1.0);
98 for (int k = 0; k < n; k++) {
99 final double t = 2.0 * FastMath.PI * k / n;
100 final String msg = String.format("n = %d, k = %d", n, k);
101 Assert.assertEquals(msg, FastMath.cos(t), roots.getReal(k), tol);
102 Assert.assertEquals(msg, FastMath.sin(t), roots.getImaginary(k), tol);
103 }
104 }
105 }