-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbithack_test.go
105 lines (91 loc) · 2.25 KB
/
bithack_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
package umap
import (
"testing"
)
func TestBitHackMatchTophash(t *testing.T) {
s := [8]uint8{emptySlot, 12, deletedSlot, 1, 13, 14}
status := matchTopHash(s, 12)
if !status.shouldHaveMatch(1) {
t.Fatal()
}
s = [8]uint8{11, 122, 0, deletedSlot, emptySlot, deletedSlot}
status = matchTopHash(s, 12)
if status.AnyMatch() {
t.Fatal()
}
s = [8]uint8{1, 127, 123, 127, 0, deletedSlot, emptySlot, deletedSlot}
status = matchTopHash(s, 127)
if !status.shouldHaveMatches(1, 3) {
t.Fatal()
}
}
func TestBitHackMatchEmpty(t *testing.T) {
s := [8]uint8{emptySlot, 12, deletedSlot}
status := matchEmpty(s)
if !status.shouldHaveMatch(0) {
t.Fatal()
}
s = [8]uint8{11, 122, 0, deletedSlot, deletedSlot, deletedSlot, 0, 1}
status = matchEmpty(s)
if status.AnyMatch() {
t.Fatal()
}
s = littleEndianUint64ToBytes(allEmpty)
status = matchEmpty(s)
if !status.AnyMatch() {
t.Fatal()
}
s = [8]uint8{1, emptySlot, 123, 127, 0, deletedSlot, emptySlot, deletedSlot}
status = matchEmpty(s)
if !status.shouldHaveMatches(1, 6) {
t.Fatal()
}
}
func TestBitHackMatchEmprtyOrDeleted(t *testing.T) {
s := [8]uint8{emptySlot, 12, 0, 1}
status := matchEmptyOrDeleted(s)
if !status.shouldHaveMatch(0) {
t.Fatal()
}
s = [8]uint8{11, 122, 0, 1, 127, 55, 0, 1}
status = matchEmptyOrDeleted(s)
if status.AnyMatch() {
t.Fatal()
}
s = [8]uint8{1, emptySlot, 123, 127, 0, deletedSlot, emptySlot, deletedSlot}
status = matchEmptyOrDeleted(s)
if !status.shouldHaveMatches(1, 5, 6, 7) {
t.Fatal()
}
}
func TestBitHackSameSizeGrow(t *testing.T) {
s := [bucketCnt]uint8{deletedSlot, 2, emptySlot, 4, deletedSlot, deletedSlot, 7, emptySlot}
ns := prepareSameSizeGrow(s)
res := [bucketCnt]uint8{emptySlot, deletedSlot, emptySlot, deletedSlot, emptySlot, emptySlot, deletedSlot, emptySlot}
for i := 0; i < bucketCnt; i++ {
if ns[i] != res[i] {
t.Fatalf("Expected ns[%d] == res[%d], got %d != %d", i, i, ns[i], res[i])
}
}
}
func (b *bitmask64) shouldHaveMatch(x uint) bool {
cb := *b
for {
m := cb.NextMatch()
if m == x {
return true
}
if m >= bucketCnt {
return false
}
cb.RemoveLowestBit()
}
}
func (b *bitmask64) shouldHaveMatches(x ...uint) bool {
for _, v := range x {
if !b.shouldHaveMatch(v) {
return false
}
}
return true
}