Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/maths/MatrixRankTest.java (Java)

Code before:
    @Test
    void computeRank() {

        double[][] identityMatrix = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
        assertEquals(3, MatrixRank.computeRank(identityMatrix));

        double[][] zeroMatrix = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
        assertEquals(0, MatrixRank.computeRank(zeroMatrix));

        double[][] square1x1Matrix = {{1}};
        assertEquals(1, MatrixRank.computeRank(square1x1Matrix));

        double[][] square2x2Matrix = {{1, 2}, {3, 4}};
        assertEquals(2, MatrixRank.computeRank(square2x2Matrix));

        double[][] square3x3Matrix = {{3, -1, 2}, {-3, 1, 2}, {-6, 2, 4}};
        assertEquals(2, MatrixRank.computeRank(square3x3Matrix));

        double[][] square4x4Matrix = {{2, 3, 0, 1}, {1, 0, 1, 2}, {-1, 1, 1, -2}, {1, 5, 3, -1}};
        assertEquals(3, MatrixRank.computeRank(square4x4Matrix));

        double[][] rectangular2X3Matrix = {{1, 2, 3}, {3, 6, 9}};
        assertEquals(1, MatrixRank.computeRank(rectangular2X3Matrix));

        double[][] rectangular3X4Matrix = {{0.25, 0.5, 0.75, 2}, {1.5, 3, 4.5, 6}, {1, 2, 3, 4}};
        assertEquals(2, MatrixRank.computeRank(rectangular3X4Matrix));
    }
}


Review comment (suggestion):
Try to express your tests as _parameterized test_, cf. [`FindMaxRecursionTest.java`](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FindMaxRecursionTest.java).

Code after:
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class MatrixRankTest {

    private static Stream<Arguments> validInputStream() {
        return Stream.of(Arguments.of(3, new double[][] {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}), Arguments.of(0, new double[][] {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}), Arguments.of(1, new double[][] {{1}}), Arguments.of(2, new double[][] {{1, 2}, {3, 4}}),
            Arguments.of(2, new double[][] {{3, -1, 2}, {-3, 1, 2}, {-6, 2, 4}}), Arguments.of(3, new double[][] {{2, 3, 0, 1}, {1, 0, 1, 2}, {-1, 1, 1, -2}, {1, 5, 3, -1}}), Arguments.of(1, new double[][] {{1, 2, 3}, {3, 6, 9}}),
            Arguments.of(2, new double[][] {{0.25, 0.5, 0.75, 2}, {1.5, 3, 4.5, 6}, {1, 2, 3, 4}}));
    }

    private static Stream<Arguments> invalidInputStream() {
        return Stream.of(Arguments.of((Object) new double[][] {{1, 2}, {10}, {100, 200, 300}}), // jagged array
            Arguments.of((Object) new double[][] {}), // empty matrix
            Arguments.of((Object) new double[][] {{}, {}}), // empty row
            Arguments.of((Object) null), // null matrix
            Arguments.of((Object) new double[][] {{1, 2}, null}) // null row
        );
    }

    @ParameterizedTest
    @MethodSource("validInputStream")
    void computeRankTests(int expectedRank, double[][] matrix) {
        int originalHashCode = Arrays.deepHashCode(matrix);
        int rank = MatrixRank.computeRank(matrix);
        int newHashCode = Arrays.deepHashCode(matrix);

        assertEquals(expectedRank, rank);
        assertEquals(originalHashCode, newHashCode);
    }

    @ParameterizedTest
    @MethodSource("invalidInputStream")
    void computeRankWithInvalidMatrix(double[][] matrix) {
        assertThrows(IllegalArgumentException.class, () -> MatrixRank.computeRank(matrix));
    }
}