From 8f1dea89f5a7b2c2d883a0310c5ef94d8980bcdb Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Fri, 25 Aug 2017 16:35:37 -0400 Subject: [PATCH] db: fix memdb iterator --- db/mem_db.go | 2 +- db/mem_db_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 db/mem_db_test.go diff --git a/db/mem_db.go b/db/mem_db.go index 286624294..db40227e8 100644 --- a/db/mem_db.go +++ b/db/mem_db.go @@ -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++ diff --git a/db/mem_db_test.go b/db/mem_db_test.go new file mode 100644 index 000000000..a76e10dc8 --- /dev/null +++ b/db/mem_db_test.go @@ -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") +}