pkgs/misc: add RemoveDuplicates function (MR 22)

This commit is contained in:
Clayton Craft
2022-09-09 15:25:50 -07:00
parent 0eacd26615
commit a6165b3a8c
2 changed files with 74 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ package misc
import (
"reflect"
"sort"
"testing"
)
@@ -69,3 +70,56 @@ func TestMerge(t *testing.T) {
})
}
}
func TestRemoveDuplicates(t *testing.T) {
subtests := []struct {
name string
in []string
expected []string
}{
{
name: "no duplicates",
in: []string{
"foo",
"bar",
"banana",
"airplane",
},
expected: []string{
"foo",
"bar",
"banana",
"airplane",
},
},
{
name: "all duplicates",
in: []string{
"foo",
"foo",
"foo",
"foo",
},
expected: []string{
"foo",
},
},
{
name: "empty",
in: []string{},
expected: []string{},
},
}
for _, st := range subtests {
t.Run(st.name, func(t *testing.T) {
// note: sorting to make comparison easier later
sort.Strings(st.expected)
out := RemoveDuplicates(st.in)
sort.Strings(out)
if !reflect.DeepEqual(st.expected, out) {
t.Fatalf("expected: %q, got: %q\n", st.expected, out)
}
})
}
}