-
Notifications
You must be signed in to change notification settings - Fork 36
/
insert_test.go
69 lines (58 loc) · 1.97 KB
/
insert_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
package offheap_test
import (
"testing"
"github.com/glycerine/offheap"
cv "github.com/glycerine/goconvey/convey"
)
func TestInsert(t *testing.T) {
h := offheap.NewHashTable(8)
cv.Convey("inserting a non-zero key should enable retrieving them with Lookup", t, func() {
cv.So(h.Population, cv.ShouldEqual, 0)
cv.So(h.Lookup(23), cv.ShouldEqual, nil)
c, ok := h.Insert(23)
c.SetInt(55)
cv.So(c, cv.ShouldNotEqual, nil)
cv.So(ok, cv.ShouldEqual, true)
cv.So(h.Population, cv.ShouldEqual, 1)
cv.So(h.Lookup(23), cv.ShouldNotEqual, nil)
cell := h.Lookup(23)
cv.So(cell.Value[0], cv.ShouldEqual, 55)
})
h.Clear()
cv.Convey("inserting a zero key should also be retrievable with Lookup", t, func() {
cv.So(h.Population, cv.ShouldEqual, 0)
cv.So(h.Lookup(0), cv.ShouldEqual, nil)
c, ok := h.Insert(0)
c.SetInt(55)
cv.So(c, cv.ShouldNotEqual, nil)
cv.So(ok, cv.ShouldEqual, true)
cv.So(h.Population, cv.ShouldEqual, 1)
cv.So(h.Lookup(0), cv.ShouldNotEqual, nil)
cell := h.Lookup(0)
cv.So(cell.Value[0], cv.ShouldEqual, 55)
})
h.Clear()
cv.Convey("Insert()-ing the same key twice should return false for the 2nd param on encountering the same key again. For 0 key.", t, func() {
cv.So(h.Population, cv.ShouldEqual, 0)
c, ok := h.Insert(0)
cv.So(c, cv.ShouldNotEqual, nil)
cv.So(c.UnHashedKey, cv.ShouldEqual, 0)
cv.So(ok, cv.ShouldEqual, true)
c, ok = h.Insert(0)
cv.So(c, cv.ShouldNotEqual, nil)
cv.So(c.UnHashedKey, cv.ShouldEqual, 0)
cv.So(ok, cv.ShouldEqual, false)
})
h.Clear()
cv.Convey("Insert()-ing the same key twice should return false for the 2nd param on encountering the same key again. For not-zero key.", t, func() {
cv.So(h.Population, cv.ShouldEqual, 0)
c, ok := h.Insert(1)
cv.So(c, cv.ShouldNotEqual, nil)
cv.So(c.UnHashedKey, cv.ShouldEqual, 1)
cv.So(ok, cv.ShouldEqual, true)
c, ok = h.Insert(1)
cv.So(c, cv.ShouldNotEqual, nil)
cv.So(c.UnHashedKey, cv.ShouldEqual, 1)
cv.So(ok, cv.ShouldEqual, false)
})
}