|
1 | 1 | package com.thealgorithms.divideandconquer; |
2 | 2 |
|
3 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | | -import static org.junit.jupiter.api.Assertions.assertThrows; |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
5 | 4 |
|
6 | 5 | import org.junit.jupiter.api.Test; |
7 | 6 |
|
8 | 7 | class DeterministicQuickSelectTest { |
9 | 8 |
|
10 | 9 | @Test |
11 | | - void testSelectKthSmallest() { |
| 10 | + void testSelectKthSmallestBasic() { |
12 | 11 | int[] arr = {7, 10, 4, 3, 20, 15}; |
13 | | - assertEquals(3, DeterministicQuickSelect.select(arr, 1)); |
14 | | - assertEquals(4, DeterministicQuickSelect.select(arr, 2)); |
15 | | - assertEquals(7, DeterministicQuickSelect.select(arr, 3)); |
16 | | - assertEquals(10, DeterministicQuickSelect.select(arr, 4)); |
17 | | - assertEquals(15, DeterministicQuickSelect.select(arr, 5)); |
18 | | - assertEquals(20, DeterministicQuickSelect.select(arr, 6)); |
| 12 | + assertEquals(3, DeterministicQuickSelect.select(arr.clone(), 1)); // smallest |
| 13 | + assertEquals(4, DeterministicQuickSelect.select(arr.clone(), 2)); |
| 14 | + assertEquals(7, DeterministicQuickSelect.select(arr.clone(), 3)); |
| 15 | + assertEquals(10, DeterministicQuickSelect.select(arr.clone(), 4)); |
| 16 | + assertEquals(15, DeterministicQuickSelect.select(arr.clone(), 5)); |
| 17 | + assertEquals(20, DeterministicQuickSelect.select(arr.clone(), 6)); // largest |
19 | 18 | } |
20 | 19 |
|
21 | 20 | @Test |
22 | | - void testInvalidInput() { |
23 | | - assertThrows(IllegalArgumentException.class, () -> DeterministicQuickSelect.select(null, 1)); |
24 | | - assertThrows(IllegalArgumentException.class, () -> DeterministicQuickSelect.select(new int[] {1, 2}, 0)); |
25 | | - assertThrows(IllegalArgumentException.class, () -> DeterministicQuickSelect.select(new int[] {1, 2}, 3)); |
| 21 | + void testSelectSingleElement() { |
| 22 | + int[] arr = {42}; |
| 23 | + assertEquals(42, DeterministicQuickSelect.select(arr, 1)); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + void testSelectWithDuplicates() { |
| 28 | + int[] arr = {5, 3, 8, 5, 2, 3}; |
| 29 | + assertEquals(2, DeterministicQuickSelect.select(arr.clone(), 1)); |
| 30 | + assertEquals(3, DeterministicQuickSelect.select(arr.clone(), 2)); |
| 31 | + assertEquals(3, DeterministicQuickSelect.select(arr.clone(), 3)); |
| 32 | + assertEquals(5, DeterministicQuickSelect.select(arr.clone(), 4)); |
| 33 | + assertEquals(5, DeterministicQuickSelect.select(arr.clone(), 5)); |
| 34 | + assertEquals(8, DeterministicQuickSelect.select(arr.clone(), 6)); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void testSelectEmptyArray() { |
| 39 | + int[] arr = {}; |
| 40 | + assertThrows(IllegalArgumentException.class, () -> DeterministicQuickSelect.select(arr, 1)); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testSelectInvalidK() { |
| 45 | + int[] arr = {1, 2, 3}; |
| 46 | + assertThrows(IllegalArgumentException.class, () -> DeterministicQuickSelect.select(arr, 0)); // k < 1 |
| 47 | + assertThrows(IllegalArgumentException.class, () -> DeterministicQuickSelect.select(arr, 4)); // k > length |
26 | 48 | } |
27 | 49 | } |
0 commit comments