github.com/searKing/golang/go@v1.2.117/exp/slices/filter_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  	"reflect"
    10  	"slices"
    11  	"strconv"
    12  	"testing"
    13  
    14  	slices_ "github.com/searKing/golang/go/exp/slices"
    15  )
    16  
    17  func TestFilter(t *testing.T) {
    18  	tests := []struct {
    19  		data []int
    20  		want []int
    21  	}{
    22  		{nil, nil},
    23  		{[]int{}, []int{}},
    24  		{[]int{0}, []int{}},
    25  		{[]int{1, 0}, []int{1}},
    26  		{[]int{1, 2}, []int{1, 2}},
    27  		{[]int{0, 1, 2}, []int{1, 2}},
    28  		{[]int{0, 1, 0, 2}, []int{1, 2}},
    29  	}
    30  	for _, tt := range tests {
    31  		t.Run(fmt.Sprintf("%v", tt.data), func(t *testing.T) {
    32  			{
    33  				got := slices_.Filter(tt.data)
    34  				if (got == nil && tt.data != nil) || (got != nil && tt.data == nil) {
    35  					t.Errorf("slices_.Filter(%v) = %v, want %v", tt.data, got, tt.want)
    36  					return
    37  				}
    38  
    39  				if slices.Compare(got, tt.want) != 0 {
    40  					t.Errorf("slices_.Filter(%v) = %v, want %v", tt.data, got, tt.want)
    41  				}
    42  			}
    43  		})
    44  	}
    45  }
    46  
    47  func TestFilterFunc(t *testing.T) {
    48  	tests := []struct {
    49  		data []int
    50  		want []int
    51  	}{
    52  		{nil, nil},
    53  		{[]int{}, []int{}},
    54  		{[]int{0}, []int{}},
    55  		{[]int{1, 0}, []int{1}},
    56  		{[]int{1, 2}, []int{1, 2}},
    57  		{[]int{0, 1, 2}, []int{1, 2}},
    58  		{[]int{0, 1, 0, 2}, []int{1, 2}},
    59  	}
    60  	for _, tt := range tests {
    61  		t.Run(fmt.Sprintf("%v", tt.data), func(t *testing.T) {
    62  			{
    63  				copy := slices.Clone(tt.data)
    64  				got := slices_.FilterFunc(copy, func(e int) bool {
    65  					return e != 0
    66  				})
    67  				if (got == nil && tt.data != nil) || (got != nil && tt.data == nil) {
    68  					t.Errorf("slices_.FilterFunc(%v, func(e int) bool {return e != 0}) = %v, want %v", tt.data, got, tt.want)
    69  					return
    70  				}
    71  
    72  				if slices.Compare(got, tt.want) != 0 {
    73  					t.Errorf("slices_.FilterFunc(%v, func(e int) bool {return e != 0}) = %v, want %v", tt.data, got, tt.want)
    74  				}
    75  			}
    76  		})
    77  	}
    78  }
    79  
    80  func TestTypeAssertFilter(t *testing.T) {
    81  	tests := []struct {
    82  		data []int
    83  		want []int8
    84  	}{
    85  		{nil, nil},
    86  		{[]int{}, []int8{}},
    87  		{[]int{0}, []int8{0}},
    88  		{[]int{1, 0}, []int8{1, 0}},
    89  		{[]int{1, 2}, []int8{1, 2}},
    90  		{[]int{0, 1, 2}, []int8{0, 1, 2}},
    91  		{[]int{0, 1, 0, 2}, []int8{0, 1, 0, 2}},
    92  	}
    93  	for _, tt := range tests {
    94  		t.Run(fmt.Sprintf("%v", tt.data), func(t *testing.T) {
    95  			{
    96  				copy := slices.Clone(tt.data)
    97  				got := slices_.TypeAssertFilter[[]int, []int8](copy)
    98  				if (got == nil && tt.data != nil) || (got != nil && tt.data == nil) {
    99  					t.Errorf("slices_.TypeAssertFilter(%v) = %v, want %v", tt.data, got, tt.want)
   100  					return
   101  				}
   102  
   103  				if slices.Compare(got, tt.want) != 0 {
   104  					t.Errorf("slices_.TypeAssertFilter(%v) = %v, want %v", tt.data, got, tt.want)
   105  				}
   106  			}
   107  		})
   108  	}
   109  }
   110  
   111  func TestTypeAssertFilterConvert(t *testing.T) {
   112  	var zeroDog Dog
   113  	var zeroMale Male
   114  	_ = zeroDog
   115  	_ = zeroMale
   116  	tests := []struct {
   117  		data []any
   118  		want []Human
   119  	}{
   120  		{nil, nil},
   121  		{[]any{}, []Human{}},
   122  		{[]any{nil}, []Human{nil}},
   123  		{[]any{0, nil}, []Human{nil}},
   124  		{[]any{0, nil, 0, nil}, []Human{nil, nil}},
   125  		{[]any{1, 0, &Dog{}, &Male{}, Dog{}, Male{}}, []Human{&Male{}, Male{}}},
   126  		{[]any{1, 2, zeroDog, zeroMale}, []Human{Male{}}},
   127  		{[]any{zeroMale, zeroDog, 2, &zeroDog, 0, &zeroMale}, []Human{Male{}, &Male{}}},
   128  		{[]any{&zeroMale, &zeroDog, 2, zeroDog, 0, zeroMale}, []Human{&Male{}, Male{}}},
   129  	}
   130  	for _, tt := range tests {
   131  		t.Run(fmt.Sprintf("%v", tt.data), func(t *testing.T) {
   132  			{
   133  				copy := slices.Clone(tt.data)
   134  				got := slices_.TypeAssertFilter[[]any, []Human](copy)
   135  				if (got == nil && tt.data != nil) || (got != nil && tt.data == nil) {
   136  					t.Errorf("slices_.TypeAssertFilterFunc(%v, func(e any) (Human, bool) {h, ok := e.(Human); return h, ok}) = %v, want %v", tt.data, got, tt.want)
   137  					return
   138  				}
   139  
   140  				for i, v := range got {
   141  					if i >= len(tt.want) {
   142  						t.Errorf("slices_.TypeAssertFilterFunc(%v, func(e any) (Human, bool) {h, ok := e.(Human); return h, ok}) = %v, want %v", tt.data, got, tt.want)
   143  						return
   144  					}
   145  
   146  					if reflect.TypeOf(v) != reflect.TypeOf(tt.want[i]) {
   147  						t.Errorf("slices_.TypeAssertFilterFunc(%v, func(e any) (Human, bool) {h, ok := e.(Human); return h, ok}) =[%d]: %v, want %v", tt.data, i, reflect.TypeOf(v).String(), reflect.TypeOf(tt.want[i]).String())
   148  					}
   149  				}
   150  			}
   151  		})
   152  	}
   153  }
   154  
   155  func TestTypeAssertFilterFunc(t *testing.T) {
   156  	tests := []struct {
   157  		data []int
   158  		want []int8
   159  	}{
   160  		{nil, nil},
   161  		{[]int{}, []int8{}},
   162  		{[]int{0}, []int8{0}},
   163  		{[]int{1, 0}, []int8{1, 0}},
   164  		{[]int{1, 2}, []int8{1, 2}},
   165  		{[]int{0, 1, 2}, []int8{0, 1, 2}},
   166  		{[]int{0, 1, 0, 2}, []int8{0, 1, 0, 2}},
   167  	}
   168  	for _, tt := range tests {
   169  		t.Run(fmt.Sprintf("%v", tt.data), func(t *testing.T) {
   170  			{
   171  				copy := slices.Clone(tt.data)
   172  				got := slices_.TypeAssertFilterFunc[[]int, []int8](copy, func(e int) (int8, bool) {
   173  					return int8(e), true
   174  				})
   175  				if (got == nil && tt.data != nil) || (got != nil && tt.data == nil) {
   176  					t.Errorf("slices_.TypeAssertFilterFunc(%v, func(e int) (int8, bool) {return int8(e), true}) = %v, want %v", tt.data, got, tt.want)
   177  					return
   178  				}
   179  
   180  				if slices.Compare(got, tt.want) != 0 {
   181  					t.Errorf("slices_.TypeAssertFilterFunc(%v, func(e int) (int8, bool) {return int8(e), true}) = %v, want %v", tt.data, got, tt.want)
   182  					return
   183  				}
   184  			}
   185  		})
   186  	}
   187  }
   188  
   189  func TestTypeAssertFilterFuncItoa(t *testing.T) {
   190  	tests := []struct {
   191  		data []int
   192  		want []string
   193  	}{
   194  		{nil, nil},
   195  		{[]int{}, []string{}},
   196  		{[]int{0}, []string{"0"}},
   197  		{[]int{1, 0}, []string{"1", "0"}},
   198  		{[]int{1, 2}, []string{"1", "2"}},
   199  		{[]int{0, 1, 2}, []string{"0", "1", "2"}},
   200  		{[]int{0, 1, 0, 2}, []string{"0", "1", "0", "2"}},
   201  	}
   202  	for _, tt := range tests {
   203  		t.Run(fmt.Sprintf("%v", tt.data), func(t *testing.T) {
   204  			{
   205  				copy := slices.Clone(tt.data)
   206  				got := slices_.TypeAssertFilterFunc[[]int, []string](copy, func(e int) (string, bool) {
   207  					return strconv.Itoa(e), true
   208  				})
   209  				if (got == nil && tt.data != nil) || (got != nil && tt.data == nil) {
   210  					t.Errorf("slices_.TypeAssertFilterFunc(%v, func(e int) (string, bool) {return strconv.Itoa(e), true}) = %v, want %v", tt.data, got, tt.want)
   211  					return
   212  				}
   213  
   214  				if slices.Compare(got, tt.want) != 0 {
   215  					t.Errorf("slices_.TypeAssertFilterFunc(%v, func(e int) (string, bool) {return strconv.Itoa(e), true}) = %v, want %v", tt.data, got, tt.want)
   216  					return
   217  				}
   218  			}
   219  		})
   220  	}
   221  }
   222  
   223  func TestTypeAssertFilterFuncConvert(t *testing.T) {
   224  	var zeroDog Dog
   225  	var zeroMale Male
   226  	_ = zeroDog
   227  	_ = zeroMale
   228  	tests := []struct {
   229  		data []any
   230  		want []Human
   231  	}{
   232  		{nil, nil},
   233  		{[]any{}, []Human{}},
   234  		{[]any{0}, []Human{}},
   235  		{[]any{1, 0, &Dog{}, &Male{}}, []Human{&Male{}}},
   236  		{[]any{1, 2, zeroDog, zeroMale}, []Human{Male{}}},
   237  		{[]any{1, 2, zeroDog, 0, zeroMale, &zeroDog, 0, &zeroMale}, []Human{Male{}, &Male{}}},
   238  	}
   239  	for _, tt := range tests {
   240  		t.Run(fmt.Sprintf("%v", tt.data), func(t *testing.T) {
   241  			{
   242  				copy := slices.Clone(tt.data)
   243  				got := slices_.TypeAssertFilterFunc[[]any, []Human](copy, func(e any) (Human, bool) {
   244  					h, ok := e.(Human)
   245  					return h, ok
   246  				})
   247  				if (got == nil && tt.data != nil) || (got != nil && tt.data == nil) {
   248  					t.Errorf("slices_.TypeAssertFilterFunc(%v, func(e int) (int8, bool) {return int8(e), true}) = %v, want %v", tt.data, got, tt.want)
   249  					return
   250  				}
   251  
   252  				for _, v := range got {
   253  					if v.Kind() != "male" {
   254  						t.Errorf("slices_.TypeAssertFilterFunc(%v, func(e any) (Human, bool) {h, ok := e.(Human); return h, ok}) = %v, want %v", tt.data, got, tt.want)
   255  						return
   256  					}
   257  				}
   258  			}
   259  		})
   260  	}
   261  }
   262  
   263  type Animal interface {
   264  	Kind() string
   265  }
   266  type Human interface {
   267  	Name() string
   268  	Animal
   269  }
   270  
   271  var _ Human = (*Male)(nil)
   272  
   273  type Male struct{}
   274  
   275  func (m Male) Kind() string {
   276  	return "male"
   277  }
   278  
   279  func (Male) Name() string {
   280  	return "bob"
   281  }
   282  
   283  var _ Animal = (*Dog)(nil)
   284  
   285  type Dog struct{}
   286  
   287  func (d Dog) Kind() string {
   288  	return "dog"
   289  }