Skip to content

Commit fcb9ae2

Browse files
committed
More understandable test cases
1 parent bcdb3ab commit fcb9ae2

File tree

4 files changed

+60
-61
lines changed

4 files changed

+60
-61
lines changed

internal/trie/node/decode_test.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func Test_Decode(t *testing.T) {
5252
},
5353
"leaf decoding error": {
5454
reader: bytes.NewReader([]byte{
55-
65, // node type 1 (leaf) and key length 1
55+
leafVariant.bits | 1, // key length 1
5656
// missing key data byte
5757
}),
5858
errWrapped: io.EOF,
@@ -63,8 +63,8 @@ func Test_Decode(t *testing.T) {
6363
reader: bytes.NewReader(
6464
append(
6565
[]byte{
66-
65, // node type 1 (leaf) and key length 1
67-
9, // key data
66+
leafVariant.bits | 1, // key length 1
67+
9, // key data
6868
},
6969
scaleEncodeBytes(t, 1, 2, 3)...,
7070
),
@@ -77,7 +77,7 @@ func Test_Decode(t *testing.T) {
7777
},
7878
"branch decoding error": {
7979
reader: bytes.NewReader([]byte{
80-
129, // node type 2 (branch without value) and key length 1
80+
branchVariant.bits | 1, // key length 1
8181
// missing key data byte
8282
}),
8383
errWrapped: io.EOF,
@@ -87,9 +87,9 @@ func Test_Decode(t *testing.T) {
8787
"branch success": {
8888
reader: bytes.NewReader(
8989
[]byte{
90-
129, // node type 2 (branch without value) and key length 1
91-
9, // key data
92-
0, 0, // no children bitmap
90+
branchVariant.bits | 1, // key length 1
91+
9, // key data
92+
0, 0, // no children bitmap
9393
},
9494
),
9595
n: &Node{
@@ -101,7 +101,7 @@ func Test_Decode(t *testing.T) {
101101
"branch with two inlined children": {
102102
reader: bytes.NewReader(
103103
[]byte{
104-
158, // node type 2 (branch w/o value) and key length 30
104+
branchVariant.bits | 30, // key length 30
105105
// Key data start
106106
195, 101, 195, 207, 89, 214,
107107
113, 235, 114, 218, 14, 122,
@@ -217,13 +217,11 @@ func Test_decodeBranch(t *testing.T) {
217217
errWrapped: ErrDecodeChildHash,
218218
errMessage: "cannot decode child hash: at index 10: EOF",
219219
},
220-
"success node type 2": {
220+
"success for branch variant": {
221221
reader: bytes.NewBuffer(
222222
concatByteSlices([][]byte{
223-
{
224-
9, // key data
225-
0, 4, // children bitmap
226-
},
223+
{9}, // key data
224+
{0, 4}, // children bitmap
227225
scaleEncodeBytes(t, 1, 2, 3, 4, 5), // child hash
228226
}),
229227
),
@@ -242,7 +240,7 @@ func Test_decodeBranch(t *testing.T) {
242240
Descendants: 1,
243241
},
244242
},
245-
"value decoding error for node type 3": {
243+
"value decoding error for branch with value variant": {
246244
reader: bytes.NewBuffer(
247245
concatByteSlices([][]byte{
248246
{9}, // key data
@@ -255,7 +253,7 @@ func Test_decodeBranch(t *testing.T) {
255253
errWrapped: ErrDecodeValue,
256254
errMessage: "cannot decode value: EOF",
257255
},
258-
"success node type 3": {
256+
"success for branch with value": {
259257
reader: bytes.NewBuffer(
260258
concatByteSlices([][]byte{
261259
{9}, // key data

internal/trie/node/encode_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func Test_Node_Encode(t *testing.T) {
6363
},
6464
writes: []writeCall{
6565
{
66-
written: []byte{leafVariant.bits | 0b0000_0001},
66+
written: []byte{leafVariant.bits | 1},
6767
err: errTest,
6868
},
6969
},
@@ -76,10 +76,10 @@ func Test_Node_Encode(t *testing.T) {
7676
},
7777
writes: []writeCall{
7878
{
79-
written: []byte{67},
79+
written: []byte{leafVariant.bits | 3}, // partial key length 3
8080
},
8181
{
82-
written: []byte{1, 35},
82+
written: []byte{0x01, 0x23},
8383
err: errTest,
8484
},
8585
},
@@ -93,10 +93,10 @@ func Test_Node_Encode(t *testing.T) {
9393
},
9494
writes: []writeCall{
9595
{
96-
written: []byte{67},
96+
written: []byte{leafVariant.bits | 3}, // partial key length 3
9797
},
9898
{
99-
written: []byte{1, 35},
99+
written: []byte{0x01, 0x23},
100100
},
101101
{
102102
written: []byte{12, 4, 5, 6},
@@ -113,10 +113,10 @@ func Test_Node_Encode(t *testing.T) {
113113
},
114114
writes: []writeCall{
115115
{
116-
written: []byte{67},
116+
written: []byte{leafVariant.bits | 3}, // partial key length 3
117117
},
118118
{
119-
written: []byte{1, 35},
119+
written: []byte{0x01, 0x23},
120120
},
121121
{
122122
written: []byte{12, 4, 5, 6},
@@ -158,7 +158,7 @@ func Test_Node_Encode(t *testing.T) {
158158
},
159159
writes: []writeCall{
160160
{ // header
161-
written: []byte{branchVariant.bits | 0b0000_0001},
161+
written: []byte{branchVariant.bits | 1}, // partial key length 1
162162
err: errTest,
163163
},
164164
},
@@ -173,10 +173,10 @@ func Test_Node_Encode(t *testing.T) {
173173
},
174174
writes: []writeCall{
175175
{ // header
176-
written: []byte{195},
176+
written: []byte{branchWithValueVariant.bits | 3}, // partial key length 3
177177
},
178178
{ // key LE
179-
written: []byte{1, 35},
179+
written: []byte{0x01, 0x23},
180180
err: errTest,
181181
},
182182
},
@@ -194,10 +194,10 @@ func Test_Node_Encode(t *testing.T) {
194194
},
195195
writes: []writeCall{
196196
{ // header
197-
written: []byte{195},
197+
written: []byte{branchWithValueVariant.bits | 3}, // partial key length 3
198198
},
199199
{ // key LE
200-
written: []byte{1, 35},
200+
written: []byte{0x01, 0x23},
201201
},
202202
{ // children bitmap
203203
written: []byte{136, 0},
@@ -218,10 +218,10 @@ func Test_Node_Encode(t *testing.T) {
218218
},
219219
writes: []writeCall{
220220
{ // header
221-
written: []byte{195},
221+
written: []byte{branchWithValueVariant.bits | 3}, // partial key length 3
222222
},
223223
{ // key LE
224-
written: []byte{1, 35},
224+
written: []byte{0x01, 0x23},
225225
},
226226
{ // children bitmap
227227
written: []byte{136, 0},
@@ -245,10 +245,10 @@ func Test_Node_Encode(t *testing.T) {
245245
},
246246
writes: []writeCall{
247247
{ // header
248-
written: []byte{195},
248+
written: []byte{branchWithValueVariant.bits | 3}, // partial key length 3
249249
},
250250
{ // key LE
251-
written: []byte{1, 35},
251+
written: []byte{0x01, 0x23},
252252
},
253253
{ // children bitmap
254254
written: []byte{136, 0},
@@ -277,10 +277,10 @@ func Test_Node_Encode(t *testing.T) {
277277
},
278278
writes: []writeCall{
279279
{ // header
280-
written: []byte{195},
280+
written: []byte{branchWithValueVariant.bits | 3}, // partial key length 3
281281
},
282282
{ // key LE
283-
written: []byte{1, 35},
283+
written: []byte{0x01, 0x23},
284284
},
285285
{ // children bitmap
286286
written: []byte{136, 0},

internal/trie/node/header.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func encodeHeader(node *Node, writer io.Writer) (err error) {
2828

2929
buffer := make([]byte, 1)
3030
buffer[0] = variant.bits
31-
partialKeyLengthMask := variant.mask ^ 0xff
31+
partialKeyLengthMask := ^variant.mask
3232

3333
if partialKeyLength < int(partialKeyLengthMask) {
3434
// Partial key length fits in header byte
@@ -141,7 +141,7 @@ func decodeHeaderByte(header byte) (variantBits,
141141
continue
142142
}
143143

144-
partialKeyLengthHeaderMask = variants[i].mask ^ 0xff
144+
partialKeyLengthHeaderMask = ^variants[i].mask
145145
partialKeyLengthHeader = header & partialKeyLengthHeaderMask
146146
return variantBits, partialKeyLengthHeader,
147147
partialKeyLengthHeaderMask, nil

0 commit comments

Comments
 (0)