2023-08-09 17:41:15 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include "base.h"
|
|
|
|
#include "storage.h"
|
|
|
|
#include "cursor.h"
|
|
|
|
|
|
|
|
class StorageCursorTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
StorageCursorTest():
|
|
|
|
::testing::Test(),
|
2023-08-10 23:07:12 +00:00
|
|
|
table (db->getStorage<uint64_t, std::string>("table1")),
|
|
|
|
emptyTable (db->getStorage<uint64_t, std::string>("empty")) {}
|
2023-08-09 17:41:15 +00:00
|
|
|
|
|
|
|
~StorageCursorTest() {}
|
|
|
|
|
|
|
|
static void SetUpTestSuite() {
|
|
|
|
if (db == nullptr) {
|
|
|
|
db = new LMDBAL::Base("testBase");
|
|
|
|
db->addStorage<uint64_t, std::string>("table1");
|
2023-08-10 23:07:12 +00:00
|
|
|
db->addStorage<uint64_t, std::string>("empty");
|
2023-08-09 17:41:15 +00:00
|
|
|
db->open();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TearDownTestSuite() {
|
2023-10-17 21:06:11 +00:00
|
|
|
transaction.terminate();
|
2023-08-09 17:41:15 +00:00
|
|
|
db->close();
|
|
|
|
db->removeDirectory();
|
|
|
|
delete db;
|
|
|
|
db = nullptr;
|
2023-08-10 23:07:12 +00:00
|
|
|
cursor = nullptr;
|
|
|
|
emptyCursor = nullptr;
|
2023-08-09 17:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static LMDBAL::Base* db;
|
|
|
|
static LMDBAL::Cursor<uint64_t, std::string>* cursor;
|
2023-08-10 23:07:12 +00:00
|
|
|
static LMDBAL::Cursor<uint64_t, std::string>* emptyCursor;
|
2023-10-17 21:06:11 +00:00
|
|
|
static LMDBAL::Transaction transaction;
|
2023-08-09 17:41:15 +00:00
|
|
|
|
|
|
|
LMDBAL::Storage<uint64_t, std::string>* table;
|
2023-08-10 23:07:12 +00:00
|
|
|
LMDBAL::Storage<uint64_t, std::string>* emptyTable;
|
2023-08-09 17:41:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
LMDBAL::Base* StorageCursorTest::db = nullptr;
|
|
|
|
LMDBAL::Cursor<uint64_t, std::string>* StorageCursorTest::cursor = nullptr;
|
2023-08-10 23:07:12 +00:00
|
|
|
LMDBAL::Cursor<uint64_t, std::string>* StorageCursorTest::emptyCursor = nullptr;
|
2023-10-17 21:06:11 +00:00
|
|
|
LMDBAL::Transaction StorageCursorTest::transaction = LMDBAL::Transaction();
|
2023-08-09 17:41:15 +00:00
|
|
|
|
|
|
|
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(StorageCursorTest, PopulatingTheTable) {
|
|
|
|
uint32_t amount = table->addRecords(data);
|
|
|
|
EXPECT_EQ(amount, data.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StorageCursorTest, Creation) {
|
|
|
|
cursor = table->createCursor();
|
2023-08-10 23:07:12 +00:00
|
|
|
emptyCursor = emptyTable->createCursor();
|
2023-08-09 17:41:15 +00:00
|
|
|
|
|
|
|
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(StorageCursorTest, FirstPrivate) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StorageCursorTest, 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_THROW(cursor->next(), LMDBAL::NotFound);
|
|
|
|
|
|
|
|
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(StorageCursorTest, LastPrivate) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StorageCursorTest, 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_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);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StorageCursorTest, 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(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);
|
|
|
|
}
|
2023-08-10 23:07:12 +00:00
|
|
|
|
|
|
|
TEST_F(StorageCursorTest, 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);
|
2023-08-12 21:28:49 +00:00
|
|
|
|
|
|
|
EXPECT_THROW(emptyTable->destroyCursor(cursor), LMDBAL::Unknown);
|
|
|
|
table->destroyCursor(cursor);
|
|
|
|
|
|
|
|
cursor = table->createCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StorageCursorTest, FirstPublic) {
|
|
|
|
transaction = db->beginReadOnlyTransaction();
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StorageCursorTest, 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_THROW(cursor->next(), LMDBAL::NotFound);
|
|
|
|
|
|
|
|
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(StorageCursorTest, 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StorageCursorTest, 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_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);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StorageCursorTest, 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(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);
|
2023-08-10 23:07:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StorageCursorTest, CornerCases) {
|
2023-10-17 21:06:11 +00:00
|
|
|
transaction.terminate();
|
2023-08-12 21:28:49 +00:00
|
|
|
EXPECT_THROW(cursor->current(), LMDBAL::Unknown);
|
|
|
|
cursor->close();
|
|
|
|
|
2023-08-10 23:07:12 +00:00
|
|
|
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);
|
|
|
|
}
|