Skip to content

Commit 05064a5

Browse files
committed
assertMatrixEquals method created and updated
1 parent 855d20f commit 05064a5

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/test/java/com/thealgorithms/matrix/MatrixMultiplicationTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77

88
public class MatrixMultiplicationTest {
9+
10+
private static final double EPSILON = 1e-9; // for floating point comparison
11+
12+
913
@Test
1014
void testMultiply2by2(){
1115
double[][] matrixA = {{1.0,2.0},{3.0,4.0}};
@@ -16,4 +20,15 @@ void testMultiply2by2(){
1620
assertMatrixEquals(expected, result); // Because assertEquals can fails due to floating point precision issues, Therfore use assertMatrixEquals
1721
}
1822

23+
private void assertMatrixEquals(double[][] expected, double[][] actual) {
24+
assertEquals(expected.length, actual.length, "Row count mismatch");
25+
for (int i = 0; i < expected.length; i++) {
26+
assertEquals(expected[i].length, actual[i].length, "Column count mismatch at row " + i); // Check if the number of columns in each row matches
27+
for (int j = 0; j < expected[i].length; j++) {
28+
assertEquals(expected[i][j], actual[i][j], EPSILON,
29+
"Mismatch at (" + i + "," + j + ")");
30+
}
31+
}
32+
}
33+
1934
}

0 commit comments

Comments
 (0)