- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathTernarySearchTest.java
81 lines (57 loc) · 2.15 KB
/
TernarySearchTest.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
packagecom.thealgorithms.searches;
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
importorg.junit.jupiter.api.Test;
classTernarySearchTest {
@Test
voidtestFindElementInSortedArray() {
Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
TernarySearchsearch = newTernarySearch();
intindex = search.find(arr, 5);
assertEquals(4, index, "Should find the element 5 at index 4");
}
@Test
voidtestElementNotFound() {
Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
TernarySearchsearch = newTernarySearch();
intindex = search.find(arr, 11);
assertEquals(-1, index, "Should return -1 for element 11 which is not present");
}
@Test
voidtestFindFirstElement() {
Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
TernarySearchsearch = newTernarySearch();
intindex = search.find(arr, 1);
assertEquals(0, index, "Should find the first element 1 at index 0");
}
@Test
voidtestFindLastElement() {
Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
TernarySearchsearch = newTernarySearch();
intindex = search.find(arr, 10);
assertEquals(9, index, "Should find the last element 10 at index 9");
}
@Test
voidtestFindInLargeArray() {
Integer[] arr = newInteger[1000];
for (inti = 0; i < 1000; i++) {
arr[i] = i + 1; // Array from 1 to 1000
}
TernarySearchsearch = newTernarySearch();
intindex = search.find(arr, 500);
assertEquals(499, index, "Should find element 500 at index 499");
}
@Test
voidtestNegativeNumbers() {
Integer[] arr = {-10, -5, -3, -1, 0, 1, 3, 5, 7, 10};
TernarySearchsearch = newTernarySearch();
intindex = search.find(arr, -3);
assertEquals(2, index, "Should find the element -3 at index 2");
}
@Test
voidtestEdgeCaseEmptyArray() {
Integer[] arr = {};
TernarySearchsearch = newTernarySearch();
intindex = search.find(arr, 5);
assertEquals(-1, index, "Should return -1 for an empty array");
}
}