Skip to content

Commit 4ec2701

Browse files
authored
Add tests, remove main in TernarySearch (#5677)
1 parent 6802029 commit 4ec2701

File tree

3 files changed

+82
-22
lines changed

3 files changed

+82
-22
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,7 @@
10221022
* [SearchInARowAndColWiseSortedMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrixTest.java)
10231023
* [SortOrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SortOrderAgnosticBinarySearchTest.java)
10241024
* [SquareRootBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SquareRootBinarySearchTest.java)
1025+
* [TernarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TernarySearchTest.java)
10251026
* [TestSearchInARowAndColWiseSortedMatrix](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java)
10261027
* [UnionFindTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UnionFindTest.java)
10271028
* sorts

src/main/java/com/thealgorithms/searches/TernarySearch.java

-22
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.thealgorithms.searches;
22

33
import com.thealgorithms.devutils.searches.SearchAlgorithm;
4-
import java.util.Arrays;
5-
import java.util.Random;
6-
import java.util.stream.Stream;
74

85
/**
96
* A ternary search algorithm is a technique in computer science for finding the
@@ -60,23 +57,4 @@ private <T extends Comparable<T>> int ternarySearch(T[] arr, T key, int start, i
6057
return ternarySearch(arr, key, mid1, mid2);
6158
}
6259
}
63-
64-
public static void main(String[] args) {
65-
// just generate data
66-
Random r = new Random();
67-
int size = 100;
68-
int maxElement = 100000;
69-
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray(Integer[] ::new);
70-
71-
// the element that should be found
72-
Integer shouldBeFound = integers[r.nextInt(size - 1)];
73-
74-
TernarySearch search = new TernarySearch();
75-
int atIndex = search.find(integers, shouldBeFound);
76-
77-
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);
78-
79-
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
80-
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
81-
}
8260
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class TernarySearchTest {
8+
9+
@Test
10+
void testFindElementInSortedArray() {
11+
Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
12+
TernarySearch search = new TernarySearch();
13+
14+
int index = search.find(arr, 5);
15+
16+
assertEquals(4, index, "Should find the element 5 at index 4");
17+
}
18+
19+
@Test
20+
void testElementNotFound() {
21+
Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
22+
TernarySearch search = new TernarySearch();
23+
24+
int index = search.find(arr, 11);
25+
26+
assertEquals(-1, index, "Should return -1 for element 11 which is not present");
27+
}
28+
29+
@Test
30+
void testFindFirstElement() {
31+
Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
32+
TernarySearch search = new TernarySearch();
33+
34+
int index = search.find(arr, 1);
35+
36+
assertEquals(0, index, "Should find the first element 1 at index 0");
37+
}
38+
39+
@Test
40+
void testFindLastElement() {
41+
Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
42+
TernarySearch search = new TernarySearch();
43+
44+
int index = search.find(arr, 10);
45+
46+
assertEquals(9, index, "Should find the last element 10 at index 9");
47+
}
48+
49+
@Test
50+
void testFindInLargeArray() {
51+
Integer[] arr = new Integer[1000];
52+
for (int i = 0; i < 1000; i++) {
53+
arr[i] = i + 1; // Array from 1 to 1000
54+
}
55+
TernarySearch search = new TernarySearch();
56+
57+
int index = search.find(arr, 500);
58+
59+
assertEquals(499, index, "Should find element 500 at index 499");
60+
}
61+
62+
@Test
63+
void testNegativeNumbers() {
64+
Integer[] arr = {-10, -5, -3, -1, 0, 1, 3, 5, 7, 10};
65+
TernarySearch search = new TernarySearch();
66+
67+
int index = search.find(arr, -3);
68+
69+
assertEquals(2, index, "Should find the element -3 at index 2");
70+
}
71+
72+
@Test
73+
void testEdgeCaseEmptyArray() {
74+
Integer[] arr = {};
75+
TernarySearch search = new TernarySearch();
76+
77+
int index = search.find(arr, 5);
78+
79+
assertEquals(-1, index, "Should return -1 for an empty array");
80+
}
81+
}

0 commit comments

Comments
 (0)