unit tests for serialization

This commit is contained in:
Blue 2023-03-30 20:00:56 +03:00
parent e2dbea21d1
commit 17fb37075c
Signed by: blue
GPG Key ID: 9B203B252A63EE38
5 changed files with 352 additions and 18 deletions

View File

@ -75,21 +75,21 @@ QDataStream& operator >> (QDataStream &in, std::multimap<K, V>& container) {
return in; return in;
} }
template <class K, class V> // template <class K, class V>
QDataStream& operator << (QDataStream &out, const std::pair<K, V>& pair) { // QDataStream& operator << (QDataStream &out, const std::pair<K, V>& pair) {
out << pair.first; // out << pair.first;
out << pair.second; // out << pair.second;
//
return out; // return out;
} // }
//
template <class K, class V> // template <class K, class V>
QDataStream& operator >> (QDataStream &in, std::pair<K, V>& container) { // QDataStream& operator >> (QDataStream &in, std::pair<K, V>& container) {
in >> container.first; // in >> container.first;
in >> container.second; // in >> container.second;
//
return in; // return in;
} // }
template <class K> template <class K>
QDataStream& operator << (QDataStream &out, const std::set<K>& container) { QDataStream& operator << (QDataStream &out, const std::set<K>& container) {

View File

@ -37,7 +37,7 @@ LMDBAL::Serializer<T>::Serializer(const T& value) :
stream(&buffer) stream(&buffer)
{ {
buffer.open(QIODevice::ReadWrite); buffer.open(QIODevice::ReadWrite);
_setValue(value); _setData(value);
} }
template<class T> template<class T>

View File

@ -31,10 +31,10 @@ public:
~Serializer() {}; ~Serializer() {};
uint8_t deserialize(const MDB_val& data) { uint8_t deserialize(const MDB_val& data) {
deserialzie(data, value); deserialize(data, value);
return value; return value;
}; };
void deserialzie(const MDB_val& data, uint8_t& result) { void deserialize(const MDB_val& data, uint8_t& result) {
std::memcpy(&result, data.mv_data, 1); std::memcpy(&result, data.mv_data, 1);
} }
MDB_val setData(const uint8_t& data) { MDB_val setData(const uint8_t& data) {

View File

@ -4,6 +4,7 @@ include_directories(${GTEST_INCLUDE_DIR})
add_executable(runUnitTests add_executable(runUnitTests
basic.cpp basic.cpp
serialization.cpp
) )
target_compile_options(runUnitTests PRIVATE -fPIC) target_compile_options(runUnitTests PRIVATE -fPIC)

333
test/serialization.cpp Normal file
View File

@ -0,0 +1,333 @@
#include <gtest/gtest.h>
#include <serializer.h>
#include <operators.hpp>
TEST(Serialization, Double) {
double source = 5344.6542;
LMDBAL::Serializer<double> serializer;
LMDBAL::Serializer<double> serializer2(source);
LMDBAL::Serializer<double> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
double destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
double dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, Float) {
float source = 5.156;
LMDBAL::Serializer<float> serializer;
LMDBAL::Serializer<float> serializer2(source);
LMDBAL::Serializer<float> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
float destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
float dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, Int8) {
int8_t source = 38;
LMDBAL::Serializer<int8_t> serializer;
LMDBAL::Serializer<int8_t> serializer2(source);
LMDBAL::Serializer<int8_t> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
int8_t destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
int8_t dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, Int16) {
int16_t source = -3469;
LMDBAL::Serializer<int16_t> serializer;
LMDBAL::Serializer<int16_t> serializer2(source);
LMDBAL::Serializer<int16_t> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
int16_t destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
int16_t dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, Int32) {
int32_t source = 454832;
LMDBAL::Serializer<int32_t> serializer;
LMDBAL::Serializer<int32_t> serializer2(source);
LMDBAL::Serializer<int32_t> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
int32_t destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
int32_t dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, Int64) {
int64_t source = -875525478136;
LMDBAL::Serializer<int64_t> serializer;
LMDBAL::Serializer<int64_t> serializer2(source);
LMDBAL::Serializer<int64_t> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
int64_t destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
int64_t dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, UInt8) {
uint8_t source = 196;
LMDBAL::Serializer<uint8_t> serializer;
LMDBAL::Serializer<uint8_t> serializer2(source);
LMDBAL::Serializer<uint8_t> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
uint8_t destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
uint8_t dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, UInt16) {
uint16_t source = 8634;
LMDBAL::Serializer<uint16_t> serializer;
LMDBAL::Serializer<uint16_t> serializer2(source);
LMDBAL::Serializer<uint16_t> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
uint16_t destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
uint16_t dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, UInt32) {
uint32_t source = 115469;
LMDBAL::Serializer<uint32_t> serializer;
LMDBAL::Serializer<uint32_t> serializer2(source);
LMDBAL::Serializer<uint32_t> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
uint32_t destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
uint32_t dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, UInt64) {
uint64_t source = 498763546873;
LMDBAL::Serializer<uint64_t> serializer;
LMDBAL::Serializer<uint64_t> serializer2(source);
LMDBAL::Serializer<uint64_t> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
uint64_t destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
uint64_t dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, StdString) {
std::string source("days just go by, some good and some are bad");
LMDBAL::Serializer<std::string> serializer;
LMDBAL::Serializer<std::string> serializer2(source);
LMDBAL::Serializer<std::string> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
std::string destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
std::string dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, QString) {
QString source("may be will find nothing new, may be I'll end up just just like you");
LMDBAL::Serializer<QString> serializer;
LMDBAL::Serializer<QString> serializer2(source);
LMDBAL::Serializer<QString> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
QString destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
QString dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, QByteArray) {
QByteArray source = QByteArray::fromHex("84 be 81 6c d3 5e c3 49 94 51 6f 8f a7 3b 0c d8 29 23 a3 21 4d b8 3f 73");
LMDBAL::Serializer<QByteArray> serializer;
LMDBAL::Serializer<QByteArray> serializer2(source);
LMDBAL::Serializer<QByteArray> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
QByteArray destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
QByteArray dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, StdMap) {
std::map<uint16_t, uint8_t> source({
{467, 123},
{1397, 8},
{551, -9},
{864, 114},
{9615, -85},
{32, 32}
});
LMDBAL::Serializer<std::map<uint16_t, uint8_t>> serializer;
LMDBAL::Serializer<std::map<uint16_t, uint8_t>> serializer2(source);
LMDBAL::Serializer<std::map<uint16_t, uint8_t>> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
std::map<uint16_t, uint8_t> destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
std::map<uint16_t, uint8_t> dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}