1
16
17 package slice
18
19 import (
20 "reflect"
21 "sort"
22 "testing"
23 )
24
25 func TestSortInts64(t *testing.T) {
26 src := []int64{10, 1, 2, 3, 4, 5, 6}
27 expected := []int64{1, 2, 3, 4, 5, 6, 10}
28 SortInts64(src)
29 if !reflect.DeepEqual(src, expected) {
30 t.Errorf("func Ints64 didnt sort correctly, %v !- %v", src, expected)
31 }
32 }
33
34 func TestToSet(t *testing.T) {
35 testCases := map[string]struct {
36 input [][]string
37 expected []string
38 }{
39 "nil should be returned if no slices are passed to the function": {
40 input: [][]string{},
41 expected: nil,
42 },
43 "empty slice should be returned if an empty slice is passed to the function": {
44 input: [][]string{{}},
45 expected: []string{},
46 },
47 "a single slice with no duplicates should have the same values": {
48 input: [][]string{{"a", "b", "c"}},
49 expected: []string{"a", "b", "c"},
50 },
51 "duplicates should be removed from a single slice": {
52 input: [][]string{{"a", "b", "a", "c", "b"}},
53 expected: []string{"a", "b", "c"},
54 },
55 "multiple slices with no duplicates should be combined": {
56 input: [][]string{{"a", "b", "c"}, {"d", "e", "f"}},
57 expected: []string{"a", "b", "c", "d", "e", "f"},
58 },
59 "duplicates should be removed from multiple slices": {
60 input: [][]string{{"a", "b", "c"}, {"d", "b", "e"}, {"e", "f", "a"}},
61 expected: []string{"a", "b", "c", "d", "e", "f"},
62 },
63 }
64 for name, tc := range testCases {
65 t.Run(name, func(t *testing.T) {
66 actual := ToSet(tc.input...)
67 sort.Strings(actual)
68 if !reflect.DeepEqual(actual, tc.expected) {
69 t.Errorf("wrong output. Actual=%v, Expected=%v", actual, tc.expected)
70 }
71 })
72 }
73 }
74
View as plain text