-
Notifications
You must be signed in to change notification settings - Fork 36
/
rand_test.go
86 lines (66 loc) · 1.71 KB
/
rand_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
package offheap_test
import (
"fmt"
"math/rand"
"testing"
"time"
"github.com/glycerine/offheap"
cv "github.com/glycerine/goconvey/convey"
)
func TestRandomOperationsOrder(t *testing.T) {
h := offheap.NewHashTable(2)
m := make(map[uint64]int)
cv.Convey("given a sequence of random operations, the result should match what Go's builtin map does", t, func() {
cv.So(hashEqualsMap(h, m), cv.ShouldEqual, true)
// basic insert
m[1] = 2
h.InsertIntValue(1, 2)
cv.So(hashEqualsMap(h, m), cv.ShouldEqual, true)
m[3] = 4
h.InsertIntValue(3, 4)
cv.So(hashEqualsMap(h, m), cv.ShouldEqual, true)
// basic delete
delete(m, 1)
h.DeleteKey(1)
cv.So(hashEqualsMap(h, m), cv.ShouldEqual, true)
delete(m, 3)
h.DeleteKey(3)
cv.So(hashEqualsMap(h, m), cv.ShouldEqual, true)
// now do random operations
N := 1000
seed := time.Now().UnixNano()
fmt.Printf("\n TestRandomOperationsOrder() using seed = '%v'\n", seed)
gen := rand.New(rand.NewSource(seed))
for i := 0; i < N; i++ {
op := gen.Int() % 4
k := uint64(gen.Int() % (N / 4))
v := gen.Int() % (N / 4)
switch op {
case 0, 1, 2:
h.InsertIntValue(k, v)
m[k] = v
cv.So(hashEqualsMap(h, m), cv.ShouldEqual, true)
case 3:
h.DeleteKey(uint64(k))
delete(m, k)
cv.So(hashEqualsMap(h, m), cv.ShouldEqual, true)
}
}
// distribution more emphasizing deletes
for i := 0; i < N; i++ {
op := gen.Int() % 2
k := uint64(gen.Int() % (N / 5))
v := gen.Int() % (N / 2)
switch op {
case 0:
h.InsertIntValue(k, v)
m[k] = v
cv.So(hashEqualsMap(h, m), cv.ShouldEqual, true)
case 1:
h.DeleteKey(uint64(k))
delete(m, k)
cv.So(hashEqualsMap(h, m), cv.ShouldEqual, true)
}
}
})
}