-
Notifications
You must be signed in to change notification settings - Fork 2
/
bitset_mem_test.go
149 lines (135 loc) · 3.53 KB
/
bitset_mem_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package gostatix
import (
"bytes"
"fmt"
"testing"
"github.com/alicebob/miniredis/v2"
)
func TestBitSetMemHas(t *testing.T) {
bitset := newBitSetMem(4)
bitset.insert(2)
bitset.insert(3)
bitset.insert(7)
if ok, _ := bitset.has(3); !ok {
t.Fatalf("should be true at index 3, got %v", ok)
}
if ok, _ := bitset.has(4); ok {
t.Fatalf("should be false at index 4, got %v", ok)
}
}
func TestBitSetMemFromData(t *testing.T) {
bitset := fromDataMem([]uint64{3, 10})
if ok, _ := bitset.has(0); !ok {
t.Fatalf("should be true at index 0, got %v", ok)
}
if ok, _ := bitset.has(1); !ok {
t.Fatalf("should be true at index 1, got %v", ok)
}
if ok, _ := bitset.has(2); ok {
t.Fatalf("should be false at index 2, got %v", ok)
}
if ok, _ := bitset.has(63); ok {
t.Fatalf("should be false at index 63, got %v", ok)
}
if ok, _ := bitset.has(64); ok {
t.Fatalf("should be false at index 64, got %v", ok)
}
if ok, _ := bitset.has(65); !ok {
t.Fatalf("should be false at index 65, got %v", ok)
}
if ok, _ := bitset.has(66); ok {
t.Fatalf("should be false at index 66, got %v", ok)
}
}
func TestBitSetMemSetBits(t *testing.T) {
bitset := fromDataMem([]uint64{3, 10})
setBits, _ := bitset.bitCount()
if setBits != 4 {
t.Fatalf("count of set bits should be 4, got %v", setBits)
}
}
func TestBitSetMemExport(t *testing.T) {
bitset := newBitSetMem(6)
bitset.insert(1)
bitset.insert(5)
bitset.insert(8)
size, data, _ := bitset.marshal()
str := "\"AAAAAAAAAAkAAAAAAAABIg==\""
if size != 6 {
t.Fatalf("size of bitset should be 6, got %v", size)
}
if string(data) != str {
t.Fatalf("exported string don't match %v, %v", string(data), str)
}
}
func TestBitSetMemImport(t *testing.T) {
bitset := newBitSetMem(6)
str := "\"AAAAAAAAAAYAAAAAAAABIg==\""
bitset.unmarshal([]byte(str))
if ok, _ := bitset.has(0); ok {
t.Fatalf("should be false at index 0, got %v", ok)
}
if ok, _ := bitset.has(1); !ok {
t.Fatalf("should be true at index 1, got %v", ok)
}
if ok, _ := bitset.has(5); !ok {
t.Fatalf("should be true at index 5, got %v", ok)
}
if ok, _ := bitset.has(7); ok {
t.Fatalf("should be false at index 1, got %v", ok)
}
}
func TestBitSetMemNotEqual(t *testing.T) {
mr, _ := miniredis.Run()
redisUri := "redis://" + mr.Addr()
connOptions, _ := ParseRedisURI(redisUri)
MakeRedisClient(*connOptions)
aBitset := newBitSetMem(0)
bBitset := newBitSetRedis(0)
if ok, _ := aBitset.equals(bBitset); ok {
t.Fatal("aBitset and bBitset shouldn't be equal")
}
}
func TestBitSetMemEqual(t *testing.T) {
aBitset := newBitSetMem(3)
aBitset.insert(0)
aBitset.insert(1)
bBitset := newBitSetMem(3)
bBitset.insert(0)
bBitset.insert(1)
ok, err := aBitset.equals(bBitset)
if err != nil {
fmt.Printf("error: %v", err)
}
if !ok {
t.Fatal("aBitset and bBitset should be equal")
}
}
func TestBitSetMemBinaryReadWrite(t *testing.T) {
aBitset := newBitSetMem(6)
aBitset.insert(1)
aBitset.insert(5)
aBitset.insert(8)
var buff bytes.Buffer
_, err := aBitset.writeTo(&buff)
if err != nil {
t.Error("error should be nil during binary write")
}
bBitSet := &BitSetMem{}
_, err = bBitSet.readFrom(&buff)
if err != nil {
t.Error("error should be nil during binary read")
}
if ok, _ := aBitset.equals(bBitSet); !ok {
t.Error("aBitset and bBitset should be equal")
}
if ok, _ := bBitSet.has(0); ok {
t.Fatalf("should be false at index 0, got %v", ok)
}
if ok, _ := bBitSet.has(1); !ok {
t.Fatalf("should be true at index 1, got %v", ok)
}
if ok, _ := bBitSet.has(5); !ok {
t.Fatalf("should be true at index 5, got %v", ok)
}
}