Skip to content

Add tests, remove main in LinearSearch #5670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,7 @@
* [IterativeTernarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/IterativeTernarySearchTest.java)
* [JumpSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/JumpSearchTest.java)
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)
* [LinearSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/LinearSearchTest.java)
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
* [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java)
* [QuickSelectTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/QuickSelectTest.java)
Expand Down
18 changes: 0 additions & 18 deletions src/main/java/com/thealgorithms/searches/LinearSearch.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.thealgorithms.searches;

import com.thealgorithms.devutils.searches.SearchAlgorithm;
import java.util.Random;
import java.util.stream.Stream;

/**
* Linear search is the easiest search algorithm It works with sorted and
Expand Down Expand Up @@ -36,20 +34,4 @@ public <T extends Comparable<T>> int find(T[] array, T value) {
}
return -1;
}

public static void main(String[] args) {
// just generate data
Random r = new Random();
int size = 200;
int maxElement = 100;
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).toArray(Integer[] ::new);

// the element that should be found
Integer shouldBeFound = integers[r.nextInt(size - 1)];

LinearSearch search = new LinearSearch();
int atIndex = search.find(integers, shouldBeFound);

System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);
}
}
118 changes: 118 additions & 0 deletions src/test/java/com/thealgorithms/searches/LinearSearchTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.thealgorithms.searches;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Random;
import org.junit.jupiter.api.Test;

/**
* Unit tests for the LinearSearch class.
*/
class LinearSearchTest {

/**
* Test for finding an element present in the array.
*/
@Test
void testLinearSearchFound() {
LinearSearch linearSearch = new LinearSearch();
Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Integer key = 5; // Element to find
assertEquals(5, linearSearch.find(array, key), "The index of the found element should be 5.");
}

/**
* Test for finding the first element in the array.
*/
@Test
void testLinearSearchFirstElement() {
LinearSearch linearSearch = new LinearSearch();
Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Integer key = 0; // First element
assertEquals(0, linearSearch.find(array, key), "The index of the first element should be 0.");
}

/**
* Test for finding the last element in the array.
*/
@Test
void testLinearSearchLastElement() {
LinearSearch linearSearch = new LinearSearch();
Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Integer key = 10; // Last element
assertEquals(10, linearSearch.find(array, key), "The index of the last element should be 10.");
}

/**
* Test for finding an element not present in the array.
*/
@Test
void testLinearSearchNotFound() {
LinearSearch linearSearch = new LinearSearch();
Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Integer key = -1; // Element not in the array
assertEquals(-1, linearSearch.find(array, key), "The element should not be found in the array.");
}

/**
* Test for finding an element in an empty array.
*/
@Test
void testLinearSearchEmptyArray() {
LinearSearch linearSearch = new LinearSearch();
Integer[] array = {}; // Empty array
Integer key = 1; // Key not present
assertEquals(-1, linearSearch.find(array, key), "The element should not be found in an empty array.");
}

/**
* Test for finding an element in a large array.
*/
@Test
void testLinearSearchLargeArray() {
LinearSearch linearSearch = new LinearSearch();
Integer[] array = new Integer[1000];
for (int i = 0; i < array.length; i++) {
array[i] = i; // Fill the array with integers 0 to 999
}
Integer key = 256; // Present in the array
assertEquals(256, linearSearch.find(array, key), "The index of the found element should be 256.");
}

/**
* Test for finding an element in a large array when it is not present.
*/
@Test
void testLinearSearchLargeArrayNotFound() {
LinearSearch linearSearch = new LinearSearch();
Integer[] array = new Integer[1000];
for (int i = 0; i < array.length; i++) {
array[i] = i; // Fill the array with integers 0 to 999
}
Integer key = 1001; // Key not present
assertEquals(-1, linearSearch.find(array, key), "The element should not be found in the array.");
}

/**
* Test for finding multiple occurrences of the same element in the array.
*/
@Test
void testLinearSearchMultipleOccurrences() {
LinearSearch linearSearch = new LinearSearch();
Integer[] array = {1, 2, 3, 4, 5, 3, 6, 7, 3}; // 3 occurs multiple times
Integer key = 3; // Key to find
assertEquals(2, linearSearch.find(array, key), "The index of the first occurrence of the element should be 2.");
}

/**
* Test for performance with random large array.
*/
@Test
void testLinearSearchRandomArray() {
LinearSearch linearSearch = new LinearSearch();
Random random = new Random();
Integer[] array = random.ints(0, 1000).distinct().limit(1000).boxed().toArray(Integer[] ::new);
Integer key = array[random.nextInt(array.length)]; // Key should be in the array
assertEquals(java.util.Arrays.asList(array).indexOf(key), linearSearch.find(array, key), "The index of the found element should match.");
}
}