Skip to content

Commit 8f55a76

Browse files
committed
Add tests, remove main in RangeInSortedArray
1 parent 596c614 commit 8f55a76

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

src/main/java/com/thealgorithms/misc/RangeInSortedArray.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
package com.thealgorithms.misc;
22

3-
import java.util.Arrays;
4-
53
public final class RangeInSortedArray {
64
private RangeInSortedArray() {
75
}
86

9-
public static void main(String[] args) {
10-
// Testcases
11-
assert Arrays.equals(sortedRange(new int[] {1, 2, 3, 3, 3, 4, 5}, 3), new int[] {2, 4});
12-
assert Arrays.equals(sortedRange(new int[] {1, 2, 3, 3, 3, 4, 5}, 4), new int[] {5, 5});
13-
assert Arrays.equals(sortedRange(new int[] {0, 1, 2}, 3), new int[] {-1, -1});
14-
}
15-
167
// Get the 1st and last occurrence index of a number 'key' in a non-decreasing array 'nums'
178
// Gives [-1, -1] in case element doesn't exist in array
189
public static int[] sortedRange(int[] nums, int key) {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.thealgorithms.misc;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class RangeInSortedArrayTest {
9+
10+
@Test
11+
public void testSortedRangeWithMultipleOccurrences() {
12+
int[] nums = {1, 2, 3, 3, 3, 4, 5};
13+
assertArrayEquals(new int[] {2, 4}, RangeInSortedArray.sortedRange(nums, 3), "Range for key 3 should be [2, 4]");
14+
}
15+
16+
@Test
17+
public void testSortedRangeWithSingleOccurrence() {
18+
int[] nums = {1, 2, 3, 3, 3, 4, 5};
19+
assertArrayEquals(new int[] {5, 5}, RangeInSortedArray.sortedRange(nums, 4), "Range for key 4 should be [5, 5]");
20+
}
21+
22+
@Test
23+
public void testSortedRangeWithNoOccurrences() {
24+
int[] nums = {0, 1, 2};
25+
assertArrayEquals(new int[] {-1, -1}, RangeInSortedArray.sortedRange(nums, 3), "Range for key 3 should be [-1, -1] since it doesn't exist");
26+
}
27+
28+
@Test
29+
public void testSortedRangeInEmptyArray() {
30+
int[] nums = {};
31+
assertArrayEquals(new int[] {-1, -1}, RangeInSortedArray.sortedRange(nums, 1), "Range for any key in an empty array should be [-1, -1]");
32+
}
33+
34+
@Test
35+
public void testSortedRangeAtStartAndEnd() {
36+
int[] nums = {1, 1, 1, 2, 3, 4, 5, 5, 5};
37+
assertArrayEquals(new int[] {0, 2}, RangeInSortedArray.sortedRange(nums, 1), "Range for key 1 should be [0, 2]");
38+
assertArrayEquals(new int[] {6, 8}, RangeInSortedArray.sortedRange(nums, 5), "Range for key 5 should be [6, 8]");
39+
}
40+
41+
@Test
42+
public void testGetCountLessThanWithExistingKey() {
43+
int[] nums = {1, 2, 3, 3, 4, 5};
44+
assertEquals(4, RangeInSortedArray.getCountLessThan(nums, 3), "Count of elements less than 3 should be 2");
45+
}
46+
47+
@Test
48+
public void testGetCountLessThanWithNonExistingKey() {
49+
int[] nums = {1, 2, 3, 3, 4, 5};
50+
assertEquals(5, RangeInSortedArray.getCountLessThan(nums, 4), "Count of elements less than 4 should be 5");
51+
}
52+
53+
@Test
54+
public void testGetCountLessThanWithAllSmallerElements() {
55+
int[] nums = {1, 2, 2, 3};
56+
assertEquals(4, RangeInSortedArray.getCountLessThan(nums, 5), "Count of elements less than 5 should be 4");
57+
}
58+
59+
@Test
60+
public void testGetCountLessThanWithNoSmallerElements() {
61+
int[] nums = {2, 3, 4, 5};
62+
assertEquals(0, RangeInSortedArray.getCountLessThan(nums, 1), "Count of elements less than 1 should be 0");
63+
}
64+
}

0 commit comments

Comments
 (0)