-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathLinearSearchThreadTest.java
74 lines (66 loc) · 2.47 KB
/
LinearSearchThreadTest.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
package com.thealgorithms.searches;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
class LinearSearchThreadTest {
/**
* Test for finding an element that is present in the array.
*/
@Test
void testSearcherFound() throws InterruptedException {
int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Searcher searcher = new Searcher(array, 0, array.length, 5);
searcher.start();
searcher.join();
assertTrue(searcher.getResult(), "The element 5 should be found in the array.");
}
/**
* Test for finding an element that is not present in the array.
*/
@Test
void testSearcherNotFound() throws InterruptedException {
int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Searcher searcher = new Searcher(array, 0, array.length, 11);
searcher.start();
searcher.join();
assertFalse(searcher.getResult(), "The element 11 should not be found in the array.");
}
/**
* Test for searching a segment of the array.
*/
@Test
void testSearcherSegmentFound() throws InterruptedException {
int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Searcher searcher = new Searcher(array, 0, 5, 3);
searcher.start();
searcher.join();
assertTrue(searcher.getResult(), "The element 3 should be found in the segment.");
}
/**
* Test for searching an empty array segment.
*/
@Test
void testSearcherEmptySegment() throws InterruptedException {
int[] array = {};
Searcher searcher = new Searcher(array, 0, 0, 1); // Empty array
searcher.start();
searcher.join();
assertFalse(searcher.getResult(), "The element should not be found in an empty segment.");
}
/**
* Test for searching with random numbers.
*/
@Test
void testSearcherRandomNumbers() throws InterruptedException {
int size = 200;
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = (int) (Math.random() * 100);
}
int target = array[(int) (Math.random() * size)]; // Randomly select a target that is present
Searcher searcher = new Searcher(array, 0, size, target);
searcher.start();
searcher.join();
assertTrue(searcher.getResult(), "The randomly selected target should be found in the array.");
}
}