mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 09:45:04 +08:00
Add tests, remove main
in RangeInSortedArray
(#5778)
This commit is contained in:
@ -1018,6 +1018,7 @@
|
|||||||
* [MedianOfRunningArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java)
|
* [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)
|
* [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)
|
* [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)
|
* [TwoSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/TwoSumProblemTest.java)
|
||||||
* others
|
* others
|
||||||
* [ArrayLeftRotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java)
|
* [ArrayLeftRotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java)
|
||||||
|
@ -1,18 +1,9 @@
|
|||||||
package com.thealgorithms.misc;
|
package com.thealgorithms.misc;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
public final class RangeInSortedArray {
|
public final class RangeInSortedArray {
|
||||||
private 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'
|
// 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
|
// Gives [-1, -1] in case element doesn't exist in array
|
||||||
public static int[] sortedRange(int[] nums, int key) {
|
public static int[] sortedRange(int[] nums, int key) {
|
||||||
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.thealgorithms.misc;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
public class RangeInSortedArrayTest {
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Test case {index}: {3}")
|
||||||
|
@MethodSource("provideSortedRangeTestCases")
|
||||||
|
void testSortedRange(int[] nums, int key, int[] expectedRange, String description) {
|
||||||
|
assertArrayEquals(expectedRange, RangeInSortedArray.sortedRange(nums, key), description);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> provideSortedRangeTestCases() {
|
||||||
|
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"),
|
||||||
|
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"),
|
||||||
|
Arguments.of(new int[] {1, 1, 1, 2, 3, 4, 5, 5, 5}, 5, new int[] {6, 8}, "Range for key at end"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Test case {index}: {3}")
|
||||||
|
@MethodSource("provideGetCountLessThanTestCases")
|
||||||
|
void testGetCountLessThan(int[] nums, int key, int expectedCount, String description) {
|
||||||
|
assertEquals(expectedCount, RangeInSortedArray.getCountLessThan(nums, key), description);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> provideGetCountLessThanTestCases() {
|
||||||
|
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"),
|
||||||
|
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"));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user