-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbits_test.go
62 lines (51 loc) · 1.58 KB
/
bits_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
package hamt
import (
"fmt"
"testing"
qt "github.com/frankban/quicktest"
)
func TestRangedInt(t *testing.T) {
t.Parallel()
tests := []struct {
bs []byte
from, to int
want int
}{
{[]byte{0b00001111}, 0, 8, 0x0f},
{[]byte{0b00001111}, 0, 4, 0x00},
{[]byte{0b00001111}, 4, 8, 0x0f},
{[]byte{0b00001111}, 4, 6, 0b0011},
{[]byte{0b11111111}, 4, 8, 0x0f},
{[]byte{0b00001111}, 3, 5, 0b0001},
{[]byte{0b11111111, 0b00000000}, 8, 16, 0x00},
{[]byte{0b11111111, 0b00000000}, 0, 16, 0xff00},
{[]byte{0b11111111, 0b00000000}, 4, 12, 0xf0},
{[]byte{0b11111111, 0b00000000}, 6, 10, 0b1100},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%x-%d-%d", test.bs, test.from, test.to), func(t *testing.T) {
got := rangedInt(test.bs, test.from, test.to)
qt.Assert(t, got, qt.Equals, test.want)
})
}
}
func TestBitset(t *testing.T) {
t.Parallel()
bs := []byte{0b00001111, 0b01010101}
qt.Assert(t, bitsetGet(bs, 0), qt.IsFalse)
qt.Assert(t, bitsetGet(bs, 3), qt.IsFalse)
qt.Assert(t, bitsetGet(bs, 4), qt.IsTrue)
qt.Assert(t, bitsetGet(bs, 7), qt.IsTrue)
qt.Assert(t, bitsetGet(bs, 8), qt.IsFalse)
qt.Assert(t, bitsetGet(bs, 10), qt.IsFalse)
qt.Assert(t, bitsetGet(bs, 13), qt.IsTrue)
qt.Assert(t, bitsetGet(bs, 15), qt.IsTrue)
bitsetClear(bs, 15)
qt.Assert(t, bitsetGet(bs, 15), qt.IsFalse)
bitsetSet(bs, 15)
qt.Assert(t, bitsetGet(bs, 15), qt.IsTrue)
qt.Assert(t, onesCountRange(bs, 4), qt.Equals, 0)
qt.Assert(t, onesCountRange(bs, 8), qt.Equals, 4)
qt.Assert(t, onesCountRange(bs, 9), qt.Equals, 4)
qt.Assert(t, onesCountRange(bs, 10), qt.Equals, 5)
}