github.com/searKing/golang/go@v1.2.117/exp/slices/uniq_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  	"slices"
     9  	"strings"
    10  	"testing"
    11  
    12  	slices_ "github.com/searKing/golang/go/exp/slices"
    13  )
    14  
    15  var uniqTests = []struct {
    16  	s    []int
    17  	want []int
    18  }{
    19  	{
    20  		nil,
    21  		nil,
    22  	},
    23  	{
    24  		[]int{1},
    25  		[]int{1},
    26  	},
    27  	{
    28  		[]int{1, 2, 3},
    29  		[]int{1, 2, 3},
    30  	},
    31  	{
    32  		[]int{1, 1, 2},
    33  		[]int{1, 2},
    34  	},
    35  	{
    36  		[]int{1, 2, 1},
    37  		[]int{1, 2},
    38  	},
    39  	{
    40  		[]int{1, 2, 2, 3, 3, 4},
    41  		[]int{1, 2, 3, 4},
    42  	},
    43  }
    44  
    45  func TestUniq(t *testing.T) {
    46  	for _, test := range uniqTests {
    47  		copy := slices.Clone(test.s)
    48  		got := slices_.Uniq(copy)
    49  		slices.Sort(got)
    50  		slices.Sort(test.want)
    51  		if !slices.Equal(got, test.want) {
    52  			t.Errorf("Uniq(%v) = %v, want %v", test.s, got, test.want)
    53  		}
    54  	}
    55  }
    56  
    57  // equal is simply ==.
    58  func equal[T comparable](v1, v2 T) bool {
    59  	return v1 == v2
    60  }
    61  
    62  func TestUniqFunc(t *testing.T) {
    63  	for _, test := range uniqTests {
    64  		copy := slices.Clone(test.s)
    65  		if got := slices_.UniqFunc(copy, equal[int]); !slices.Equal(got, test.want) {
    66  			t.Errorf("UniqFunc(%v, equal[int]) = %v, want %v", test.s, got, test.want)
    67  		}
    68  	}
    69  
    70  	s1 := []string{"a", "a", "A", "B", "b"}
    71  	want := []string{"a", "B"}
    72  	if got := slices_.UniqFunc(s1, strings.EqualFold); !slices.Equal(got, want) {
    73  		t.Errorf("UniqFunc(%v, strings.EqualFold) = %v, want %v", s1, got, want)
    74  	}
    75  }