-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Add tests, remove main
, fix bug in Sparsity
#5780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6c35f05
Add tests, remove `main`, fix bug in `RangeInSortedArray`
Hardvan dc987b8
Update directory
Hardvan 6d05e38
Fix
Hardvan 89bc6b0
Merge remote-tracking branch 'origin/sparsity_add_tests' into sparsit…
Hardvan 4d1fc7b
Fix
Hardvan f09cfd9
Merge branch 'master' into sparsity_add_tests
Hardvan e5d00f9
Update directory
Hardvan 8d799c6
Update Sparsity.java
Hardvan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.thealgorithms.misc; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class SparsityTest { | ||
|
||
private static final double DELTA = 1e-9; | ||
|
||
@Test | ||
public void testAllZeroElements() { | ||
double[][] mat = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; | ||
double expectedSparsity = 1.0; | ||
assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of a matrix with all zero elements should be 1.0"); | ||
} | ||
|
||
@Test | ||
public void testNoZeroElements() { | ||
double[][] mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; | ||
double expectedSparsity = 0.0; | ||
assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of a matrix with no zero elements should be 0.0"); | ||
} | ||
|
||
@Test | ||
public void testMixedElements() { | ||
double[][] mat = {{0, 2, 0}, {4, 0, 6}, {0, 8, 0}}; | ||
double expectedSparsity = 5.0 / 9.0; | ||
assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of the matrix should be 5/9"); | ||
} | ||
|
||
@Test | ||
public void testSingleRowMatrix() { | ||
double[][] mat = {{0, 1, 0, 2, 0}}; | ||
double expectedSparsity = 3.0 / 5.0; | ||
assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of the single-row matrix should be 3/5"); | ||
} | ||
|
||
@Test | ||
public void testSingleColumnMatrix() { | ||
double[][] mat = {{1}, {0}, {0}, {2}}; | ||
double expectedSparsity = 2.0 / 4.0; | ||
assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of the single-column matrix should be 2/4"); | ||
} | ||
|
||
@Test | ||
public void testEmptyMatrix() { | ||
double[][] mat = {}; | ||
assertThrows(IllegalArgumentException.class, () -> Sparsity.sparsity(mat), "Sparsity of an empty matrix should throw an IllegalArgumentException"); | ||
} | ||
|
||
@Test | ||
public void testMatrixWithSingleElementZero() { | ||
double[][] mat = {{0}}; | ||
double expectedSparsity = 1.0; | ||
assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of a matrix with a single zero element should be 1.0"); | ||
} | ||
|
||
@Test | ||
public void testMatrixWithSingleElementNonZero() { | ||
double[][] mat = {{5}}; | ||
double expectedSparsity = 0.0; | ||
assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of a matrix with a single non-zero element should be 0.0"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.