import static org.junit.Assert.*; import java.util.Arrays; import org.junit.Test; public class TestBinarySearchnSort { @Test public void testBinarySearch() { Integer[] arr = {2, 5, 7, 8, 10, 13, 17, 18, 19, 21, 25, 32, 35, 36, 39, 42}; int x = 7; // arr is sorted in ascending order. // Preconditions says array must be sorted. // So the input in each cases must meet this condition. int idx = Arrays.binarySearch(arr, x); // Post-condition doesn't specify which index is returned if there are several // elements in arr equal to x. // But all elements in our array are distinct, so there is only one possible answer // and it should be equal to what Arrays.binarySearch returns. int idx2 = BinarySearch.binarySearch(x, arr); // call to tested method assertEquals(idx, idx2); // test succeeds if idx and idx2 are the same } @Test public void testMergesort() { int[] arr = {35, 25, 21, 19, 32, 39, 36, 42, 18, 8, 5, 2, 7, 13, 10, 17}; int[] arr2 = arr.clone(); // make a copy Arrays.sort(arr); // Sort arr with method in the API // which is hopefully bug-free. Sort.mergeSort(arr2); // call to tested method assertTrue(Arrays.equals(arr, arr2)); // test succeeds if the arrays are the same } }