|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
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 SparsityTest {
|
9 | 12 |
|
10 | 13 | private static final double DELTA = 1e-9;
|
11 | 14 |
|
12 |
| - @Test |
13 |
| - public void testAllZeroElements() { |
14 |
| - double[][] mat = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; |
15 |
| - double expectedSparsity = 1.0; |
16 |
| - assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of a matrix with all zero elements should be 1.0"); |
| 15 | + @ParameterizedTest(name = "Test case {index}: {2}") |
| 16 | + @MethodSource("provideTestCases") |
| 17 | + public void testSparsity(double[][] matrix, double expectedSparsity, String description) { |
| 18 | + assertEquals(expectedSparsity, Sparsity.sparsity(matrix), DELTA, description); |
17 | 19 | }
|
18 | 20 |
|
19 |
| - @Test |
20 |
| - public void testNoZeroElements() { |
21 |
| - double[][] mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; |
22 |
| - double expectedSparsity = 0.0; |
23 |
| - assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of a matrix with no zero elements should be 0.0"); |
| 21 | + private static Stream<Arguments> provideTestCases() { |
| 22 | + return Stream.of(Arguments.of(new double[][] {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, 1.0, "Matrix with all zero elements"), Arguments.of(new double[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, 0.0, "Matrix with no zero elements"), |
| 23 | + Arguments.of(new double[][] {{0, 2, 0}, {4, 0, 6}, {0, 8, 0}}, 5.0 / 9.0, "Matrix with mixed elements"), Arguments.of(new double[][] {{0, 1, 0, 2, 0}}, 3.0 / 5.0, "Single-row matrix"), Arguments.of(new double[][] {{1}, {0}, {0}, {2}}, 2.0 / 4.0, "Single-column matrix"), |
| 24 | + Arguments.of(new double[][] {{0}}, 1.0, "Matrix with a single zero element"), Arguments.of(new double[][] {{5}}, 0.0, "Matrix with a single non-zero element")); |
24 | 25 | }
|
25 | 26 |
|
26 |
| - @Test |
27 |
| - public void testMixedElements() { |
28 |
| - double[][] mat = {{0, 2, 0}, {4, 0, 6}, {0, 8, 0}}; |
29 |
| - double expectedSparsity = 5.0 / 9.0; |
30 |
| - assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of the matrix should be 5/9"); |
| 27 | + @ParameterizedTest(name = "Test case {index}: {1}") |
| 28 | + @MethodSource("provideExceptionTestCases") |
| 29 | + public void testSparsityExceptions(double[][] matrix, String description) { |
| 30 | + assertThrows(IllegalArgumentException.class, () -> Sparsity.sparsity(matrix), description); |
31 | 31 | }
|
32 | 32 |
|
33 |
| - @Test |
34 |
| - public void testSingleRowMatrix() { |
35 |
| - double[][] mat = {{0, 1, 0, 2, 0}}; |
36 |
| - double expectedSparsity = 3.0 / 5.0; |
37 |
| - assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of the single-row matrix should be 3/5"); |
38 |
| - } |
39 |
| - |
40 |
| - @Test |
41 |
| - public void testSingleColumnMatrix() { |
42 |
| - double[][] mat = {{1}, {0}, {0}, {2}}; |
43 |
| - double expectedSparsity = 2.0 / 4.0; |
44 |
| - assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of the single-column matrix should be 2/4"); |
45 |
| - } |
46 |
| - |
47 |
| - @Test |
48 |
| - public void testEmptyMatrix() { |
49 |
| - double[][] mat = {}; |
50 |
| - assertThrows(IllegalArgumentException.class, () -> Sparsity.sparsity(mat), "Sparsity of an empty matrix should throw an IllegalArgumentException"); |
51 |
| - } |
52 |
| - |
53 |
| - @Test |
54 |
| - public void testMatrixWithSingleElementZero() { |
55 |
| - double[][] mat = {{0}}; |
56 |
| - double expectedSparsity = 1.0; |
57 |
| - assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of a matrix with a single zero element should be 1.0"); |
58 |
| - } |
59 |
| - |
60 |
| - @Test |
61 |
| - public void testMatrixWithSingleElementNonZero() { |
62 |
| - double[][] mat = {{5}}; |
63 |
| - double expectedSparsity = 0.0; |
64 |
| - assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of a matrix with a single non-zero element should be 0.0"); |
| 33 | + private static Stream<Arguments> provideExceptionTestCases() { |
| 34 | + return Stream.of(Arguments.of(new double[][] {}, "Empty matrix should throw IllegalArgumentException")); |
65 | 35 | }
|
66 | 36 | }
|
0 commit comments