Skip to content

Commit

Permalink
added nibbles.Advance test
Browse files Browse the repository at this point in the history
  • Loading branch information
timwu20 committed Oct 11, 2024
1 parent bed20e2 commit dddbecc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
5 changes: 1 addition & 4 deletions pkg/trie/triedb/nibbles/nibbles.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,7 @@ func (n Nibbles) CommonPrefix(them Nibbles) uint {
func (n Nibbles) NodeKey() NodeKey {
split := n.offset / NibblesPerByte
offset := n.offset % NibblesPerByte
return struct {
Offset uint
Data []byte
}{offset, n.data[split:]}
return NodeKey{offset, n.data[split:]}
}

// Helper function to create a [NodeKey] for a given number of nibbles.
Expand Down
17 changes: 17 additions & 0 deletions pkg/trie/triedb/nibbles/nibbles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNibbles(t *testing.T) {
Expand Down Expand Up @@ -73,3 +74,19 @@ func TestNibbles_Compare(t *testing.T) {
assert.Equal(t, -1, n.Compare(m))
assert.Equal(t, 1, m.Compare(n))
}

func TestNibbles_Advance(t *testing.T) {
n := NewNibbles([]byte{1, 35})
n.Advance(1)
n.Advance(1)
n.Advance(1)
n.Advance(1)
require.Panics(t, func() { n.Advance(1) })

n = NewNibbles([]byte{1, 35})
require.Panics(t, func() { n.Advance(5) })

n = NewNibbles([]byte{1, 35})
n.Advance(4)
require.Panics(t, func() { n.Advance(1) })
}

0 comments on commit dddbecc

Please sign in to comment.