-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_std_test.go
119 lines (85 loc) · 2.7 KB
/
map_std_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
package g
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestKeys(t *testing.T) {
is := assert.New(t)
keys := Keys(map[string]int{"a": 1, "b": 2})
is.ElementsMatch([]string{"a", "b"}, keys)
}
func TestValues(t *testing.T) {
is := assert.New(t)
values := Values(map[string]int{"a": 1, "b": 2})
is.ElementsMatch([]int{1, 2}, values)
}
func TestMapKeys(t *testing.T) {
is := assert.New(t)
keys := MapKeys(map[string]int{"a": 1, "b": 2}, func(v string) string { return v + "_" })
is.ElementsMatch([]string{"a_", "b_"}, keys)
}
func TestMapValues(t *testing.T) {
is := assert.New(t)
values := MapValues(map[string]int{"a": 1, "b": 2}, func(v int) int { return v * 2 })
is.ElementsMatch([]int{2, 4}, values)
}
func TestMapEntries(t *testing.T) {
is := assert.New(t)
entries := MapEntries(map[string]int{"a": 1, "b": 2},
func(k string, v int) string { return fmt.Sprintf("%s_%d", k, v*2) })
is.ElementsMatch([]string{"a_2", "b_4"}, entries)
}
func TestMaxKey(t *testing.T) {
is := assert.New(t)
is.Equal("b", MaxKey(map[string]int{"a": 1, "b": 2}))
}
func TestMinKey(t *testing.T) {
is := assert.New(t)
is.Equal("a", MinKey(map[string]int{"a": 1, "b": 2}))
}
func TestMaxValue(t *testing.T) {
is := assert.New(t)
is.Equal(2, MaxValue(map[string]int{"a": 1, "b": 2}))
}
func TestMinValue(t *testing.T) {
is := assert.New(t)
is.Equal(1, MinValue(map[string]int{"a": 1, "b": 2}))
}
func TestMaxKeyBy(t *testing.T) {
is := assert.New(t)
is.Equal("aa", MaxKeyBy(map[string]int{"a": 1, "aa": 2}, func(v1 string, v2 string) bool { return len(v1) < len(v2) }))
}
func TestMinKeyBy(t *testing.T) {
is := assert.New(t)
is.Equal("a", MinKeyBy(map[string]int{"a": 1, "aa": 2}, func(v1 string, v2 string) bool { return len(v1) < len(v2) }))
}
func TestMaxValueBy(t *testing.T) {
is := assert.New(t)
is.Equal(2, MaxValueBy(map[string]int{"a": 1, "aa": 2}, func(v1 int, v2 int) bool { return v1 < v2 }))
}
func TestMinValueBy(t *testing.T) {
is := assert.New(t)
is.Equal(1, MinValueBy(map[string]int{"a": 1, "aa": 2}, func(v1 int, v2 int) bool { return v1 < v2 }))
}
func TestPutAll(t *testing.T) {
is := assert.New(t)
m := map[string]int{"a": 1, "b": 2}
PutAll(m, map[string]int{"c": 3, "d": 4})
is.Equal(map[string]int{"a": 1, "b": 2, "c": 3, "d": 4}, m)
}
// copy test copy map
func TestCopy(t *testing.T) {
is := assert.New(t)
m := map[string]int{"a": 1, "b": 2}
m2 := CopyMap(m)
is.Equal(map[string]int{"a": 1, "b": 2}, m2)
// modify m2
m2["c"] = 3
is.Equal(map[string]int{"a": 1, "b": 2, "c": 3}, m2)
is.Equal(map[string]int{"a": 1, "b": 2}, m)
// modify m
m["d"] = 4
is.Equal(map[string]int{"a": 1, "b": 2, "d": 4}, m)
is.Equal(map[string]int{"a": 1, "b": 2, "c": 3}, m2)
}