Skip to content

Commit b1724fa

Browse files
authored
Add tests, remove main in LinearSearch (#5670)
1 parent 401d873 commit b1724fa

File tree

3 files changed

+119
-18
lines changed

3 files changed

+119
-18
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,7 @@
10091009
* [IterativeTernarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/IterativeTernarySearchTest.java)
10101010
* [JumpSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/JumpSearchTest.java)
10111011
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)
1012+
* [LinearSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/LinearSearchTest.java)
10121013
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
10131014
* [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java)
10141015
* [QuickSelectTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/QuickSelectTest.java)

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.thealgorithms.searches;
22

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

75
/**
86
* Linear search is the easiest search algorithm It works with sorted and
@@ -36,20 +34,4 @@ public <T extends Comparable<T>> int find(T[] array, T value) {
3634
}
3735
return -1;
3836
}
39-
40-
public static void main(String[] args) {
41-
// just generate data
42-
Random r = new Random();
43-
int size = 200;
44-
int maxElement = 100;
45-
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).toArray(Integer[] ::new);
46-
47-
// the element that should be found
48-
Integer shouldBeFound = integers[r.nextInt(size - 1)];
49-
50-
LinearSearch search = new LinearSearch();
51-
int atIndex = search.find(integers, shouldBeFound);
52-
53-
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);
54-
}
5537
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Random;
6+
import org.junit.jupiter.api.Test;
7+
8+
/**
9+
* Unit tests for the LinearSearch class.
10+
*/
11+
class LinearSearchTest {
12+
13+
/**
14+
* Test for finding an element present in the array.
15+
*/
16+
@Test
17+
void testLinearSearchFound() {
18+
LinearSearch linearSearch = new LinearSearch();
19+
Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
20+
Integer key = 5; // Element to find
21+
assertEquals(5, linearSearch.find(array, key), "The index of the found element should be 5.");
22+
}
23+
24+
/**
25+
* Test for finding the first element in the array.
26+
*/
27+
@Test
28+
void testLinearSearchFirstElement() {
29+
LinearSearch linearSearch = new LinearSearch();
30+
Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
31+
Integer key = 0; // First element
32+
assertEquals(0, linearSearch.find(array, key), "The index of the first element should be 0.");
33+
}
34+
35+
/**
36+
* Test for finding the last element in the array.
37+
*/
38+
@Test
39+
void testLinearSearchLastElement() {
40+
LinearSearch linearSearch = new LinearSearch();
41+
Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
42+
Integer key = 10; // Last element
43+
assertEquals(10, linearSearch.find(array, key), "The index of the last element should be 10.");
44+
}
45+
46+
/**
47+
* Test for finding an element not present in the array.
48+
*/
49+
@Test
50+
void testLinearSearchNotFound() {
51+
LinearSearch linearSearch = new LinearSearch();
52+
Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
53+
Integer key = -1; // Element not in the array
54+
assertEquals(-1, linearSearch.find(array, key), "The element should not be found in the array.");
55+
}
56+
57+
/**
58+
* Test for finding an element in an empty array.
59+
*/
60+
@Test
61+
void testLinearSearchEmptyArray() {
62+
LinearSearch linearSearch = new LinearSearch();
63+
Integer[] array = {}; // Empty array
64+
Integer key = 1; // Key not present
65+
assertEquals(-1, linearSearch.find(array, key), "The element should not be found in an empty array.");
66+
}
67+
68+
/**
69+
* Test for finding an element in a large array.
70+
*/
71+
@Test
72+
void testLinearSearchLargeArray() {
73+
LinearSearch linearSearch = new LinearSearch();
74+
Integer[] array = new Integer[1000];
75+
for (int i = 0; i < array.length; i++) {
76+
array[i] = i; // Fill the array with integers 0 to 999
77+
}
78+
Integer key = 256; // Present in the array
79+
assertEquals(256, linearSearch.find(array, key), "The index of the found element should be 256.");
80+
}
81+
82+
/**
83+
* Test for finding an element in a large array when it is not present.
84+
*/
85+
@Test
86+
void testLinearSearchLargeArrayNotFound() {
87+
LinearSearch linearSearch = new LinearSearch();
88+
Integer[] array = new Integer[1000];
89+
for (int i = 0; i < array.length; i++) {
90+
array[i] = i; // Fill the array with integers 0 to 999
91+
}
92+
Integer key = 1001; // Key not present
93+
assertEquals(-1, linearSearch.find(array, key), "The element should not be found in the array.");
94+
}
95+
96+
/**
97+
* Test for finding multiple occurrences of the same element in the array.
98+
*/
99+
@Test
100+
void testLinearSearchMultipleOccurrences() {
101+
LinearSearch linearSearch = new LinearSearch();
102+
Integer[] array = {1, 2, 3, 4, 5, 3, 6, 7, 3}; // 3 occurs multiple times
103+
Integer key = 3; // Key to find
104+
assertEquals(2, linearSearch.find(array, key), "The index of the first occurrence of the element should be 2.");
105+
}
106+
107+
/**
108+
* Test for performance with random large array.
109+
*/
110+
@Test
111+
void testLinearSearchRandomArray() {
112+
LinearSearch linearSearch = new LinearSearch();
113+
Random random = new Random();
114+
Integer[] array = random.ints(0, 1000).distinct().limit(1000).boxed().toArray(Integer[] ::new);
115+
Integer key = array[random.nextInt(array.length)]; // Key should be in the array
116+
assertEquals(java.util.Arrays.asList(array).indexOf(key), linearSearch.find(array, key), "The index of the found element should match.");
117+
}
118+
}

0 commit comments

Comments
 (0)