github.com/searKing/golang/go@v1.2.117/exp/slices/union_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  	"fmt"
     9  	"slices"
    10  	"testing"
    11  
    12  	slices_ "github.com/searKing/golang/go/exp/slices"
    13  )
    14  
    15  var unionTests = []struct {
    16  	s1   []int
    17  	s2   []int
    18  	want []int
    19  }{
    20  	{
    21  		[]int{},
    22  		[]int{},
    23  		[]int{},
    24  	},
    25  	{
    26  		[]int{1, 2, 3},
    27  		[]int{-1, -2, -3},
    28  		[]int{1, 2, 3, -1, -2, -3},
    29  	},
    30  	{
    31  		[]int{1, 2, 3},
    32  		[]int{1, -2, -3},
    33  		[]int{1, 2, 3, -2, -3},
    34  	},
    35  	{
    36  		[]int{1, 2, 3},
    37  		[]int{1, -2, 3},
    38  		[]int{1, 2, 3, -2},
    39  	},
    40  	{
    41  		[]int{3, 2, 1},
    42  		[]int{-3, -2, -1},
    43  		[]int{3, 2, 1, -3, -2, -1},
    44  	},
    45  }
    46  
    47  func TestUnionFunc(t *testing.T) {
    48  	for _, tt := range unionTests {
    49  		t.Run(fmt.Sprintf("slices_.Union(%v, %v, equal[int])", tt.s1, tt.s2), func(t *testing.T) {
    50  			{
    51  				got := slices_.UnionFunc(tt.s1, tt.s2, equal[int])
    52  				if !slices.Equal(got, tt.want) {
    53  					t.Errorf("slices_.Union(%v, %v, equal[int]) = %v, want %v", tt.s1, tt.s2, got, tt.want)
    54  				}
    55  			}
    56  		})
    57  	}
    58  }
    59  
    60  func TestUnion(t *testing.T) {
    61  	for _, tt := range unionTests {
    62  		t.Run(fmt.Sprintf("slices_.Union(%v, %v)", tt.s1, tt.s2), func(t *testing.T) {
    63  			{
    64  				got := slices_.Union(tt.s1, tt.s2)
    65  				slices.Sort(got)
    66  				slices.Sort(tt.want)
    67  				if !slices.Equal(got, tt.want) {
    68  					t.Errorf("slices_.Union(%v, %v) = %v, want %v", tt.s1, tt.s2, got, tt.want)
    69  				}
    70  			}
    71  		})
    72  	}
    73  }