forked from cosmos/iavl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_format_test.go
102 lines (84 loc) · 2.87 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
package iavl
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestKeyFormatBytes(t *testing.T) {
kf := NewKeyFormat(byte('e'), 8, 8, 8)
assert.Equal(t, []byte{'e', 0, 0, 0, 0, 0, 1, 2, 3}, kf.KeyBytes([]byte{1, 2, 3}))
assert.Equal(t, []byte{'e', 1, 2, 3, 4, 5, 6, 7, 8}, kf.KeyBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8}))
assert.Equal(t, []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},
kf.KeyBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8}, []byte{1, 2, 3, 4, 5, 6, 7, 8}, []byte{1, 1, 2, 2, 3, 3}))
assert.Equal(t, []byte{'e'}, kf.KeyBytes())
}
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...)
}