- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathMatrixRankTest.java
45 lines (37 loc) · 2.08 KB
/
MatrixRankTest.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
34
35
36
37
38
39
40
41
42
43
44
45
packagecom.thealgorithms.matrix;
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
importstaticorg.junit.jupiter.api.Assertions.assertThrows;
importjava.util.Arrays;
importjava.util.stream.Stream;
importorg.junit.jupiter.params.ParameterizedTest;
importorg.junit.jupiter.params.provider.Arguments;
importorg.junit.jupiter.params.provider.MethodSource;
classMatrixRankTest {
privatestaticStream<Arguments> validInputStream() {
returnStream.of(Arguments.of(3, newdouble[][] {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}), Arguments.of(0, newdouble[][] {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}), Arguments.of(1, newdouble[][] {{1}}), Arguments.of(2, newdouble[][] {{1, 2}, {3, 4}}),
Arguments.of(2, newdouble[][] {{3, -1, 2}, {-3, 1, 2}, {-6, 2, 4}}), Arguments.of(3, newdouble[][] {{2, 3, 0, 1}, {1, 0, 1, 2}, {-1, 1, 1, -2}, {1, 5, 3, -1}}), Arguments.of(1, newdouble[][] {{1, 2, 3}, {3, 6, 9}}),
Arguments.of(2, newdouble[][] {{0.25, 0.5, 0.75, 2}, {1.5, 3, 4.5, 6}, {1, 2, 3, 4}}));
}
privatestaticStream<Arguments> invalidInputStream() {
returnStream.of(Arguments.of((Object) newdouble[][] {{1, 2}, {10}, {100, 200, 300}}), // jagged array
Arguments.of((Object) newdouble[][] {}), // empty matrix
Arguments.of((Object) newdouble[][] {{}, {}}), // empty row
Arguments.of((Object) null), // null matrix
Arguments.of((Object) newdouble[][] {{1, 2}, null}) // null row
);
}
@ParameterizedTest
@MethodSource("validInputStream")
voidcomputeRankTests(intexpectedRank, double[][] matrix) {
intoriginalHashCode = Arrays.deepHashCode(matrix);
intrank = MatrixRank.computeRank(matrix);
intnewHashCode = Arrays.deepHashCode(matrix);
assertEquals(expectedRank, rank);
assertEquals(originalHashCode, newHashCode);
}
@ParameterizedTest
@MethodSource("invalidInputStream")
voidcomputeRankWithInvalidMatrix(double[][] matrix) {
assertThrows(IllegalArgumentException.class, () -> MatrixRank.computeRank(matrix));
}
}