-
Notifications
You must be signed in to change notification settings - Fork 24
/
map_test.go
155 lines (137 loc) · 3.11 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
package kasper
import (
"testing"
"github.com/stretchr/testify/assert"
)
var mercury = []byte("mercury")
var venus = []byte("venus")
var earth = []byte("earth")
var mars = []byte("mars")
var jupiter = []byte("jupiter")
var saturn = []byte("saturn")
var uranus = []byte("uranus")
var neptune = []byte("neptune")
func newTestMap() *Map {
s := &Map{
m: make(map[string][]byte, 10),
}
return s
}
func TestMap(t *testing.T) {
NewMap(10)
}
func TestMap_Get_ExistingKey(t *testing.T) {
s := newTestMap()
s.Put("mercury", mercury)
actual, err := s.Get("mercury")
assert.Equal(t, nil, err)
assert.Equal(t, mercury, actual)
}
func TestMap_Get_MissingKey(t *testing.T) {
s := newTestMap()
s.Put("earth", earth)
actual, err := s.Get("venus")
assert.Nil(t, err)
assert.Nil(t, actual)
}
func TestMap_Put_Normal(t *testing.T) {
s := newTestMap()
err := s.Put("venus", venus)
assert.Nil(t, err)
assert.Equal(t, venus, s.m["venus"])
}
func TestMap_Put_Overwrite(t *testing.T) {
s := newTestMap()
s.Put("earth", jupiter)
err := s.Put("earth", earth)
assert.Nil(t, err)
assert.Equal(t, earth, s.m["earth"])
}
func TestMap_Delete_Existing(t *testing.T) {
s := newTestMap()
s.m["neptune"] = neptune
err := s.Delete("neptune")
assert.Nil(t, err)
assert.Equal(t, 0, len(s.m))
}
func TestMap_Delete_NonExisting(t *testing.T) {
s := newTestMap()
s.m["uranus"] = uranus
err := s.Delete("mercury")
assert.Nil(t, err)
assert.Equal(t, 1, len(s.m))
}
func TestMap_GetAll(t *testing.T) {
s := newTestMap()
s.m["mars"] = mars
kvs, err := s.GetAll([]string{"venus", "mars"})
assert.Equal(t, nil, err)
assert.Equal(t, 1, len(kvs))
assert.Equal(t, kvs["mars"], mars)
}
func TestMap_GetAll_Empty(t *testing.T) {
s := newTestMap()
_, err := s.GetAll([]string{})
assert.Nil(t, err)
_, err = s.GetAll(nil)
assert.Nil(t, err)
}
func TestMap_PutAll(t *testing.T) {
s := newTestMap()
err := s.PutAll(map[string][]byte{"jupiter": jupiter, "neptune": neptune})
assert.Nil(t, err)
assert.Equal(t, len(s.m), 2)
assert.Equal(t, s.m["jupiter"], jupiter)
assert.Equal(t, s.m["neptune"], neptune)
}
func TestMap_PutAll_Empty(t *testing.T) {
s := newTestMap()
err := s.PutAll(map[string][]byte{})
assert.Nil(t, err)
err = s.PutAll(nil)
assert.Nil(t, err)
}
func TestMap_Flush(t *testing.T) {
s := newTestMap()
err := s.Flush()
assert.Nil(t, err)
}
func BenchmarkMap_Get(b *testing.B) {
s := newTestMap()
s.m["earth"] = earth
s.m["jupiter"] = jupiter
s.m["mercury"] = mercury
s.m["neptune"] = neptune
keys := []string{"earth", "mars", "jupiter", "saturn", "mercury", "venus", "neptune"}
keysLength := len(keys)
for i := 0; i < b.N; i++ {
_, err := s.Get(keys[i%keysLength])
assert.Nil(b, err)
}
}
func BenchmarkMap_Put(b *testing.B) {
s := newTestMap()
kvs := map[string][]byte{
"earth": earth,
"mars": mars,
"jupiter": jupiter,
"saturn": saturn,
"mercury": mercury,
"venus": venus,
"neptune": neptune,
}
keys := []string{
"earth",
"mars",
"jupiter",
"saturn",
"mercury",
"venus",
"neptune",
}
for i := 0; i < b.N; i++ {
key := keys[i%len(keys)]
err := s.Put(key, kvs[key])
assert.Nil(b, err)
}
}