github.com/hamba/slices@v0.2.1-0.20220316050741-75c057d92699/contains_test.go (about)

     1  package slices_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hamba/slices"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestContains(t *testing.T) {
    11  	tests := []struct {
    12  		name  string
    13  		slice interface{}
    14  		val   interface{}
    15  		want  bool
    16  	}{
    17  		{
    18  			name:  "bool contains",
    19  			slice: []bool{false, false, true},
    20  			val:   true,
    21  			want:  true,
    22  		},
    23  		{
    24  			name:  "bool not contains",
    25  			slice: []bool{false, false, false},
    26  			val:   true,
    27  			want:  false,
    28  		},
    29  		{
    30  			name:  "string contains",
    31  			slice: []string{"foo", "bar", "baz", "bat"},
    32  			val:   "bat",
    33  			want:  true,
    34  		},
    35  		{
    36  			name:  "string not contains",
    37  			slice: []string{"foo", "bar", "baz", "bat"},
    38  			val:   "test",
    39  			want:  false,
    40  		},
    41  		{
    42  			name:  "int contains",
    43  			slice: []int{1, 2, 3, 4},
    44  			val:   4,
    45  			want:  true,
    46  		},
    47  		{
    48  			name:  "int not contains",
    49  			slice: []int{1, 2, 3, 4},
    50  			val:   5,
    51  			want:  false,
    52  		},
    53  		{
    54  			name:  "int8 contains",
    55  			slice: []int8{1, 2, 3, 4},
    56  			val:   int8(4),
    57  			want:  true,
    58  		},
    59  		{
    60  			name:  "int8 not contains",
    61  			slice: []int8{1, 2, 3, 4},
    62  			val:   int8(5),
    63  			want:  false,
    64  		},
    65  		{
    66  			name:  "int16 contains",
    67  			slice: []int16{1, 2, 3, 4},
    68  			val:   int16(4),
    69  			want:  true,
    70  		},
    71  		{
    72  			name:  "int16 not contains",
    73  			slice: []int16{1, 2, 3, 4},
    74  			val:   int16(5),
    75  			want:  false,
    76  		},
    77  		{
    78  			name:  "int32 contains",
    79  			slice: []int32{1, 2, 3, 4},
    80  			val:   int32(4),
    81  			want:  true,
    82  		},
    83  		{
    84  			name:  "int32 not contains",
    85  			slice: []int32{1, 2, 3, 4},
    86  			val:   int32(5),
    87  			want:  false,
    88  		},
    89  		{
    90  			name:  "int64 contains",
    91  			slice: []int64{1, 2, 3, 4},
    92  			val:   int64(4),
    93  			want:  true,
    94  		},
    95  		{
    96  			name:  "int64 not contains",
    97  			slice: []int64{1, 2, 3, 4},
    98  			val:   int64(5),
    99  			want:  false,
   100  		},
   101  		{
   102  			name:  "uint contains",
   103  			slice: []uint{1, 2, 3, 4},
   104  			val:   uint(4),
   105  			want:  true,
   106  		},
   107  		{
   108  			name:  "uint not contains",
   109  			slice: []uint{1, 2, 3, 4},
   110  			val:   uint(5),
   111  			want:  false,
   112  		},
   113  		{
   114  			name:  "uint8 contains",
   115  			slice: []uint8{1, 2, 3, 4},
   116  			val:   uint8(4),
   117  			want:  true,
   118  		},
   119  		{
   120  			name:  "uint8 not contains",
   121  			slice: []uint8{1, 2, 3, 4},
   122  			val:   uint8(5),
   123  			want:  false,
   124  		},
   125  		{
   126  			name:  "uint16 contains",
   127  			slice: []uint16{1, 2, 3, 4},
   128  			val:   uint16(4),
   129  			want:  true,
   130  		},
   131  		{
   132  			name:  "uint16 not contains",
   133  			slice: []uint16{1, 2, 3, 4},
   134  			val:   uint16(5),
   135  			want:  false,
   136  		},
   137  		{
   138  			name:  "uint32 contains",
   139  			slice: []uint32{1, 2, 3, 4},
   140  			val:   uint32(4),
   141  			want:  true,
   142  		},
   143  		{
   144  			name:  "uint32 not contains",
   145  			slice: []uint32{1, 2, 3, 4},
   146  			val:   uint32(5),
   147  			want:  false,
   148  		},
   149  		{
   150  			name:  "uint64 contains",
   151  			slice: []uint64{1, 2, 3, 4},
   152  			val:   uint64(4),
   153  			want:  true,
   154  		},
   155  		{
   156  			name:  "uint64 not contains",
   157  			slice: []uint64{1, 2, 3, 4},
   158  			val:   uint64(5),
   159  			want:  false,
   160  		},
   161  		{
   162  			name:  "float32 contains",
   163  			slice: []float32{1, 2, 3, 4},
   164  			val:   float32(4),
   165  			want:  true,
   166  		},
   167  		{
   168  			name:  "float32 not contains",
   169  			slice: []float32{1, 2, 3, 4},
   170  			val:   float32(5),
   171  			want:  false,
   172  		},
   173  		{
   174  			name:  "float64 contains",
   175  			slice: []float64{1, 2, 3, 4},
   176  			val:   float64(4),
   177  			want:  true,
   178  		},
   179  		{
   180  			name:  "float64 not contains",
   181  			slice: []float64{1, 2, 3, 4},
   182  			val:   float64(5),
   183  			want:  false,
   184  		},
   185  	}
   186  
   187  	for _, tt := range tests {
   188  		t.Run(tt.name, func(t *testing.T) {
   189  			got := slices.Contains(tt.slice, tt.val)
   190  
   191  			assert.Equal(t, tt.want, got)
   192  		})
   193  	}
   194  }
   195  
   196  func TestContains_ChecksSliceType(t *testing.T) {
   197  	assert.Panics(t, func() {
   198  		slices.Contains("test", "test")
   199  	})
   200  }
   201  
   202  func TestContains_ChecksValType(t *testing.T) {
   203  	assert.Panics(t, func() {
   204  		slices.Contains([]string{"test"}, 1)
   205  	})
   206  }
   207  
   208  func BenchmarkContains(b *testing.B) {
   209  	slice := []string{"foo", "bar", "baz", "bat"}
   210  	val := "bat"
   211  
   212  	b.ReportAllocs()
   213  	b.ResetTimer()
   214  	for i := 0; i < b.N; i++ {
   215  		slices.Contains(slice, val)
   216  	}
   217  }
   218  
   219  func contains(slice []string, val string) bool {
   220  	for _, vv := range slice {
   221  		if vv == val {
   222  			return true
   223  		}
   224  	}
   225  	return false
   226  }
   227  
   228  func BenchmarkContainsNative(b *testing.B) {
   229  	slice := []string{"foo", "bar", "baz", "bat"}
   230  	val := "bat"
   231  
   232  	b.ReportAllocs()
   233  	b.ResetTimer()
   234  	for i := 0; i < b.N; i++ {
   235  		contains(slice, val)
   236  	}
   237  }