2018-10-31 17:22:51 +08:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2019-05-02 20:24:11 +08:00
|
|
|
#include "slk/serialization.hpp"
|
|
|
|
|
|
|
|
#include "slk_common.hpp"
|
2018-10-31 17:22:51 +08:00
|
|
|
|
|
|
|
#define CREATE_PRIMITIVE_TEST(primitive_type, original_value, decoded_value) \
|
|
|
|
{ \
|
|
|
|
ASSERT_NE(original_value, decoded_value); \
|
|
|
|
primitive_type original = original_value; \
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback; \
|
|
|
|
auto builder = loopback.GetBuilder(); \
|
|
|
|
slk::Save(original, builder); \
|
2018-10-31 17:22:51 +08:00
|
|
|
primitive_type decoded = decoded_value; \
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader(); \
|
|
|
|
slk::Load(&decoded, reader); \
|
2018-10-31 17:22:51 +08:00
|
|
|
ASSERT_EQ(original, decoded); \
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(), sizeof(primitive_type)); \
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, Primitive) {
|
2018-10-31 17:22:51 +08:00
|
|
|
CREATE_PRIMITIVE_TEST(bool, true, false);
|
|
|
|
CREATE_PRIMITIVE_TEST(int8_t, 0x12, 0);
|
|
|
|
CREATE_PRIMITIVE_TEST(uint8_t, 0x12, 0);
|
|
|
|
CREATE_PRIMITIVE_TEST(int16_t, 0x1234, 0);
|
|
|
|
CREATE_PRIMITIVE_TEST(uint16_t, 0x1234, 0);
|
|
|
|
CREATE_PRIMITIVE_TEST(int32_t, 0x12345678, 0);
|
|
|
|
CREATE_PRIMITIVE_TEST(uint32_t, 0x12345678, 0);
|
|
|
|
CREATE_PRIMITIVE_TEST(int64_t, 0x1234567890abcdef, 0);
|
|
|
|
CREATE_PRIMITIVE_TEST(uint64_t, 0x1234567890abcdef, 0);
|
|
|
|
CREATE_PRIMITIVE_TEST(float, 1.23456789, 0);
|
|
|
|
CREATE_PRIMITIVE_TEST(double, 1234567890.1234567890, 0);
|
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, String) {
|
2018-10-31 17:22:51 +08:00
|
|
|
std::string original = "hello world";
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
2018-10-31 17:22:51 +08:00
|
|
|
std::string decoded;
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
2018-10-31 17:22:51 +08:00
|
|
|
ASSERT_EQ(original, decoded);
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(), sizeof(uint64_t) + original.size());
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2020-05-26 18:45:07 +08:00
|
|
|
TEST(SlkCore, ConstStringLiteral) {
|
|
|
|
const char *original = "hello world";
|
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
|
|
|
std::string decoded;
|
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
|
|
|
ASSERT_EQ(original, decoded);
|
|
|
|
ASSERT_EQ(loopback.size(), sizeof(uint64_t) + strlen(original));
|
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, VectorPrimitive) {
|
2018-10-31 17:22:51 +08:00
|
|
|
std::vector<int> original{1, 2, 3, 4, 5};
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
2018-10-31 17:22:51 +08:00
|
|
|
std::vector<int> decoded;
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
2018-10-31 17:22:51 +08:00
|
|
|
ASSERT_EQ(original, decoded);
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(), sizeof(uint64_t) + original.size() * sizeof(int));
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, VectorString) {
|
2018-10-31 17:22:51 +08:00
|
|
|
std::vector<std::string> original{"hai hai hai", "nandare!"};
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
2018-10-31 17:22:51 +08:00
|
|
|
uint64_t size = sizeof(uint64_t);
|
|
|
|
for (const auto &item : original) {
|
|
|
|
size += sizeof(uint64_t) + item.size();
|
|
|
|
}
|
|
|
|
std::vector<std::string> decoded;
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
2018-10-31 17:22:51 +08:00
|
|
|
ASSERT_EQ(original, decoded);
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(), size);
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, SetPrimitive) {
|
2018-10-31 17:22:51 +08:00
|
|
|
std::set<int> original{1, 2, 3, 4, 5};
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
2018-10-31 17:22:51 +08:00
|
|
|
std::set<int> decoded;
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
2018-10-31 17:22:51 +08:00
|
|
|
ASSERT_EQ(original, decoded);
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(), sizeof(uint64_t) + original.size() * sizeof(int));
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, SetString) {
|
2018-10-31 17:22:51 +08:00
|
|
|
std::set<std::string> original{"hai hai hai", "nandare!"};
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
2018-10-31 17:22:51 +08:00
|
|
|
uint64_t size = sizeof(uint64_t);
|
|
|
|
for (const auto &item : original) {
|
|
|
|
size += sizeof(uint64_t) + item.size();
|
|
|
|
}
|
|
|
|
std::set<std::string> decoded;
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
2018-10-31 17:22:51 +08:00
|
|
|
ASSERT_EQ(original, decoded);
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(), size);
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, MapPrimitive) {
|
2018-10-31 17:22:51 +08:00
|
|
|
std::map<int, int> original{{1, 2}, {3, 4}, {5, 6}};
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
2018-10-31 17:22:51 +08:00
|
|
|
std::map<int, int> decoded;
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
2018-10-31 17:22:51 +08:00
|
|
|
ASSERT_EQ(original, decoded);
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(),
|
|
|
|
sizeof(uint64_t) + original.size() * sizeof(int) * 2);
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, MapString) {
|
2018-10-31 17:22:51 +08:00
|
|
|
std::map<std::string, std::string> original{{"hai hai hai", "nandare!"}};
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
2018-10-31 17:22:51 +08:00
|
|
|
uint64_t size = sizeof(uint64_t);
|
|
|
|
for (const auto &item : original) {
|
|
|
|
size += sizeof(uint64_t) + item.first.size();
|
|
|
|
size += sizeof(uint64_t) + item.second.size();
|
|
|
|
}
|
|
|
|
std::map<std::string, std::string> decoded;
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
2018-10-31 17:22:51 +08:00
|
|
|
ASSERT_EQ(original, decoded);
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(), size);
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, UnorderedMapPrimitive) {
|
2018-10-31 17:22:51 +08:00
|
|
|
std::unordered_map<int, int> original{{1, 2}, {3, 4}, {5, 6}};
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
2018-10-31 17:22:51 +08:00
|
|
|
std::unordered_map<int, int> decoded;
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
2018-10-31 17:22:51 +08:00
|
|
|
ASSERT_EQ(original, decoded);
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(),
|
|
|
|
sizeof(uint64_t) + original.size() * sizeof(int) * 2);
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, UnorderedMapString) {
|
2018-10-31 17:22:51 +08:00
|
|
|
std::unordered_map<std::string, std::string> original{
|
|
|
|
{"hai hai hai", "nandare!"}};
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
2018-10-31 17:22:51 +08:00
|
|
|
uint64_t size = sizeof(uint64_t);
|
|
|
|
for (const auto &item : original) {
|
|
|
|
size += sizeof(uint64_t) + item.first.size();
|
|
|
|
size += sizeof(uint64_t) + item.second.size();
|
|
|
|
}
|
|
|
|
std::unordered_map<std::string, std::string> decoded;
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
2018-10-31 17:22:51 +08:00
|
|
|
ASSERT_EQ(original, decoded);
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(), size);
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, UniquePtrEmpty) {
|
2018-10-31 17:22:51 +08:00
|
|
|
std::unique_ptr<std::string> original;
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
2018-10-31 17:22:51 +08:00
|
|
|
std::unique_ptr<std::string> decoded =
|
|
|
|
std::make_unique<std::string>("nandare!");
|
|
|
|
ASSERT_NE(decoded.get(), nullptr);
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
2018-10-31 17:22:51 +08:00
|
|
|
ASSERT_EQ(decoded.get(), nullptr);
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(), sizeof(bool));
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, UniquePtrFull) {
|
2018-10-31 17:22:51 +08:00
|
|
|
std::unique_ptr<std::string> original =
|
|
|
|
std::make_unique<std::string>("nandare!");
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
2018-10-31 17:22:51 +08:00
|
|
|
std::unique_ptr<std::string> decoded;
|
|
|
|
ASSERT_EQ(decoded.get(), nullptr);
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
2018-10-31 17:22:51 +08:00
|
|
|
ASSERT_NE(decoded.get(), nullptr);
|
|
|
|
ASSERT_EQ(*original.get(), *decoded.get());
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(),
|
|
|
|
sizeof(bool) + sizeof(uint64_t) + original.get()->size());
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, OptionalPrimitiveEmpty) {
|
2019-04-23 17:00:49 +08:00
|
|
|
std::optional<int> original;
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
2019-04-23 17:00:49 +08:00
|
|
|
std::optional<int> decoded = 5;
|
|
|
|
ASSERT_NE(decoded, std::nullopt);
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
2019-04-23 17:00:49 +08:00
|
|
|
ASSERT_EQ(decoded, std::nullopt);
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(), sizeof(bool));
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, OptionalPrimitiveFull) {
|
2019-04-23 17:00:49 +08:00
|
|
|
std::optional<int> original = 5;
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
2019-04-23 17:00:49 +08:00
|
|
|
std::optional<int> decoded;
|
|
|
|
ASSERT_EQ(decoded, std::nullopt);
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
2019-04-23 17:00:49 +08:00
|
|
|
ASSERT_NE(decoded, std::nullopt);
|
2018-10-31 17:22:51 +08:00
|
|
|
ASSERT_EQ(*original, *decoded);
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(), sizeof(bool) + sizeof(int));
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, OptionalStringEmpty) {
|
2019-04-23 17:00:49 +08:00
|
|
|
std::optional<std::string> original;
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
2019-04-23 17:00:49 +08:00
|
|
|
std::optional<std::string> decoded = "nandare!";
|
|
|
|
ASSERT_NE(decoded, std::nullopt);
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
2019-04-23 17:00:49 +08:00
|
|
|
ASSERT_EQ(decoded, std::nullopt);
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(), sizeof(bool));
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, OptionalStringFull) {
|
2019-04-23 17:00:49 +08:00
|
|
|
std::optional<std::string> original = "nandare!";
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
2019-04-23 17:00:49 +08:00
|
|
|
std::optional<std::string> decoded;
|
|
|
|
ASSERT_EQ(decoded, std::nullopt);
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
2019-04-23 17:00:49 +08:00
|
|
|
ASSERT_NE(decoded, std::nullopt);
|
2018-10-31 17:22:51 +08:00
|
|
|
ASSERT_EQ(*original, *decoded);
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(),
|
|
|
|
sizeof(bool) + sizeof(uint64_t) + original->size());
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, Pair) {
|
2018-10-31 21:41:45 +08:00
|
|
|
std::pair<std::string, int> original{"nandare!", 5};
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
2018-10-31 21:41:45 +08:00
|
|
|
std::pair<std::string, int> decoded;
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
2018-10-31 21:41:45 +08:00
|
|
|
ASSERT_EQ(original, decoded);
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(),
|
|
|
|
sizeof(uint64_t) + original.first.size() + sizeof(int));
|
2018-10-31 21:41:45 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, SharedPtrEmpty) {
|
2018-10-31 17:22:51 +08:00
|
|
|
std::shared_ptr<std::string> original;
|
|
|
|
std::vector<std::string *> saved;
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder, &saved);
|
2018-10-31 17:22:51 +08:00
|
|
|
std::shared_ptr<std::string> decoded =
|
|
|
|
std::make_shared<std::string>("nandare!");
|
|
|
|
std::vector<std::shared_ptr<std::string>> loaded;
|
|
|
|
ASSERT_NE(decoded.get(), nullptr);
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader, &loaded);
|
2018-10-31 17:22:51 +08:00
|
|
|
ASSERT_EQ(decoded.get(), nullptr);
|
|
|
|
ASSERT_EQ(saved.size(), 0);
|
|
|
|
ASSERT_EQ(loaded.size(), 0);
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(), sizeof(bool));
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, SharedPtrFull) {
|
2018-10-31 17:22:51 +08:00
|
|
|
std::shared_ptr<std::string> original =
|
|
|
|
std::make_shared<std::string>("nandare!");
|
|
|
|
std::vector<std::string *> saved;
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder, &saved);
|
2018-10-31 17:22:51 +08:00
|
|
|
std::shared_ptr<std::string> decoded;
|
|
|
|
std::vector<std::shared_ptr<std::string>> loaded;
|
|
|
|
ASSERT_EQ(decoded.get(), nullptr);
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader, &loaded);
|
2018-10-31 17:22:51 +08:00
|
|
|
ASSERT_NE(decoded.get(), nullptr);
|
|
|
|
ASSERT_EQ(*original.get(), *decoded.get());
|
|
|
|
ASSERT_EQ(saved.size(), 1);
|
|
|
|
ASSERT_EQ(loaded.size(), 1);
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(),
|
|
|
|
sizeof(bool) * 2 + sizeof(uint64_t) + original.get()->size());
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, SharedPtrMultiple) {
|
2018-10-31 17:22:51 +08:00
|
|
|
std::shared_ptr<std::string> ptr1 = std::make_shared<std::string>("nandare!");
|
|
|
|
std::shared_ptr<std::string> ptr2;
|
|
|
|
std::shared_ptr<std::string> ptr3 =
|
|
|
|
std::make_shared<std::string>("hai hai hai");
|
|
|
|
std::vector<std::string *> saved;
|
|
|
|
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(ptr1, builder, &saved);
|
|
|
|
slk::Save(ptr2, builder, &saved);
|
|
|
|
slk::Save(ptr3, builder, &saved);
|
|
|
|
slk::Save(ptr1, builder, &saved);
|
|
|
|
slk::Save(ptr3, builder, &saved);
|
2018-10-31 17:22:51 +08:00
|
|
|
|
|
|
|
std::shared_ptr<std::string> dec1, dec2, dec3, dec4, dec5;
|
|
|
|
std::vector<std::shared_ptr<std::string>> loaded;
|
|
|
|
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&dec1, reader, &loaded);
|
|
|
|
slk::Load(&dec2, reader, &loaded);
|
|
|
|
slk::Load(&dec3, reader, &loaded);
|
|
|
|
slk::Load(&dec4, reader, &loaded);
|
|
|
|
slk::Load(&dec5, reader, &loaded);
|
2018-10-31 17:22:51 +08:00
|
|
|
|
|
|
|
ASSERT_EQ(saved.size(), 2);
|
|
|
|
ASSERT_EQ(loaded.size(), 2);
|
|
|
|
|
|
|
|
ASSERT_NE(dec1.get(), nullptr);
|
|
|
|
ASSERT_EQ(dec2.get(), nullptr);
|
|
|
|
ASSERT_NE(dec3.get(), nullptr);
|
|
|
|
ASSERT_NE(dec4.get(), nullptr);
|
|
|
|
ASSERT_NE(dec5.get(), nullptr);
|
|
|
|
|
|
|
|
ASSERT_EQ(*dec1.get(), *ptr1.get());
|
|
|
|
ASSERT_EQ(*dec3.get(), *ptr3.get());
|
|
|
|
ASSERT_EQ(*dec4.get(), *ptr1.get());
|
|
|
|
ASSERT_EQ(*dec5.get(), *ptr3.get());
|
|
|
|
|
|
|
|
ASSERT_NE(dec1.get(), dec3.get());
|
|
|
|
|
|
|
|
ASSERT_EQ(dec4.get(), dec1.get());
|
|
|
|
ASSERT_EQ(dec5.get(), dec3.get());
|
2019-05-02 20:24:11 +08:00
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
ASSERT_EQ(loopback.size(),
|
|
|
|
sizeof(bool) * 2 + sizeof(uint64_t) + ptr1.get()->size() +
|
|
|
|
sizeof(bool) +
|
|
|
|
sizeof(bool) * 2 + sizeof(uint64_t) + ptr3.get()->size() +
|
|
|
|
sizeof(bool) * 2 + sizeof(uint64_t) +
|
|
|
|
sizeof(bool) * 2 + sizeof(uint64_t));
|
|
|
|
// clang-format on
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SlkCore, SharedPtrInvalid) {
|
|
|
|
std::shared_ptr<std::string> ptr = std::make_shared<std::string>("nandare!");
|
|
|
|
std::vector<std::string *> saved;
|
|
|
|
|
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(ptr, builder, &saved);
|
|
|
|
// Here we mess with the `saved` vector to cause an invalid index to be
|
|
|
|
// written to the SLK stream so that we can check the error handling in the
|
|
|
|
// `Load` function later.
|
|
|
|
saved.insert(saved.begin(), nullptr);
|
|
|
|
// Save the pointer again with an invalid index.
|
|
|
|
slk::Save(ptr, builder, &saved);
|
|
|
|
|
|
|
|
std::shared_ptr<std::string> dec1, dec2;
|
|
|
|
std::vector<std::shared_ptr<std::string>> loaded;
|
|
|
|
|
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&dec1, reader, &loaded);
|
|
|
|
ASSERT_THROW(slk::Load(&dec2, reader, &loaded), slk::SlkDecodeException);
|
2018-10-31 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, Complex) {
|
2019-04-23 17:00:49 +08:00
|
|
|
std::unique_ptr<std::vector<std::optional<std::string>>> original =
|
|
|
|
std::make_unique<std::vector<std::optional<std::string>>>();
|
2018-10-31 17:22:51 +08:00
|
|
|
original.get()->push_back("nandare!");
|
2019-04-23 17:00:49 +08:00
|
|
|
original.get()->push_back(std::nullopt);
|
2018-10-31 17:22:51 +08:00
|
|
|
original.get()->push_back("hai hai hai");
|
|
|
|
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
|
|
|
|
|
|
|
std::unique_ptr<std::vector<std::optional<std::string>>> decoded;
|
|
|
|
ASSERT_EQ(decoded.get(), nullptr);
|
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
|
|
|
ASSERT_NE(decoded.get(), nullptr);
|
|
|
|
ASSERT_EQ(*original.get(), *decoded.get());
|
2018-10-31 17:22:51 +08:00
|
|
|
|
|
|
|
// clang-format off
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(),
|
2018-10-31 17:22:51 +08:00
|
|
|
sizeof(bool) +
|
|
|
|
sizeof(uint64_t) +
|
|
|
|
sizeof(bool) + sizeof(uint64_t) + (*original.get())[0]->size() +
|
|
|
|
sizeof(bool) +
|
|
|
|
sizeof(bool) + sizeof(uint64_t) + (*original.get())[2]->size());
|
|
|
|
// clang-format on
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Foo {
|
|
|
|
std::string name;
|
2019-04-23 17:00:49 +08:00
|
|
|
std::optional<int> value;
|
2018-10-31 17:22:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
bool operator==(const Foo &a, const Foo &b) {
|
|
|
|
return a.name == b.name && a.value == b.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace slk {
|
|
|
|
void Save(const Foo &obj, Builder *builder) {
|
|
|
|
Save(obj.name, builder);
|
|
|
|
Save(obj.value, builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Load(Foo *obj, Reader *reader) {
|
|
|
|
Load(&obj->name, reader);
|
|
|
|
Load(&obj->value, reader);
|
|
|
|
}
|
|
|
|
} // namespace slk
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, VectorStruct) {
|
2018-10-31 17:22:51 +08:00
|
|
|
std::vector<Foo> original;
|
|
|
|
original.push_back({"hai hai hai", 5});
|
2019-04-23 17:00:49 +08:00
|
|
|
original.push_back({"nandare!", std::nullopt});
|
2018-10-31 17:22:51 +08:00
|
|
|
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save(original, builder);
|
|
|
|
|
|
|
|
std::vector<Foo> decoded;
|
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load(&decoded, reader);
|
|
|
|
ASSERT_EQ(original, decoded);
|
2018-10-31 17:22:51 +08:00
|
|
|
|
|
|
|
// clang-format off
|
2019-05-02 20:24:11 +08:00
|
|
|
ASSERT_EQ(loopback.size(),
|
2018-10-31 17:22:51 +08:00
|
|
|
sizeof(uint64_t) +
|
2019-05-02 20:24:11 +08:00
|
|
|
sizeof(uint64_t) + original[0].name.size() + sizeof(bool) +
|
|
|
|
sizeof(int) + sizeof(uint64_t) + original[1].name.size() + sizeof(bool));
|
2018-10-31 17:22:51 +08:00
|
|
|
// clang-format on
|
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, VectorSharedPtr) {
|
2018-10-31 17:22:51 +08:00
|
|
|
std::shared_ptr<std::string> ptr1 = std::make_shared<std::string>("nandare!");
|
|
|
|
std::shared_ptr<std::string> ptr2;
|
|
|
|
std::shared_ptr<std::string> ptr3 =
|
|
|
|
std::make_shared<std::string>("hai hai hai");
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<std::string>> original;
|
|
|
|
std::vector<std::string *> saved;
|
|
|
|
|
|
|
|
original.push_back(ptr1);
|
|
|
|
original.push_back(ptr2);
|
|
|
|
original.push_back(ptr3);
|
|
|
|
original.push_back(ptr1);
|
|
|
|
original.push_back(ptr3);
|
|
|
|
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
2018-10-31 17:22:51 +08:00
|
|
|
slk::Save<std::shared_ptr<std::string>>(
|
2019-05-02 20:24:11 +08:00
|
|
|
original, builder, [&saved](const auto &item, auto *builder) {
|
2018-10-31 17:22:51 +08:00
|
|
|
Save(item, builder, &saved);
|
|
|
|
});
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<std::string>> decoded;
|
|
|
|
std::vector<std::shared_ptr<std::string>> loaded;
|
|
|
|
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
2018-10-31 17:22:51 +08:00
|
|
|
slk::Load<std::shared_ptr<std::string>>(
|
2019-05-02 20:24:11 +08:00
|
|
|
&decoded, reader,
|
2018-10-31 17:22:51 +08:00
|
|
|
[&loaded](auto *item, auto *reader) { Load(item, reader, &loaded); });
|
|
|
|
|
|
|
|
ASSERT_EQ(decoded.size(), original.size());
|
|
|
|
|
|
|
|
ASSERT_EQ(saved.size(), 2);
|
|
|
|
ASSERT_EQ(loaded.size(), saved.size());
|
|
|
|
|
|
|
|
ASSERT_EQ(*decoded[0].get(), *ptr1.get());
|
|
|
|
ASSERT_EQ(decoded[1].get(), nullptr);
|
|
|
|
ASSERT_EQ(*decoded[2].get(), *ptr3.get());
|
|
|
|
ASSERT_EQ(*decoded[3].get(), *ptr1.get());
|
|
|
|
ASSERT_EQ(*decoded[4].get(), *ptr3.get());
|
|
|
|
|
|
|
|
ASSERT_NE(decoded[0].get(), decoded[2].get());
|
|
|
|
|
|
|
|
ASSERT_EQ(decoded[3].get(), decoded[0].get());
|
|
|
|
ASSERT_EQ(decoded[4].get(), decoded[2].get());
|
|
|
|
}
|
|
|
|
|
2018-11-08 20:27:52 +08:00
|
|
|
TEST(SlkCore, OptionalSharedPtr) {
|
2019-04-23 17:00:49 +08:00
|
|
|
std::optional<std::shared_ptr<std::string>> original =
|
2018-10-31 21:41:45 +08:00
|
|
|
std::make_shared<std::string>("nandare!");
|
2018-10-31 17:22:51 +08:00
|
|
|
std::vector<std::string *> saved;
|
|
|
|
|
2019-05-02 20:24:11 +08:00
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
2018-10-31 17:22:51 +08:00
|
|
|
slk::Save<std::shared_ptr<std::string>>(
|
2019-05-02 20:24:11 +08:00
|
|
|
original, builder, [&saved](const auto &item, auto *builder) {
|
2018-10-31 17:22:51 +08:00
|
|
|
Save(item, builder, &saved);
|
|
|
|
});
|
|
|
|
|
2019-04-23 17:00:49 +08:00
|
|
|
std::optional<std::shared_ptr<std::string>> decoded;
|
2018-10-31 17:22:51 +08:00
|
|
|
std::vector<std::shared_ptr<std::string>> loaded;
|
|
|
|
|
2019-05-02 20:24:11 +08:00
|
|
|
auto reader = loopback.GetReader();
|
2018-10-31 17:22:51 +08:00
|
|
|
slk::Load<std::shared_ptr<std::string>>(
|
2019-05-02 20:24:11 +08:00
|
|
|
&decoded, reader,
|
2018-10-31 17:22:51 +08:00
|
|
|
[&loaded](auto *item, auto *reader) { Load(item, reader, &loaded); });
|
|
|
|
|
2019-04-23 17:00:49 +08:00
|
|
|
ASSERT_NE(decoded, std::nullopt);
|
2018-10-31 17:22:51 +08:00
|
|
|
|
|
|
|
ASSERT_EQ(saved.size(), 1);
|
|
|
|
ASSERT_EQ(loaded.size(), 1);
|
|
|
|
|
|
|
|
ASSERT_EQ(*decoded->get(), *original->get());
|
|
|
|
}
|
2019-05-02 20:24:11 +08:00
|
|
|
|
|
|
|
TEST(SlkCore, OptionalSharedPtrEmpty) {
|
|
|
|
std::optional<std::shared_ptr<std::string>> original;
|
|
|
|
std::vector<std::string *> saved;
|
|
|
|
|
|
|
|
slk::Loopback loopback;
|
|
|
|
auto builder = loopback.GetBuilder();
|
|
|
|
slk::Save<std::shared_ptr<std::string>>(
|
|
|
|
original, builder, [&saved](const auto &item, auto *builder) {
|
|
|
|
Save(item, builder, &saved);
|
|
|
|
});
|
|
|
|
|
|
|
|
std::optional<std::shared_ptr<std::string>> decoded;
|
|
|
|
std::vector<std::shared_ptr<std::string>> loaded;
|
|
|
|
|
|
|
|
auto reader = loopback.GetReader();
|
|
|
|
slk::Load<std::shared_ptr<std::string>>(
|
|
|
|
&decoded, reader,
|
|
|
|
[&loaded](auto *item, auto *reader) { Load(item, reader, &loaded); });
|
|
|
|
|
|
|
|
ASSERT_EQ(decoded, std::nullopt);
|
|
|
|
|
|
|
|
ASSERT_EQ(saved.size(), 0);
|
|
|
|
ASSERT_EQ(loaded.size(), 0);
|
|
|
|
}
|