|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
4 | 4 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
5 | 5 |
|
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; |
7 | 10 |
|
8 | 11 | public class MatrixTransposeTest {
|
9 | 12 |
|
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")); |
17 | 16 | }
|
18 | 17 |
|
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")); |
26 | 20 | }
|
27 | 21 |
|
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); |
51 | 26 | }
|
52 | 27 |
|
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); |
57 | 32 | }
|
58 | 33 | }
|
0 commit comments