|
| 1 | +package com.thealgorithms.misc; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +public class SparsityTest { |
| 9 | + |
| 10 | + private static final double DELTA = 1e-9; |
| 11 | + |
| 12 | + @Test |
| 13 | + public void testAllZeroElements() { |
| 14 | + double[][] mat = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; |
| 15 | + double expectedSparsity = 1.0; // All elements are zero |
| 16 | + assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of a matrix with all zero elements should be 1.0"); |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + public void testNoZeroElements() { |
| 21 | + double[][] mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; |
| 22 | + double expectedSparsity = 0.0; // No zero elements |
| 23 | + assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of a matrix with no zero elements should be 0.0"); |
| 24 | + } |
| 25 | + |
| 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; // 5 out of 9 elements are zero |
| 30 | + assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of the matrix should be 5/9"); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testSingleRowMatrix() { |
| 35 | + double[][] mat = {{0, 1, 0, 2, 0}}; |
| 36 | + double expectedSparsity = 3.0 / 5.0; // 3 out of 5 elements are zero |
| 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; // 2 out of 4 elements are zero |
| 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; // Only one element which is zero |
| 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; // Only one element which is non-zero |
| 64 | + assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of a matrix with a single non-zero element should be 0.0"); |
| 65 | + } |
| 66 | +} |
0 commit comments