fix: FindKthNumberTest (#5444)

refactor: fix FindKthNumberTest
This commit is contained in:
Alex Klymenko 2024-09-08 22:25:13 +02:00 committed by GitHub
parent e0a1164cf5
commit fa2231788f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -41,14 +41,14 @@ public class FindKthNumberTest {
@Test @Test
public void testFindKthMaxLargeArray() { public void testFindKthMaxLargeArray() {
int[] array = generateArray(1000); int[] array = generateArray(1000);
int k = new Random().nextInt(array.length); int k = new Random().nextInt(1, array.length);
int result = FindKthNumber.findKthMax(array, k); int result = FindKthNumber.findKthMax(array, k);
Arrays.sort(array); Arrays.sort(array);
assertEquals(array[array.length - k], result); assertEquals(array[array.length - k], result);
} }
public static int[] generateArray(int capacity) { public static int[] generateArray(int capacity) {
int size = new Random().nextInt(capacity) + 1; int size = new Random().nextInt(2, capacity);
int[] array = new int[size]; int[] array = new int[size];
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {