From aa92cab4e5077e022399e33022b04606bfcdd746 Mon Sep 17 00:00:00 2001 From: Martchus Date: Sun, 28 Oct 2018 21:35:32 +0100 Subject: [PATCH] Test pointer handling of binary serialization --- generator/tests/binarygenerator.cpp | 49 +++++++++++++++++++++++++++++ generator/tests/jsongenerator.cpp | 1 + generator/tests/morestructs.h | 38 ++++++++++++++++++++++ 3 files changed, 88 insertions(+) diff --git a/generator/tests/binarygenerator.cpp b/generator/tests/binarygenerator.cpp index 9779037..49fefa7 100644 --- a/generator/tests/binarygenerator.cpp +++ b/generator/tests/binarygenerator.cpp @@ -1,4 +1,5 @@ #include "./helper.h" +#include "./morestructs.h" #include "./structs.h" #include "../codefactory.h" @@ -14,6 +15,7 @@ #include #include +#include #include using namespace CPPUNIT_NS; @@ -28,11 +30,13 @@ using namespace ConversionUtilities; class BinaryGeneratorTests : public TestFixture { CPPUNIT_TEST_SUITE(BinaryGeneratorTests); CPPUNIT_TEST(testSerializationAndDeserialization); + CPPUNIT_TEST(testPointerHandling); CPPUNIT_TEST_SUITE_END(); public: BinaryGeneratorTests(); void testSerializationAndDeserialization(); + void testPointerHandling(); }; CPPUNIT_TEST_SUITE_REGISTRATION(BinaryGeneratorTests); @@ -63,3 +67,48 @@ void BinaryGeneratorTests::testSerializationAndDeserialization() CPPUNIT_ASSERT_EQUAL(obj.someString, deserializedObj.someString); CPPUNIT_ASSERT_EQUAL(obj.someBool, deserializedObj.someBool); } + +/*! + * \brief Tests handling of std::unique_ptr and std::shared_ptr. + * + * In particular, the same object referred by 2 related std::shared_ptr instances + * + * - should be serialized only once + * - and result in 2 related std::shared_ptr instances again after deserialization. + */ +void BinaryGeneratorTests::testPointerHandling() +{ + PointerStruct ps; + ps.s1 = make_shared(0xF1F2F3F3); + ps.s2 = ps.s1; + ps.s3 = make_shared(0xBBBBBBBB); + ps.u1 = make_unique(0xF1F2F3F4); + ps.u2 = make_unique(0xDDDDDDDD); + ps.u3 = make_unique(0xEEEEEEEE); + + // check whether shared pointer are "wired" as expected + ++ps.s1->n; // should affect s2 but not s3 + CPPUNIT_ASSERT_EQUAL(asHexNumber(0xF1F2F3F4), asHexNumber(ps.s1->n)); + CPPUNIT_ASSERT_EQUAL(asHexNumber(0xF1F2F3F4), asHexNumber(ps.s2->n)); + CPPUNIT_ASSERT_EQUAL(asHexNumber(0xBBBBBBBB), asHexNumber(ps.s3->n)); + + // serialize and deserialize + stringstream stream(ios_base::in | ios_base::out | ios_base::binary); + stream.exceptions(ios_base::failbit | ios_base::badbit); + ps.toBinary(stream); + const auto deserializedPs(PointerStruct::fromBinary(stream)); + + // check shared pointer + CPPUNIT_ASSERT_EQUAL(asHexNumber(0xF1F2F3F4), asHexNumber(deserializedPs.s1->n)); + CPPUNIT_ASSERT_EQUAL(asHexNumber(0xF1F2F3F4), asHexNumber(deserializedPs.s2->n)); + CPPUNIT_ASSERT_EQUAL(asHexNumber(0xBBBBBBBB), asHexNumber(deserializedPs.s3->n)); + ++deserializedPs.s1->n; // should affect s2 but not s3 + CPPUNIT_ASSERT_EQUAL(asHexNumber(0xF1F2F3F5), asHexNumber(deserializedPs.s1->n)); + CPPUNIT_ASSERT_EQUAL(asHexNumber(0xF1F2F3F5), asHexNumber(deserializedPs.s2->n)); + CPPUNIT_ASSERT_EQUAL(asHexNumber(0xBBBBBBBB), asHexNumber(deserializedPs.s3->n)); + + // check unique pointer + CPPUNIT_ASSERT_EQUAL(asHexNumber(0xF1F2F3F4), asHexNumber(deserializedPs.u1->n)); + CPPUNIT_ASSERT_EQUAL(asHexNumber(0xDDDDDDDD), asHexNumber(deserializedPs.u2->n)); + CPPUNIT_ASSERT_EQUAL(asHexNumber(0xEEEEEEEE), asHexNumber(deserializedPs.u3->n)); +} diff --git a/generator/tests/jsongenerator.cpp b/generator/tests/jsongenerator.cpp index f67d26f..6f6564c 100644 --- a/generator/tests/jsongenerator.cpp +++ b/generator/tests/jsongenerator.cpp @@ -6,6 +6,7 @@ #include "resources/config.h" +#include #include #include #include diff --git a/generator/tests/morestructs.h b/generator/tests/morestructs.h index e9cfe78..d355236 100644 --- a/generator/tests/morestructs.h +++ b/generator/tests/morestructs.h @@ -29,4 +29,42 @@ struct ConstStruct : public JsonSerializable, public BinarySerializ const int constInt = 42; }; +/*! + * \brief The PointerTarget struct is used to test the behavior of the binary (de)serialization with smart pointer. + */ +struct PointerTarget : public BinarySerializable { + PointerTarget() + : n(0xAAAAAAAA) + , dummy1(0x1111111111111111) + , dummy2(0x1111111111111111) + , dummy3(0x1111111111111111) + { + } + + PointerTarget(uint32_t n) + : n(n) + , dummy1(0x1111111111111111) + , dummy2(0x1111111111111111) + , dummy3(0x1111111111111111) + { + } + + uint32_t n; + uint64_t dummy1; + uint64_t dummy2; + uint64_t dummy3; +}; + +/*! + * \brief The PointerStruct struct is used to test the behavior of the binary (de)serialization with smart pointer. + */ +struct PointerStruct : public BinarySerializable { + std::shared_ptr s1; + std::unique_ptr u2; + std::unique_ptr u3; + std::shared_ptr s2; + std::unique_ptr u1; + std::shared_ptr s3; +}; + #endif // REFLECTIVE_RAPIDJSON_TESTS_MORE_STRUCTS_H