summaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2023-01-05 18:10:39 +0100
committerRoland Reichwein <mail@reichwein.it>2023-01-05 18:10:39 +0100
commitcdf2c1132ec9a12ea6d91c8deb1700c1a590c18d (patch)
tree1e792e0de184c2cabf9e923479e224d8b8126edf /tests
parent7973293c311e27ff08a1488c9759c1b5b0fda30e (diff)
Added archive.h
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile1
-rw-r--r--tests/test-archive.cpp197
2 files changed, 198 insertions, 0 deletions
diff --git a/tests/Makefile b/tests/Makefile
index 44f50dc..6ae6f12 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -21,6 +21,7 @@ UNITS=\
url.cpp
UNITTESTS=\
+ test-archive.cpp \
test-base64.cpp \
test-file.cpp \
test-mime.cpp \
diff --git a/tests/test-archive.cpp b/tests/test-archive.cpp
new file mode 100644
index 0000000..2ffa2cd
--- /dev/null
+++ b/tests/test-archive.cpp
@@ -0,0 +1,197 @@
+#include <gtest/gtest.h>
+
+#include <sstream>
+#include <string>
+
+#include "archive.h"
+
+using namespace std::string_literals;
+
+class ArchiveTest: public ::testing::Test
+{
+protected:
+ ArchiveTest(){}
+ ~ArchiveTest() override{}
+ void SetUp() override{}
+ void TearDown() override{}
+};
+
+struct MyStruct
+{
+ uint8_t member1;
+ uint16_t member2;
+ uint32_t member3;
+ uint64_t member4;
+ int64_t member5;
+ std::vector<uint8_t> member6;
+ std::string member7;
+
+ template <class Archive>
+ void serialize (Archive& ar)
+ {
+ ar & member1;
+ ar & member2;
+ ar & member3;
+ ar & member4;
+ ar & member5;
+ ar & member6;
+ ar & member7;
+ }
+
+};
+
+bool operator==(const MyStruct& lhs, const MyStruct& rhs)
+{
+ return lhs.member1 == rhs.member1 &&
+ lhs.member2 == rhs.member2 &&
+ lhs.member3 == rhs.member3 &&
+ lhs.member4 == rhs.member4 &&
+ lhs.member5 == rhs.member5 &&
+ lhs.member6 == rhs.member6 &&
+ lhs.member7 == rhs.member7;
+}
+
+TEST_F(ArchiveTest, archive)
+{
+ std::stringstream ss;
+ Serialization::OArchive oa(ss);
+ MyStruct mystruct0{123, 45678, 3, 4, 5, {1, 2}, "abc"};
+ oa << mystruct0;
+
+ EXPECT_EQ(ss.str().size(), 36);
+
+ Serialization::IArchive ia(ss);
+ MyStruct mystruct1{};
+ ia >> mystruct1;
+
+ EXPECT_EQ(mystruct0, mystruct1);
+}
+
+TEST_F(ArchiveTest, out)
+{
+ std::stringstream ss(std::ios_base::in|std::ios_base::out|std::ios_base::binary);
+
+ Serialization::OArchive oarchive(ss);
+
+ uint64_t v1(123456789);
+ oarchive << v1;
+
+ ASSERT_EQ(ss.str().size(), sizeof(uint64_t));
+
+ uint32_t v2(87654321);
+ oarchive << v2;
+
+ ASSERT_EQ(ss.str().size(), sizeof(uint64_t) + sizeof(uint32_t));
+
+ uint16_t v3(9876);
+ oarchive << v3;
+
+ ASSERT_EQ(ss.str().size(), sizeof(uint64_t) + sizeof(uint32_t) + sizeof(uint16_t));
+
+ uint8_t v4(2);
+ oarchive << v4;
+
+ ASSERT_EQ(ss.str().size(), sizeof(uint64_t) + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint8_t));
+
+ std::string v5("abcd");
+ oarchive << v5;
+
+ ASSERT_EQ(ss.str().size(), sizeof(uint64_t) + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint8_t) + 4+4);
+
+ std::vector<uint8_t> v6 = {1, 2, 3, 4, 5};
+ oarchive << v6;
+
+ ASSERT_EQ(ss.str().size(), sizeof(uint64_t) + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint8_t) + 4+4 + 4+5);
+
+ ASSERT_EQ(ss.str(), std::string("\x00\x00\x00\x00\x07\x5b\xcd\x15\x05\x39\x7f\xb1\x26\x94\x02" "\x00\x00\x00\x04" "abcd\x00\x00\x00\x05\x01\x02\x03\x04\x05", 32));
+}
+
+TEST_F(ArchiveTest, in)
+{
+ std::stringstream ss(std::ios_base::in|std::ios_base::out|std::ios_base::binary);
+
+ std::string v1("\x00\x00\x00\x00\x07\x5b\xcd\x15\x05\x39\x7f\xb1\x26\x94\x02" "\x00\x00\x00\x04" "abcd\x00\x00\x00\x05\x01\x02\x03\x04\x05", 32);
+ ss.write(v1.data(), v1.size());
+
+ Serialization::IArchive iarchive(ss);
+
+ ASSERT_EQ(ss.str().size(), 32);
+
+ uint64_t x1;
+ iarchive >> x1;
+
+ ASSERT_EQ(x1, 123456789);
+
+ uint32_t x2;
+ iarchive >> x2;
+
+ ASSERT_EQ(x2, 87654321);
+
+ uint16_t x3;
+ iarchive >> x3;
+
+ ASSERT_EQ(x3, 9876);
+
+ uint8_t x4;
+ iarchive >> x4;
+
+ ASSERT_EQ(x4, 2);
+
+ std::string x5;
+ iarchive >> x5;
+ EXPECT_EQ(x5, "abcd");
+
+ std::vector<uint8_t> x6;
+ iarchive >> x6;
+
+ std::vector<uint8_t> v{1, 2, 3, 4, 5};
+ ASSERT_EQ(x6, v);
+}
+
+TEST_F(ArchiveTest, in_coro)
+{
+ std::stringstream ss(std::ios_base::in|std::ios_base::out|std::ios_base::binary);
+
+ coro_t::pull_type coro([&](coro_t::push_type& sink)
+ {
+ sink();
+ sink();
+ std::string v1("\x00\x00\x00\x00\x07\x5b\xcd\x15\x05\x39\x7f\xb1\x26\x94\x02" "\x00\x00\x00\x04" "abcd\x00\x00\x00\x05\x01\x02\x03\x04\x05", 32);
+ for (int i = 0; i < v1.size(); i++) {
+ ss.write(v1.data() + i, 1);
+ sink();
+ }
+ sink();
+ });
+ Serialization::IArchive iarchive(ss, coro);
+
+ uint64_t x1;
+ iarchive >> x1;
+
+ ASSERT_EQ(x1, 123456789);
+
+ uint32_t x2;
+ iarchive >> x2;
+
+ ASSERT_EQ(x2, 87654321);
+
+ uint16_t x3;
+ iarchive >> x3;
+
+ ASSERT_EQ(x3, 9876);
+
+ uint8_t x4;
+ iarchive >> x4;
+
+ ASSERT_EQ(x4, 2);
+
+ std::string x5;
+ iarchive >> x5;
+ EXPECT_EQ(x5, "abcd");
+
+ std::vector<uint8_t> x6;
+ iarchive >> x6;
+ std::vector<uint8_t> v{1, 2, 3, 4, 5};
+ ASSERT_EQ(x6, v);
+}
+