|
5 | 5 |
|
6 | 6 | import org.junit.jupiter.api.Test; |
7 | 7 |
|
| 8 | +/** |
| 9 | + * Unit tests for ChebyshevIteration class |
| 10 | + */ |
8 | 11 | class ChebyshevIterationTest { |
9 | 12 |
|
10 | | - private static final double[][] M1_A = { { 4.0, 1.0 }, { 1.0, 3.0 } }; |
11 | | - private static final double[] M1_B = { 1.0, 2.0 }; |
12 | | - private static final double[] M1_X0 = { 0.0, 0.0 }; |
| 13 | + private static final double[][] M1_A = {{4.0, 1.0}, {1.0, 3.0}}; |
| 14 | + private static final double[] M1_B = {1.0, 2.0}; |
| 15 | + private static final double[] M1_X0 = {0.0, 0.0}; |
13 | 16 | private static final double M1_LAMBDA_MIN = (7.0 - Math.sqrt(5.0)) / 2.0; |
14 | 17 | private static final double M1_LAMBDA_MAX = (7.0 + Math.sqrt(5.0)) / 2.0; |
15 | | - private static final double[] M1_EXPECTED = { 1.0 / 11.0, 7.0 / 11.0 }; |
| 18 | + private static final double[] M1_EXPECTED = {1.0 / 11.0, 7.0 / 11.0}; |
16 | 19 |
|
17 | | - private static final double[][] M2_A = { { 5.0, 0.0, 0.0 }, { 0.0, 2.0, 0.0 }, { 0.0, 0.0, 8.0 } }; |
18 | | - private static final double[] M2_B = { 10.0, -4.0, 24.0 }; |
19 | | - private static final double[] M2_X0 = { 0.0, 0.0, 0.0 }; |
20 | | - private static final double[] M2_EXPECTED = { 2.0, -2.0, 3.0 }; |
| 20 | + private static final double[][] M2_A = {{5.0, 0.0, 0.0}, {0.0, 2.0, 0.0}, {0.0, 0.0, 8.0}}; |
| 21 | + private static final double[] M2_B = {10.0, -4.0, 24.0}; |
| 22 | + private static final double[] M2_X0 = {0.0, 0.0, 0.0}; |
| 23 | + private static final double[] M2_EXPECTED = {2.0, -2.0, 3.0}; |
21 | 24 | private static final double M2_LAMBDA_MIN = 2.0; |
22 | 25 | private static final double M2_LAMBDA_MAX = 8.0; |
23 | 26 |
|
24 | | - private static final double MAX_ITERATIONS = 100; |
| 27 | + private static final int MAX_ITERATIONS = 1000; |
25 | 28 | private static final double TOLERANCE = 1e-10; |
26 | 29 | private static final double ASSERT_TOLERANCE = 1e-9; |
27 | 30 |
|
28 | 31 | @Test |
29 | | - void testSolveSimple2x2System() { |
30 | | - double[] solution = ChebyshevIteration.solve(M1_A, M1_B, M1_X0, M1_LAMBDA_MIN, M1_LAMBDA_MAX, 100, TOLERANCE); |
| 32 | + void testSolve2x2System() { |
| 33 | + double[] solution = ChebyshevIteration.solve( |
| 34 | + M1_A, M1_B, M1_X0, M1_LAMBDA_MIN, M1_LAMBDA_MAX, MAX_ITERATIONS, TOLERANCE); |
31 | 35 | assertArrayEquals(M1_EXPECTED, solution, ASSERT_TOLERANCE); |
32 | 36 | } |
33 | 37 |
|
34 | 38 | @Test |
35 | 39 | void testSolve3x3System() { |
36 | | - double[] solution = ChebyshevIteration.solve(M2_A, M2_B, M2_X0, M2_LAMBDA_MIN, M2_LAMBDA_MAX, 100, TOLERANCE); |
| 40 | + double[] solution = ChebyshevIteration.solve( |
| 41 | + M2_A, M2_B, M2_X0, M2_LAMBDA_MIN, M2_LAMBDA_MAX, MAX_ITERATIONS, TOLERANCE); |
37 | 42 | assertArrayEquals(M2_EXPECTED, solution, ASSERT_TOLERANCE); |
38 | 43 | } |
39 | 44 |
|
40 | 45 | @Test |
41 | 46 | void testAlreadyConverged() { |
42 | | - double[] solution = ChebyshevIteration.solve(M1_A, M1_B, M1_EXPECTED, M1_LAMBDA_MIN, M1_LAMBDA_MAX, 100, TOLERANCE); |
| 47 | + double[] solution = ChebyshevIteration.solve( |
| 48 | + M1_A, M1_B, M1_EXPECTED, M1_LAMBDA_MIN, M1_LAMBDA_MAX, MAX_ITERATIONS, TOLERANCE); |
43 | 49 | assertArrayEquals(M1_EXPECTED, solution, ASSERT_TOLERANCE); |
44 | 50 | } |
45 | 51 |
|
46 | 52 | @Test |
47 | 53 | void testInvalidEigenvalues() { |
48 | | - assertThrows(IllegalArgumentException.class, |
49 | | - () -> ChebyshevIteration.solve(M1_A, M1_B, M1_X0, 2.0, 1.0, 10, 1e-5)); |
50 | | - assertThrows(IllegalArgumentException.class, |
51 | | - () -> ChebyshevIteration.solve(M1_A, M1_B, M1_X0, 0.0, 2.0, 10, 1e-5)); |
| 54 | + assertThrows(IllegalArgumentException.class, () -> |
| 55 | + ChebyshevIteration.solve(M1_A, M1_B, M1_X0, 2.0, 1.0, 10, 1e-5)); |
| 56 | + assertThrows(IllegalArgumentException.class, () -> |
| 57 | + ChebyshevIteration.solve(M1_A, M1_B, M1_X0, 0.0, 2.0, 10, 1e-5)); |
52 | 58 | } |
53 | 59 | } |
0 commit comments