github.com/searKing/golang/go@v1.2.117/exp/slices/contains_test.go (about)

     1  // Copyright 2022 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package slices_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	slices_ "github.com/searKing/golang/go/exp/slices"
    11  )
    12  
    13  var containsTests = []struct {
    14  	s    []int
    15  	v    int
    16  	want int
    17  }{
    18  	{
    19  		nil,
    20  		0,
    21  		-1,
    22  	},
    23  	{
    24  		[]int{},
    25  		0,
    26  		-1,
    27  	},
    28  	{
    29  		[]int{1, 2, 3},
    30  		2,
    31  		1,
    32  	},
    33  	{
    34  		[]int{1, 2, 2, 3},
    35  		2,
    36  		1,
    37  	},
    38  	{
    39  		[]int{1, 2, 3, 2},
    40  		2,
    41  		1,
    42  	},
    43  }
    44  
    45  func equalToContains[T any](f func(T, T) bool, v1 T) func(T) bool {
    46  	return func(v2 T) bool {
    47  		return f(v1, v2)
    48  	}
    49  }
    50  
    51  func TestContainsFunc(t *testing.T) {
    52  	for _, test := range containsTests {
    53  		if got := slices_.ContainsFunc(test.s, equalToContains(equal[int], test.v)); got != (test.want != -1) {
    54  			t.Errorf("ContainsFunc(%v, equalToContains(equal[int], %v)) = %t, want %d", test.s, test.v, got, test.want)
    55  		}
    56  	}
    57  }
    58  
    59  func TestContains(t *testing.T) {
    60  	for _, test := range containsTests {
    61  		if got := slices_.Contains(test.s, test.v); got != (test.want != -1) {
    62  			t.Errorf("Contains(%v, %v) = %t, want %t", test.s, test.v, got, test.want != -1)
    63  		}
    64  	}
    65  }