Update BinarySearchTest.java
parent
022850f76c
commit
b5059f6996
|
|
@ -2,6 +2,10 @@ package com.nbee.solution.practice;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
public class BinarySearchTest {
|
public class BinarySearchTest {
|
||||||
|
|
@ -50,6 +54,7 @@ public class BinarySearchTest {
|
||||||
int result = BinarySearch.binarySearchBasic(nums, target);
|
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||||
Assert.assertEquals("Binary search should work on decreasing arrays", 2, result);
|
Assert.assertEquals("Binary search should work on decreasing arrays", 2, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBinarySearchAlternativeFound() {
|
public void testBinarySearchAlternativeFound() {
|
||||||
// Test that the method correctly returns the index of an existing element
|
// Test that the method correctly returns the index of an existing element
|
||||||
|
|
@ -84,4 +89,41 @@ public class BinarySearchTest {
|
||||||
assertEquals(0, result, "The target should be found at index 0 in a single-element array");
|
assertEquals(0, result, "The target should be found at index 0 in a single-element array");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Test Binary Search Java")
|
||||||
|
public void testBinarySearchJava() {
|
||||||
|
int[] nums = {1, 3, 5, 7, 9};
|
||||||
|
int target = 4;
|
||||||
|
int result = Arrays.binarySearch(nums, target);
|
||||||
|
if (result < 0) {
|
||||||
|
int insertIndex = Math.abs(result + 1);
|
||||||
|
System.out.printf("nums: " + Arrays.toString(nums) + "\n");
|
||||||
|
System.out.printf("insertIndex: " + insertIndex + "\n");
|
||||||
|
int[] newNums = new int[nums.length + 1];
|
||||||
|
//第一次拷贝
|
||||||
|
System.arraycopy(nums, 0, newNums, 0, nums.length);
|
||||||
|
System.out.printf("newNums: " + Arrays.toString(newNums) + "\n");
|
||||||
|
newNums[insertIndex] = target;
|
||||||
|
// 第二次拷贝
|
||||||
|
System.arraycopy(nums, insertIndex, newNums, insertIndex + 1, nums.length - insertIndex);
|
||||||
|
System.out.printf("newNums: " + Arrays.toString(newNums) + "\n");
|
||||||
|
}
|
||||||
|
System.out.printf("result: " + result);
|
||||||
|
assertEquals(-3, result, "The target should not be found");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchLeftMost() {
|
||||||
|
int[] nums = new int[]{1, 3, 3, 5, 7};
|
||||||
|
assertEquals(0, BinarySearch.binarySearchLeftMost(nums, 1));
|
||||||
|
assertEquals(1, BinarySearch.binarySearchLeftMost(nums, 3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchRightMost() {
|
||||||
|
int[] nums = new int[]{1, 3, 3, 5, 5, 5, 5, 5, 7};
|
||||||
|
assertEquals(0, BinarySearch.binarySearchRightMost(nums, 1));
|
||||||
|
assertEquals(2, BinarySearch.binarySearchRightMost(nums, 3));
|
||||||
|
assertEquals(7, BinarySearch.binarySearchRightMost(nums, 5));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue