-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_test.go
169 lines (125 loc) · 3.47 KB
/
map_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package lo
import (
"sort"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestKeys(t *testing.T) {
is := assert.New(t)
r1 := Keys(map[string]int{"foo": 1, "bar": 2})
sort.Strings(r1)
is.Equal(r1, []string{"bar", "foo"})
}
func TestValues(t *testing.T) {
is := assert.New(t)
r1 := Values(map[string]int{"foo": 1, "bar": 2})
sort.Ints(r1)
is.Equal(r1, []int{1, 2})
}
func TestPickBy(t *testing.T) {
is := assert.New(t)
r1 := PickBy(map[string]int{"foo": 1, "bar": 2, "baz": 3}, func(key string, value int) bool {
return value%2 == 1
})
is.Equal(r1, map[string]int{"foo": 1, "baz": 3})
}
func TestPickByKeys(t *testing.T) {
is := assert.New(t)
r1 := PickByKeys(map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"foo", "baz"})
is.Equal(r1, map[string]int{"foo": 1, "baz": 3})
}
func TestPickByValues(t *testing.T) {
is := assert.New(t)
r1 := PickByValues(map[string]int{"foo": 1, "bar": 2, "baz": 3}, []int{1, 3})
is.Equal(r1, map[string]int{"foo": 1, "baz": 3})
}
func TestOmitBy(t *testing.T) {
is := assert.New(t)
r1 := OmitBy(map[string]int{"foo": 1, "bar": 2, "baz": 3}, func(key string, value int) bool {
return value%2 == 1
})
is.Equal(r1, map[string]int{"bar": 2})
}
func TestOmitByKeys(t *testing.T) {
is := assert.New(t)
r1 := OmitByKeys(map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"foo", "baz"})
is.Equal(r1, map[string]int{"bar": 2})
}
func TestOmitByValues(t *testing.T) {
is := assert.New(t)
r1 := OmitByValues(map[string]int{"foo": 1, "bar": 2, "baz": 3}, []int{1, 3})
is.Equal(r1, map[string]int{"bar": 2})
}
func TestEntries(t *testing.T) {
is := assert.New(t)
r1 := Entries(map[string]int{"foo": 1, "bar": 2})
sort.Slice(r1, func(i, j int) bool {
return r1[i].Value < r1[j].Value
})
is.EqualValues(r1, []Entry[string, int]{
{
Key: "foo",
Value: 1,
},
{
Key: "bar",
Value: 2,
},
})
}
func TestFromEntries(t *testing.T) {
is := assert.New(t)
r1 := FromEntries([]Entry[string, int]{
{
Key: "foo",
Value: 1,
},
{
Key: "bar",
Value: 2,
},
})
is.Len(r1, 2)
is.Equal(r1["foo"], 1)
is.Equal(r1["bar"], 2)
}
func TestInvert(t *testing.T) {
is := assert.New(t)
r1 := Invert(map[string]int{"a": 1, "b": 2})
r2 := Invert(map[string]int{"a": 1, "b": 2, "c": 1})
is.Len(r1, 2)
is.EqualValues(map[int]string{1: "a", 2: "b"}, r1)
is.Len(r2, 2)
}
func TestAssign(t *testing.T) {
is := assert.New(t)
result1 := Assign(map[string]int{"a": 1, "b": 2}, map[string]int{"b": 3, "c": 4})
is.Len(result1, 3)
is.Equal(result1, map[string]int{"a": 1, "b": 3, "c": 4})
}
func TestMapKeys(t *testing.T) {
is := assert.New(t)
result1 := MapKeys(map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, func(x int, _ int) string {
return "Hello"
})
result2 := MapKeys(map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, func(_ int, v int) string {
return strconv.FormatInt(int64(v), 10)
})
is.Equal(len(result1), 1)
is.Equal(len(result2), 4)
is.Equal(result2, map[string]int{"1": 1, "2": 2, "3": 3, "4": 4})
}
func TestMapValues(t *testing.T) {
is := assert.New(t)
result1 := MapValues(map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, func(x int, _ int) string {
return "Hello"
})
result2 := MapValues(map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, func(x int, _ int) string {
return strconv.FormatInt(int64(x), 10)
})
is.Equal(len(result1), 4)
is.Equal(len(result2), 4)
is.Equal(result1, map[int]string{1: "Hello", 2: "Hello", 3: "Hello", 4: "Hello"})
is.Equal(result2, map[int]string{1: "1", 2: "2", 3: "3", 4: "4"})
}