Some more tests over cache cursor operations

This commit is contained in:
Blue 2023-08-13 13:30:17 -03:00
parent d57d27f952
commit 161a850088
Signed by: blue
GPG Key ID: 9B203B252A63EE38
4 changed files with 351 additions and 0 deletions

View File

@ -1,5 +1,14 @@
# Changelog
## LMDBAL 0.4.0 (August 13, 2023)
### Bug fixes
- possible cache unsync
### Improvements
- read only cursors
- some more documentation
- more tests
## LMDBAL 0.3.1 (April 14, 2023)
### Bug fixes
- build with qt5 now is possible again

View File

@ -8,6 +8,7 @@ add_executable(runUnitTests
storagetransaction.cpp
cachetransaction.cpp
storagecursor.cpp
cachecursor.cpp
)
target_compile_options(runUnitTests PRIVATE -fPIC)

View File

@ -106,6 +106,10 @@ TEST_F(BaseTest, GettingNotExistingKeys) {
TEST_F(BaseTest, Persistence) {
EXPECT_EQ(db->ready(), true);
uint32_t t1Size = t1->count();
uint32_t t2Size = t2->count();
uint32_t c1Size = c1->count();
db->close();
delete db;
@ -115,20 +119,31 @@ TEST_F(BaseTest, Persistence) {
c1 = db->addCache<int8_t, std::string>("cache1");
db->open();
EXPECT_EQ(t1->count(), t1Size);
EXPECT_EQ(t1->getRecord(3), 15);
EXPECT_EQ(t1->getRecord(1), 2);
EXPECT_EQ(t1->getRecord(2), 2);
EXPECT_EQ(t1->count(), t1Size);
EXPECT_EQ(t2->count(), t2Size);
EXPECT_EQ(t2->getRecord("hello"), "world");
EXPECT_EQ(t2->getRecord("aaa"), "gagdfsdf");
EXPECT_EQ(t2->getRecord("sdfhga"), "DSFFDG");
EXPECT_EQ(t2->getRecord("sdfsda"), "shgsdgfa");
EXPECT_EQ(t2->count(), t2Size);
EXPECT_EQ(c1->count(), c1Size);
EXPECT_EQ(c1->checkRecord(-116), true);
EXPECT_EQ(c1->getRecord(-116), "whatever");
EXPECT_EQ(c1->checkRecord(-4), true);
EXPECT_EQ(c1->getRecord(-4), "testing goes brrr");
EXPECT_EQ(c1->getRecord(-4), "testing goes brrr");
EXPECT_EQ(c1->checkRecord(-4), true);
EXPECT_EQ(c1->count(), c1Size);
EXPECT_EQ(c1->getRecord(-37), "aaaaa tss tsss tsss tsss aaaaaaa");
EXPECT_EQ(c1->getRecord(2), "blah balah");
EXPECT_EQ(c1->count(), c1Size);
EXPECT_THROW(t2->getRecord("cats"), LMDBAL::NotFound);
EXPECT_THROW(t1->getRecord(7893), LMDBAL::NotFound);

326
test/cachecursor.cpp Normal file
View File

@ -0,0 +1,326 @@
#include <gtest/gtest.h>
#include "base.h"
#include "storage.h"
#include "cache.h"
#include "cursor.h"
class CacheCursorTest : public ::testing::Test {
protected:
CacheCursorTest():
::testing::Test(),
cache (db->getCache<uint64_t, std::string>("table1")),
emptyCache (db->getCache<uint64_t, std::string>("empty")) {}
~CacheCursorTest() {}
static void SetUpTestSuite() {
if (db == nullptr) {
db = new LMDBAL::Base("testBase");
db->addCache<uint64_t, std::string>("table1");
db->addCache<uint64_t, std::string>("empty");
db->open();
}
}
static void TearDownTestSuite() {
db->close();
db->removeDirectory();
delete db;
db = nullptr;
cursor = nullptr;
emptyCursor = nullptr;
}
static LMDBAL::Base* db;
static LMDBAL::Cursor<uint64_t, std::string>* cursor;
static LMDBAL::Cursor<uint64_t, std::string>* emptyCursor;
static LMDBAL::TransactionID transaction;
LMDBAL::Cache<uint64_t, std::string>* cache;
LMDBAL::Cache<uint64_t, std::string>* emptyCache;
};
LMDBAL::Base* CacheCursorTest::db = nullptr;
LMDBAL::Cursor<uint64_t, std::string>* CacheCursorTest::cursor = nullptr;
LMDBAL::Cursor<uint64_t, std::string>* CacheCursorTest::emptyCursor = nullptr;
LMDBAL::TransactionID CacheCursorTest::transaction = nullptr;
static const std::map<uint64_t, std::string> data({
{245665783, "bothering nerds"},
{3458, "resilent pick forefront"},
{105190, "apportunity legal bat"},
{6510, "outside"},
{7438537, "damocles plush apparently rusty"},
{19373572, "local guidence"},
{138842, "forgetting tusks prepare"},
{981874, "butchered soaking pawn"},
{19302, "tanned inmate"},
{178239, "custody speaks neurotic"},
});
TEST_F(CacheCursorTest, PopulatingTheTable) {
uint32_t amount = cache->addRecords(data);
EXPECT_EQ(amount, data.size());
}
TEST_F(CacheCursorTest, Creation) {
cursor = cache->createCursor();
emptyCursor = emptyCache->createCursor();
EXPECT_THROW(cursor->first(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->last(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->next(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->prev(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->current(), LMDBAL::CursorNotReady);
cursor->open();
}
TEST_F(CacheCursorTest, FirstPrivate) {
EXPECT_EQ(cache->count(), data.size());
std::pair<uint64_t, std::string> element = cursor->first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
TEST_F(CacheCursorTest, NextPrivate) {
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
reference++;
for (; reference != data.end(); ++reference) {
std::pair<uint64_t, std::string> element = cursor->next();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
EXPECT_THROW(cursor->next(), LMDBAL::NotFound);
EXPECT_EQ(cache->count(), data.size());
std::pair<uint64_t, std::string> element = cursor->first();
reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
TEST_F(CacheCursorTest, LastPrivate) {
EXPECT_EQ(cache->count(), data.size());
std::pair<uint64_t, std::string> element = cursor->last();
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
TEST_F(CacheCursorTest, PrevPrivate) {
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
reference++;
for (; reference != data.rend(); ++reference) {
std::pair<uint64_t, std::string> element = cursor->prev();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
EXPECT_THROW(cursor->prev(), LMDBAL::NotFound);
EXPECT_EQ(cache->count(), data.size());
std::pair<uint64_t, std::string> element = cursor->last();
reference = data.rbegin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
TEST_F(CacheCursorTest, CurrentPrivate) {
std::pair<uint64_t, std::string> element = cursor->first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
element = cursor->current();
EXPECT_EQ(cache->count(), data.size());
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
cursor->next();
element = cursor->current();
++reference;
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
cursor->next();
cursor->next();
cursor->prev();
element = cursor->current();
++reference;
++reference;
--reference;
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
TEST_F(CacheCursorTest, Destruction) {
cursor->close();
EXPECT_THROW(cursor->first(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->last(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->next(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->prev(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->current(), LMDBAL::CursorNotReady);
EXPECT_THROW(emptyCache->destroyCursor(cursor), LMDBAL::Unknown);
cache->destroyCursor(cursor);
cursor = cache->createCursor();
}
TEST_F(CacheCursorTest, FirstPublic) {
transaction = db->beginTransaction();
cursor->open(transaction);
std::pair<uint64_t, std::string> element = cursor->first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
TEST_F(CacheCursorTest, NextPublic) {
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
reference++;
for (; reference != data.end(); ++reference) {
std::pair<uint64_t, std::string> element = cursor->next();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
EXPECT_THROW(cursor->next(), LMDBAL::NotFound);
EXPECT_EQ(cache->count(), data.size());
std::pair<uint64_t, std::string> element = cursor->first();
reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
}
TEST_F(CacheCursorTest, LastPublic) {
std::pair<uint64_t, std::string> element = cursor->last();
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
TEST_F(CacheCursorTest, PrevPublic) {
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
reference++;
for (; reference != data.rend(); ++reference) {
std::pair<uint64_t, std::string> element = cursor->prev();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
EXPECT_THROW(cursor->prev(), LMDBAL::NotFound);
std::pair<uint64_t, std::string> element = cursor->last();
reference = data.rbegin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
TEST_F(CacheCursorTest, CurrentPublic) {
std::pair<uint64_t, std::string> element = cursor->first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
element = cursor->current();
EXPECT_EQ(cache->count(), data.size());
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
cursor->next();
element = cursor->current();
++reference;
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
cursor->next();
cursor->next();
cursor->prev();
element = cursor->current();
++reference;
++reference;
--reference;
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
TEST_F(CacheCursorTest, CornerCases) {
db->abortTransaction(transaction);
EXPECT_THROW(cursor->current(), LMDBAL::Unknown);
cursor->close();
emptyCursor->open();
EXPECT_THROW(emptyCursor->first(), LMDBAL::NotFound);
EXPECT_THROW(emptyCursor->last(), LMDBAL::NotFound);
EXPECT_THROW(emptyCursor->next(), LMDBAL::NotFound);
EXPECT_THROW(emptyCursor->prev(), LMDBAL::NotFound);
EXPECT_THROW(emptyCursor->current(), LMDBAL::Unknown);
emptyCursor->close();
cursor->open();
EXPECT_THROW(cursor->current(), LMDBAL::Unknown); //yeah, nice thing to write in the doc
std::map<uint64_t, std::string>::const_reverse_iterator breference = data.rbegin();
std::pair<uint64_t, std::string> element(cursor->prev());
EXPECT_EQ(element.first, breference->first); //nice thing to write in the doc, again!
EXPECT_EQ(element.second, breference->second);
element = cursor->current();
EXPECT_EQ(element.first, breference->first);
EXPECT_EQ(element.second, breference->second);
EXPECT_THROW(cursor->next(), LMDBAL::NotFound);
cursor->close();
cursor->open();
element = cursor->next();
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
element = cursor->current();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_THROW(cursor->prev(), LMDBAL::NotFound);
}