-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrows_test.go
149 lines (125 loc) · 3.56 KB
/
rows_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 main
import (
"bytes"
"testing"
)
var testKey [][]byte
var testValue [][]byte
func init() {
rnd := NewRandom(99)
buf := &bytes.Buffer{}
for i := 1; i <= 4; i++ {
buf.Reset()
err := rnd.Bytes(buf, 32*i)
if err != nil {
panic(err)
}
testKey = append(testKey, buf.Bytes())
buf.Reset()
err = rnd.Bytes(buf, 128*i)
if err != nil {
panic(err)
}
testValue = append(testValue, buf.Bytes())
}
}
func TestRowKeyDecodeEncode(t *testing.T) {
if len(testKey) != len(testValue) {
t.Fatalf("len(testKey) [%d] != len(testValue) [%d]", len(testKey), len(testValue))
}
for i := 0; i < len(testKey); i++ {
rk, err := DecodeRowKey(testKey[i])
if err != nil {
t.Error(i, err)
continue
}
bk, err := rk.Bytes()
if err != nil {
t.Error(i, err)
continue
}
if bytes.Compare(testKey[i], bk) != 0 {
t.Errorf("%d: key and re-encoded key produced different values:%v (original)\n%v (re-encoded)",
i, testKey[i], bk)
}
}
}
func TestRowValueDecodeEncode(t *testing.T) {
if len(testKey) != len(testValue) {
t.Fatalf("len(testKey) [%d] != len(testValue) [%d]", len(testKey), len(testValue))
}
for i := 0; i < len(testValue); i++ {
rv, err := DecodeRowValue(testValue[i])
if err != nil {
t.Error(i, err)
continue
}
bv, err := rv.Bytes()
if err != nil {
t.Error(i, err)
continue
}
if bytes.Compare(testValue[i], bv) != 0 {
t.Errorf("%d: key and re-encoded value produced different values:%v (original)\n%v (re-encoded)",
i, testValue[i], bv)
}
}
}
func BenchmarkRowKeyEncode32(b *testing.B) { benchRowKeyEncode(b, testKey[0]) }
func BenchmarkRowKeyDecode32(b *testing.B) { benchRowKeyDecode(b, testKey[0]) }
func BenchmarkRowKeyEncode64(b *testing.B) { benchRowKeyEncode(b, testKey[1]) }
func BenchmarkRowKeyDecode64(b *testing.B) { benchRowKeyDecode(b, testKey[1]) }
func BenchmarkRowKeyEncode128(b *testing.B) { benchRowKeyEncode(b, testKey[2]) }
func BenchmarkRowKeyDecode128(b *testing.B) { benchRowKeyDecode(b, testKey[2]) }
func BenchmarkRowKeyEncode256(b *testing.B) { benchRowKeyEncode(b, testKey[3]) }
func BenchmarkRowKeyDecode256(b *testing.B) { benchRowKeyDecode(b, testKey[3]) }
func benchRowKeyDecode(b *testing.B, v []byte) {
for i := 0; i < b.N; i++ {
_, err := DecodeRowKey(v)
if err != nil {
b.Error(err)
}
}
}
func benchRowKeyEncode(b *testing.B, v []byte) {
rk, err := DecodeRowKey(v)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err = rk.Bytes()
if err != nil {
b.Error(err)
}
}
}
func BenchmarkRowValueEncode128(b *testing.B) { benchRowValueEncode(b, testValue[0]) }
func BenchmarkRowValueDecode128(b *testing.B) { benchRowValueDecode(b, testValue[0]) }
func BenchmarkRowValueEncode256(b *testing.B) { benchRowValueEncode(b, testValue[1]) }
func BenchmarkRowValueDecode256(b *testing.B) { benchRowValueDecode(b, testValue[1]) }
func BenchmarkRowValueEncode512(b *testing.B) { benchRowValueEncode(b, testValue[2]) }
func BenchmarkRowValueDecode512(b *testing.B) { benchRowValueDecode(b, testValue[2]) }
func BenchmarkRowValueEncode1024(b *testing.B) { benchRowValueEncode(b, testValue[3]) }
func BenchmarkRowValueDecode1024(b *testing.B) { benchRowValueDecode(b, testValue[3]) }
func benchRowValueDecode(b *testing.B, v []byte) {
for i := 0; i < b.N; i++ {
_, err := DecodeRowValue(v)
if err != nil {
b.Error(err)
}
}
}
func benchRowValueEncode(b *testing.B, v []byte) {
rv, err := DecodeRowValue(v)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err = rv.Bytes()
if err != nil {
b.Error(err)
}
}
}