File indexing completed on 2023-10-25 09:37:26
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include "catch.hpp"
0015
0016
0017 #include "CondFormats/SerializationHelper/interface/SerializationHelper.h"
0018
0019 namespace {
0020 struct Test {
0021 Test() = default;
0022 Test(float iA, int iB) : a_{iA}, b_{iB} {}
0023
0024 float a_;
0025 int b_;
0026
0027 template <class Archive>
0028 void serialize(Archive& ar, const unsigned int version) {
0029 ar& a_;
0030 ar& b_;
0031 ++counter_;
0032 }
0033
0034 int counter_ = 0;
0035 };
0036
0037 struct TestBase {
0038 virtual ~TestBase() noexcept {}
0039
0040 virtual int value() const = 0;
0041
0042 template <class Archive>
0043 void serialize(Archive& ar, const unsigned int version) {}
0044 };
0045
0046 struct TestInheritance : public TestBase {
0047 ~TestInheritance() noexcept override = default;
0048
0049 int value() const final { return value_; }
0050
0051 template <class Archive>
0052 void serialize(Archive& ar, const unsigned int version) {
0053 ar& value_;
0054 }
0055 int value_ = 3145;
0056 };
0057
0058 }
0059
0060 namespace cond::serialization {
0061 template <>
0062 struct ClassName<Test> {
0063 constexpr static std::string_view kName = "Test";
0064 };
0065 template <>
0066 struct ClassName<TestBase> {
0067 [[maybe_unused]] constexpr static std::string_view kName = "TestBase";
0068 };
0069 template <>
0070 struct ClassName<TestInheritance> {
0071 constexpr static std::string_view kName = "TestInheritance";
0072 };
0073
0074 template <>
0075 struct BaseClassInfo<TestBase> : public BaseClassInfoImpl<TestBase, true, TestInheritance> {};
0076 }
0077
0078 using namespace cond::serialization;
0079
0080 TEST_CASE("Test SerializationHelper", "[SerializationHelper]") {
0081 SECTION("serialize") {
0082 SerializationHelper<Test> helper;
0083
0084 std::stringbuf dataBuffer;
0085
0086 Test test{1.3, -4};
0087 REQUIRE(test.counter_ == 0);
0088
0089 auto typeName = helper.serialize(dataBuffer, &test);
0090
0091 REQUIRE(dataBuffer.str().size() > 0);
0092 REQUIRE(test.counter_ == 1);
0093 REQUIRE(typeName == "Test");
0094 }
0095
0096 SECTION("deserialize") {
0097 SerializationHelper<Test> helper;
0098
0099 std::stringbuf dataBuffer;
0100 std::string_view typeName;
0101 {
0102 Test test{1.3, -4};
0103 REQUIRE(test.counter_ == 0);
0104
0105 typeName = helper.serialize(dataBuffer, &test);
0106 }
0107
0108 auto voidPtr = helper.deserialize(dataBuffer, typeName);
0109
0110 const Test* pTest = static_cast<const Test*>(voidPtr.get());
0111 REQUIRE_THAT(pTest->a_, Catch::Matchers::WithinAbs(1.3, 0.001));
0112 REQUIRE(pTest->b_ == -4);
0113 REQUIRE(pTest->counter_ == 1);
0114 }
0115
0116 SECTION("polymorphic serialize") {
0117 SerializationHelper<TestBase> helper;
0118
0119 std::stringbuf dataBuffer;
0120
0121 TestInheritance test{};
0122
0123 auto typeName = helper.serialize(dataBuffer, &test);
0124
0125 REQUIRE(typeName == "TestInheritance");
0126 REQUIRE(dataBuffer.str().size() > 0);
0127 REQUIRE(test.value() == 3145);
0128 }
0129
0130 SECTION("deserialize") {
0131 SerializationHelper<TestBase> helper;
0132
0133 std::stringbuf dataBuffer;
0134 std::string_view typeName;
0135 {
0136 TestInheritance test;
0137
0138 typeName = helper.serialize(dataBuffer, &test);
0139 }
0140
0141 auto voidPtr = helper.deserialize(dataBuffer, typeName);
0142 REQUIRE(voidPtr.get() != nullptr);
0143
0144 const TestBase* pTest = static_cast<const TestBase*>(voidPtr.get());
0145 REQUIRE(pTest->value() == 3145);
0146 }
0147 }