Skip to content

Commit

Permalink
More understandable test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jun 6, 2022
1 parent bcdb3ab commit fcb9ae2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 61 deletions.
28 changes: 13 additions & 15 deletions internal/trie/node/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func Test_Decode(t *testing.T) {
},
"leaf decoding error": {
reader: bytes.NewReader([]byte{
65, // node type 1 (leaf) and key length 1
leafVariant.bits | 1, // key length 1
// missing key data byte
}),
errWrapped: io.EOF,
Expand All @@ -63,8 +63,8 @@ func Test_Decode(t *testing.T) {
reader: bytes.NewReader(
append(
[]byte{
65, // node type 1 (leaf) and key length 1
9, // key data
leafVariant.bits | 1, // key length 1
9, // key data
},
scaleEncodeBytes(t, 1, 2, 3)...,
),
Expand All @@ -77,7 +77,7 @@ func Test_Decode(t *testing.T) {
},
"branch decoding error": {
reader: bytes.NewReader([]byte{
129, // node type 2 (branch without value) and key length 1
branchVariant.bits | 1, // key length 1
// missing key data byte
}),
errWrapped: io.EOF,
Expand All @@ -87,9 +87,9 @@ func Test_Decode(t *testing.T) {
"branch success": {
reader: bytes.NewReader(
[]byte{
129, // node type 2 (branch without value) and key length 1
9, // key data
0, 0, // no children bitmap
branchVariant.bits | 1, // key length 1
9, // key data
0, 0, // no children bitmap
},
),
n: &Node{
Expand All @@ -101,7 +101,7 @@ func Test_Decode(t *testing.T) {
"branch with two inlined children": {
reader: bytes.NewReader(
[]byte{
158, // node type 2 (branch w/o value) and key length 30
branchVariant.bits | 30, // key length 30
// Key data start
195, 101, 195, 207, 89, 214,
113, 235, 114, 218, 14, 122,
Expand Down Expand Up @@ -217,13 +217,11 @@ func Test_decodeBranch(t *testing.T) {
errWrapped: ErrDecodeChildHash,
errMessage: "cannot decode child hash: at index 10: EOF",
},
"success node type 2": {
"success for branch variant": {
reader: bytes.NewBuffer(
concatByteSlices([][]byte{
{
9, // key data
0, 4, // children bitmap
},
{9}, // key data
{0, 4}, // children bitmap
scaleEncodeBytes(t, 1, 2, 3, 4, 5), // child hash
}),
),
Expand All @@ -242,7 +240,7 @@ func Test_decodeBranch(t *testing.T) {
Descendants: 1,
},
},
"value decoding error for node type 3": {
"value decoding error for branch with value variant": {
reader: bytes.NewBuffer(
concatByteSlices([][]byte{
{9}, // key data
Expand All @@ -255,7 +253,7 @@ func Test_decodeBranch(t *testing.T) {
errWrapped: ErrDecodeValue,
errMessage: "cannot decode value: EOF",
},
"success node type 3": {
"success for branch with value": {
reader: bytes.NewBuffer(
concatByteSlices([][]byte{
{9}, // key data
Expand Down
36 changes: 18 additions & 18 deletions internal/trie/node/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func Test_Node_Encode(t *testing.T) {
},
writes: []writeCall{
{
written: []byte{leafVariant.bits | 0b0000_0001},
written: []byte{leafVariant.bits | 1},
err: errTest,
},
},
Expand All @@ -76,10 +76,10 @@ func Test_Node_Encode(t *testing.T) {
},
writes: []writeCall{
{
written: []byte{67},
written: []byte{leafVariant.bits | 3}, // partial key length 3
},
{
written: []byte{1, 35},
written: []byte{0x01, 0x23},
err: errTest,
},
},
Expand All @@ -93,10 +93,10 @@ func Test_Node_Encode(t *testing.T) {
},
writes: []writeCall{
{
written: []byte{67},
written: []byte{leafVariant.bits | 3}, // partial key length 3
},
{
written: []byte{1, 35},
written: []byte{0x01, 0x23},
},
{
written: []byte{12, 4, 5, 6},
Expand All @@ -113,10 +113,10 @@ func Test_Node_Encode(t *testing.T) {
},
writes: []writeCall{
{
written: []byte{67},
written: []byte{leafVariant.bits | 3}, // partial key length 3
},
{
written: []byte{1, 35},
written: []byte{0x01, 0x23},
},
{
written: []byte{12, 4, 5, 6},
Expand Down Expand Up @@ -158,7 +158,7 @@ func Test_Node_Encode(t *testing.T) {
},
writes: []writeCall{
{ // header
written: []byte{branchVariant.bits | 0b0000_0001},
written: []byte{branchVariant.bits | 1}, // partial key length 1
err: errTest,
},
},
Expand All @@ -173,10 +173,10 @@ func Test_Node_Encode(t *testing.T) {
},
writes: []writeCall{
{ // header
written: []byte{195},
written: []byte{branchWithValueVariant.bits | 3}, // partial key length 3
},
{ // key LE
written: []byte{1, 35},
written: []byte{0x01, 0x23},
err: errTest,
},
},
Expand All @@ -194,10 +194,10 @@ func Test_Node_Encode(t *testing.T) {
},
writes: []writeCall{
{ // header
written: []byte{195},
written: []byte{branchWithValueVariant.bits | 3}, // partial key length 3
},
{ // key LE
written: []byte{1, 35},
written: []byte{0x01, 0x23},
},
{ // children bitmap
written: []byte{136, 0},
Expand All @@ -218,10 +218,10 @@ func Test_Node_Encode(t *testing.T) {
},
writes: []writeCall{
{ // header
written: []byte{195},
written: []byte{branchWithValueVariant.bits | 3}, // partial key length 3
},
{ // key LE
written: []byte{1, 35},
written: []byte{0x01, 0x23},
},
{ // children bitmap
written: []byte{136, 0},
Expand All @@ -245,10 +245,10 @@ func Test_Node_Encode(t *testing.T) {
},
writes: []writeCall{
{ // header
written: []byte{195},
written: []byte{branchWithValueVariant.bits | 3}, // partial key length 3
},
{ // key LE
written: []byte{1, 35},
written: []byte{0x01, 0x23},
},
{ // children bitmap
written: []byte{136, 0},
Expand Down Expand Up @@ -277,10 +277,10 @@ func Test_Node_Encode(t *testing.T) {
},
writes: []writeCall{
{ // header
written: []byte{195},
written: []byte{branchWithValueVariant.bits | 3}, // partial key length 3
},
{ // key LE
written: []byte{1, 35},
written: []byte{0x01, 0x23},
},
{ // children bitmap
written: []byte{136, 0},
Expand Down
4 changes: 2 additions & 2 deletions internal/trie/node/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func encodeHeader(node *Node, writer io.Writer) (err error) {

buffer := make([]byte, 1)
buffer[0] = variant.bits
partialKeyLengthMask := variant.mask ^ 0xff
partialKeyLengthMask := ^variant.mask

if partialKeyLength < int(partialKeyLengthMask) {
// Partial key length fits in header byte
Expand Down Expand Up @@ -141,7 +141,7 @@ func decodeHeaderByte(header byte) (variantBits,
continue
}

partialKeyLengthHeaderMask = variants[i].mask ^ 0xff
partialKeyLengthHeaderMask = ^variants[i].mask
partialKeyLengthHeader = header & partialKeyLengthHeaderMask
return variantBits, partialKeyLengthHeader,
partialKeyLengthHeaderMask, nil
Expand Down
Loading

0 comments on commit fcb9ae2

Please sign in to comment.