Compare commits

..

2 Commits

Author SHA1 Message Date
wsy182 c664823f9c Update BinarySearchTest.java 2024-07-09 22:48:47 +08:00
wsy182 2c2ee20dd2 Update pom.xml 2024-07-09 22:48:44 +08:00
2 changed files with 15 additions and 27 deletions

View File

@ -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>

View File

@ -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);
}
}