|
7 | 7 |
|
8 | 8 | public class MatrixMultiplicationTest { |
9 | 9 |
|
10 | | - private static final double EPSILON = 1e-9; // for floating point comparison |
11 | | - |
12 | | - @Test |
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}}; |
16 | | - double[][] expected = {{19.0, 22.0}, {43.0, 50.0}}; |
17 | | - |
18 | | - double[][] result = MatrixMultiplication.multiply(matrixA, matrixB); |
19 | | - assertMatrixEquals(expected, result); // Use custom method due to floating point issues |
20 | | - } |
21 | | - |
22 | | - @Test |
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}}; |
26 | | - double[][] expected = {{23.0}, {53.0}, {83.0}}; |
27 | | - |
28 | | - double[][] result = MatrixMultiplication.multiply(matrixA, matrixB); |
29 | | - assertMatrixEquals(expected, result); |
30 | | - } |
31 | | - |
32 | | - @Test |
33 | | - void testNullMatrixA() { |
34 | | - double[][] b = {{1, 2}, {3, 4}}; |
35 | | - assertThrows(IllegalArgumentException.class, () -> MatrixMultiplication.multiply(null, b)); |
36 | | - } |
37 | | - |
38 | | - @Test |
39 | | - void testNullMatrixB() { |
40 | | - double[][] a = {{1, 2}, {3, 4}}; |
41 | | - assertThrows(IllegalArgumentException.class, () -> MatrixMultiplication.multiply(a, null)); |
42 | | - } |
43 | | - |
44 | | - @Test |
45 | | - void testMultiplyNull() { |
46 | | - double[][] matrixA = {{1.0, 2.0}, {3.0, 4.0}}; |
47 | | - double[][] matrixB = null; |
48 | | - |
49 | | - Exception exception = |
50 | | - assertThrows( |
51 | | - IllegalArgumentException.class, () -> MatrixMultiplication.multiply(matrixA, matrixB)); |
52 | | - |
53 | | - String expectedMessage = "Input matrices cannot be null"; |
54 | | - String actualMessage = exception.getMessage(); |
55 | | - |
56 | | - assertTrue(actualMessage.contains(expectedMessage)); |
57 | | - } |
58 | | - |
59 | | - @Test |
60 | | - void testIncompatibleDimensions() { |
61 | | - double[][] a = {{1.0, 2.0}}; |
62 | | - double[][] b = {{1.0, 2.0}}; |
63 | | - assertThrows(IllegalArgumentException.class, () -> MatrixMultiplication.multiply(a, b)); |
64 | | - } |
65 | | - |
66 | | - @Test |
67 | | - void testEmptyMatrices() { |
68 | | - double[][] a = new double[0][0]; |
69 | | - double[][] b = new double[0][0]; |
70 | | - assertThrows(IllegalArgumentException.class, () -> MatrixMultiplication.multiply(a, b)); |
71 | | - } |
72 | | - |
73 | | - private void assertMatrixEquals(double[][] expected, double[][] actual) { |
74 | | - assertEquals(expected.length, actual.length, "Row count mismatch"); |
75 | | - for (int i = 0; i < expected.length; i++) { |
76 | | - assertEquals(expected[i].length, actual[i].length, "Column count mismatch at row " + i); |
77 | | - for (int j = 0; j < expected[i].length; j++) { |
78 | | - assertEquals(expected[i][j], actual[i][j], EPSILON, "Mismatch at (" + i + "," + j + ")"); |
79 | | - } |
| 10 | + private static final double EPSILON = 1e-9; // for floating point comparison |
| 11 | + |
| 12 | + @Test |
| 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}}; |
| 16 | + double[][] expected = {{19.0, 22.0}, {43.0, 50.0}}; |
| 17 | + |
| 18 | + double[][] result = MatrixMultiplication.multiply(matrixA, matrixB); |
| 19 | + assertMatrixEquals(expected, result); // Use custom method due to floating point issues |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 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}}; |
| 26 | + double[][] expected = {{23.0}, {53.0}, {83.0}}; |
| 27 | + |
| 28 | + double[][] result = MatrixMultiplication.multiply(matrixA, matrixB); |
| 29 | + assertMatrixEquals(expected, result); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void testNullMatrixA() { |
| 34 | + double[][] b = {{1, 2}, {3, 4}}; |
| 35 | + assertThrows(IllegalArgumentException.class, |
| 36 | + () -> MatrixMultiplication.multiply(null, b)); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void testNullMatrixB() { |
| 41 | + double[][] a = {{1, 2}, {3, 4}}; |
| 42 | + assertThrows(IllegalArgumentException.class, |
| 43 | + () -> MatrixMultiplication.multiply(a, null)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void testMultiplyNull() { |
| 48 | + double[][] matrixA = {{1.0, 2.0}, {3.0, 4.0}}; |
| 49 | + double[][] matrixB = null; |
| 50 | + |
| 51 | + Exception exception = assertThrows(IllegalArgumentException.class, |
| 52 | + () -> MatrixMultiplication.multiply(matrixA, matrixB)); |
| 53 | + |
| 54 | + String expectedMessage = "Input matrices cannot be null"; |
| 55 | + String actualMessage = exception.getMessage(); |
| 56 | + |
| 57 | + assertTrue(actualMessage.contains(expectedMessage)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void testIncompatibleDimensions() { |
| 62 | + double[][] a = {{1.0, 2.0}}; |
| 63 | + double[][] b = {{1.0, 2.0}}; |
| 64 | + assertThrows(IllegalArgumentException.class, |
| 65 | + () -> MatrixMultiplication.multiply(a, b)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void testEmptyMatrices() { |
| 70 | + double[][] a = new double[0][0]; |
| 71 | + double[][] b = new double[0][0]; |
| 72 | + assertThrows(IllegalArgumentException.class, |
| 73 | + () -> MatrixMultiplication.multiply(a, b)); |
| 74 | + } |
| 75 | + |
| 76 | + private void assertMatrixEquals(double[][] expected, double[][] actual) { |
| 77 | + assertEquals(expected.length, actual.length, "Row count mismatch"); |
| 78 | + for (int i = 0; i < expected.length; i++) { |
| 79 | + assertEquals(expected[i].length, actual[i].length, "Column count mismatch at row " + i); |
| 80 | + for (int j = 0; j < expected[i].length; j++) { |
| 81 | + assertEquals(expected[i][j], actual[i][j], EPSILON, "Mismatch at (" + i + "," + j + ")"); |
| 82 | + } |
| 83 | + } |
80 | 84 | } |
81 | | - } |
82 | 85 | } |
0 commit comments