From 7abf5f77339ca3a5d56e84ff57e1f0d85f577016 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 13 Oct 2024 16:02:57 +0530 Subject: [PATCH 1/5] Add tests, remove `main` in `EulerMethod` --- .../thealgorithms/misc/MatrixTranspose.java | 73 +++++-------------- .../misc/MatrixTransposeTest.java | 59 +++++++++++++++ 2 files changed, 77 insertions(+), 55 deletions(-) create mode 100644 src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java diff --git a/src/main/java/com/thealgorithms/misc/MatrixTranspose.java b/src/main/java/com/thealgorithms/misc/MatrixTranspose.java index 153cf4e9df99..61f2fa1c7d25 100644 --- a/src/main/java/com/thealgorithms/misc/MatrixTranspose.java +++ b/src/main/java/com/thealgorithms/misc/MatrixTranspose.java @@ -1,7 +1,5 @@ package com.thealgorithms.misc; -import java.util.Scanner; - /** * * @@ -22,62 +20,27 @@ public final class MatrixTranspose { private MatrixTranspose() { } - public static void main(String[] args) { - /* - * This is the main method - * - * @param args Unused. - * - * @return Nothing. - */ - Scanner sc = new Scanner(System.in); - int i; - int j; - int row; - int column; - System.out.println("Enter the number of rows in the 2D matrix:"); - - /* - * Take input from user for how many rows to be print - */ - row = sc.nextInt(); - - System.out.println("Enter the number of columns in the 2D matrix:"); - - /* - * Take input from user for how many coloumn to be print - */ - column = sc.nextInt(); - int[][] arr = new int[row][column]; - System.out.println("Enter the elements"); - for (i = 0; i < row; i++) { - for (j = 0; j < column; j++) { - arr[i][j] = sc.nextInt(); - } - } - - /* - * Print matrix before the Transpose in proper way - */ - System.out.println("The matrix is:"); - for (i = 0; i < row; i++) { - for (j = 0; j < column; j++) { - System.out.print(arr[i][j] + "\t"); - } - System.out.print("\n"); + /** + * Calculate the transpose of the given matrix. + * + * @param matrix The matrix to be transposed + * @throws IllegalArgumentException if the matrix is empty + * @throws NullPointerException if the matrix is null + * @return The transposed matrix + */ + public static int[][] transpose(int[][] matrix) { + if (matrix.length == 0 || matrix == null) { + throw new IllegalArgumentException("Matrix is empty"); } - /* - * Print matrix after the tranpose in proper way Transpose means Interchanging - * of rows wth column so we interchange the rows in next loop Thus at last - * matrix of transpose is obtained through user input... - */ - System.out.println("The Transpose of the given matrix is:"); - for (i = 0; i < column; i++) { - for (j = 0; j < row; j++) { - System.out.print(arr[j][i] + "\t"); + int rows = matrix.length; + int cols = matrix[0].length; + int[][] transposedMatrix = new int[cols][rows]; + for (int i = 0; i < cols; i++) { + for (int j = 0; j < rows; j++) { + transposedMatrix[i][j] = matrix[j][i]; } - System.out.print("\n"); } + return transposedMatrix; } } diff --git a/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java b/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java new file mode 100644 index 000000000000..d0ba1432bbd7 --- /dev/null +++ b/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java @@ -0,0 +1,59 @@ +package com.thealgorithms.misc; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +public class MatrixTransposeTest { + + @Test + public void testTransposeSquareMatrix() { + int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; + + int[][] expected = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}}; + + assertArrayEquals(expected, MatrixTranspose.transpose(matrix), "Transpose of the square matrix is incorrect."); + } + + @Test + public void testTransposeRectangularMatrix() { + int[][] matrix = {{1, 2}, {3, 4}, {5, 6}}; + + int[][] expected = {{1, 3, 5}, {2, 4, 6}}; + + assertArrayEquals(expected, MatrixTranspose.transpose(matrix), "Transpose of the rectangular matrix is incorrect."); + } + + @Test + public void testTransposeSingleRowMatrix() { + int[][] matrix = {{1, 2, 3}}; + + int[][] expected = {{1}, {2}, {3}}; + + assertArrayEquals(expected, MatrixTranspose.transpose(matrix), "Transpose of the single-row matrix is incorrect."); + } + + @Test + public void testTransposeSingleColumnMatrix() { + int[][] matrix = {{1}, {2}, {3}}; + + int[][] expected = {{1, 2, 3}}; + + assertArrayEquals(expected, MatrixTranspose.transpose(matrix), "Transpose of the single-column matrix is incorrect."); + } + + @Test + public void testTransposeEmptyMatrix() { + int[][] matrix = new int[0][0]; + + assertThrows(IllegalArgumentException.class, () -> MatrixTranspose.transpose(matrix), "Expected IllegalArgumentException for empty matrix."); + } + + @Test + public void testTransposeNullMatrix() { + int[][] matrix = null; + + assertThrows(NullPointerException.class, () -> MatrixTranspose.transpose(matrix), "Expected IllegalArgumentException for null matrix."); + } +} From ea4c330ee09b0e0b960301aca40b20c4459f8b9d Mon Sep 17 00:00:00 2001 From: Hardvan Date: Sun, 13 Oct 2024 10:33:15 +0000 Subject: [PATCH 2/5] Update directory --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 06307d4aca78..89e6738b9426 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -991,6 +991,7 @@ * [ColorContrastRatioTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ColorContrastRatioTest.java) * [InverseOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/InverseOfMatrixTest.java) * [MapReduceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MapReduceTest.java) + * [MatrixTransposeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java) * [MedianOfMatrixtest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfMatrixtest.java) * [MedianOfRunningArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java) * [MirrorOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java) From f94a56a52a553310c8027d3541389afe02ba15b5 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 13 Oct 2024 20:07:04 +0530 Subject: [PATCH 3/5] Fix --- src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java b/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java index d0ba1432bbd7..c45181be19d1 100644 --- a/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java +++ b/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java @@ -53,7 +53,6 @@ public void testTransposeEmptyMatrix() { @Test public void testTransposeNullMatrix() { int[][] matrix = null; - assertThrows(NullPointerException.class, () -> MatrixTranspose.transpose(matrix), "Expected IllegalArgumentException for null matrix."); } } From 9e32e06ff68c766a833c40bd2ef2da70fee872f5 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 13 Oct 2024 20:11:04 +0530 Subject: [PATCH 4/5] Fix --- src/main/java/com/thealgorithms/misc/MatrixTranspose.java | 2 +- src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/misc/MatrixTranspose.java b/src/main/java/com/thealgorithms/misc/MatrixTranspose.java index 61f2fa1c7d25..743682780b01 100644 --- a/src/main/java/com/thealgorithms/misc/MatrixTranspose.java +++ b/src/main/java/com/thealgorithms/misc/MatrixTranspose.java @@ -29,7 +29,7 @@ private MatrixTranspose() { * @return The transposed matrix */ public static int[][] transpose(int[][] matrix) { - if (matrix.length == 0 || matrix == null) { + if (matrix == null || matrix.length == 0) { throw new IllegalArgumentException("Matrix is empty"); } diff --git a/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java b/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java index c45181be19d1..9658d4bfa1cf 100644 --- a/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java +++ b/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java @@ -53,6 +53,6 @@ public void testTransposeEmptyMatrix() { @Test public void testTransposeNullMatrix() { int[][] matrix = null; - assertThrows(NullPointerException.class, () -> MatrixTranspose.transpose(matrix), "Expected IllegalArgumentException for null matrix."); + assertThrows(IllegalArgumentException.class, () -> MatrixTranspose.transpose(matrix), "Expected IllegalArgumentException for null matrix."); } } From 182b77b31099503ceed4a6bc2f87d31360113be8 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Mon, 14 Oct 2024 08:47:56 +0530 Subject: [PATCH 5/5] Fix --- .../misc/MatrixTransposeTest.java | 59 ++++++------------- 1 file changed, 17 insertions(+), 42 deletions(-) diff --git a/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java b/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java index 9658d4bfa1cf..cf668807b819 100644 --- a/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java +++ b/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java @@ -3,56 +3,31 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import org.junit.jupiter.api.Test; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; public class MatrixTransposeTest { - @Test - public void testTransposeSquareMatrix() { - int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; - - int[][] expected = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}}; - - assertArrayEquals(expected, MatrixTranspose.transpose(matrix), "Transpose of the square matrix is incorrect."); + private static Stream provideValidMatrixTestCases() { + return Stream.of(Arguments.of(new int[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, new int[][] {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}}, "Transpose of square matrix"), Arguments.of(new int[][] {{1, 2}, {3, 4}, {5, 6}}, new int[][] {{1, 3, 5}, {2, 4, 6}}, "Transpose of rectangular matrix"), + Arguments.of(new int[][] {{1, 2, 3}}, new int[][] {{1}, {2}, {3}}, "Transpose of single-row matrix"), Arguments.of(new int[][] {{1}, {2}, {3}}, new int[][] {{1, 2, 3}}, "Transpose of single-column matrix")); } - @Test - public void testTransposeRectangularMatrix() { - int[][] matrix = {{1, 2}, {3, 4}, {5, 6}}; - - int[][] expected = {{1, 3, 5}, {2, 4, 6}}; - - assertArrayEquals(expected, MatrixTranspose.transpose(matrix), "Transpose of the rectangular matrix is incorrect."); + private static Stream provideInvalidMatrixTestCases() { + return Stream.of(Arguments.of(new int[0][0], "Empty matrix should throw IllegalArgumentException"), Arguments.of(null, "Null matrix should throw IllegalArgumentException")); } - @Test - public void testTransposeSingleRowMatrix() { - int[][] matrix = {{1, 2, 3}}; - - int[][] expected = {{1}, {2}, {3}}; - - assertArrayEquals(expected, MatrixTranspose.transpose(matrix), "Transpose of the single-row matrix is incorrect."); - } - - @Test - public void testTransposeSingleColumnMatrix() { - int[][] matrix = {{1}, {2}, {3}}; - - int[][] expected = {{1, 2, 3}}; - - assertArrayEquals(expected, MatrixTranspose.transpose(matrix), "Transpose of the single-column matrix is incorrect."); - } - - @Test - public void testTransposeEmptyMatrix() { - int[][] matrix = new int[0][0]; - - assertThrows(IllegalArgumentException.class, () -> MatrixTranspose.transpose(matrix), "Expected IllegalArgumentException for empty matrix."); + @ParameterizedTest(name = "Test case {index}: {2}") + @MethodSource("provideValidMatrixTestCases") + void testValidMatrixTranspose(int[][] input, int[][] expected, String description) { + assertArrayEquals(expected, MatrixTranspose.transpose(input), description); } - @Test - public void testTransposeNullMatrix() { - int[][] matrix = null; - assertThrows(IllegalArgumentException.class, () -> MatrixTranspose.transpose(matrix), "Expected IllegalArgumentException for null matrix."); + @ParameterizedTest(name = "Test case {index}: {1}") + @MethodSource("provideInvalidMatrixTestCases") + void testInvalidMatrixTranspose(int[][] input, String description) { + assertThrows(IllegalArgumentException.class, () -> MatrixTranspose.transpose(input), description); } }