-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_test.go
More file actions
100 lines (92 loc) · 2.04 KB
/
map_test.go
File metadata and controls
100 lines (92 loc) · 2.04 KB
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
package gdk
import (
"testing"
)
func TestMapClear(t *testing.T) {
t.Run("map[string]string", func(t *testing.T) {
m := map[string]string{"key": "value"}
MapClear(m)
if len(m) != 0 {
t.Errorf("len(m)=%v, want 0", len(m))
}
})
t.Run("nil map", func(t *testing.T) {
var m map[string]int
MapClear(m)
if m != nil {
t.Errorf("m=%+v, want nil", m)
}
})
}
func TestMapSize(t *testing.T) {
m := make(map[string]struct{})
t.Run("nil map", func(t *testing.T) {
if got := MapSize(m); got != 0 {
t.Errorf("got %v, want 0", got)
}
})
t.Run("map[string]struct", func(t *testing.T) {
m["test"] = struct{}{}
if got := MapSize(m); got != 1 {
t.Errorf("got %v, want 1", got)
}
})
}
func TestMapRange(t *testing.T) {
t.Run("value add number 1", func(t *testing.T) {
m := map[string]int{"a": 1, "b": 2}
MapRange(m, func(key string, value int) bool {
m[key] = value + 1
return true
})
want := map[string]int{"a": 2, "b": 3}
for k, v := range m {
if v != want[k] {
t.Errorf("got %v, want %v", v, want[k])
}
}
})
}
func TestMapFilter(t *testing.T) {
t.Run("filter < 5", func(t *testing.T) {
m := map[string]int{"a": 1, "b": 2, "c": 6}
got := MapFilter(m, func(key string, value int) bool {
if value > 5 {
return false
}
return true
})
for k, v := range got {
if v > 5 {
t.Errorf("got %v, want %v, key %v", v, "<5", k)
}
}
if len(got) != 2 {
t.Errorf("got %v, want 2", len(got))
}
})
}
func TestMapValues(t *testing.T) {
t.Run("map values", func(t *testing.T) {
m := map[string]int{"a": 1, "b": 2, "c": 3}
want := map[int]bool{1: true, 2: true, 3: true}
got := MapValues(m)
for _, v := range got {
if _, ok := want[v]; !ok {
t.Errorf("got %v, want %v", got, want)
}
}
})
}
func TestMapKeys(t *testing.T) {
t.Run("map keys", func(t *testing.T) {
m := map[string]int{"a": 1, "b": 2}
want := map[string]bool{"a": true, "b": true}
got := MapKeys(m)
for _, v := range got {
if _, ok := want[v]; !ok {
t.Errorf("got %v, want %v", got, want)
}
}
})
}