2022-01-13 01:57:23 +00:00
|
|
|
package iterator_test
|
|
|
|
|
|
|
|
import (
|
2022-02-22 08:13:29 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2022-01-13 01:57:23 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-09-19 16:58:23 +00:00
|
|
|
iter "github.com/cerc-io/eth-iterator-utils"
|
2023-09-26 11:34:41 +00:00
|
|
|
"github.com/cerc-io/eth-iterator-utils/internal"
|
2022-01-13 01:57:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMakePaths(t *testing.T) {
|
|
|
|
var prefix []byte
|
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
nbins := uint(1) << i
|
|
|
|
paths := iter.MakePaths(prefix, nbins)
|
|
|
|
if len(paths) != int(nbins) {
|
2022-02-24 14:56:27 +00:00
|
|
|
t.Errorf("wrong number of paths; expected %d, have %d", nbins, len(paths))
|
2022-01-13 01:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 08:13:29 +00:00
|
|
|
|
|
|
|
func TestIterator(t *testing.T) {
|
2023-09-26 11:34:41 +00:00
|
|
|
tree, edb := internal.OpenFixtureTrie(t, 1)
|
|
|
|
t.Cleanup(func() { edb.Close() })
|
2022-02-22 08:13:29 +00:00
|
|
|
|
|
|
|
t.Run("in bounds", func(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
lower, upper []byte
|
|
|
|
}
|
|
|
|
cases := []testCase{
|
2022-05-31 15:08:23 +00:00
|
|
|
{nil, []byte{0, 0}},
|
|
|
|
{[]byte{1, 0}, []byte{2, 0}},
|
|
|
|
{[]byte{3, 5}, []byte{4, 2, 0}},
|
|
|
|
{[]byte{5, 6, 9, 0}, []byte{7, 0}},
|
|
|
|
{[]byte{8, 0}, []byte{8, 0}},
|
|
|
|
{[]byte{8, 0}, []byte{7, 0}},
|
2022-02-22 08:13:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
runCase := func(t *testing.T, tc testCase) {
|
2024-03-15 04:53:34 +00:00
|
|
|
nit, err := tree.NodeIterator(iter.HexToKeyBytes(tc.lower))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create iterator: %v", err)
|
|
|
|
}
|
|
|
|
it := iter.NewPrefixBoundIterator(nit, tc.upper)
|
2022-02-22 08:13:29 +00:00
|
|
|
for it.Next(true) {
|
|
|
|
if bytes.Compare(it.Path(), tc.lower) < 0 {
|
|
|
|
t.Fatalf("iterator outside lower bound: %v", it.Path())
|
|
|
|
}
|
2022-05-31 15:08:23 +00:00
|
|
|
if bytes.Compare(tc.upper, it.Path()) < 0 {
|
|
|
|
t.Fatalf("iterator outside upper bound: %v <= %v", tc.upper, it.Path())
|
2022-02-22 08:13:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run("case", func(t *testing.T) { runCase(t, tc) })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("trie is covered", func(t *testing.T) {
|
2023-09-26 11:34:41 +00:00
|
|
|
allPaths := internal.FixtureNodePaths
|
2022-02-22 08:13:29 +00:00
|
|
|
cases := []uint{1, 2, 4, 8, 16, 32}
|
|
|
|
runCase := func(t *testing.T, nbins uint) {
|
2024-03-15 04:53:34 +00:00
|
|
|
iters, err := iter.SubtrieIterators(tree.NodeIterator, nbins)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create subtrie iterators: %v", err)
|
|
|
|
}
|
2022-02-22 08:13:29 +00:00
|
|
|
ix := 0
|
|
|
|
for b := uint(0); b < nbins; b++ {
|
2022-02-24 14:56:27 +00:00
|
|
|
for it := iters[b]; it.Next(true); ix++ {
|
2022-02-22 08:13:29 +00:00
|
|
|
if !bytes.Equal(allPaths[ix], it.Path()) {
|
2023-09-19 16:58:23 +00:00
|
|
|
t.Fatalf("wrong path value in bin %d (index %d)\nexpected:\t%v\nactual:\t\t%v",
|
|
|
|
b, ix, allPaths[ix], it.Path())
|
2022-02-22 08:13:29 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-19 16:58:23 +00:00
|
|
|
// if the last node path for the previous bin was even-length, the next iterator
|
|
|
|
// will seek to the same node and it will be duplicated (see comment in Next()).
|
|
|
|
if len(allPaths[ix-1])&1 == 0 {
|
2022-05-31 15:08:23 +00:00
|
|
|
ix--
|
|
|
|
}
|
2022-02-22 08:13:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(fmt.Sprintf("%d bins", tc), func(t *testing.T) { runCase(t, tc) })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|