|
| 1 | +import java.util.Arrays; |
| 2 | +import java.util.List; |
| 3 | + |
1 | 4 | import org.junit.Assert; |
2 | 5 | import org.junit.Test; |
3 | 6 |
|
4 | 7 |
|
5 | 8 | public class BinarySearchTest { |
6 | 9 |
|
| 10 | + int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 }; |
| 11 | + int key = 6; |
| 12 | + int expectedIndexForSearchKey = 7; |
| 13 | + int low = 0; |
| 14 | + int high = sortedArray.length - 1; |
| 15 | + List<Integer> sortedList = Arrays.asList(0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9); |
| 16 | + |
7 | 17 | @Test |
8 | | - public void givenASortedArrayOfIntegers_whenBinarySearchRunForANumber_thenGetIndexOfTheNumber() { |
| 18 | + public void givenASortedArrayOfIntegers_whenBinarySearchRunIterativelyForANumber_thenGetIndexOfTheNumber() { |
9 | 19 | BinarySearch binSearch = new BinarySearch(); |
10 | | - int expectedIndexForSearchKey = 7; |
11 | | - Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearch()); |
| 20 | + Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchIteratively(sortedArray, key, low, high)); |
12 | 21 | } |
13 | 22 |
|
| 23 | + @Test |
| 24 | + public void givenASortedArrayOfIntegers_whenBinarySearchRunRecursivelyForANumber_thenGetIndexOfTheNumber() { |
| 25 | + BinarySearch binSearch = new BinarySearch(); |
| 26 | + Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchRecursively(sortedArray, key, low, high)); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void givenASortedArrayOfIntegers_whenBinarySearchRunUsingArraysClassStaticMethodForANumber_thenGetIndexOfTheNumber() { |
| 31 | + BinarySearch binSearch = new BinarySearch(); |
| 32 | + Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaArrays(sortedArray, key)); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void givenASortedListOfIntegers_whenBinarySearchRunUsingCollectionsClassStaticMethodForANumber_thenGetIndexOfTheNumber() { |
| 37 | + BinarySearch binSearch = new BinarySearch(); |
| 38 | + Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaCollections(sortedList, key)); |
| 39 | + } |
| 40 | + |
14 | 41 | } |
0 commit comments