Skip to content

Commit 4a4ddb8

Browse files
laanwjfurszy
authored andcommitted
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 109446f commit 4a4ddb8

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/test/dbwrapper_tests.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,38 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
111111
}
112112
}
113113

114+
BOOST_AUTO_TEST_CASE(iterator_ordering)
115+
{
116+
fs::path ph = fs::temp_directory_path() / fs::unique_path();
117+
CDBWrapper dbw(ph, (1 << 20), true, false);
118+
for (int x=0x00; x<256; ++x) {
119+
uint8_t key = x;
120+
uint32_t value = x*x;
121+
BOOST_CHECK(dbw.Write(key, value));
122+
}
123+
124+
std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper*>(&dbw)->NewIterator());
125+
for (int c=0; c<2; ++c) {
126+
int seek_start;
127+
if (c == 0)
128+
seek_start = 0x00;
129+
else
130+
seek_start = 0x80;
131+
it->Seek((uint8_t)seek_start);
132+
for (int x=seek_start; x<256; ++x) {
133+
uint8_t key;
134+
uint32_t value;
135+
BOOST_CHECK(it->Valid());
136+
if (!it->Valid()) // Avoid spurious errors about invalid iterator's key and value in case of failure
137+
break;
138+
BOOST_CHECK(it->GetKey(key));
139+
BOOST_CHECK(it->GetValue(value));
140+
BOOST_CHECK_EQUAL(key, x);
141+
BOOST_CHECK_EQUAL(value, x*x);
142+
it->Next();
143+
}
144+
BOOST_CHECK(!it->Valid());
145+
}
146+
}
147+
114148
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)