diff --git a/src/main/java/com/packt/datastructuresandalg/lesson2/sorting/BinarySearchRecursive.java b/src/main/java/com/packt/datastructuresandalg/lesson2/sorting/BinarySearchRecursive.java index 08a209f..97b4c4c 100644 --- a/src/main/java/com/packt/datastructuresandalg/lesson2/sorting/BinarySearchRecursive.java +++ b/src/main/java/com/packt/datastructuresandalg/lesson2/sorting/BinarySearchRecursive.java @@ -3,7 +3,7 @@ public class BinarySearchRecursive { public boolean binarySearch(int x, int[] sortedNumbers) { - return binarySearch(x, sortedNumbers, 0, sortedNumbers.length); + return binarySearch(x, sortedNumbers, 0, sortedNumbers.length - 1); } public boolean binarySearch(int x, int[] sortedNumbers, int start, int end) { diff --git a/src/test/java/com/packt/datastructuresandalg/lesson2/sorting/BinarySearchRecursiveTest.java b/src/test/java/com/packt/datastructuresandalg/lesson2/sorting/BinarySearchRecursiveTest.java new file mode 100644 index 0000000..f5902a6 --- /dev/null +++ b/src/test/java/com/packt/datastructuresandalg/lesson2/sorting/BinarySearchRecursiveTest.java @@ -0,0 +1,44 @@ +package com.packt.datastructuresandalg.lesson2.sorting; + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class BinarySearchRecursiveTest { + + private BinarySearchRecursive sut; + @Before + public void setUp() { + sut = new BinarySearchRecursive(); + } + + @Test + public void shouldNotFindElementGreaterThanLastInputElement() { + boolean result = sut.binarySearch(12, new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + assertFalse("12 is not included in the input", result); + } + @Test + public void shouldNotFindElementSmallerThanFirstInputElement() { + boolean result = sut.binarySearch(0, new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + assertFalse("0 is not included in the input", result); + } + + @Test + public void shouldFindElementFromEndOfInput() { + boolean result = sut.binarySearch(10, new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + assertTrue("10 is included in the input", result); + } + @Test + public void shouldFindElementFromStartOfInput() { + boolean result = sut.binarySearch(1, new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + assertTrue("1 is included in the input", result); + } + + @Test + public void shouldFindElementInMiddleOfInput() { + boolean result = sut.binarySearch(7, new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + assertTrue("7 is included in the input", result); + } + +} \ No newline at end of file