forked from cosmos/iavl
-
Notifications
You must be signed in to change notification settings - Fork 4
/
key_format_test.go
149 lines (130 loc) · 3.96 KB
/
key_format_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 iavl
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestKeyFormatBytes(t *testing.T) {
type keyPairs struct {
key [][]byte
expected []byte
}
emptyTestVector := keyPairs{key: [][]byte{}, expected: []byte{'e'}}
threeByteTestVector := keyPairs{
key: [][]byte{{1, 2, 3}},
expected: []byte{'e', 0, 0, 0, 0, 0, 1, 2, 3},
}
eightByteTestVector := keyPairs{
key: [][]byte{{1, 2, 3, 4, 5, 6, 7, 8}},
expected: []byte{'e', 1, 2, 3, 4, 5, 6, 7, 8},
}
tests := []struct {
name string
kf *KeyFormat
testVectors []keyPairs
}{{
name: "simple 3 int key format",
kf: NewKeyFormat(byte('e'), 8, 8, 8),
testVectors: []keyPairs{
emptyTestVector,
threeByteTestVector,
eightByteTestVector,
{
key: [][]byte{{1, 2, 3, 4, 5, 6, 7, 8}, {1, 2, 3, 4, 5, 6, 7, 8}, {1, 1, 2, 2, 3, 3}},
expected: []byte{'e', 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 1, 1, 2, 2, 3, 3},
},
},
}, {
name: "zero suffix key format",
kf: NewKeyFormat(byte('e'), 8, 0),
testVectors: []keyPairs{
emptyTestVector,
threeByteTestVector,
eightByteTestVector,
{
key: [][]byte{{1, 2, 3, 4, 5, 6, 7, 8}, {1, 2, 3, 4, 5, 6, 7, 8, 9}},
expected: []byte{'e', 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9},
},
{
key: [][]byte{{1, 2, 3, 4, 5, 6, 7, 8}, []byte("hellohello")},
expected: []byte{'e', 1, 2, 3, 4, 5, 6, 7, 8, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x68, 0x65, 0x6c, 0x6c, 0x6f},
},
},
}}
for _, tc := range tests {
kf := tc.kf
for i, v := range tc.testVectors {
assert.Equal(t, v.expected, kf.KeyBytes(v.key...), "key format %s, test case %d", tc.name, i)
}
}
}
func TestKeyFormat(t *testing.T) {
kf := NewKeyFormat(byte('e'), 8, 8, 8)
key := []byte{'e', 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 200, 0, 0, 0, 0, 0, 0, 1, 144}
var a, b, c int64 = 100, 200, 400
assert.Equal(t, key, kf.Key(a, b, c))
var ao, bo, co = new(int64), new(int64), new(int64)
kf.Scan(key, ao, bo, co)
assert.Equal(t, a, *ao)
assert.Equal(t, b, *bo)
assert.Equal(t, c, *co)
bs := new([]byte)
kf.Scan(key, ao, bo, bs)
assert.Equal(t, a, *ao)
assert.Equal(t, b, *bo)
assert.Equal(t, []byte{0, 0, 0, 0, 0, 0, 1, 144}, *bs)
assert.Equal(t, []byte{'e', 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 200}, kf.Key(a, b))
}
func TestNegativeKeys(t *testing.T) {
kf := NewKeyFormat(byte('e'), 8, 8)
var a, b int64 = -100, -200
// One's complement plus one
key := []byte{'e',
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, byte(0xff + a + 1),
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, byte(0xff + b + 1)}
assert.Equal(t, key, kf.Key(a, b))
var ao, bo = new(int64), new(int64)
kf.Scan(key, ao, bo)
assert.Equal(t, a, *ao)
assert.Equal(t, b, *bo)
}
func TestOverflow(t *testing.T) {
kf := NewKeyFormat(byte('o'), 8, 8)
var a int64 = 1 << 62
var b uint64 = 1 << 63
key := []byte{'o',
0x40, 0, 0, 0, 0, 0, 0, 0,
0x80, 0, 0, 0, 0, 0, 0, 0,
}
assert.Equal(t, key, kf.Key(a, b))
var ao, bo = new(int64), new(int64)
kf.Scan(key, ao, bo)
assert.Equal(t, a, *ao)
assert.Equal(t, int64(b), *bo)
}
func benchmarkKeyFormatBytes(b *testing.B, kf *KeyFormat, segments ...[]byte) {
for i := 0; i < b.N; i++ {
kf.KeyBytes(segments...)
}
}
func BenchmarkKeyFormat_KeyBytesOneSegment(b *testing.B) {
benchmarkKeyFormatBytes(b, NewKeyFormat('e', 8, 8, 8), nil)
}
func BenchmarkKeyFormat_KeyBytesThreeSegment(b *testing.B) {
segments := [][]byte{
{1, 2, 3, 4, 5, 6, 7, 8},
{1, 2, 3, 4, 5, 6, 7, 8},
{1, 1, 2, 2, 3, 3},
}
benchmarkKeyFormatBytes(b, NewKeyFormat('e', 8, 8, 8), segments...)
}
func BenchmarkKeyFormat_KeyBytesOneSegmentWithVariousLayouts(b *testing.B) {
benchmarkKeyFormatBytes(b, NewKeyFormat('e', 8, 16, 32), nil)
}
func BenchmarkKeyFormat_KeyBytesThreeSegmentWithVariousLayouts(b *testing.B) {
segments := [][]byte{
{1, 2, 3, 4, 5, 6, 7, 8},
{1, 2, 3, 4, 5, 6, 7, 8},
{1, 1, 2, 2, 3, 3},
}
benchmarkKeyFormatBytes(b, NewKeyFormat('e', 8, 16, 32), segments...)
}