Skip to content

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 6 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@
* [MedianOfRunningArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java)
* [MirrorOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java)
* [PalindromeSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromeSinglyLinkedListTest.java)
* [RangeInSortedArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/RangeInSortedArrayTest.java)
* [TwoSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/TwoSumProblemTest.java)
* others
* [ArrayLeftRotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
package com.thealgorithms.misc;

import java.util.Arrays;

public final class RangeInSortedArray {
private RangeInSortedArray() {
}

public static void main(String[] args) {
// Testcases
assert Arrays.equals(sortedRange(new int[] {1, 2, 3, 3, 3, 4, 5}, 3), new int[] {2, 4});
assert Arrays.equals(sortedRange(new int[] {1, 2, 3, 3, 3, 4, 5}, 4), new int[] {5, 5});
assert Arrays.equals(sortedRange(new int[] {0, 1, 2}, 3), new int[] {-1, -1});
}

// Get the 1st and last occurrence index of a number 'key' in a non-decreasing array 'nums'
// Gives [-1, -1] in case element doesn't exist in array
public static int[] sortedRange(int[] nums, int key) {
Expand Down
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");
}
}