22
33import org .junit .jupiter .api .Test ;
44import static org .junit .jupiter .api .Assertions .assertThrows ;
5- import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
65import static org .junit .jupiter .api .Assertions .assertEquals ;
7-
8- import static org .junit .jupiter .api .Assertions .*;
9-
6+ import static org .junit .jupiter .api .Assertions .assertTrue ;
107
118public class MatrixMultiplicationTest {
129
1310 private static final double EPSILON = 1e-9 ; // for floating point comparison
1411
15-
1612 @ Test
17- void testMultiply2by2 (){
18- double [][] matrixA = {{1.0 ,2.0 },{3.0 ,4.0 }};
19- double [][] matrixB = {{5.0 ,6.0 },{7.0 ,8.0 }};
13+ void testMultiply2by2 () {
14+ double [][] matrixA = {{1.0 , 2.0 }, {3.0 , 4.0 }};
15+ double [][] matrixB = {{5.0 , 6.0 }, {7.0 , 8.0 }};
2016 double [][] expected = {{19.0 , 22.0 }, {43.0 , 50.0 }};
2117
2218 double [][] result = MatrixMultiplication .multiply (matrixA , matrixB );
23- assertMatrixEquals (expected , result ); // Because assertEquals can fails due to floating point precision issues, Therfore use assertMatrixEquals
19+ assertMatrixEquals (expected , result ); // Use custom method due to floating point issues
2420 }
2521
2622 @ Test
27- void testMultiply3by2and2by1 (){
28- double [][] matrixA = {{1.0 ,2.0 },{3.0 ,4.0 },{5.0 ,6.0 }};
29- double [][] matrixB = {{7.0 },{8.0 }};
23+ void testMultiply3by2and2by1 () {
24+ double [][] matrixA = {{1.0 , 2.0 }, {3.0 , 4.0 }, {5.0 , 6.0 }};
25+ double [][] matrixB = {{7.0 }, {8.0 }};
3026 double [][] expected = {{23.0 }, {53.0 }, {83.0 }};
3127
3228 double [][] result = MatrixMultiplication .multiply (matrixA , matrixB );
@@ -35,61 +31,51 @@ void testMultiply3by2and2by1(){
3531
3632 @ Test
3733 void testNullMatrixA () {
38- double [][] B = {{1 , 2 }, {3 , 4 }};
39- assertThrows (IllegalArgumentException .class , () -> MatrixMultiplication .multiply (null , B ));
34+ double [][] b = {{1 , 2 }, {3 , 4 }};
35+ assertThrows (IllegalArgumentException .class , () -> MatrixMultiplication .multiply (null , b ));
4036 }
4137
4238 @ Test
4339 void testNullMatrixB () {
44- double [][] A = {{1 , 2 }, {3 , 4 }};
45- assertThrows (IllegalArgumentException .class , () -> MatrixMultiplication .multiply (A , null ));
40+ double [][] a = {{1 , 2 }, {3 , 4 }};
41+ assertThrows (IllegalArgumentException .class , () -> MatrixMultiplication .multiply (a , null ));
4642 }
4743
48-
4944 @ Test
50- void testMultiplyNull (){
51- double [][] matrixA = {{1.0 ,2.0 },{3.0 ,4.0 }};
45+ void testMultiplyNull () {
46+ double [][] matrixA = {{1.0 , 2.0 }, {3.0 , 4.0 }};
5247 double [][] matrixB = null ;
5348
54- Exception exception = assertThrows (IllegalArgumentException .class , () -> {
55- MatrixMultiplication .multiply (matrixA , matrixB );
56- });
49+ Exception exception = assertThrows (IllegalArgumentException .class ,
50+ () -> MatrixMultiplication .multiply (matrixA , matrixB ));
5751
5852 String expectedMessage = "Input matrices cannot be null" ;
5953 String actualMessage = exception .getMessage ();
6054
6155 assertTrue (actualMessage .contains (expectedMessage ));
6256 }
6357
64-
6558 @ Test
6659 void testIncompatibleDimensions () {
67- double [][] A = {
68- {1.0 , 2.0 }
69- };
70- double [][] B = {
71- {1.0 , 2.0 }
72- };
73- assertThrows (IllegalArgumentException .class , () -> MatrixMultiplication .multiply (A , B ));
60+ double [][] a = {{1.0 , 2.0 }};
61+ double [][] b = {{1.0 , 2.0 }};
62+ assertThrows (IllegalArgumentException .class , () -> MatrixMultiplication .multiply (a , b ));
7463 }
7564
7665 @ Test
7766 void testEmptyMatrices () {
78- double [][] A = new double [0 ][0 ];
79- double [][] B = new double [0 ][0 ];
80- assertThrows (IllegalArgumentException .class , () -> MatrixMultiplication .multiply (A , B ));
67+ double [][] a = new double [0 ][0 ];
68+ double [][] b = new double [0 ][0 ];
69+ assertThrows (IllegalArgumentException .class , () -> MatrixMultiplication .multiply (a , b ));
8170 }
8271
83-
8472 private void assertMatrixEquals (double [][] expected , double [][] actual ) {
8573 assertEquals (expected .length , actual .length , "Row count mismatch" );
8674 for (int i = 0 ; i < expected .length ; i ++) {
87- assertEquals (expected [i ].length , actual [i ].length , "Column count mismatch at row " + i ); // Check if the number of columns in each row matches
75+ assertEquals (expected [i ].length , actual [i ].length , "Column count mismatch at row " + i );
8876 for (int j = 0 ; j < expected [i ].length ; j ++) {
89- assertEquals (expected [i ][j ], actual [i ][j ], EPSILON ,
90- "Mismatch at (" + i + "," + j + ")" );
77+ assertEquals (expected [i ][j ], actual [i ][j ], EPSILON , "Mismatch at (" + i + "," + j + ")" );
9178 }
9279 }
9380 }
94-
9581}
0 commit comments