|
| 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