github.com/iotexproject/iotex-core@v1.14.1-rc1/db/trie/mptrie/leafiterator_test.go (about) 1 // Copyright (c) 2020 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package mptrie 7 8 import ( 9 "context" 10 "testing" 11 12 "github.com/stretchr/testify/require" 13 14 "github.com/iotexproject/iotex-core/db/trie" 15 ) 16 17 func TestIterator(t *testing.T) { 18 var ( 19 require = require.New(t) 20 items = []struct{ k, v string }{ 21 {"iotex", "coin"}, 22 {"block", "chain"}, 23 {"chain", "link"}, 24 {"puppy", "dog"}, 25 {"night", "knight"}, 26 } 27 memStore = trie.NewMemKVStore() 28 ) 29 30 mpt, err := New(KVStoreOption(memStore), KeyLengthOption(5), AsyncOption()) 31 require.NoError(err) 32 require.NoError(mpt.Start(context.Background())) 33 34 for _, item := range items { 35 require.NoError(mpt.Upsert([]byte(item.k), []byte(item.v))) 36 } 37 38 iter, err := NewLeafIterator(mpt) 39 require.NoError(err) 40 41 found := make(map[string]string) 42 for { 43 k, v, err := iter.Next() 44 if err != nil { 45 require.Equal(trie.ErrEndOfIterator, err) 46 break 47 } 48 found[string(k)] = string(v) 49 } 50 51 for _, item := range items { 52 require.Equal(item.v, found[item.k], "key: %s", item.k) 53 } 54 }