You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
519 B
25 lines
519 B
2 years ago
|
// File: binary_search_test.go
|
||
2 years ago
|
// Created Time: 2022-12-05
|
||
2 years ago
|
// Author: Slone123c (274325721@qq.com)
|
||
|
|
||
2 years ago
|
package chapter_binary_search
|
||
2 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestBinarySearch(t *testing.T) {
|
||
|
var (
|
||
|
target = 3
|
||
|
nums = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||
|
expected = 2
|
||
|
)
|
||
|
// 在数组中执行二分查找
|
||
|
actual := binarySearch(nums, target)
|
||
|
fmt.Println("目标元素 3 的索引 =", actual)
|
||
|
if actual != expected {
|
||
|
t.Errorf("目标元素 3 的索引 = %d, 应该为 %d", actual, expected)
|
||
|
}
|
||
|
}
|