|
@ -149,3 +149,67 @@ func TestGoLevelDBBackend(t *testing.T) { |
|
|
_, ok := db.(*GoLevelDB) |
|
|
_, ok := db.(*GoLevelDB) |
|
|
assert.True(t, ok) |
|
|
assert.True(t, ok) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestDBIterator(t *testing.T) { |
|
|
|
|
|
for dbType := range backends { |
|
|
|
|
|
t.Run(fmt.Sprintf("%v", dbType), func(t *testing.T) { |
|
|
|
|
|
testDBIterator(t, dbType) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func testDBIterator(t *testing.T, backend DBBackendType) { |
|
|
|
|
|
name := cmn.Fmt("test_%x", cmn.RandStr(12)) |
|
|
|
|
|
db := NewDB(name, backend, "") |
|
|
|
|
|
defer cleanupDBDir("", name) |
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ { |
|
|
|
|
|
if i != 6 { // but skip 6.
|
|
|
|
|
|
db.Set(int642Bytes(int64(i)), nil) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
verifyIterator(t, db.Iterator(nil, nil), []int64{0, 1, 2, 3, 4, 5, 7, 8, 9}, "forward iterator") |
|
|
|
|
|
verifyIterator(t, db.ReverseIterator(nil, nil), []int64{9, 8, 7, 5, 4, 3, 2, 1, 0}, "reverse iterator") |
|
|
|
|
|
|
|
|
|
|
|
verifyIterator(t, db.Iterator(nil, int642Bytes(0)), []int64(nil), "forward iterator to 0") |
|
|
|
|
|
verifyIterator(t, db.ReverseIterator(nil, int642Bytes(10)), []int64(nil), "reverse iterator 10") |
|
|
|
|
|
|
|
|
|
|
|
verifyIterator(t, db.Iterator(int642Bytes(0), nil), []int64{0, 1, 2, 3, 4, 5, 7, 8, 9}, "forward iterator from 0") |
|
|
|
|
|
verifyIterator(t, db.Iterator(int642Bytes(1), nil), []int64{1, 2, 3, 4, 5, 7, 8, 9}, "forward iterator from 1") |
|
|
|
|
|
verifyIterator(t, db.ReverseIterator(int642Bytes(10), nil), []int64{9, 8, 7, 5, 4, 3, 2, 1, 0}, "reverse iterator from 10") |
|
|
|
|
|
verifyIterator(t, db.ReverseIterator(int642Bytes(9), nil), []int64{9, 8, 7, 5, 4, 3, 2, 1, 0}, "reverse iterator from 9") |
|
|
|
|
|
verifyIterator(t, db.ReverseIterator(int642Bytes(8), nil), []int64{8, 7, 5, 4, 3, 2, 1, 0}, "reverse iterator from 8") |
|
|
|
|
|
|
|
|
|
|
|
verifyIterator(t, db.Iterator(int642Bytes(5), int642Bytes(6)), []int64{5}, "forward iterator from 5 to 6") |
|
|
|
|
|
verifyIterator(t, db.Iterator(int642Bytes(5), int642Bytes(7)), []int64{5}, "forward iterator from 5 to 7") |
|
|
|
|
|
verifyIterator(t, db.Iterator(int642Bytes(5), int642Bytes(8)), []int64{5, 7}, "forward iterator from 5 to 8") |
|
|
|
|
|
verifyIterator(t, db.Iterator(int642Bytes(6), int642Bytes(7)), []int64(nil), "forward iterator from 6 to 7") |
|
|
|
|
|
verifyIterator(t, db.Iterator(int642Bytes(6), int642Bytes(8)), []int64{7}, "forward iterator from 6 to 8") |
|
|
|
|
|
verifyIterator(t, db.Iterator(int642Bytes(7), int642Bytes(8)), []int64{7}, "forward iterator from 7 to 8") |
|
|
|
|
|
|
|
|
|
|
|
verifyIterator(t, db.ReverseIterator(int642Bytes(5), int642Bytes(4)), []int64{5}, "reverse iterator from 5 to 4") |
|
|
|
|
|
verifyIterator(t, db.ReverseIterator(int642Bytes(6), int642Bytes(4)), []int64{5}, "reverse iterator from 6 to 4") |
|
|
|
|
|
verifyIterator(t, db.ReverseIterator(int642Bytes(7), int642Bytes(4)), []int64{7, 5}, "reverse iterator from 7 to 4") |
|
|
|
|
|
verifyIterator(t, db.ReverseIterator(int642Bytes(6), int642Bytes(5)), []int64(nil), "reverse iterator from 6 to 5") |
|
|
|
|
|
verifyIterator(t, db.ReverseIterator(int642Bytes(7), int642Bytes(5)), []int64{7}, "reverse iterator from 7 to 5") |
|
|
|
|
|
verifyIterator(t, db.ReverseIterator(int642Bytes(7), int642Bytes(6)), []int64{7}, "reverse iterator from 7 to 6") |
|
|
|
|
|
|
|
|
|
|
|
verifyIterator(t, db.Iterator(int642Bytes(0), int642Bytes(1)), []int64{0}, "forward iterator from 0 to 1") |
|
|
|
|
|
verifyIterator(t, db.ReverseIterator(int642Bytes(9), int642Bytes(8)), []int64{9}, "reverse iterator from 9 to 8") |
|
|
|
|
|
|
|
|
|
|
|
verifyIterator(t, db.Iterator(int642Bytes(2), int642Bytes(4)), []int64{2, 3}, "forward iterator from 2 to 4") |
|
|
|
|
|
verifyIterator(t, db.Iterator(int642Bytes(4), int642Bytes(2)), []int64(nil), "forward iterator from 4 to 2") |
|
|
|
|
|
verifyIterator(t, db.ReverseIterator(int642Bytes(4), int642Bytes(2)), []int64{4, 3}, "reverse iterator from 4 to 2") |
|
|
|
|
|
verifyIterator(t, db.ReverseIterator(int642Bytes(2), int642Bytes(4)), []int64(nil), "reverse iterator from 2 to 4") |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func verifyIterator(t *testing.T, itr Iterator, expected []int64, msg string) { |
|
|
|
|
|
var list []int64 |
|
|
|
|
|
for itr.Valid() { |
|
|
|
|
|
list = append(list, bytes2Int64(itr.Key())) |
|
|
|
|
|
itr.Next() |
|
|
|
|
|
} |
|
|
|
|
|
assert.Equal(t, expected, list, msg) |
|
|
|
|
|
} |