Browse Source

db: fix memdb iterator

pull/1842/head
Ethan Buchman 7 years ago
parent
commit
8f1dea89f5
2 changed files with 29 additions and 1 deletions
  1. +1
    -1
      db/mem_db.go
  2. +28
    -0
      db/mem_db_test.go

+ 1
- 1
db/mem_db.go View File

@ -82,7 +82,7 @@ func newMemDBIterator() *memDBIterator {
}
func (it *memDBIterator) Next() bool {
if it.last >= len(it.keys) {
if it.last >= len(it.keys)-1 {
return false
}
it.last++


+ 28
- 0
db/mem_db_test.go View File

@ -0,0 +1,28 @@
package db
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMemDbIterator(t *testing.T) {
db := NewMemDB()
keys := make([][]byte, 100)
for i := 0; i < 100; i++ {
keys[i] = []byte{byte(i)}
}
value := []byte{5}
for _, k := range keys {
db.Set(k, value)
}
iter := db.Iterator()
i := 0
for iter.Next() {
assert.Equal(t, db.Get(iter.Key()), iter.Value(), "values dont match for key")
i += 1
}
assert.Equal(t, i, len(db.db), "iterator didnt cover whole db")
}

Loading…
Cancel
Save