@@ -19,6 +19,8 @@ package rawdb
1919import (
2020 "bytes"
2121 "testing"
22+
23+ "github.com/ethereum/go-ethereum/ethdb"
2224)
2325
2426func TestTableDatabase (t * testing.T ) { testTableDatabase (t , "prefix" ) }
@@ -96,48 +98,31 @@ func testTableDatabase(t *testing.T, prefix string) {
9698 }
9799 }
98100
99- // Test iterators
100- iter := db .NewIterator ()
101- var index int
102- for iter .Next () {
103- key , value := iter .Key (), iter .Value ()
104- if ! bytes .Equal (key , entries [index ].key ) {
105- t .Fatalf ("Key mismatch: want=%v, got=%v" , entries [index ].key , key )
101+ check := func (iter ethdb.Iterator , expCount , index int ) {
102+ count := 0
103+ for iter .Next () {
104+ key , value := iter .Key (), iter .Value ()
105+ if ! bytes .Equal (key , entries [index ].key ) {
106+ t .Fatalf ("Key mismatch: want=%v, got=%v" , entries [index ].key , key )
107+ }
108+ if ! bytes .Equal (value , entries [index ].value ) {
109+ t .Fatalf ("Value mismatch: want=%v, got=%v" , entries [index ].value , value )
110+ }
111+ index += 1
112+ count ++
106113 }
107- if ! bytes . Equal ( value , entries [ index ]. value ) {
108- t .Fatalf ("Value mismatch: want=%v, got=%v " , entries [ index ]. value , value )
114+ if count != expCount {
115+ t .Fatalf ("Wrong number of elems, exp %d got %d " , expCount , count )
109116 }
110- index += 1
117+ iter . Release ()
111118 }
112- iter . Release ()
113-
119+ // Test iterators
120+ check ( db . NewIterator ( nil , nil ), 6 , 0 )
114121 // Test iterators with prefix
115- iter = db .NewIteratorWithPrefix ([]byte {0xff , 0xff })
116- index = 3
117- for iter .Next () {
118- key , value := iter .Key (), iter .Value ()
119- if ! bytes .Equal (key , entries [index ].key ) {
120- t .Fatalf ("Key mismatch: want=%v, got=%v" , entries [index ].key , key )
121- }
122- if ! bytes .Equal (value , entries [index ].value ) {
123- t .Fatalf ("Value mismatch: want=%v, got=%v" , entries [index ].value , value )
124- }
125- index += 1
126- }
127- iter .Release ()
128-
122+ check (db .NewIterator ([]byte {0xff , 0xff }, nil ), 3 , 3 )
129123 // Test iterators with start point
130- iter = db .NewIteratorWithStart ([]byte {0xff , 0xff , 0x02 })
131- index = 4
132- for iter .Next () {
133- key , value := iter .Key (), iter .Value ()
134- if ! bytes .Equal (key , entries [index ].key ) {
135- t .Fatalf ("Key mismatch: want=%v, got=%v" , entries [index ].key , key )
136- }
137- if ! bytes .Equal (value , entries [index ].value ) {
138- t .Fatalf ("Value mismatch: want=%v, got=%v" , entries [index ].value , value )
139- }
140- index += 1
141- }
142- iter .Release ()
124+ check (db .NewIterator (nil , []byte {0xff , 0xff , 0x02 }), 2 , 4 )
125+ // Test iterators with prefix and start point
126+ check (db .NewIterator ([]byte {0xee }, nil ), 0 , 0 )
127+ check (db .NewIterator (nil , []byte {0x00 }), 6 , 0 )
143128}
0 commit comments