github.com/lbryio/lbcd@v0.22.119/claimtrie/temporal/temporalrepo/temporalrepo_test.go (about)

     1  package temporalrepo
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/lbryio/lbcd/claimtrie/temporal"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestMemory(t *testing.T) {
    12  
    13  	repo := NewMemory()
    14  	testTemporalRepo(t, repo)
    15  }
    16  
    17  func TestPebble(t *testing.T) {
    18  
    19  	repo, err := NewPebble(t.TempDir())
    20  	require.NoError(t, err)
    21  
    22  	testTemporalRepo(t, repo)
    23  }
    24  
    25  func testTemporalRepo(t *testing.T, repo temporal.Repo) {
    26  
    27  	r := require.New(t)
    28  
    29  	nameA := []byte("a")
    30  	nameB := []byte("b")
    31  	nameC := []byte("c")
    32  
    33  	testcases := []struct {
    34  		name    []byte
    35  		heights []int32
    36  	}{
    37  		{nameA, []int32{1, 3, 2}},
    38  		{nameA, []int32{2, 3}},
    39  		{nameB, []int32{5, 4}},
    40  		{nameB, []int32{5, 1}},
    41  		{nameC, []int32{4, 3, 8}},
    42  	}
    43  
    44  	for _, i := range testcases {
    45  		names := make([][]byte, 0, len(i.heights))
    46  		for range i.heights {
    47  			names = append(names, i.name)
    48  		}
    49  		err := repo.SetNodesAt(names, i.heights)
    50  		r.NoError(err)
    51  	}
    52  
    53  	// a: 1, 2, 3
    54  	// b: 1, 5, 4
    55  	// c: 4, 3, 8
    56  
    57  	names, err := repo.NodesAt(2)
    58  	r.NoError(err)
    59  	r.ElementsMatch([][]byte{nameA}, names)
    60  
    61  	names, err = repo.NodesAt(5)
    62  	r.NoError(err)
    63  	r.ElementsMatch([][]byte{nameB}, names)
    64  
    65  	names, err = repo.NodesAt(8)
    66  	r.NoError(err)
    67  	r.ElementsMatch([][]byte{nameC}, names)
    68  
    69  	names, err = repo.NodesAt(1)
    70  	r.NoError(err)
    71  	r.ElementsMatch([][]byte{nameA, nameB}, names)
    72  
    73  	names, err = repo.NodesAt(4)
    74  	r.NoError(err)
    75  	r.ElementsMatch([][]byte{nameB, nameC}, names)
    76  
    77  	names, err = repo.NodesAt(3)
    78  	r.NoError(err)
    79  	r.ElementsMatch([][]byte{nameA, nameC}, names)
    80  }