23 lines
570 B
Go
23 lines
570 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestBinarySearchWithWorkingTarget(t *testing.T) {
|
||
|
nums := []int{2, 3, 5, 7, 8}
|
||
|
target := 7
|
||
|
resu, err := binarySearch(nums, target)
|
||
|
if err != nil || resu != 3 {
|
||
|
t.Errorf("Test failed! expected: %v, but got: %v", 3, resu)
|
||
|
}
|
||
|
}
|
||
|
func TestBinarySearchWithNonWorkingTarget(t *testing.T) {
|
||
|
nums := []int{1, 4, 5, 8, 9}
|
||
|
target := 2
|
||
|
resu, err := binarySearch(nums, target)
|
||
|
if err == nil || resu != -1 {
|
||
|
t.Errorf("Test failed! expected: %v, but got: %v", "binarySearch: your target is not found in the given array", err)
|
||
|
}
|
||
|
}
|