Compare commits
2 Commits
45b40688c7
...
c664823f9c
| Author | SHA1 | Date |
|---|---|---|
|
|
c664823f9c | |
|
|
2c2ee20dd2 |
2
pom.xml
2
pom.xml
|
|
@ -15,7 +15,7 @@
|
|||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<version>4.13.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
|
|
|||
|
|
@ -1,63 +1,51 @@
|
|||
package com.nbee.solution.practice;
|
||||
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class BinarySearchTest {
|
||||
|
||||
@Test
|
||||
public void testBinarySearchBasic_Found() {
|
||||
int[] nums = {1, 3, 5, 7, 9};
|
||||
int target = 5;
|
||||
int expected = 2;
|
||||
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||
assertEquals(expected, result);
|
||||
Assert.assertEquals("Element should be found at index 2", 2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBinarySearchBasic_NotFound() {
|
||||
int[] nums = {1, 3, 5, 7, 9};
|
||||
int target = 4;
|
||||
int expected = -1;
|
||||
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||
assertEquals(expected, result);
|
||||
Assert.assertEquals("Element should not be found", -1, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBinarySearchBasic_EmptyArray() {
|
||||
int[] nums = {};
|
||||
int target = 0;
|
||||
int expected = -1;
|
||||
int target = 1;
|
||||
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||
assertEquals(expected, result);
|
||||
Assert.assertEquals("Element should not be found in an empty array", -1, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBinarySearchBasic_SingleElementFound() {
|
||||
int[] nums = {2};
|
||||
int target = 2;
|
||||
int expected = 0;
|
||||
public void testBinarySearchBasic_SingleElementArray() {
|
||||
int[] nums = {1};
|
||||
int target = 1;
|
||||
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
Assert.assertEquals("Element should be found at index 0", 0, result);
|
||||
|
||||
@Test
|
||||
public void testBinarySearchBasic_SingleElementNotFound() {
|
||||
int[] nums = {2};
|
||||
int target = 3;
|
||||
int expected = -1;
|
||||
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||
assertEquals(expected, result);
|
||||
target = 2;
|
||||
result = BinarySearch.binarySearchBasic(nums, target);
|
||||
Assert.assertEquals("Element should not be found", -1, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBinarySearchBasic_DecreasingArray() {
|
||||
int[] nums = {9, 7, 5, 3, 1};
|
||||
int target = 3;
|
||||
int expected = 2;
|
||||
int target = 5;
|
||||
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||
assertEquals(expected, result);
|
||||
Assert.assertEquals("Binary search should work on decreasing arrays", 2, result);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue