Compare commits

..

No commits in common. "c664823f9c22d478a7fc2b9a0a43e3ecea9bb1ff" and "45b40688c79178f74098a273ad80968a15eaf8a1" have entirely different histories.

2 changed files with 27 additions and 15 deletions

View File

@ -15,7 +15,7 @@
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.13.1</version> <version>4.12</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -1,51 +1,63 @@
package com.nbee.solution.practice; package com.nbee.solution.practice;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class BinarySearchTest { public class BinarySearchTest {
@Test @Test
public void testBinarySearchBasic_Found() { public void testBinarySearchBasic_Found() {
int[] nums = {1, 3, 5, 7, 9}; int[] nums = {1, 3, 5, 7, 9};
int target = 5; int target = 5;
int expected = 2;
int result = BinarySearch.binarySearchBasic(nums, target); int result = BinarySearch.binarySearchBasic(nums, target);
Assert.assertEquals("Element should be found at index 2", 2, result); assertEquals(expected, result);
} }
@Test @Test
public void testBinarySearchBasic_NotFound() { public void testBinarySearchBasic_NotFound() {
int[] nums = {1, 3, 5, 7, 9}; int[] nums = {1, 3, 5, 7, 9};
int target = 4; int target = 4;
int expected = -1;
int result = BinarySearch.binarySearchBasic(nums, target); int result = BinarySearch.binarySearchBasic(nums, target);
Assert.assertEquals("Element should not be found", -1, result); assertEquals(expected, result);
} }
@Test @Test
public void testBinarySearchBasic_EmptyArray() { public void testBinarySearchBasic_EmptyArray() {
int[] nums = {}; int[] nums = {};
int target = 1; int target = 0;
int expected = -1;
int result = BinarySearch.binarySearchBasic(nums, target); int result = BinarySearch.binarySearchBasic(nums, target);
Assert.assertEquals("Element should not be found in an empty array", -1, result); assertEquals(expected, result);
} }
@Test @Test
public void testBinarySearchBasic_SingleElementArray() { public void testBinarySearchBasic_SingleElementFound() {
int[] nums = {1}; int[] nums = {2};
int target = 1; int target = 2;
int expected = 0;
int result = BinarySearch.binarySearchBasic(nums, target); int result = BinarySearch.binarySearchBasic(nums, target);
Assert.assertEquals("Element should be found at index 0", 0, result); assertEquals(expected, result);
}
target = 2; @Test
result = BinarySearch.binarySearchBasic(nums, target); public void testBinarySearchBasic_SingleElementNotFound() {
Assert.assertEquals("Element should not be found", -1, result); int[] nums = {2};
int target = 3;
int expected = -1;
int result = BinarySearch.binarySearchBasic(nums, target);
assertEquals(expected, result);
} }
@Test @Test
public void testBinarySearchBasic_DecreasingArray() { public void testBinarySearchBasic_DecreasingArray() {
int[] nums = {9, 7, 5, 3, 1}; int[] nums = {9, 7, 5, 3, 1};
int target = 5; int target = 3;
int expected = 2;
int result = BinarySearch.binarySearchBasic(nums, target); int result = BinarySearch.binarySearchBasic(nums, target);
Assert.assertEquals("Binary search should work on decreasing arrays", 2, result); assertEquals(expected, result);
} }
} }