v8.run/go/exp@v0.0.26-0.20230226010534-afcdbd3f782d/util/slice/search_test.go (about)

     1  package slice
     2  
     3  import "testing"
     4  
     5  func TestSearch(t *testing.T) {
     6  	t.Run("empty", func(t *testing.T) {
     7  		if Search([]int{}, 0) != -1 {
     8  			t.Error("empty slice should return -1")
     9  		}
    10  	})
    11  
    12  	t.Run("no match", func(t *testing.T) {
    13  		if Search([]int{1, 2, 3}, 0) != -1 {
    14  			t.Error("not found should return -1")
    15  		}
    16  	})
    17  
    18  	t.Run("single match", func(t *testing.T) {
    19  		if Search([]int{1, 2, 3}, 2) != 1 {
    20  			t.Error("found should return index")
    21  		}
    22  	})
    23  
    24  	t.Run("multiple matches", func(t *testing.T) {
    25  		if Search([]int{1, 2, 2, 3}, 2) != 1 {
    26  			t.Error("found should return first index")
    27  		}
    28  	})
    29  }