From bed43d7010457543500c84cd40568ba157807b83 Mon Sep 17 00:00:00 2001 From: Brandon Kohn Date: Thu, 23 Jan 2014 14:01:32 -0500 Subject: [PATCH] Cleaned up some tab/space for consistency. Added new cycle test with multiple base types. --- test/Jamfile.v2 | 5 +- test/test_cyclic_ptrs_multiple_inheritance.cpp | 146 +++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 test/test_cyclic_ptrs_multiple_inheritance.cpp diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 84f12dd..c16567c 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -62,7 +62,8 @@ test-suite "serialization" : [ test-bsl-run_files test_complex ] [ test-bsl-run_files test_contained_class : A ] [ test-bsl-run_files test_cyclic_ptrs : A ] - [ test-bsl-run_files test_cyclic_ptrs_trac9601 ] + [ test-bsl-run_files test_cyclic_ptrs_trac9601 ] + [ test-bsl-run_files test_cyclic_ptrs_multiple_inheritance ] [ test-bsl-run_files test_delete_pointer ] [ test-bsl-run_files test_deque : A ] [ test-bsl-run_files test_derived ] @@ -122,7 +123,7 @@ if ! $(BOOST_ARCHIVE_LIST) { [ test-bsl-run-no-lib test_iterators ] [ test-bsl-run-no-lib test_iterators_base64 ] - [ test-bsl-run-no-lib test_inclusion ] + [ test-bsl-run-no-lib test_inclusion ] [ test-bsl-run-no-lib test_smart_cast ] [ test-bsl-run-no-lib test_utf8_codecvt diff --git a/test/test_cyclic_ptrs_multiple_inheritance.cpp b/test/test_cyclic_ptrs_multiple_inheritance.cpp new file mode 100644 index 0000000..a655767 --- /dev/null +++ b/test/test_cyclic_ptrs_multiple_inheritance.cpp @@ -0,0 +1,146 @@ +/////////////////////////////////////////////////////////////////////////////// +// test_cyclic_ptrs_multiple_inheritance.cpp +// +// (C) Copyright 2014 +// Brandon Kohn +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +#include + +#include "test_tools.hpp" + +#include +#include +#include +#include + +#include // remove +#include +#include // auto_ptr + +#if defined(BOOST_NO_STDC_NAMESPACE) +namespace std { + using ::remove; +} +#endif + +struct base_a; +struct base_b; +struct common_ab; + +struct base_a +{ + base_a(base_b * ptr = reinterpret_cast(0xDEADBEEF)) + : ptr_a(ptr) + {} + + virtual ~base_a(){} + + base_b * ptr_a; + + template + void serialize(Archive& ar, const unsigned int /*v*/) + { + ar & BOOST_SERIALIZATION_NVP(ptr_a); + } +}; + +BOOST_CLASS_VERSION(base_a,1) + +struct base_b +{ + base_b(base_a * ptr = reinterpret_cast(0xDEADDEAD)) + : ptr_b(ptr) + {} + + virtual ~base_b(){} + + base_a * ptr_b; + + template + void serialize(Archive& ar, const unsigned int /*v*/) + { + ar & BOOST_SERIALIZATION_NVP(ptr_b); + } +}; + +BOOST_CLASS_VERSION(base_b,1) + +struct common_ab : base_a, base_b +{ + common_ab() + : base_a() + , base_b() + , ptr(reinterpret_cast(0xBAADF00D)) + {} + + virtual ~common_ab(){} + + common_ab * ptr; + + template + void serialize(Archive& ar, const unsigned int /*v*/) + { + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(base_a); + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(base_b); + ar & BOOST_SERIALIZATION_NVP(ptr); + } +}; + +BOOST_CLASS_VERSION(common_ab,1) +BOOST_CLASS_EXPORT(common_ab) + +//! Create an instance of common_ab where all ptr member variables point to the single instance. +std::auto_ptr make_common() +{ + std::auto_ptr p(new common_ab()); + p->ptr = p.get(); + p->ptr_a = static_cast(p.get()); + p->ptr_b = static_cast(p.get()); + return p; +} + +int test_main( int /* argc */, char* /* argv */[] ) +{ + const char * testfile = boost::archive::tmpnam(0); + BOOST_REQUIRE(0 != testfile); + + //! Write it. + { + std::auto_ptr store(make_common()); + + test_ostream os(testfile, TEST_STREAM_FLAGS); + test_oarchive ar(os, TEST_ARCHIVE_FLAGS); + + common_ab* pPtrHldr = store.get(); + ar << BOOST_SERIALIZATION_NVP(pPtrHldr); + } + + //! Read it. + { + common_ab* pPtrHldr = reinterpret_cast(0xBAADBAAD); + + test_istream is(testfile, TEST_STREAM_FLAGS); + test_iarchive ar(is, TEST_ARCHIVE_FLAGS); + + ar >> BOOST_SERIALIZATION_NVP(pPtrHldr); + + BOOST_CHECK(0 != pPtrHldr); + if (0 != pPtrHldr) + { + //! Check that all the ptrs are the same object. + BOOST_CHECK(pPtrHldr->ptr == pPtrHldr); + BOOST_CHECK(pPtrHldr->ptr_a == static_cast(pPtrHldr)); + BOOST_CHECK(pPtrHldr->ptr_b == static_cast(pPtrHldr)); + } + + delete pPtrHldr; + } + + std::remove(testfile); + + return EXIT_SUCCESS; +} -- 1.8.0.msysgit.0