From 652b09135c8294b3e14d723cb87966f5d1f7ee6b Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 28 Dec 2022 17:58:56 +0800 Subject: [PATCH] test: add examples for search function --- algorithm/search.go | 18 ++++++----- algorithm/search_example_test.go | 51 ++++++++++++++++++++++++++++++++ algorithm/search_test.go | 16 ++++++---- 3 files changed, 72 insertions(+), 13 deletions(-) create mode 100644 algorithm/search_example_test.go diff --git a/algorithm/search.go b/algorithm/search.go index 73c2ebc..73a01b7 100644 --- a/algorithm/search.go +++ b/algorithm/search.go @@ -8,19 +8,20 @@ import "github.com/duke-git/lancet/v2/lancetconstraints" // Search algorithms see https://github.com/TheAlgorithms/Go/tree/master/search -// LinearSearch Simple linear search algorithm that iterates over all elements of an slice -// If a target is found, the index of the target is returned. Else the function return -1 -func LinearSearch[T any](slice []T, target T, comparator lancetconstraints.Comparator) int { +// LinearSearch return the index of target in slice base on equal function. +// If not found return -1 +func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int { for i, v := range slice { - if comparator.Compare(v, target) == 0 { + if equal(v, target) { return i } } return -1 } -// BinarySearch search for target within a sorted slice, recursive call itself. -// If a target is found, the index of the target is returned. Else the function return -1 +// BinarySearch return the index of target within a sorted slice, use binary search (recursive call itself). +// If not found return -1. +// Play: https://go.dev/play/p/t6MeGiUSN47 func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int { if highIndex < lowIndex || len(sortedSlice) == 0 { return -1 @@ -39,8 +40,9 @@ func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, com return midIndex } -// BinaryIterativeSearch search for target within a sorted slice. -// If a target is found, the index of the target is returned. Else the function return -1 +// BinaryIterativeSearch return the index of target within a sorted slice, use binary search (no recursive). +// If not found return -1. +// Play: https://go.dev/play/p/Anozfr8ZLH3 func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int { startIndex := lowIndex endIndex := highIndex diff --git a/algorithm/search_example_test.go b/algorithm/search_example_test.go new file mode 100644 index 0000000..7d5583b --- /dev/null +++ b/algorithm/search_example_test.go @@ -0,0 +1,51 @@ +package algorithm + +import "fmt" + +func ExampleLinearSearch() { + numbers := []int{3, 4, 5, 3, 2, 1} + + equalFunc := func(a, b int) bool { + return a == b + } + + result1 := LinearSearch(numbers, 3, equalFunc) + result2 := LinearSearch(numbers, 6, equalFunc) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 0 + // -1 +} + +func ExampleBinarySearch() { + numbers := []int{1, 2, 3, 4, 5, 6, 7, 8} + comparator := &intComparator{} + + result1 := BinarySearch(numbers, 5, 0, len(numbers)-1, comparator) + result2 := BinarySearch(numbers, 9, 0, len(numbers)-1, comparator) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 4 + // -1 +} + +func ExampleBinaryIterativeSearch() { + numbers := []int{1, 2, 3, 4, 5, 6, 7, 8} + comparator := &intComparator{} + + result1 := BinaryIterativeSearch(numbers, 5, 0, len(numbers)-1, comparator) + result2 := BinaryIterativeSearch(numbers, 9, 0, len(numbers)-1, comparator) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 4 + // -1 +} diff --git a/algorithm/search_test.go b/algorithm/search_test.go index cd31800..2ee1f35 100644 --- a/algorithm/search_test.go +++ b/algorithm/search_test.go @@ -6,20 +6,24 @@ import ( "github.com/duke-git/lancet/v2/internal" ) -var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8} - func TestLinearSearch(t *testing.T) { asssert := internal.NewAssert(t, "TestLinearSearch") - comparator := &intComparator{} - asssert.Equal(4, LinearSearch(sortedNumbers, 5, comparator)) - asssert.Equal(-1, LinearSearch(sortedNumbers, 9, comparator)) + numbers := []int{3, 4, 5, 3, 2, 1} + equalFunc := func(a, b int) bool { + return a == b + } + + asssert.Equal(0, LinearSearch(numbers, 3, equalFunc)) + asssert.Equal(-1, LinearSearch(numbers, 6, equalFunc)) } func TestBinarySearch(t *testing.T) { asssert := internal.NewAssert(t, "TestBinarySearch") + sortedNumbers := []int{1, 2, 3, 4, 5, 6, 7, 8} comparator := &intComparator{} + asssert.Equal(4, BinarySearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)) asssert.Equal(-1, BinarySearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator)) } @@ -27,7 +31,9 @@ func TestBinarySearch(t *testing.T) { func TestBinaryIterativeSearch(t *testing.T) { asssert := internal.NewAssert(t, "TestBinaryIterativeSearch") + sortedNumbers := []int{1, 2, 3, 4, 5, 6, 7, 8} comparator := &intComparator{} + asssert.Equal(4, BinaryIterativeSearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)) asssert.Equal(-1, BinaryIterativeSearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator)) }