-
Notifications
You must be signed in to change notification settings - Fork 1
/
key_test.go
114 lines (111 loc) · 2.25 KB
/
key_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
package nuts
import (
"bytes"
"strconv"
"testing"
)
func TestKeyLen(t *testing.T) {
for _, test := range []struct {
x uint64
exp int
}{
{0, 1},
{1, 1},
{1 << 8, 2},
{1 << 16, 3},
{1 << 24, 4},
{1 << 32, 5},
{1 << 40, 6},
{1 << 48, 7},
{1 << 56, 8},
} {
got := KeyLen(test.x)
if got != test.exp {
t.Errorf("%d: expected length %d but got %d", test.x, test.exp, got)
}
}
}
func TestKey(t *testing.T) {
for _, test := range []struct {
max int64
xs []uint64
bs [][]byte
}{
{
max: 1 << 7,
xs: []uint64{0, 1, (1 << 8) - 1},
bs: [][]byte{
{0x00}, {0x01}, {0xFF},
},
},
{
max: 1 << 15,
xs: []uint64{0, 1, (1 << 16) - 1},
bs: [][]byte{
{0x00, 0x00}, {0x00, 0x01}, {0xFF, 0xFF},
},
},
{
max: 1 << 23,
xs: []uint64{0, 1, (1 << 24) - 1},
bs: [][]byte{
{0x00, 0x00, 0x00}, {0x00, 0x00, 0x01}, {0xFF, 0xFF, 0xFF},
},
},
{
max: 1 << 31,
xs: []uint64{0, 1, (1 << 32) - 1},
bs: [][]byte{
{0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x00, 0x01},
{0xFF, 0xFF, 0xFF, 0xFF},
},
},
{
max: 1 << 39,
xs: []uint64{0, 1, (1 << 40) - 1},
bs: [][]byte{
{0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x00, 0x00, 0x01},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
},
},
{
max: 1 << 47,
xs: []uint64{0, 1, (1 << 48) - 1},
bs: [][]byte{
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
},
},
{
max: 1 << 55,
xs: []uint64{0, 1, (1 << 56) - 1},
bs: [][]byte{
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
},
},
{
max: 1 << 60,
xs: []uint64{0, 1, (1 << 60) - 1},
bs: [][]byte{
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
{0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
},
},
} {
t.Run(strconv.FormatInt(test.max, 10), func(t *testing.T) {
k := make(Key, KeyLen(uint64(test.max)))
for i, x := range test.xs {
k.Put(x)
if !bytes.Equal(k, test.bs[i]) {
t.Errorf("unexpected serialized integer %d:\n\t(GOT): %#x\n\t(WNT): %#x", x, k, test.bs[i])
}
}
})
}
}