Skip to content

Commit e83fe20

Browse files
committed
testing: improve test coverage RangeInSortedArrayTest
1 parent dd1a51b commit e83fe20

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/test/java/com/thealgorithms/misc/RangeInSortedArrayTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,24 @@ private static Stream<Arguments> provideGetCountLessThanTestCases() {
3232
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"),
3333
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"));
3434
}
35+
36+
@ParameterizedTest(name = "Edge case {index}: {3}")
37+
@MethodSource("provideEdgeCasesForSortedRange")
38+
void testSortedRangeEdgeCases(int[] nums, int key, int[] expectedRange, String description) {
39+
assertArrayEquals(expectedRange, RangeInSortedArray.sortedRange(nums, key), description);
40+
}
41+
42+
private static Stream<Arguments> provideEdgeCasesForSortedRange() {
43+
return Stream.of(Arguments.of(new int[] {5, 5, 5, 5, 5}, 5, new int[] {0, 4}, "All elements same as key"), Arguments.of(new int[] {1, 2, 3, 4, 5}, 1, new int[] {0, 0}, "Key is first element"), Arguments.of(new int[] {1, 2, 3, 4, 5}, 5, new int[] {4, 4}, "Key is last element"), Arguments.of(new int[] {1, 2, 3, 4, 5}, 0, new int[] {-1, -1}, "Key less than all elements"), Arguments.of(new int[] {1, 2, 3, 4, 5}, 6, new int[] {-1, -1}, "Key greater than all elements"), Arguments.of(new int[] {1, 2, 2, 2, 3, 3, 3, 4}, 3, new int[] {4, 6}, "Multiple occurrences spread"), Arguments.of(new int[] {2}, 2, new int[] {0, 0}, "Single element array key exists"), Arguments.of(new int[] {2}, 3, new int[] {-1, -1}, "Single element array key missing"));
44+
}
45+
46+
@ParameterizedTest(name = "Edge case {index}: {3}")
47+
@MethodSource("provideEdgeCasesForGetCountLessThan")
48+
void testGetCountLessThanEdgeCases(int[] nums, int key, int expectedCount, String description) {
49+
assertEquals(expectedCount, RangeInSortedArray.getCountLessThan(nums, key), description);
50+
}
51+
52+
private static Stream<Arguments> provideEdgeCasesForGetCountLessThan() {
53+
return Stream.of(Arguments.of(new int[] {1, 2, 3, 4, 5}, 0, 0, "Key less than all elements"), Arguments.of(new int[] {1, 2, 3, 4, 5}, 6, 5, "Key greater than all elements"), Arguments.of(new int[] {1}, 0, 0, "Single element greater than key"));
54+
}
3555
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy