Use slices package from the stdlib when possible (#5360)

* Use slices from the stdlib when possible

* Add some unit tests

* More small tweaks + add benchmark func
This commit is contained in:
its-josh4
2024-10-28 17:26:23 -07:00
committed by GitHub
parent 093de3bce2
commit c6bcdd89be
38 changed files with 200 additions and 110 deletions

View File

@@ -1,6 +1,7 @@
package sliceutil
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
@@ -66,3 +67,85 @@ func TestSliceSame(t *testing.T) {
})
}
}
func TestAppendUniques(t *testing.T) {
type args struct {
vs []int
toAdd []int
}
tests := []struct {
name string
args args
want []int
}{
{
name: "append to empty slice",
args: args{
vs: []int{},
toAdd: []int{1, 2, 3},
},
want: []int{1, 2, 3},
},
{
name: "append all unique values",
args: args{
vs: []int{1, 2, 3},
toAdd: []int{4, 5, 6},
},
want: []int{1, 2, 3, 4, 5, 6},
},
{
name: "append with some duplicates",
args: args{
vs: []int{1, 2, 3},
toAdd: []int{3, 4, 5},
},
want: []int{1, 2, 3, 4, 5},
},
{
name: "append all duplicates",
args: args{
vs: []int{1, 2, 3},
toAdd: []int{1, 2, 3},
},
want: []int{1, 2, 3},
},
{
name: "append to nil slice",
args: args{
vs: nil,
toAdd: []int{1, 2, 3},
},
want: []int{1, 2, 3},
},
{
name: "append empty slice",
args: args{
vs: []int{1, 2, 3},
toAdd: []int{},
},
want: []int{1, 2, 3},
},
{
name: "append nil to slice",
args: args{
vs: []int{1, 2, 3},
toAdd: nil,
},
want: []int{1, 2, 3},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := AppendUniques(tt.args.vs, tt.args.toAdd); !reflect.DeepEqual(got, tt.want) {
t.Errorf("AppendUniques() = %v, want %v", got, tt.want)
}
})
}
}
func BenchmarkAppendUniques(b *testing.B) {
for i := 0; i < b.N; i++ {
AppendUniques([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, []int{3, 4, 4, 11, 12, 13, 14, 15, 16, 17, 18})
}
}