Skip to content

Commit 8d88349

Browse files
authored
Add tests, remove main in EulerMethod (#5771)
1 parent e19378d commit 8d88349

File tree

3 files changed

+52
-55
lines changed

3 files changed

+52
-55
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,7 @@
10141014
* [ColorContrastRatioTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ColorContrastRatioTest.java)
10151015
* [InverseOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/InverseOfMatrixTest.java)
10161016
* [MapReduceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MapReduceTest.java)
1017+
* [MatrixTransposeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java)
10171018
* [MedianOfMatrixtest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfMatrixtest.java)
10181019
* [MedianOfRunningArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java)
10191020
* [MirrorOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java)
Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.misc;
22

3-
import java.util.Scanner;
4-
53
/**
64
*
75
*
@@ -22,62 +20,27 @@ public final class MatrixTranspose {
2220
private MatrixTranspose() {
2321
}
2422

25-
public static void main(String[] args) {
26-
/*
27-
* This is the main method
28-
*
29-
* @param args Unused.
30-
*
31-
* @return Nothing.
32-
*/
33-
Scanner sc = new Scanner(System.in);
34-
int i;
35-
int j;
36-
int row;
37-
int column;
38-
System.out.println("Enter the number of rows in the 2D matrix:");
39-
40-
/*
41-
* Take input from user for how many rows to be print
42-
*/
43-
row = sc.nextInt();
44-
45-
System.out.println("Enter the number of columns in the 2D matrix:");
46-
47-
/*
48-
* Take input from user for how many coloumn to be print
49-
*/
50-
column = sc.nextInt();
51-
int[][] arr = new int[row][column];
52-
System.out.println("Enter the elements");
53-
for (i = 0; i < row; i++) {
54-
for (j = 0; j < column; j++) {
55-
arr[i][j] = sc.nextInt();
56-
}
57-
}
58-
59-
/*
60-
* Print matrix before the Transpose in proper way
61-
*/
62-
System.out.println("The matrix is:");
63-
for (i = 0; i < row; i++) {
64-
for (j = 0; j < column; j++) {
65-
System.out.print(arr[i][j] + "\t");
66-
}
67-
System.out.print("\n");
23+
/**
24+
* Calculate the transpose of the given matrix.
25+
*
26+
* @param matrix The matrix to be transposed
27+
* @throws IllegalArgumentException if the matrix is empty
28+
* @throws NullPointerException if the matrix is null
29+
* @return The transposed matrix
30+
*/
31+
public static int[][] transpose(int[][] matrix) {
32+
if (matrix == null || matrix.length == 0) {
33+
throw new IllegalArgumentException("Matrix is empty");
6834
}
6935

70-
/*
71-
* Print matrix after the tranpose in proper way Transpose means Interchanging
72-
* of rows wth column so we interchange the rows in next loop Thus at last
73-
* matrix of transpose is obtained through user input...
74-
*/
75-
System.out.println("The Transpose of the given matrix is:");
76-
for (i = 0; i < column; i++) {
77-
for (j = 0; j < row; j++) {
78-
System.out.print(arr[j][i] + "\t");
36+
int rows = matrix.length;
37+
int cols = matrix[0].length;
38+
int[][] transposedMatrix = new int[cols][rows];
39+
for (int i = 0; i < cols; i++) {
40+
for (int j = 0; j < rows; j++) {
41+
transposedMatrix[i][j] = matrix[j][i];
7942
}
80-
System.out.print("\n");
8143
}
44+
return transposedMatrix;
8245
}
8346
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.misc;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
public class MatrixTransposeTest {
12+
13+
private static Stream<Arguments> provideValidMatrixTestCases() {
14+
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"),
15+
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"));
16+
}
17+
18+
private static Stream<Arguments> provideInvalidMatrixTestCases() {
19+
return Stream.of(Arguments.of(new int[0][0], "Empty matrix should throw IllegalArgumentException"), Arguments.of(null, "Null matrix should throw IllegalArgumentException"));
20+
}
21+
22+
@ParameterizedTest(name = "Test case {index}: {2}")
23+
@MethodSource("provideValidMatrixTestCases")
24+
void testValidMatrixTranspose(int[][] input, int[][] expected, String description) {
25+
assertArrayEquals(expected, MatrixTranspose.transpose(input), description);
26+
}
27+
28+
@ParameterizedTest(name = "Test case {index}: {1}")
29+
@MethodSource("provideInvalidMatrixTestCases")
30+
void testInvalidMatrixTranspose(int[][] input, String description) {
31+
assertThrows(IllegalArgumentException.class, () -> MatrixTranspose.transpose(input), description);
32+
}
33+
}

0 commit comments

Comments
 (0)