Skip to content

Commit 182b77b

Browse files
committed
Fix
1 parent 9e32e06 commit 182b77b

File tree

1 file changed

+17
-42
lines changed

1 file changed

+17
-42
lines changed

src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,31 @@
33
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
55

6-
import org.junit.jupiter.api.Test;
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;
710

811
public class MatrixTransposeTest {
912

10-
@Test
11-
public void testTransposeSquareMatrix() {
12-
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
13-
14-
int[][] expected = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}};
15-
16-
assertArrayEquals(expected, MatrixTranspose.transpose(matrix), "Transpose of the square matrix is incorrect.");
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"));
1716
}
1817

19-
@Test
20-
public void testTransposeRectangularMatrix() {
21-
int[][] matrix = {{1, 2}, {3, 4}, {5, 6}};
22-
23-
int[][] expected = {{1, 3, 5}, {2, 4, 6}};
24-
25-
assertArrayEquals(expected, MatrixTranspose.transpose(matrix), "Transpose of the rectangular matrix is incorrect.");
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"));
2620
}
2721

28-
@Test
29-
public void testTransposeSingleRowMatrix() {
30-
int[][] matrix = {{1, 2, 3}};
31-
32-
int[][] expected = {{1}, {2}, {3}};
33-
34-
assertArrayEquals(expected, MatrixTranspose.transpose(matrix), "Transpose of the single-row matrix is incorrect.");
35-
}
36-
37-
@Test
38-
public void testTransposeSingleColumnMatrix() {
39-
int[][] matrix = {{1}, {2}, {3}};
40-
41-
int[][] expected = {{1, 2, 3}};
42-
43-
assertArrayEquals(expected, MatrixTranspose.transpose(matrix), "Transpose of the single-column matrix is incorrect.");
44-
}
45-
46-
@Test
47-
public void testTransposeEmptyMatrix() {
48-
int[][] matrix = new int[0][0];
49-
50-
assertThrows(IllegalArgumentException.class, () -> MatrixTranspose.transpose(matrix), "Expected IllegalArgumentException for empty matrix.");
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);
5126
}
5227

53-
@Test
54-
public void testTransposeNullMatrix() {
55-
int[][] matrix = null;
56-
assertThrows(IllegalArgumentException.class, () -> MatrixTranspose.transpose(matrix), "Expected IllegalArgumentException for null matrix.");
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);
5732
}
5833
}

0 commit comments

Comments
 (0)