From 17fb37075c780b7029f83e5faba079c641a6f75d Mon Sep 17 00:00:00 2001 From: blue Date: Thu, 30 Mar 2023 20:00:56 +0300 Subject: [PATCH] unit tests for serialization --- src/operators.hpp | 30 ++-- src/serializer.hpp | 2 +- src/serializer_uint8.hpp | 4 +- test/CMakeLists.txt | 1 + test/serialization.cpp | 333 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 352 insertions(+), 18 deletions(-) create mode 100644 test/serialization.cpp diff --git a/src/operators.hpp b/src/operators.hpp index 795496a..8adea66 100644 --- a/src/operators.hpp +++ b/src/operators.hpp @@ -75,21 +75,21 @@ QDataStream& operator >> (QDataStream &in, std::multimap& container) { return in; } -template -QDataStream& operator << (QDataStream &out, const std::pair& pair) { - out << pair.first; - out << pair.second; - - return out; -} - -template -QDataStream& operator >> (QDataStream &in, std::pair& container) { - in >> container.first; - in >> container.second; - - return in; -} +// template +// QDataStream& operator << (QDataStream &out, const std::pair& pair) { +// out << pair.first; +// out << pair.second; +// +// return out; +// } +// +// template +// QDataStream& operator >> (QDataStream &in, std::pair& container) { +// in >> container.first; +// in >> container.second; +// +// return in; +// } template QDataStream& operator << (QDataStream &out, const std::set& container) { diff --git a/src/serializer.hpp b/src/serializer.hpp index 36bb7d5..bbece55 100644 --- a/src/serializer.hpp +++ b/src/serializer.hpp @@ -37,7 +37,7 @@ LMDBAL::Serializer::Serializer(const T& value) : stream(&buffer) { buffer.open(QIODevice::ReadWrite); - _setValue(value); + _setData(value); } template diff --git a/src/serializer_uint8.hpp b/src/serializer_uint8.hpp index 8f4cd04..31daca6 100644 --- a/src/serializer_uint8.hpp +++ b/src/serializer_uint8.hpp @@ -31,10 +31,10 @@ public: ~Serializer() {}; uint8_t deserialize(const MDB_val& data) { - deserialzie(data, value); + deserialize(data, 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); } MDB_val setData(const uint8_t& data) { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5f1a0e0..d01a810 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -4,6 +4,7 @@ include_directories(${GTEST_INCLUDE_DIR}) add_executable(runUnitTests basic.cpp + serialization.cpp ) target_compile_options(runUnitTests PRIVATE -fPIC) diff --git a/test/serialization.cpp b/test/serialization.cpp new file mode 100644 index 0000000..1d7d0b3 --- /dev/null +++ b/test/serialization.cpp @@ -0,0 +1,333 @@ +#include + +#include +#include + +TEST(Serialization, Double) { + double source = 5344.6542; + LMDBAL::Serializer serializer; + LMDBAL::Serializer serializer2(source); + LMDBAL::Serializer 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 serializer; + LMDBAL::Serializer serializer2(source); + LMDBAL::Serializer 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 serializer; + LMDBAL::Serializer serializer2(source); + LMDBAL::Serializer 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 serializer; + LMDBAL::Serializer serializer2(source); + LMDBAL::Serializer 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 serializer; + LMDBAL::Serializer serializer2(source); + LMDBAL::Serializer 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 serializer; + LMDBAL::Serializer serializer2(source); + LMDBAL::Serializer 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 serializer; + LMDBAL::Serializer serializer2(source); + LMDBAL::Serializer 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 serializer; + LMDBAL::Serializer serializer2(source); + LMDBAL::Serializer 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 serializer; + LMDBAL::Serializer serializer2(source); + LMDBAL::Serializer 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 serializer; + LMDBAL::Serializer serializer2(source); + LMDBAL::Serializer 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 serializer; + LMDBAL::Serializer serializer2(source); + LMDBAL::Serializer 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 serializer; + LMDBAL::Serializer serializer2(source); + LMDBAL::Serializer 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 serializer; + LMDBAL::Serializer serializer2(source); + LMDBAL::Serializer 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 source({ + {467, 123}, + {1397, 8}, + {551, -9}, + {864, 114}, + {9615, -85}, + {32, 32} + }); + LMDBAL::Serializer> serializer; + LMDBAL::Serializer> serializer2(source); + LMDBAL::Serializer> 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 destination; + serializer.deserialize(data, destination); + + EXPECT_EQ(source, destination); + + std::map dest2 = serializer.deserialize(data); + + EXPECT_EQ(source, dest2); +}