|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
4 | 4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
5 | 5 |
|
6 |
| -import org.junit.jupiter.api.Test; |
| 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; |
7 | 10 |
|
8 | 11 | public class RangeInSortedArrayTest {
|
9 | 12 |
|
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]"); |
| 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); |
14 | 17 | }
|
15 | 18 |
|
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]"); |
| 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")); |
20 | 23 | }
|
21 | 24 |
|
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"); |
| 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); |
26 | 29 | }
|
27 | 30 |
|
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 |
| - |
65 |
| - @Test |
66 |
| - public void testGetCountLessThanWithEmptyArray() { |
67 |
| - int[] nums = {}; |
68 |
| - assertEquals(0, RangeInSortedArray.getCountLessThan(nums, 1), "Count of elements less than 1 in an empty array should be 0"); |
| 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")); |
69 | 34 | }
|
70 | 35 | }
|
0 commit comments