In: Computer Science
considering a binary class write five-unit test modules to test the class which must include 5 different solutions
Here is the completed JUnit test code for this program. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// SearchTest.java
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class SearchTest {
// Search class object
private Search search;
// this method gets called before executing each test module
@Before
public void setUp() {
// initializing search object
search = new Search();
}
// first test module to test if an element exists in array of positive
// numbers, using an item that is present
@Test
public void testBinSearchUsingPositiveArray1() {
int arr[] = { 1, 3, 5, 7, 8, 9, 10 };
int item = 8;
assertEquals(search.BinSearch(arr, item), 4);
}
// second test module to test if an element exists in array of positive
// numbers, using an item that is NOT present
@Test
public void testBinSearchUsingPositiveArray2() {
int arr[] = { 1, 3, 5, 7, 8, 9, 10 };
int item = 15;
assertEquals(search.BinSearch(arr, item), -1);
}
// third test module to test if an element exists in array of negative
// numbers, using an item that is present
@Test
public void testBinSearchUsingNegativeArray1() {
int arr[] = { -111, -33, -15, -7, -5, -2, -1 };
int item = -1;
assertEquals(search.BinSearch(arr, item), 6);
}
// fourth test module to test if an element exists in array of negative
// numbers, using an item that is NOT present
@Test
public void testBinSearchUsingNegativeArray2() {
int arr[] = { -111, -33, -15, -7, -5, -2, -1 };
int item = -48;
assertEquals(search.BinSearch(arr, item), -1);
}
// fifth test module to test if an element exists in array containing both
// positive and negative values; using an item that is present and another
// item that is not present
@Test
public void testBinSearchUsingNormalArray() {
int arr[] = { -35, -1, -2, 1, 5, 6, 9, 24 };
int item = -35;
assertEquals(search.BinSearch(arr, item), 0);
item = 119;
assertEquals(search.BinSearch(arr, item), -1);
}
}
OUTPUT: