Skip to content

Commit babefd6

Browse files
authored
Update ChebyshevIterationTest.java
1 parent d66c700 commit babefd6

File tree

1 file changed

+66
-44
lines changed

1 file changed

+66
-44
lines changed

src/test/java/com/thealgorithms/maths/ChebyshevIterationTest.java

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,47 @@ class ChebyshevIterationTest {
2727
private static final double[] M1_EXPECTED = { M1_EXPECTED_X1, M1_EXPECTED_X2 };
2828

2929
// --- Constants for testSolve3x3System ---
30-
private static final double[][] M2_A = { { 5.0, 0.0, 0.0 }, { 0.0, 2.0, 0.0 }, { 0.0, 0.0, 8.0 } };
31-
private static final double[] M2_B = { 10.0, -4.0, 24.0 };
30+
private static final double M2_A11 = 5.0;
31+
private static final double M2_A22 = 2.0;
32+
private static final double M2_A33 = 8.0;
33+
private static final double[][] M2_A = {
34+
{ M2_A11, 0.0, 0.0 },
35+
{ 0.0, M2_A22, 0.0 },
36+
{ 0.0, 0.0, M2_A33 },
37+
};
38+
private static final double M2_B1 = 10.0;
39+
private static final double M2_B2 = -4.0;
40+
private static final double M2_B3 = 24.0;
41+
private static final double[] M2_B = { M2_B1, M2_B2, M2_B3 };
3242
private static final double[] M2_X0 = { 0.0, 0.0, 0.0 };
33-
private static final double[] M2_EXPECTED = { 2.0, -2.0, 3.0 };
43+
private static final double M2_E1 = 2.0;
44+
private static final double M2_E2 = -2.0;
45+
private static final double M2_E3 = 3.0;
46+
private static final double[] M2_EXPECTED = { M2_E1, M2_E2, M2_E3 };
3447
private static final double M2_LAMBDA_MIN = 2.0;
3548
private static final double M2_LAMBDA_MAX = 8.0;
3649

3750
// --- Constants for testAlreadyConverged ---
3851
private static final double M3_LAMBDA_MIN = 2.38;
3952
private static final double M3_LAMBDA_MAX = 4.62;
4053

54+
// --- Constants for Invalid/Dimension Tests ---
55+
private static final double VAL_0_0 = 0.0;
56+
private static final double VAL_0_5 = 0.5;
57+
private static final double VAL_1_0 = 1.0;
58+
private static final double VAL_1_5 = 1.5;
59+
private static final double VAL_2_0 = 2.0;
60+
private static final double[][] M4_A = { { VAL_1_0, VAL_0_0 }, { VAL_0_0, VAL_1_0 } };
61+
private static final double[] M4_B = { VAL_1_0, VAL_1_0 };
62+
private static final double[] M4_X0 = { VAL_0_0, VAL_0_0 };
63+
private static final double[] M5_B = { VAL_1_0, VAL_1_0, VAL_1_0 };
64+
private static final double[][] M6_A = {
65+
{ VAL_1_0, VAL_0_0, VAL_0_0 },
66+
{ VAL_0_0, VAL_1_0, VAL_0_0 },
67+
};
68+
private static final double[] M6_B = { VAL_1_0, VAL_1_0 };
69+
private static final double[] M6_X0 = { VAL_0_0, VAL_0_0 };
70+
4171
// --- General Constants ---
4272
private static final int MAX_ITERATIONS = 100;
4373
private static final double TOLERANCE = 1e-10;
@@ -90,79 +120,71 @@ void testAlreadyConverged() {
90120

91121
@Test
92122
void testInvalidEigenvalues() {
93-
double[][] A = { { 1.0, 0.0 }, { 0.0, 1.0 } };
94-
double[] b = { 1.0, 1.0 };
95-
double[] x0 = { 0.0, 0.0 };
96-
97123
// lambdaMin >= lambdaMax
98124
assertThrows(
99125
IllegalArgumentException.class,
100-
() ->
126+
() -> {
101127
ChebyshevIteration.solve(
102-
A,
103-
b,
104-
x0,
105-
2.0,
106-
1.0,
128+
M4_A,
129+
M4_B,
130+
M4_X0,
131+
VAL_2_0,
132+
VAL_1_0,
107133
TEST_ITERATIONS,
108134
TEST_TOLERANCE
109-
)
135+
);
136+
}
110137
);
111138
// lambdaMin <= 0
112139
assertThrows(
113140
IllegalArgumentException.class,
114-
() ->
141+
() -> {
115142
ChebyshevIteration.solve(
116-
A,
117-
b,
118-
x0,
119-
0.0,
120-
2.0,
143+
M4_A,
144+
M4_B,
145+
M4_X0,
146+
VAL_0_0,
147+
VAL_2_0,
121148
TEST_ITERATIONS,
122149
TEST_TOLERANCE
123-
)
150+
);
151+
}
124152
);
125153
}
126154

127155
@Test
128156
void testMismatchedDimensions() {
129-
double[][] A = { { 1.0, 0.0 }, { 0.0, 1.0 } };
130-
double[] b = { 1.0, 1.0, 1.0 }; // b.length = 3
131-
double[] x0 = { 0.0, 0.0 }; // x0.length = 2
132-
133157
assertThrows(
134158
IllegalArgumentException.class,
135-
() ->
159+
() -> {
136160
ChebyshevIteration.solve(
137-
A,
138-
b,
139-
x0,
140-
0.5,
141-
1.5,
161+
M4_A,
162+
M5_B,
163+
M4_X0,
164+
VAL_0_5,
165+
VAL_1_5,
142166
TEST_ITERATIONS,
143167
TEST_TOLERANCE
144-
)
168+
);
169+
}
145170
);
146171
}
147172

148173
@Test
149174
void testNonSquareMatrix() {
150-
double[][] A = { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 } }; // 2x3 matrix
151-
double[] b = { 1.0, 1.0 };
152-
double[] x0 = { 0.0, 0.0 };
153-
154175
assertThrows(
155176
IllegalArgumentException.class,
156-
() ->
177+
() -> {
157178
ChebyshevIteration.solve(
158-
A,
159-
b,
160-
x0,
161-
0.5,
162-
1.5,
179+
M6_A,
180+
M6_B,
181+
M6_X0,
182+
VAL_0_5,
183+
VAL_1_5,
163184
TEST_ITERATIONS,
164185
TEST_TOLERANCE
165-
)
186+
);
187+
}
166188
);
167189
}
168-
}
190+
}

0 commit comments

Comments
 (0)