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> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.12</version> <version>4.13.1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -1,63 +1,51 @@
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);
assertEquals(expected, result); Assert.assertEquals("Element should be found at index 2", 2, 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);
assertEquals(expected, result); Assert.assertEquals("Element should not be found", -1, result);
} }
@Test @Test
public void testBinarySearchBasic_EmptyArray() { public void testBinarySearchBasic_EmptyArray() {
int[] nums = {}; int[] nums = {};
int target = 0; int target = 1;
int expected = -1;
int result = BinarySearch.binarySearchBasic(nums, target); int result = BinarySearch.binarySearchBasic(nums, target);
assertEquals(expected, result); Assert.assertEquals("Element should not be found in an empty array", -1, result);
} }
@Test @Test
public void testBinarySearchBasic_SingleElementFound() { public void testBinarySearchBasic_SingleElementArray() {
int[] nums = {2}; int[] nums = {1};
int target = 2; int target = 1;
int expected = 0;
int result = BinarySearch.binarySearchBasic(nums, target); int result = BinarySearch.binarySearchBasic(nums, target);
assertEquals(expected, result); Assert.assertEquals("Element should be found at index 0", 0, result);
}
@Test target = 2;
public void testBinarySearchBasic_SingleElementNotFound() { result = BinarySearch.binarySearchBasic(nums, target);
int[] nums = {2}; Assert.assertEquals("Element should not be found", -1, result);
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 = 3; int target = 5;
int expected = 2;
int result = BinarySearch.binarySearchBasic(nums, target); int result = BinarySearch.binarySearchBasic(nums, target);
assertEquals(expected, result); Assert.assertEquals("Binary search should work on decreasing arrays", 2, result);
} }
} }