-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathencoding_test.go
118 lines (93 loc) · 3.06 KB
/
encoding_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
package lsmtree
import (
"bytes"
"testing"
)
func TestEncodePut(t *testing.T) {
buffer := &bytes.Buffer{}
key := []byte{1, 2, 3}
value := []byte{4, 5, 6}
if _, err := encode(key, value, buffer); err != nil {
t.Fatalf("unexpected error: %s", err)
}
// total = 14, key = 3, key and value
expected := []byte{0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 3, 1, 2, 3, 4, 5, 6}
if !bytes.Equal(expected, buffer.Bytes()) {
t.Fatalf("failed to encoded key/value, expected %v, but received %v", expected, buffer.Bytes())
}
}
func TestEncodeDelete(t *testing.T) {
buffer := &bytes.Buffer{}
key := []byte{1, 2, 3}
if _, err := encode(key, nil, buffer); err != nil {
t.Fatalf("unexpected error: %s", err)
}
// total = 11, key = 3, key and value
expected := []byte{0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 3, 1, 2, 3}
if !bytes.Equal(expected, buffer.Bytes()) {
t.Fatalf("failed to encode key/value, expected %v, but received %v", expected, buffer.Bytes())
}
}
func TestDecodePut(t *testing.T) {
data := []byte{0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 3, 1, 2, 3, 4, 5, 6}
buffer := bytes.NewBuffer(data)
key, value, err := decode(buffer)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !bytes.Equal([]byte{1, 2, 3}, key) {
t.Fatalf("failed to decode key, expected %v, but received %v", []byte{1, 2, 3}, key)
}
if !bytes.Equal([]byte{4, 5, 6}, value) {
t.Fatalf("failed to decode value, expected %v, but received %v", []byte{4, 5, 6}, value)
}
}
func TestDecodeDelete(t *testing.T) {
data := []byte{0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 3, 1, 2, 3}
buffer := bytes.NewBuffer(data)
key, value, err := decode(buffer)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !bytes.Equal([]byte{1, 2, 3}, key) {
t.Fatalf("failed to decode key, expected %v, but received %v", []byte{1, 2, 3}, key)
}
if value != nil {
t.Fatalf("failed to decode value, expected nil, but received %v", value)
}
}
func TestEncodePutDecode(t *testing.T) {
buffer := &bytes.Buffer{}
key := []byte{1, 2, 3}
value := []byte{4, 5, 6}
if _, err := encode(key, value, buffer); err != nil {
t.Fatalf("unexpected error: %s", err)
}
decodedKey, decodedValue, err := decode(buffer)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !bytes.Equal(key, decodedKey) {
t.Fatalf("failed to encode/decode key, expected %v, but received %v", key, decodedKey)
}
if !bytes.Equal(value, decodedValue) {
t.Fatalf("failed to encode/decode value, expected %v, but received %v", value, decodedValue)
}
}
func TestEncodeDeleteDecode(t *testing.T) {
buffer := &bytes.Buffer{}
key := []byte{1, 2, 3}
if _, err := encode(key, nil, buffer); err != nil {
t.Fatalf("unexpected error: %s", err)
}
decodedKey, decodedValue, err := decode(buffer)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !bytes.Equal(key, decodedKey) {
t.Fatalf("failed to encode/decode key, expected %v, but received %v", key, decodedKey)
}
if nil != decodedValue {
t.Fatalf("failed to encode/decode value, expected %v, but received %v", nil, decodedValue)
}
}