-
Notifications
You must be signed in to change notification settings - Fork 20k
Add tests, remove main
in RangeInSortedArray
#5778
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
6 commits
Select commit
Hold shift + click to select a range
8f55a76
Add tests, remove `main` in `RangeInSortedArray`
Hardvan abc9f3b
Update directory
Hardvan c8c415c
Fix
Hardvan bd6f6a5
Merge remote-tracking branch 'origin/range_sorted_add_tests' into ran…
Hardvan 4345eeb
Fix
Hardvan da662c8
Merge branch 'master' into range_sorted_add_tests
alxkm 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
70 changes: 70 additions & 0 deletions
70
src/test/java/com/thealgorithms/misc/RangeInSortedArrayTest.java
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,70 @@ | ||
package com.thealgorithms.misc; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class RangeInSortedArrayTest { | ||
|
||
@Test | ||
public void testSortedRangeWithMultipleOccurrences() { | ||
int[] nums = {1, 2, 3, 3, 3, 4, 5}; | ||
assertArrayEquals(new int[] {2, 4}, RangeInSortedArray.sortedRange(nums, 3), "Range for key 3 should be [2, 4]"); | ||
} | ||
|
||
@Test | ||
public void testSortedRangeWithSingleOccurrence() { | ||
int[] nums = {1, 2, 3, 3, 3, 4, 5}; | ||
assertArrayEquals(new int[] {5, 5}, RangeInSortedArray.sortedRange(nums, 4), "Range for key 4 should be [5, 5]"); | ||
} | ||
|
||
@Test | ||
public void testSortedRangeWithNoOccurrences() { | ||
int[] nums = {0, 1, 2}; | ||
assertArrayEquals(new int[] {-1, -1}, RangeInSortedArray.sortedRange(nums, 3), "Range for key 3 should be [-1, -1] since it doesn't exist"); | ||
} | ||
|
||
@Test | ||
public void testSortedRangeInEmptyArray() { | ||
int[] nums = {}; | ||
assertArrayEquals(new int[] {-1, -1}, RangeInSortedArray.sortedRange(nums, 1), "Range for any key in an empty array should be [-1, -1]"); | ||
} | ||
|
||
@Test | ||
public void testSortedRangeAtStartAndEnd() { | ||
int[] nums = {1, 1, 1, 2, 3, 4, 5, 5, 5}; | ||
assertArrayEquals(new int[] {0, 2}, RangeInSortedArray.sortedRange(nums, 1), "Range for key 1 should be [0, 2]"); | ||
assertArrayEquals(new int[] {6, 8}, RangeInSortedArray.sortedRange(nums, 5), "Range for key 5 should be [6, 8]"); | ||
} | ||
|
||
@Test | ||
public void testGetCountLessThanWithExistingKey() { | ||
int[] nums = {1, 2, 3, 3, 4, 5}; | ||
assertEquals(4, RangeInSortedArray.getCountLessThan(nums, 3), "Count of elements less than 3 should be 2"); | ||
} | ||
|
||
@Test | ||
public void testGetCountLessThanWithNonExistingKey() { | ||
int[] nums = {1, 2, 3, 3, 4, 5}; | ||
assertEquals(5, RangeInSortedArray.getCountLessThan(nums, 4), "Count of elements less than 4 should be 5"); | ||
} | ||
|
||
@Test | ||
public void testGetCountLessThanWithAllSmallerElements() { | ||
int[] nums = {1, 2, 2, 3}; | ||
assertEquals(4, RangeInSortedArray.getCountLessThan(nums, 5), "Count of elements less than 5 should be 4"); | ||
} | ||
|
||
@Test | ||
public void testGetCountLessThanWithNoSmallerElements() { | ||
int[] nums = {2, 3, 4, 5}; | ||
assertEquals(0, RangeInSortedArray.getCountLessThan(nums, 1), "Count of elements less than 1 should be 0"); | ||
} | ||
|
||
@Test | ||
public void testGetCountLessThanWithEmptyArray() { | ||
int[] nums = {}; | ||
assertEquals(0, RangeInSortedArray.getCountLessThan(nums, 1), "Count of elements less than 1 in an empty array should be 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.