-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathMatrixTransposeTest.java
33 lines (26 loc) · 1.77 KB
/
MatrixTransposeTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.thealgorithms.matrix;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
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 {
private static Stream<Arguments> 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"));
}
private static Stream<Arguments> provideInvalidMatrixTestCases() {
return Stream.of(Arguments.of(new int[0][0], "Empty matrix should throw IllegalArgumentException"), Arguments.of(null, "Null matrix should throw IllegalArgumentException"));
}
@ParameterizedTest(name = "Test case {index}: {2}")
@MethodSource("provideValidMatrixTestCases")
void testValidMatrixTranspose(int[][] input, int[][] expected, String description) {
assertArrayEquals(expected, MatrixTranspose.transpose(input), description);
}
@ParameterizedTest(name = "Test case {index}: {1}")
@MethodSource("provideInvalidMatrixTestCases")
void testInvalidMatrixTranspose(int[][] input, String description) {
assertThrows(IllegalArgumentException.class, () -> MatrixTranspose.transpose(input), description);
}
}