-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_test.go
59 lines (55 loc) · 1.09 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
package dmap
import (
"fmt"
"sync"
"testing"
)
func TestMap(t *testing.T) {
m := NewMap()
m.Put("test", []byte("Hello, World!"))
r := string(m.Get("test"))
if r != "Hello, World!" {
t.Logf("retrieved %s\n", r)
t.Fail()
}
m.Put("test1", []byte("Hello, World! 1"))
r = string(m.Get("test1"))
if r != "Hello, World! 1" {
t.Logf("retrieved %s\n", r)
t.Fail()
}
s := m.Size()
if s != 2 {
t.Logf("expected size 2: %d\n", s)
t.Fail()
}
}
func TestMapConcurrent(t *testing.T) {
m := NewMap()
var wg sync.WaitGroup
ids := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
for i := 0; i < 10; i++ {
wg.Add(1)
go tester(ids[i], m, t, &wg)
}
wg.Wait()
t.Logf("size: %d\n", m.Size())
m.Clear()
size := m.Size()
if size != 0 {
t.Logf("not cleaned up: %d\n", size)
t.Fail()
}
}
func tester(id string, m *Map, t *testing.T, wg *sync.WaitGroup) {
defer wg.Done()
for i := 0; i < 100000; i++ {
key := fmt.Sprintf("%s%d", id, i)
m.Put(key, []byte(key))
value := string(m.Get(key))
if value != key {
t.Logf("expected %s: got %s\n", key, value)
t.Fail()
}
}
}