github.com/coocood/badger@v1.5.1-0.20200528065104-c02ac3616d04/table/memtable/table_test.go (about)

     1  package memtable
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/coocood/badger/y"
     6  	"github.com/stretchr/testify/require"
     7  	"testing"
     8  )
     9  
    10  func TestListNodeIterator(t *testing.T) {
    11  	var entries []Entry
    12  	for i := 0; i < 100; i++ {
    13  		numVer := i%10 + 1
    14  		for j := 0; j < numVer; j++ {
    15  			entries = append(entries, newTestEntry(newKey(i), 10-j))
    16  		}
    17  	}
    18  	ln := newListNode(entries)
    19  	it := ln.newIterator(true)
    20  	require.Equal(t, len(ln.latestOffs), 100)
    21  	for i := 0; i < 100; i++ {
    22  		key := newKey(i)
    23  		it.Seek(key)
    24  		require.EqualValues(t, it.Key().UserKey, key)
    25  		numVer := i%10 + 1
    26  		require.True(t, it.Key().Version == 10)
    27  		for j := 1; j < numVer; j++ {
    28  			require.True(t, it.NextVersion())
    29  			require.True(t, it.Key().Version == uint64(10-j))
    30  		}
    31  	}
    32  	it.Rewind()
    33  	for i := 98; i >= 0; i-- {
    34  		it.Next()
    35  		key := newKey(i)
    36  		require.EqualValues(t, it.Key().UserKey, key)
    37  	}
    38  	it = ln.newIterator(false)
    39  	for i := 1; i < 100; i++ {
    40  		it.Next()
    41  		key := newKey(i)
    42  		require.EqualValues(t, it.Key().UserKey, key)
    43  	}
    44  }
    45  
    46  func newKey(i int) []byte {
    47  	return []byte(fmt.Sprintf("key%.3d", i))
    48  }
    49  
    50  func newTestEntry(key []byte, version int) Entry {
    51  	return Entry{
    52  		Key: key,
    53  		Value: y.ValueStruct{
    54  			Value:   key,
    55  			Version: uint64(version),
    56  		},
    57  	}
    58  }