Skip to content

Commit 3e10f8f

Browse files
authored
Add tests, remove main, fix bug in Sparsity (#5780)
1 parent 7e11e9b commit 3e10f8f

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,7 @@
10331033
* [PalindromePrimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromePrimeTest.java)
10341034
* [PalindromeSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromeSinglyLinkedListTest.java)
10351035
* [RangeInSortedArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/RangeInSortedArrayTest.java)
1036+
* [SparsityTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/SparsityTest.java)
10361037
* [ThreeSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ThreeSumProblemTest.java)
10371038
* [TwoSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/TwoSumProblemTest.java)
10381039
* [WordBoggleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/WordBoggleTest.java)
Lines changed: 6 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
@@ -16,12 +14,17 @@ private Sparsity() {
1614
}
1715

1816
/*
17+
* @param mat the input matrix
1918
* @return Sparsity of matrix
2019
*
2120
* where sparsity = number of zeroes/total elements in matrix
2221
*
2322
*/
2423
static double sparsity(double[][] mat) {
24+
if (mat == null || mat.length == 0) {
25+
throw new IllegalArgumentException("Matrix cannot be null or empty");
26+
}
27+
2528
int zero = 0;
2629
// Traversing the matrix to count number of zeroes
2730
for (int i = 0; i < mat.length; i++) {
@@ -32,25 +35,6 @@ static double sparsity(double[][] mat) {
3235
}
3336
}
3437
// 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();
38+
return ((double) zero / (mat.length * mat[0].length));
5539
}
5640
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 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;
10+
11+
public class SparsityTest {
12+
13+
private static final double DELTA = 1e-9;
14+
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);
19+
}
20+
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"));
25+
}
26+
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+
}
32+
33+
private static Stream<Arguments> provideExceptionTestCases() {
34+
return Stream.of(Arguments.of(new double[][] {}, "Empty matrix should throw IllegalArgumentException"));
35+
}
36+
}

0 commit comments

Comments
 (0)