github.com/pingcap/badger@v1.5.1-0.20230103063557-828f39b09b6d/table/memtable/table_test.go (about) 1 package memtable 2 3 import ( 4 "fmt" 5 "github.com/pingcap/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.Close() 39 it = ln.newIterator(false) 40 for i := 1; i < 100; i++ { 41 it.Next() 42 key := newKey(i) 43 require.EqualValues(t, it.Key().UserKey, key) 44 } 45 it.Close() 46 } 47 48 func newKey(i int) []byte { 49 return []byte(fmt.Sprintf("key%.3d", i)) 50 } 51 52 func newTestEntry(key []byte, version int) Entry { 53 return Entry{ 54 Key: key, 55 Value: y.ValueStruct{ 56 Value: key, 57 Version: uint64(version), 58 }, 59 } 60 }