Testing Binary Search Recursive Implementation in hello-algo
This test suite validates the binary search recursive implementation in Go, focusing on both successful and unsuccessful search scenarios. The tests verify the algorithm’s ability to find target elements in a sorted array and handle cases where the target is not present.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
krahets/hello-algo
zh-hant/codes/go/chapter_divide_and_conquer/binary_search_recur_test.go
// File: binary_search_recur_test.go
// Created Time: 2023-07-19
// Author: hongyun-robot ([email protected])
package chapter_divide_and_conquer
import (
"fmt"
"testing"
)
func TestBinarySearch(t *testing.T) {
nums := []int{1, 3, 6, 8, 12, 15, 23, 26, 31, 35}
target := 6
noTarget := 99
targetIndex := binarySearch(nums, target)
fmt.Println("目標元素 6 的索引 = ", targetIndex)
noTargetIndex := binarySearch(nums, noTarget)
fmt.Println("不存在目標元素的索引 = ", noTargetIndex)
}