Skip to content

Commit 6030625

Browse files
committed
test: Add more thorough test for dbwrapper iterators
I made a silly mistake in a database wrapper where keys were sorted by char instead of uint8_t. As x86 char is signed the sorting for the block index database was messed up, resulting in a segfault due to missing records. Add a test to catch: - Wrong sorting - Seeking errors - Iteration result not complete
1 parent 84c13e7 commit 6030625

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/test/dbwrapper_tests.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,39 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
203203
BOOST_CHECK(odbw.Read(key, res3));
204204
BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString());
205205
}
206-
206+
207+
BOOST_AUTO_TEST_CASE(iterator_ordering)
208+
{
209+
path ph = temp_directory_path() / unique_path();
210+
CDBWrapper dbw(ph, (1 << 20), true, false, false);
211+
for (int x=0x00; x<256; ++x) {
212+
uint8_t key = x;
213+
uint32_t value = x*x;
214+
BOOST_CHECK(dbw.Write(key, value));
215+
}
216+
217+
boost::scoped_ptr<CDBIterator> it(const_cast<CDBWrapper*>(&dbw)->NewIterator());
218+
for (int c=0; c<2; ++c) {
219+
int seek_start;
220+
if (c == 0)
221+
seek_start = 0x00;
222+
else
223+
seek_start = 0x80;
224+
it->Seek((uint8_t)seek_start);
225+
for (int x=seek_start; x<256; ++x) {
226+
uint8_t key;
227+
uint32_t value;
228+
BOOST_CHECK(it->Valid());
229+
if (!it->Valid()) // Avoid spurious errors about invalid iterator's key and value in case of failure
230+
break;
231+
BOOST_CHECK(it->GetKey(key));
232+
BOOST_CHECK(it->GetValue(value));
233+
BOOST_CHECK_EQUAL(key, x);
234+
BOOST_CHECK_EQUAL(value, x*x);
235+
it->Next();
236+
}
237+
BOOST_CHECK(!it->Valid());
238+
}
239+
}
240+
207241
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)