- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathRandomSearchTest.java
87 lines (67 loc) · 2.57 KB
/
RandomSearchTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
packagecom.thealgorithms.searches;
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
importstaticorg.junit.jupiter.api.Assertions.assertNotEquals;
importorg.junit.jupiter.api.BeforeEach;
importorg.junit.jupiter.api.Test;
classRandomSearchTest {
privateRandomSearchrandomSearch;
@BeforeEach
voidsetUp() {
randomSearch = newRandomSearch();
}
@Test
voidtestElementFound() {
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Integerkey = 5;
intindex = randomSearch.find(array, key);
assertNotEquals(-1, index, "Element should be found in the array.");
assertEquals(key, array[index], "Element found should match the key.");
}
@Test
voidtestElementNotFound() {
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Integerkey = 11;
intindex = randomSearch.find(array, key);
assertEquals(-1, index, "Element not present in the array should return -1.");
}
@Test
voidtestEmptyArray() {
Integer[] emptyArray = {};
Integerkey = 5;
intindex = randomSearch.find(emptyArray, key);
assertEquals(-1, index, "Searching in an empty array should return -1.");
}
@Test
voidtestSingleElementArrayFound() {
Integer[] array = {5};
Integerkey = 5;
intindex = randomSearch.find(array, key);
assertEquals(0, index, "The key should be found at index 0 in a single-element array.");
}
@Test
voidtestSingleElementArrayNotFound() {
Integer[] array = {1};
Integerkey = 5;
intindex = randomSearch.find(array, key);
assertEquals(-1, index, "The key should not be found in a single-element array if it does not match.");
}
@Test
voidtestDuplicateElementsFound() {
Integer[] array = {1, 2, 3, 4, 5, 5, 5, 7, 8, 9, 10};
Integerkey = 5;
intindex = randomSearch.find(array, key);
assertNotEquals(-1, index, "The key should be found in the array with duplicates.");
assertEquals(key, array[index], "The key found should be 5.");
}
@Test
voidtestLargeArray() {
Integer[] largeArray = newInteger[1000];
for (inti = 0; i < largeArray.length; i++) {
largeArray[i] = i + 1; // Fill with values 1 to 1000
}
Integerkey = 500;
intindex = randomSearch.find(largeArray, key);
assertNotEquals(-1, index, "The key should be found in the large array.");
assertEquals(key, largeArray[index], "The key found should match 500.");
}
}