Test pointer handling of binary serialization
This commit is contained in:
parent
5835cd85a5
commit
aa92cab4e5
|
@ -1,4 +1,5 @@
|
||||||
#include "./helper.h"
|
#include "./helper.h"
|
||||||
|
#include "./morestructs.h"
|
||||||
#include "./structs.h"
|
#include "./structs.h"
|
||||||
|
|
||||||
#include "../codefactory.h"
|
#include "../codefactory.h"
|
||||||
|
@ -14,6 +15,7 @@
|
||||||
#include <cppunit/extensions/HelperMacros.h>
|
#include <cppunit/extensions/HelperMacros.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
using namespace CPPUNIT_NS;
|
using namespace CPPUNIT_NS;
|
||||||
|
@ -28,11 +30,13 @@ using namespace ConversionUtilities;
|
||||||
class BinaryGeneratorTests : public TestFixture {
|
class BinaryGeneratorTests : public TestFixture {
|
||||||
CPPUNIT_TEST_SUITE(BinaryGeneratorTests);
|
CPPUNIT_TEST_SUITE(BinaryGeneratorTests);
|
||||||
CPPUNIT_TEST(testSerializationAndDeserialization);
|
CPPUNIT_TEST(testSerializationAndDeserialization);
|
||||||
|
CPPUNIT_TEST(testPointerHandling);
|
||||||
CPPUNIT_TEST_SUITE_END();
|
CPPUNIT_TEST_SUITE_END();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BinaryGeneratorTests();
|
BinaryGeneratorTests();
|
||||||
void testSerializationAndDeserialization();
|
void testSerializationAndDeserialization();
|
||||||
|
void testPointerHandling();
|
||||||
};
|
};
|
||||||
|
|
||||||
CPPUNIT_TEST_SUITE_REGISTRATION(BinaryGeneratorTests);
|
CPPUNIT_TEST_SUITE_REGISTRATION(BinaryGeneratorTests);
|
||||||
|
@ -63,3 +67,48 @@ void BinaryGeneratorTests::testSerializationAndDeserialization()
|
||||||
CPPUNIT_ASSERT_EQUAL(obj.someString, deserializedObj.someString);
|
CPPUNIT_ASSERT_EQUAL(obj.someString, deserializedObj.someString);
|
||||||
CPPUNIT_ASSERT_EQUAL(obj.someBool, deserializedObj.someBool);
|
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<PointerTarget>(0xF1F2F3F3);
|
||||||
|
ps.s2 = ps.s1;
|
||||||
|
ps.s3 = make_shared<PointerTarget>(0xBBBBBBBB);
|
||||||
|
ps.u1 = make_unique<PointerTarget>(0xF1F2F3F4);
|
||||||
|
ps.u2 = make_unique<PointerTarget>(0xDDDDDDDD);
|
||||||
|
ps.u3 = make_unique<PointerTarget>(0xEEEEEEEE);
|
||||||
|
|
||||||
|
// check whether shared pointer are "wired" as expected
|
||||||
|
++ps.s1->n; // should affect s2 but not s3
|
||||||
|
CPPUNIT_ASSERT_EQUAL(asHexNumber<uint32_t>(0xF1F2F3F4), asHexNumber<uint32_t>(ps.s1->n));
|
||||||
|
CPPUNIT_ASSERT_EQUAL(asHexNumber<uint32_t>(0xF1F2F3F4), asHexNumber<uint32_t>(ps.s2->n));
|
||||||
|
CPPUNIT_ASSERT_EQUAL(asHexNumber<uint32_t>(0xBBBBBBBB), asHexNumber<uint32_t>(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<uint32_t>(0xF1F2F3F4), asHexNumber<uint32_t>(deserializedPs.s1->n));
|
||||||
|
CPPUNIT_ASSERT_EQUAL(asHexNumber<uint32_t>(0xF1F2F3F4), asHexNumber<uint32_t>(deserializedPs.s2->n));
|
||||||
|
CPPUNIT_ASSERT_EQUAL(asHexNumber<uint32_t>(0xBBBBBBBB), asHexNumber<uint32_t>(deserializedPs.s3->n));
|
||||||
|
++deserializedPs.s1->n; // should affect s2 but not s3
|
||||||
|
CPPUNIT_ASSERT_EQUAL(asHexNumber<uint32_t>(0xF1F2F3F5), asHexNumber<uint32_t>(deserializedPs.s1->n));
|
||||||
|
CPPUNIT_ASSERT_EQUAL(asHexNumber<uint32_t>(0xF1F2F3F5), asHexNumber<uint32_t>(deserializedPs.s2->n));
|
||||||
|
CPPUNIT_ASSERT_EQUAL(asHexNumber<uint32_t>(0xBBBBBBBB), asHexNumber<uint32_t>(deserializedPs.s3->n));
|
||||||
|
|
||||||
|
// check unique pointer
|
||||||
|
CPPUNIT_ASSERT_EQUAL(asHexNumber<uint32_t>(0xF1F2F3F4), asHexNumber<uint32_t>(deserializedPs.u1->n));
|
||||||
|
CPPUNIT_ASSERT_EQUAL(asHexNumber<uint32_t>(0xDDDDDDDD), asHexNumber<uint32_t>(deserializedPs.u2->n));
|
||||||
|
CPPUNIT_ASSERT_EQUAL(asHexNumber<uint32_t>(0xEEEEEEEE), asHexNumber<uint32_t>(deserializedPs.u3->n));
|
||||||
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include "resources/config.h"
|
#include "resources/config.h"
|
||||||
|
|
||||||
|
#include <c++utilities/conversion/stringbuilder.h>
|
||||||
#include <c++utilities/conversion/stringconversion.h>
|
#include <c++utilities/conversion/stringconversion.h>
|
||||||
#include <c++utilities/io/misc.h>
|
#include <c++utilities/io/misc.h>
|
||||||
#include <c++utilities/tests/testutils.h>
|
#include <c++utilities/tests/testutils.h>
|
||||||
|
|
|
@ -29,4 +29,42 @@ struct ConstStruct : public JsonSerializable<ConstStruct>, public BinarySerializ
|
||||||
const int constInt = 42;
|
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> {
|
||||||
|
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<PointerStruct> {
|
||||||
|
std::shared_ptr<PointerTarget> s1;
|
||||||
|
std::unique_ptr<PointerTarget> u2;
|
||||||
|
std::unique_ptr<PointerTarget> u3;
|
||||||
|
std::shared_ptr<PointerTarget> s2;
|
||||||
|
std::unique_ptr<PointerTarget> u1;
|
||||||
|
std::shared_ptr<PointerTarget> s3;
|
||||||
|
};
|
||||||
|
|
||||||
#endif // REFLECTIVE_RAPIDJSON_TESTS_MORE_STRUCTS_H
|
#endif // REFLECTIVE_RAPIDJSON_TESTS_MORE_STRUCTS_H
|
||||||
|
|
Loading…
Reference in New Issue