Skip to content

Commit e19378d

Browse files
authored
Add tests, remove main in RangeInSortedArray (#5778)
1 parent 9c76b30 commit e19378d

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,7 @@
10181018
* [MedianOfRunningArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java)
10191019
* [MirrorOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java)
10201020
* [PalindromeSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromeSinglyLinkedListTest.java)
1021+
* [RangeInSortedArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/RangeInSortedArrayTest.java)
10211022
* [TwoSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/TwoSumProblemTest.java)
10221023
* others
10231024
* [ArrayLeftRotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java)

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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 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 RangeInSortedArrayTest {
12+
13+
@ParameterizedTest(name = "Test case {index}: {3}")
14+
@MethodSource("provideSortedRangeTestCases")
15+
void testSortedRange(int[] nums, int key, int[] expectedRange, String description) {
16+
assertArrayEquals(expectedRange, RangeInSortedArray.sortedRange(nums, key), description);
17+
}
18+
19+
private static Stream<Arguments> provideSortedRangeTestCases() {
20+
return Stream.of(Arguments.of(new int[] {1, 2, 3, 3, 3, 4, 5}, 3, new int[] {2, 4}, "Range for key 3 with multiple occurrences"), Arguments.of(new int[] {1, 2, 3, 3, 3, 4, 5}, 4, new int[] {5, 5}, "Range for key 4 with single occurrence"),
21+
Arguments.of(new int[] {0, 1, 2}, 3, new int[] {-1, -1}, "Range for non-existent key"), Arguments.of(new int[] {}, 1, new int[] {-1, -1}, "Range in empty array"), Arguments.of(new int[] {1, 1, 1, 2, 3, 4, 5, 5, 5}, 1, new int[] {0, 2}, "Range for key at start"),
22+
Arguments.of(new int[] {1, 1, 1, 2, 3, 4, 5, 5, 5}, 5, new int[] {6, 8}, "Range for key at end"));
23+
}
24+
25+
@ParameterizedTest(name = "Test case {index}: {3}")
26+
@MethodSource("provideGetCountLessThanTestCases")
27+
void testGetCountLessThan(int[] nums, int key, int expectedCount, String description) {
28+
assertEquals(expectedCount, RangeInSortedArray.getCountLessThan(nums, key), description);
29+
}
30+
31+
private static Stream<Arguments> provideGetCountLessThanTestCases() {
32+
return Stream.of(Arguments.of(new int[] {1, 2, 3, 3, 4, 5}, 3, 4, "Count of elements less than existing key"), Arguments.of(new int[] {1, 2, 3, 3, 4, 5}, 4, 5, "Count of elements less than non-existing key"), Arguments.of(new int[] {1, 2, 2, 3}, 5, 4, "Count with all smaller elements"),
33+
Arguments.of(new int[] {2, 3, 4, 5}, 1, 0, "Count with no smaller elements"), Arguments.of(new int[] {}, 1, 0, "Count in empty array"));
34+
}
35+
}

0 commit comments

Comments
 (0)