-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_test.go
101 lines (92 loc) · 2.13 KB
/
test_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
package cuckoo
import (
"fmt"
"github.com/tchajed/goose/machine"
"sync"
"testing"
)
func checkEquivalence(m map[uint64]uint64, c *CuckooMap) bool {
var vtemp uint64
for k, v := range m {
if !c.Get(k, &vtemp) {
panic("Missing key")
} else if vtemp != v {
fmt.Printf("%d -> %d instead of %d", k, vtemp, v)
panic("Value doesn't match")
}
}
return true
}
func TestMapSingleThreaded(t *testing.T) {
c := MakeCuckooMap(15)
m := make(map[uint64]uint64)
N := 8 * (1 << 15) * SLOTS_PER_BUCKET / 10
for i := 0; i < N; i++ {
k := machine.RandomUint64()
v := machine.RandomUint64()
m[k] = v
r := c.Insert(k, v)
if r == INSERT_FAIL {
t.Fatalf("CuckooMap.Insert() failed")
} else if r == INSERT_DUP {
t.Fatalf("CuckooMap.Insert() duplicate")
}
}
if !checkEquivalence(m, c) {
t.Fatalf("Maps not equivalent")
}
}
func TestMapSequential(t *testing.T) {
hashpower := uint64(18)
c := MakeCuckooMap(hashpower)
m := make(map[uint64]uint64)
// when inserting sequentially, can get to 100% load factor!
N := SLOTS_PER_BUCKET * (1 << hashpower)
for i := 0; i < N; i++ {
k := uint64(i)
v := machine.RandomUint64()
m[k] = v
r := c.Insert(k, v)
if r == INSERT_FAIL {
t.Fatalf("CuckooMap.Insert() failed")
} else if r == INSERT_DUP {
t.Fatalf("CuckooMap.Insert() duplicate")
}
}
if !checkEquivalence(m, c) {
t.Fatalf("Maps not equivalent")
}
}
func TestMapDisjointConcurrent(t *testing.T) {
hashpower := uint64(16)
c := MakeCuckooMap(hashpower)
N := 10000
nthread := uint64(5)
var wg sync.WaitGroup
for i := uint64(0); i < nthread; i++ {
wg.Add(1)
go func(e uint64) {
defer wg.Done()
m := make(map[uint64]uint64)
for j := 0; j < N; j++ {
k := machine.RandomUint64()
k = (k/nthread) * nthread + e
v := machine.RandomUint64()
if _, ok := m[k]; ok {
continue // don't insert duplicates
}
m[k] = v
r := c.Insert(k, v)
if r == INSERT_FAIL {
t.Fatalf("CuckooMap.Insert() failed")
} else if r == INSERT_DUP {
t.Fatalf("CuckooMap.Insert() duplicate")
}
}
if !checkEquivalence(m, c) {
t.Fatalf("Maps not equivalent")
}
}(i)
}
wg.Wait()
}