mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(package): [algorithm] handle empty iterative searches (#399)
This commit is contained in:
1 parent
13b7b1c61e
commit
e49a01e725
2 files changed
+14
No files matched your search
@@ -45,6 +45,9 @@ func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, com
|
||||
// If not found return -1.
|
||||
// Play: https://go.dev/play/p/Anozfr8ZLH3
|
||||
func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator constraints.Comparator) int {
|
||||
if len(sortedSlice) == 0 {
|
||||
return -1
|
||||
}
|
||||
startIndex := lowIndex
|
||||
endIndex := highIndex
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package algorithm
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestBinaryIterativeSearchEmpty(t *testing.T) {
|
||||
for _, source := range [][]int{nil, {}} {
|
||||
if got := BinaryIterativeSearch(source, 1, 0, 0, &intComparator{}); got != -1 {
|
||||
t.Errorf("got %d, want -1", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user