This repository has been archived by the owner on Feb 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmap_test.go
189 lines (165 loc) · 3.94 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package ps
import . "strconv"
import "testing"
import "sort"
func TestMapNil(t *testing.T) {
m := NewMap()
keys := m.Keys()
if len(keys) != 0 {
t.Errorf("Empty map has keys")
}
}
func TestMapImmutable(t *testing.T) {
// build a couple small maps
world := NewMap().Set("hello", "world")
kids := world.Set("hello", "kids")
// both maps should still retain their data
if v, _ := world.Lookup("hello"); v != "world" {
t.Errorf("Set() modified the receiving map")
}
if size := world.Size(); size != 1 {
t.Errorf("world size is not 1 : %d", size)
}
if v, _ := kids.Lookup("hello"); v != "kids" {
t.Errorf("Set() did not modify the resulting map")
}
if size := kids.Size(); size != 1 {
t.Errorf("kids size is not 1 : %d", size)
}
// both maps have the right keys
if keys := world.Keys(); len(keys) != 1 || keys[0] != "hello" {
t.Errorf("world has the wrong keys: %#v", keys)
}
if keys := kids.Keys(); len(keys) != 1 || keys[0] != "hello" {
t.Errorf("kids has the wrong keys: %#v", keys)
}
// test deletion
empty := kids.Delete("hello")
if size := empty.Size(); size != 0 {
t.Errorf("empty size is not 1 : %d", size)
}
if keys := empty.Keys(); len(keys) != 0 {
t.Errorf("empty has the wrong keys: %#v", keys)
}
}
func TestMapMultipleKeys(t *testing.T) {
// map with multiple keys each with pointer values
one := 1
two := 2
three := 3
m := NewMap().Set("one", &one).Set("two", &two).Set("three", &three)
// do we have the right number of keys?
keys := m.Keys()
if len(keys) != 3 {
t.Logf("wrong size keys: %d", len(keys))
t.FailNow()
}
// do we have the right keys?
sort.Strings(keys)
if keys[0] != "one" {
t.Errorf("unexpected key: %s", keys[0])
}
if keys[1] != "three" {
t.Errorf("unexpected key: %s", keys[1])
}
if keys[2] != "two" {
t.Errorf("unexpected key: %s", keys[2])
}
// do we have the right values?
vp, ok := m.Lookup("one")
if !ok {
t.Logf("missing value for one")
t.FailNow()
}
if v := vp.(*int); *v != 1 {
t.Errorf("wrong value: %d\n", *v)
}
vp, ok = m.Lookup("two")
if !ok {
t.Logf("missing value for two")
t.FailNow()
}
if v := vp.(*int); *v != 2 {
t.Errorf("wrong value: %d\n", *v)
}
vp, ok = m.Lookup("three")
if !ok {
t.Logf("missing value for three")
t.FailNow()
}
if v := vp.(*int); *v != 3 {
t.Errorf("wrong value: %d\n", *v)
}
}
func TestMapManyKeys(t *testing.T) {
// build a map with many keys and values
count := 100
m := NewMap()
for i := 0; i < count; i++ {
m = m.Set(Itoa(i), i)
}
if m.Size() != 100 {
t.Errorf("Wrong number of keys: %d", m.Size())
}
m = m.Delete("42").Delete("7").Delete("19").Delete("99")
if m.Size() != 96 {
t.Errorf("Wrong number of keys: %d", m.Size())
}
for i := 43; i < 99; i++ {
v, ok := m.Lookup(Itoa(i))
if !ok || v != i {
t.Errorf("Wrong value for key %d", i)
}
}
}
func TestMapUnsafeMutableSet(t *testing.T) {
// build a map with many keys and values
count := 100
m := NewMap()
for i := 0; i < count; i++ {
m = m.UnsafeMutableSet(Itoa(i), i)
}
if m.Size() != 100 {
t.Errorf("Wrong number of keys: %d", m.Size())
}
m = m.Delete("42").Delete("7").Delete("19").Delete("99")
if m.Size() != 96 {
t.Errorf("Wrong number of keys: %d", m.Size())
}
for i := 43; i < 99; i++ {
v, ok := m.Lookup(Itoa(i))
if !ok || v != i {
t.Errorf("Wrong value for key %d", i)
}
}
}
func TestMapHashKey(t *testing.T) {
hash := hashKey("this is a key")
if hash != 10424450902216330915 {
t.Errorf("This isn't FNV-1a hashing: %d", hash)
}
}
func BenchmarkMapSet(b *testing.B) {
m := NewMap()
for i := 0; i < b.N; i++ {
m = m.Set("foo", i)
}
}
func BenchmarkMapUnsafeMutableSet(b *testing.B) {
m := NewMap()
for i := 0; i < b.N; i++ {
m = m.UnsafeMutableSet("foo", i)
}
}
func BenchmarkMapDelete(b *testing.B) {
m := NewMap().Set("key", "value")
for i := 0; i < b.N; i++ {
m.Delete("key")
}
}
func BenchmarkHashKey(b *testing.B) {
key := "this is a key"
for i := 0; i < b.N; i++ {
_ = hashKey(key)
}
}