aboutsummaryrefslogtreecommitdiff
path: root/src/test/dbwrapper_tests.cpp
diff options
context:
space:
mode:
authorMatt Corallo <git@bluematt.me>2016-05-02 17:52:31 -0700
committerMatt Corallo <git@bluematt.me>2016-05-17 19:44:58 -0700
commit269a4402a8617a539a70b2c332e86f0fe292a7a6 (patch)
tree588a8dece93c446e5aa534deebe2bc425bb8b5bb /src/test/dbwrapper_tests.cpp
parent6030625631c62b0ffab2ac545c8351fa59dca483 (diff)
downloadbitcoin-269a4402a8617a539a70b2c332e86f0fe292a7a6.tar.xz
Add test for dbwrapper iterators with same-prefix keys.
Diffstat (limited to 'src/test/dbwrapper_tests.cpp')
-rw-r--r--src/test/dbwrapper_tests.cpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/test/dbwrapper_tests.cpp b/src/test/dbwrapper_tests.cpp
index 8745d1439c..a0bdcf4afb 100644
--- a/src/test/dbwrapper_tests.cpp
+++ b/src/test/dbwrapper_tests.cpp
@@ -238,4 +238,90 @@ BOOST_AUTO_TEST_CASE(iterator_ordering)
}
}
+struct StringContentsSerializer {
+ // Used to make two serialized objects the same while letting them have a different lengths
+ // This is a terrible idea
+ string str;
+ StringContentsSerializer() {}
+ StringContentsSerializer(const string& inp) : str(inp) {}
+
+ StringContentsSerializer& operator+=(const string& s) {
+ str += s;
+ return *this;
+ }
+ StringContentsSerializer& operator+=(const StringContentsSerializer& s) { return *this += s.str; }
+
+ ADD_SERIALIZE_METHODS;
+
+ template <typename Stream, typename Operation>
+ inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
+ if (ser_action.ForRead()) {
+ str.clear();
+ char c = 0;
+ while (true) {
+ try {
+ READWRITE(c);
+ str.push_back(c);
+ } catch (const std::ios_base::failure& e) {
+ break;
+ }
+ }
+ } else {
+ for (size_t i = 0; i < str.size(); i++)
+ READWRITE(str[i]);
+ }
+ }
+};
+
+BOOST_AUTO_TEST_CASE(iterator_string_ordering)
+{
+ char buf[10];
+
+ path ph = temp_directory_path() / unique_path();
+ CDBWrapper dbw(ph, (1 << 20), true, false, false);
+ for (int x=0x00; x<10; ++x) {
+ for (int y = 0; y < 10; y++) {
+ sprintf(buf, "%d", x);
+ StringContentsSerializer key(buf);
+ for (int z = 0; z < y; z++)
+ key += key;
+ uint32_t value = x*x;
+ BOOST_CHECK(dbw.Write(key, value));
+ }
+ }
+
+ boost::scoped_ptr<CDBIterator> it(const_cast<CDBWrapper*>(&dbw)->NewIterator());
+ for (int c=0; c<2; ++c) {
+ int seek_start;
+ if (c == 0)
+ seek_start = 0;
+ else
+ seek_start = 5;
+ sprintf(buf, "%d", seek_start);
+ StringContentsSerializer seek_key(buf);
+ it->Seek(seek_key);
+ for (int x=seek_start; x<10; ++x) {
+ for (int y = 0; y < 10; y++) {
+ sprintf(buf, "%d", x);
+ string exp_key(buf);
+ for (int z = 0; z < y; z++)
+ exp_key += exp_key;
+ StringContentsSerializer key;
+ uint32_t value;
+ BOOST_CHECK(it->Valid());
+ if (!it->Valid()) // Avoid spurious errors about invalid iterator's key and value in case of failure
+ break;
+ BOOST_CHECK(it->GetKey(key));
+ BOOST_CHECK(it->GetValue(value));
+ BOOST_CHECK_EQUAL(key.str, exp_key);
+ BOOST_CHECK_EQUAL(value, x*x);
+ it->Next();
+ }
+ }
+ BOOST_CHECK(!it->Valid());
+ }
+}
+
+
+
BOOST_AUTO_TEST_SUITE_END()