Debian Codegraph @

maps/maps_test.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 package maps
2
3 import (
4 "fmt"
5 "testing"
•6 "slices"
7
8 "github.com/jesseduffield/generics/internal/testutils"
9 )
10
11 func TestKeys(t *testing.T) {
12 tests := []struct {
13 hashMap map[string]int
14 expected []string
15 }{
16 {map[string]int{}, []string{}},
17 {map[string]int{"a": 1}, []string{"a"}},
18 }
19 for _, test := range tests {
20 testutils.ExpectSlice(t, test.expected, Keys(test.hashMap))
21 }
22 }
23
24 func TestValues(t *testing.T) {
25 tests := []struct {
26 hashMap map[string]int
27 expected []int
28 }{
29 {map[string]int{}, []int{}},
30 {map[string]int{"a": 1}, []int{1}},
31 }
32 for _, test := range tests {
33 testutils.ExpectSlice(t, test.expected, Values(test.hashMap))
34 }
35 }
36
37 func TestTransformKeys(t *testing.T) {
38 tests := []struct {
39 hashMap map[int]string
40 transform func(int) int64
41 expected map[int64]string
42 }{
43 {
44 hashMap: map[int]string{},
45 transform: func(i int) int64 { return (int64)(i) },
46 expected: map[int64]string{},
47 },
48 {
49 hashMap: map[int]string{1: "a"},
50 transform: func(i int) int64 { return 2 * (int64)(i) },
51 expected: map[int64]string{2: "a"},
52 },
53 }
54 for _, test := range tests {
55 testutils.ExpectMap(t, test.expected, TransformKeys(test.hashMap, test.transform))
56 }
57 }
58
59 func TestTransformValues(t *testing.T) {
60 tests := []struct {
61 hashMap map[string]int
62 transform func(int) int64
63 expected map[string]int64
64 }{
65 {
66 hashMap: map[string]int{},
67 transform: func(i int) int64 { return (int64)(i) },
68 expected: map[string]int64{},
69 },
70 {
71 hashMap: map[string]int{"a": 1},
72 transform: func(i int) int64 { return 2 * (int64)(i) },
73 expected: map[string]int64{"a": 2},
74 },
75 }
76 for _, test := range tests {
77 testutils.ExpectMap(t, test.expected, TransformValues(test.hashMap, test.transform))
78 }
79 }
80
81 func TestMapToSlice(t *testing.T) {
82 tests := []struct {
83 hashMap map[int64]int
84 f func(int64, int) string
85 expected []string
86 }{
87 {
88 hashMap: map[int64]int{},
89 f: func(k int64, v int) string { return fmt.Sprintf("%d:%d", k, v) },
90 expected: []string{},
91 },
92 {
93 hashMap: map[int64]int{2: 5},
94 f: func(k int64, v int) string { return fmt.Sprintf("%d:%d", k, v) },
95 expected: []string{"2:5"},
96 },
97 {
98 hashMap: map[int64]int{2: 5, 3: 4},
99 f: func(k int64, v int) string { return fmt.Sprintf("%d:%d", k, v) },
100 expected: []string{"2:5", "3:4"},
101 },
102 }
103 for _, test := range tests {
•104 result := MapToSlice(test.hashMap, test.f)
105 slices.Sort(result)
106 testutils.ExpectSlice(t, test.expected, result)
107 }
108 }
109
110 func TestFilter(t *testing.T) {
111 tests := []struct {
112 hashMap map[int64]int
113 f func(int64, int) bool
114 expected map[int64]int
115 }{
116 {
117 hashMap: map[int64]int{},
118 f: func(k int64, v int) bool { return int(k)+v > 0 },
119 expected: map[int64]int{},
120 },
121 {
122 hashMap: map[int64]int{2: 5},
123 f: func(k int64, v int) bool { return int(k)+v > 0 },
124 expected: map[int64]int{2: 5},
125 },
126 {
127 hashMap: map[int64]int{2: 5, 3: -4},
128 f: func(k int64, v int) bool { return int(k)+v > 0 },
129 expected: map[int64]int{2: 5},
130 },
131 }
132 for _, test := range tests {
133 testutils.ExpectMap(t, test.expected, Filter(test.hashMap, test.f))
134 }
135 }