github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/util/slice_test.go (about) 1 package util 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestInSlice(t *testing.T) { 10 type args struct { 11 ss []int 12 s int 13 } 14 tests := []struct { 15 name string 16 args args 17 want bool 18 }{ 19 {name: "InSlice-true", args: args{ 20 ss: []int{1, 2, 3}, 21 s: 1, 22 }, want: true}, 23 {name: "InSlice-false", args: args{ 24 ss: []int{1, 2, 3}, 25 s: 4, 26 }, want: false}, 27 } 28 for _, tt := range tests { 29 t.Run(tt.name, func(t *testing.T) { 30 assert.Equalf(t, tt.want, InSliceInt(tt.args.s, tt.args.ss), "InSlice(%v, %v)", tt.args.ss, tt.args.s) 31 }) 32 } 33 } 34 35 func TestSliceBigFilter(t *testing.T) { 36 type args struct { 37 a []int 38 f func(v int) bool 39 } 40 tests := []struct { 41 name string 42 args args 43 want []int 44 }{ 45 {name: "SliceBigFilter", args: args{ 46 a: []int{1, 2, 3}, 47 f: func(v int) bool { 48 return v > 2 49 }, 50 }, want: []int{3}}, 51 } 52 for _, tt := range tests { 53 t.Run(tt.name, func(t *testing.T) { 54 assert.Equalf(t, tt.want, SliceBigFilter(tt.args.a, tt.args.f), "SliceBigFilter(%v)", tt.args.a) 55 }) 56 } 57 } 58 59 func TestSliceDeleteElem(t *testing.T) { 60 type args struct { 61 i int 62 s []any 63 } 64 tests := []struct { 65 name string 66 args args 67 want []any 68 }{ 69 {name: "SliceDeleteElem", args: args{ 70 i: 1, 71 s: []any{1, 2, 3}, 72 }, want: []any{1, 3}}, 73 } 74 for _, tt := range tests { 75 t.Run(tt.name, func(t *testing.T) { 76 s, err := SliceDeleteElem(tt.args.i, tt.args.s) 77 assert.Nil(t, err) 78 assert.Equal(t, tt.want, s) 79 }) 80 } 81 } 82 83 func TestSliceIntJoin(t *testing.T) { 84 type args struct { 85 s []int 86 sep string 87 } 88 tests := []struct { 89 name string 90 args args 91 want string 92 }{ 93 {name: "SliceIntJoin", args: args{ 94 s: []int{1, 2, 3}, 95 sep: "-", 96 }, want: "1-2-3"}, 97 } 98 for _, tt := range tests { 99 t.Run(tt.name, func(t *testing.T) { 100 assert.Equalf(t, tt.want, SliceIntJoin(tt.args.s, tt.args.sep), "SliceIntJoin(%v, %v)", tt.args.s, tt.args.sep) 101 }) 102 } 103 } 104 105 func TestSliceReverse(t *testing.T) { 106 type args struct { 107 s []any 108 } 109 tests := []struct { 110 name string 111 args args 112 want []any 113 }{ 114 {name: "SliceReverse", args: args{s: []any{1, 2, 3}}, want: []any{3, 2, 1}}, 115 } 116 for _, tt := range tests { 117 t.Run(tt.name, func(t *testing.T) { 118 SliceReverse(tt.args.s) 119 assert.Equalf(t, tt.want, tt.args.s, "SliceReverse(%v)", tt.args.s) 120 }) 121 } 122 } 123 124 func TestSliceShuffle(t *testing.T) { 125 type args struct { 126 s []any 127 } 128 tests := []struct { 129 name string 130 args args 131 }{ 132 {name: "SliceShuffle", args: args{s: []any{1, 2, 3}}}, 133 } 134 for _, tt := range tests { 135 t.Run(tt.name, func(t *testing.T) { 136 SliceShuffle(tt.args.s) 137 t.Logf("shuffle:%v", tt.args.s) 138 }) 139 } 140 } 141 142 func TestSliceSmallFilter(t *testing.T) { 143 type args struct { 144 a []int 145 f func(v int) bool 146 } 147 tests := []struct { 148 name string 149 args args 150 want []int 151 }{ 152 {name: "SliceSmallFilter", args: args{ 153 a: []int{1, 2, 3}, 154 f: func(v int) bool { 155 return v > 2 156 }, 157 }, want: []int{3}}, 158 } 159 for _, tt := range tests { 160 t.Run(tt.name, func(t *testing.T) { 161 assert.Equalf(t, tt.want, SliceSmallFilter(tt.args.a, tt.args.f), "SliceSmallFilter(%v)", tt.args.a) 162 }) 163 } 164 } 165 166 func TestSliceToInt(t *testing.T) { 167 type args struct { 168 ss []string 169 } 170 tests := []struct { 171 name string 172 args args 173 wantIi []int 174 }{ 175 {name: "", args: args{ss: []string{"1", "2", "a", "123a"}}, wantIi: []int{1, 2, 0, 0}}, 176 } 177 for _, tt := range tests { 178 t.Run(tt.name, func(t *testing.T) { 179 assert.Equalf(t, tt.wantIi, SliceToInt(tt.args.ss), "SliceToInt(%v)", tt.args.ss) 180 }) 181 } 182 } 183 184 func TestSliceIntDeduplication(t *testing.T) { 185 type args struct { 186 a []int 187 } 188 tests := []struct { 189 name string 190 args args 191 want []int 192 }{ 193 { 194 name: "TestSliceIntDeduplication", 195 args: args{a: []int{1, 1, 2, 2, 3, 3, 4, 5}}, 196 want: []int{1, 2, 3, 4, 5}, 197 }, 198 } 199 for _, tt := range tests { 200 t.Run(tt.name, func(t *testing.T) { 201 assert.Equalf(t, tt.want, SliceIntDeduplication(tt.args.a), "SliceIntDeduplication(%v)", tt.args.a) 202 }) 203 } 204 }