Changeset View
Changeset View
Standalone View
Standalone View
src/test/dbwrapper_tests.cpp
Show All 11 Lines | |||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
#include <memory> | #include <memory> | ||||
// Test if a string consists entirely of null characters | // Test if a string consists entirely of null characters | ||||
static bool is_null_key(const std::vector<uint8_t> &key) { | static bool is_null_key(const std::vector<uint8_t> &key) { | ||||
bool isnull = true; | bool isnull = true; | ||||
for (unsigned int i = 0; i < key.size(); i++) | for (unsigned int i = 0; i < key.size(); i++) { | ||||
isnull &= (key[i] == '\x00'); | isnull &= (key[i] == '\x00'); | ||||
} | |||||
return isnull; | return isnull; | ||||
} | } | ||||
BOOST_FIXTURE_TEST_SUITE(dbwrapper_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(dbwrapper_tests, BasicTestingSetup) | ||||
BOOST_AUTO_TEST_CASE(dbwrapper) { | BOOST_AUTO_TEST_CASE(dbwrapper) { | ||||
// Perform tests both obfuscated and non-obfuscated. | // Perform tests both obfuscated and non-obfuscated. | ||||
▲ Show 20 Lines • Show All 199 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(iterator_ordering) { | ||||
for (const int seek_start : {0x00, 0x80}) { | for (const int seek_start : {0x00, 0x80}) { | ||||
it->Seek((uint8_t)seek_start); | it->Seek((uint8_t)seek_start); | ||||
for (unsigned int x = seek_start; x < 255; ++x) { | for (unsigned int x = seek_start; x < 255; ++x) { | ||||
uint8_t key; | uint8_t key; | ||||
uint32_t value; | uint32_t value; | ||||
BOOST_CHECK(it->Valid()); | BOOST_CHECK(it->Valid()); | ||||
// Avoid spurious errors about invalid iterator's key and value in | // Avoid spurious errors about invalid iterator's key and value in | ||||
// case of failure | // case of failure | ||||
if (!it->Valid()) break; | if (!it->Valid()) { | ||||
break; | |||||
} | |||||
BOOST_CHECK(it->GetKey(key)); | BOOST_CHECK(it->GetKey(key)); | ||||
if (x & 1) { | if (x & 1) { | ||||
BOOST_CHECK_EQUAL(key, x + 1); | BOOST_CHECK_EQUAL(key, x + 1); | ||||
continue; | continue; | ||||
} | } | ||||
BOOST_CHECK(it->GetValue(value)); | BOOST_CHECK(it->GetValue(value)); | ||||
BOOST_CHECK_EQUAL(key, x); | BOOST_CHECK_EQUAL(key, x); | ||||
BOOST_CHECK_EQUAL(value, x * x); | BOOST_CHECK_EQUAL(value, x * x); | ||||
Show All 29 Lines | inline void SerializationOp(Stream &s, Operation ser_action) { | ||||
try { | try { | ||||
READWRITE(c); | READWRITE(c); | ||||
str.push_back(c); | str.push_back(c); | ||||
} catch (const std::ios_base::failure &e) { | } catch (const std::ios_base::failure &e) { | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
} else { | } else { | ||||
for (size_t i = 0; i < str.size(); i++) | for (size_t i = 0; i < str.size(); i++) { | ||||
READWRITE(str[i]); | READWRITE(str[i]); | ||||
} | } | ||||
} | } | ||||
} | |||||
}; | }; | ||||
BOOST_AUTO_TEST_CASE(iterator_string_ordering) { | BOOST_AUTO_TEST_CASE(iterator_string_ordering) { | ||||
char buf[10]; | char buf[10]; | ||||
fs::path ph = SetDataDir("iterator_string_ordering"); | fs::path ph = SetDataDir("iterator_string_ordering"); | ||||
CDBWrapper dbw(ph, (1 << 20), true, false, false); | CDBWrapper dbw(ph, (1 << 20), true, false, false); | ||||
for (int x = 0x00; x < 10; ++x) { | for (int x = 0x00; x < 10; ++x) { | ||||
for (int y = 0; y < 10; y++) { | for (int y = 0; y < 10; y++) { | ||||
snprintf(buf, sizeof(buf), "%d", x); | snprintf(buf, sizeof(buf), "%d", x); | ||||
StringContentsSerializer key(buf); | StringContentsSerializer key(buf); | ||||
for (int z = 0; z < y; z++) | for (int z = 0; z < y; z++) { | ||||
key += key; | key += key; | ||||
} | |||||
uint32_t value = x * x; | uint32_t value = x * x; | ||||
BOOST_CHECK(dbw.Write(key, value)); | BOOST_CHECK(dbw.Write(key, value)); | ||||
} | } | ||||
} | } | ||||
std::unique_ptr<CDBIterator> it( | std::unique_ptr<CDBIterator> it( | ||||
const_cast<CDBWrapper &>(dbw).NewIterator()); | const_cast<CDBWrapper &>(dbw).NewIterator()); | ||||
for (const int seek_start : {0, 5}) { | for (const int seek_start : {0, 5}) { | ||||
snprintf(buf, sizeof(buf), "%d", seek_start); | snprintf(buf, sizeof(buf), "%d", seek_start); | ||||
StringContentsSerializer seek_key(buf); | StringContentsSerializer seek_key(buf); | ||||
it->Seek(seek_key); | it->Seek(seek_key); | ||||
for (unsigned int x = seek_start; x < 10; ++x) { | for (unsigned int x = seek_start; x < 10; ++x) { | ||||
for (int y = 0; y < 10; y++) { | for (int y = 0; y < 10; y++) { | ||||
snprintf(buf, sizeof(buf), "%d", x); | snprintf(buf, sizeof(buf), "%d", x); | ||||
std::string exp_key(buf); | std::string exp_key(buf); | ||||
for (int z = 0; z < y; z++) | for (int z = 0; z < y; z++) { | ||||
exp_key += exp_key; | exp_key += exp_key; | ||||
} | |||||
StringContentsSerializer key; | StringContentsSerializer key; | ||||
uint32_t value; | uint32_t value; | ||||
BOOST_CHECK(it->Valid()); | BOOST_CHECK(it->Valid()); | ||||
// Avoid spurious errors about invalid iterator's key and value | // Avoid spurious errors about invalid iterator's key and value | ||||
// in case of failure | // in case of failure | ||||
if (!it->Valid()) break; | if (!it->Valid()) { | ||||
break; | |||||
} | |||||
BOOST_CHECK(it->GetKey(key)); | BOOST_CHECK(it->GetKey(key)); | ||||
BOOST_CHECK(it->GetValue(value)); | BOOST_CHECK(it->GetValue(value)); | ||||
BOOST_CHECK_EQUAL(key.str, exp_key); | BOOST_CHECK_EQUAL(key.str, exp_key); | ||||
BOOST_CHECK_EQUAL(value, x * x); | BOOST_CHECK_EQUAL(value, x * x); | ||||
it->Next(); | it->Next(); | ||||
} | } | ||||
} | } | ||||
BOOST_CHECK(!it->Valid()); | BOOST_CHECK(!it->Valid()); | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |