Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make sure to add correct end boundary for input #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}