| 1 | #define BOOST_TEST_MODULE Serialization
|
|---|
| 2 |
|
|---|
| 3 | #include <boost/test/test_tools.hpp>
|
|---|
| 4 | #include <boost/test/included/unit_test.hpp>
|
|---|
| 5 |
|
|---|
| 6 | class Base1
|
|---|
| 7 | {
|
|---|
| 8 | int
|
|---|
| 9 | m_i1;
|
|---|
| 10 |
|
|---|
| 11 | protected:
|
|---|
| 12 |
|
|---|
| 13 | Base1() : m_i1(1)
|
|---|
| 14 | { }
|
|---|
| 15 |
|
|---|
| 16 | ~Base1()
|
|---|
| 17 | { }
|
|---|
| 18 |
|
|---|
| 19 | };
|
|---|
| 20 |
|
|---|
| 21 | class Base2
|
|---|
| 22 | {
|
|---|
| 23 | double
|
|---|
| 24 | m_i2;
|
|---|
| 25 |
|
|---|
| 26 | protected:
|
|---|
| 27 |
|
|---|
| 28 | Base2() : m_i2(2)
|
|---|
| 29 | { }
|
|---|
| 30 |
|
|---|
| 31 | ~Base2()
|
|---|
| 32 | { }
|
|---|
| 33 |
|
|---|
| 34 | };
|
|---|
| 35 |
|
|---|
| 36 | class Derived1 : public Base1, public Base2
|
|---|
| 37 | {
|
|---|
| 38 | double
|
|---|
| 39 | m_d1;
|
|---|
| 40 | public:
|
|---|
| 41 |
|
|---|
| 42 | Derived1(
|
|---|
| 43 | ) : m_d1(1.0)
|
|---|
| 44 | {}
|
|---|
| 45 |
|
|---|
| 46 | };
|
|---|
| 47 |
|
|---|
| 48 | class Derived2 : public Base2, public Base1
|
|---|
| 49 | {
|
|---|
| 50 | double
|
|---|
| 51 | m_d2;
|
|---|
| 52 | public:
|
|---|
| 53 |
|
|---|
| 54 | Derived2(
|
|---|
| 55 | ) : m_d2(1.0)
|
|---|
| 56 | {}
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | class Derived : public Base1
|
|---|
| 60 | {
|
|---|
| 61 | double
|
|---|
| 62 | m_d;
|
|---|
| 63 | public:
|
|---|
| 64 |
|
|---|
| 65 | Derived(
|
|---|
| 66 | ) : m_d(0.0)
|
|---|
| 67 | {}
|
|---|
| 68 | };
|
|---|
| 69 |
|
|---|
| 70 | BOOST_AUTO_TEST_SUITE(Serialization)
|
|---|
| 71 |
|
|---|
| 72 | BOOST_AUTO_TEST_CASE(pointerTest)
|
|---|
| 73 | {
|
|---|
| 74 | // Declare the derived objects
|
|---|
| 75 | Derived
|
|---|
| 76 | d;
|
|---|
| 77 | Derived1
|
|---|
| 78 | d1;
|
|---|
| 79 | Derived2
|
|---|
| 80 | d2;
|
|---|
| 81 |
|
|---|
| 82 | // Assign the derived objects to void pointers
|
|---|
| 83 | void
|
|---|
| 84 | *pVoidD = &d,
|
|---|
| 85 | *pVoidD1 = &d1,
|
|---|
| 86 | *pVoidD2 = &d2;
|
|---|
| 87 |
|
|---|
| 88 | // Cast the void pointers back to their derived types,
|
|---|
| 89 | // and assign them to Base1 pointers
|
|---|
| 90 | Base1
|
|---|
| 91 | *pB = static_cast<Derived*>(pVoidD),
|
|---|
| 92 | *pB1 = static_cast<Derived1*>(pVoidD1),
|
|---|
| 93 | *pB2 = static_cast<Derived2*>(pVoidD2);
|
|---|
| 94 |
|
|---|
| 95 | // Cast the Base1 pointers back to the respective derived type,
|
|---|
| 96 | // and assign them to the derived pointers
|
|---|
| 97 | Derived
|
|---|
| 98 | *pD = static_cast<Derived*>(pB);
|
|---|
| 99 | Derived1
|
|---|
| 100 | *pD1 = static_cast<Derived1*>(pB1);
|
|---|
| 101 | Derived2
|
|---|
| 102 | *pD2 = static_cast<Derived2*>(pB2);
|
|---|
| 103 |
|
|---|
| 104 | // Cast the processed derived pointers to integers, for easier comparison
|
|---|
| 105 | unsigned long long
|
|---|
| 106 | iD = reinterpret_cast<unsigned long long>(pD),
|
|---|
| 107 | iD1 = reinterpret_cast<unsigned long long>(pD1),
|
|---|
| 108 | iD2 = reinterpret_cast<unsigned long long>(pD2),
|
|---|
| 109 | iB = reinterpret_cast<unsigned long long>(pB),
|
|---|
| 110 | iB1 = reinterpret_cast<unsigned long long>(pB1),
|
|---|
| 111 | iB2 = reinterpret_cast<unsigned long long>(pB2);
|
|---|
| 112 |
|
|---|
| 113 | BOOST_REQUIRE(iD == iB); // We expect that Derived is equal to Base1
|
|---|
| 114 | BOOST_REQUIRE(iD1 == iB1); // We expect that Derived1 is equal to Base1
|
|---|
| 115 | BOOST_REQUIRE(iD2 != iB2); // We expect that Derived2 is NOT equal to Base1,
|
|---|
| 116 | // since it inherits from Base2, Base1, respectively, it will be equal to Base2
|
|---|
| 117 |
|
|---|
| 118 | // This means that if using static_cast one can use multiple inheritance
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | BOOST_AUTO_TEST_SUITE_END()
|
|---|