Create BinarySearchTest.java

master
wsy182 2024-07-09 22:46:10 +08:00
parent 438abeca95
commit 45b40688c7
1 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,63 @@
package com.nbee.solution.practice;
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);
}
@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);
}
@Test
public void testBinarySearchBasic_EmptyArray() {
int[] nums = {};
int target = 0;
int expected = -1;
int result = BinarySearch.binarySearchBasic(nums, target);
assertEquals(expected, result);
}
@Test
public void testBinarySearchBasic_SingleElementFound() {
int[] nums = {2};
int target = 2;
int expected = 0;
int result = BinarySearch.binarySearchBasic(nums, target);
assertEquals(expected, result);
}
@Test
public void testBinarySearchBasic_SingleElementNotFound() {
int[] nums = {2};
int target = 3;
int expected = -1;
int result = BinarySearch.binarySearchBasic(nums, target);
assertEquals(expected, result);
}
@Test
public void testBinarySearchBasic_DecreasingArray() {
int[] nums = {9, 7, 5, 3, 1};
int target = 3;
int expected = 2;
int result = BinarySearch.binarySearchBasic(nums, target);
assertEquals(expected, result);
}
}