Skip to content

Commit 6c35f05

Browse files
committed
Add tests, remove main, fix bug in RangeInSortedArray
1 parent 596c614 commit 6c35f05

File tree

2 files changed

+71
-22
lines changed

2 files changed

+71
-22
lines changed
Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.misc;
22

3-
import java.util.Scanner;
4-
53
/*
64
*A matrix is sparse if many of its coefficients are zero (In general if 2/3rd of matrix elements
75
*are 0, it is considered as sparse). The interest in sparsity arises because its exploitation can
@@ -22,6 +20,10 @@ private Sparsity() {
2220
*
2321
*/
2422
static double sparsity(double[][] mat) {
23+
if (mat == null || mat.length == 0) {
24+
throw new IllegalArgumentException("Matrix cannot be null or empty");
25+
}
26+
2527
int zero = 0;
2628
// Traversing the matrix to count number of zeroes
2729
for (int i = 0; i < mat.length; i++) {
@@ -32,25 +34,6 @@ static double sparsity(double[][] mat) {
3234
}
3335
}
3436
// return sparsity
35-
return ((double) zero / (mat.length * mat[1].length));
36-
}
37-
38-
// Driver method
39-
public static void main(String[] args) {
40-
Scanner in = new Scanner(System.in);
41-
System.out.println("Enter number of rows in matrix: ");
42-
int n = in.nextInt();
43-
System.out.println("Enter number of Columns in matrix: ");
44-
int m = in.nextInt();
45-
46-
System.out.println("Enter Matrix elements: ");
47-
double[][] mat = new double[n][m];
48-
for (int i = 0; i < n; i++) {
49-
for (int j = 0; j < m; j++) {
50-
mat[i][j] = in.nextDouble();
51-
}
52-
}
53-
System.out.println("Sparsity of matrix is: " + sparsity(mat));
54-
in.close();
37+
return ((double) zero / (mat.length * mat[0].length));
5538
}
5639
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)