Ticket #9601: 0001-Added-unit-test-for-regression-testing-of-trac9601-i.patch

File 0001-Added-unit-test-for-regression-testing-of-trac9601-i.patch, 3.1 KB (added by Brandon Kohn, 9 years ago)

Proposed unit test for the issue

  • test/Jamfile.v2

    From 7f084e8ae89313811c45f94125504e16d3a9d958 Mon Sep 17 00:00:00 2001
    From: Brandon Kohn <blkohn@hotmail.com>
    Date: Wed, 22 Jan 2014 17:56:16 -0500
    Subject: [PATCH] Added unit-test for regression testing of trac9601 issue.
    
    ---
     test/Jamfile.v2                    |  1 +
     test/test_cyclic_ptrs_trac9601.cpp | 83 ++++++++++++++++++++++++++++++++++++++
     2 files changed, 84 insertions(+)
     create mode 100644 test/test_cyclic_ptrs_trac9601.cpp
    
    diff --git a/test/Jamfile.v2 b/test/Jamfile.v2
    index e335860..84f12dd 100644
    a b test-suite "serialization" :  
    6262     [ test-bsl-run_files test_complex ]
    6363     [ test-bsl-run_files test_contained_class : A ]
    6464     [ test-bsl-run_files test_cyclic_ptrs : A ]
     65         [ test-bsl-run_files test_cyclic_ptrs_trac9601 ]
    6566     [ test-bsl-run_files test_delete_pointer ]
    6667     [ test-bsl-run_files test_deque : A ]
    6768     [ test-bsl-run_files test_derived ]
  • new file test/test_cyclic_ptrs_trac9601.cpp

    diff --git a/test/test_cyclic_ptrs_trac9601.cpp b/test/test_cyclic_ptrs_trac9601.cpp
    new file mode 100644
    index 0000000..5b4c3f1
    - +  
     1///////////////////////////////////////////////////////////////////////////////
     2// test_cyclic_ptrs_trac9601.cpp
     3//
     4//  (C) Copyright 2014
     5//  Brandon Kohn
     6//
     7//  Distributed under the Boost Software License, Version 1.0. (See
     8//  accompanying file LICENSE_1_0.txt or copy at
     9//  http://www.boost.org/LICENSE_1_0.txt)
     10//
     11#include <boost/config.hpp>
     12
     13#include "test_tools.hpp"
     14
     15#include <boost/serialization/nvp.hpp>
     16#include <boost/serialization/version.hpp>
     17
     18#include <cstdio> // remove
     19#include <fstream>
     20#include <memory> // auto_ptr
     21
     22#if defined(BOOST_NO_STDC_NAMESPACE)
     23namespace std {
     24    using ::remove;
     25}
     26#endif
     27
     28struct pointer_holder
     29{
     30    pointer_holder(pointer_holder* ptr = 0)
     31    : ptr(ptr)
     32    {}
     33
     34    pointer_holder* ptr;
     35
     36    template <typename Archive>
     37    void serialize(Archive& ar, const unsigned int /*v*/)
     38    {
     39        ar & BOOST_SERIALIZATION_NVP(ptr);
     40    }
     41};
     42
     43BOOST_CLASS_VERSION(pointer_holder,1)
     44
     45int test_main( int /* argc */, char* /* argv */[] )
     46{
     47    const char * testfile = boost::archive::tmpnam(0);
     48    BOOST_REQUIRE(0 != testfile);
     49
     50    //! Write it.
     51    {
     52        std::auto_ptr<pointer_holder> store(new pointer_holder());
     53        store->ptr = store.get();
     54         
     55        test_ostream os(testfile, TEST_STREAM_FLAGS);
     56        test_oarchive ar(os, TEST_ARCHIVE_FLAGS);
     57
     58        pointer_holder* pPtrHldr = store.get();
     59        ar << BOOST_SERIALIZATION_NVP(pPtrHldr);
     60    }
     61
     62    //! Read it.
     63    {
     64        pointer_holder* pPtrHldr = reinterpret_cast<pointer_holder*>(0xBAADF00D);
     65
     66        test_istream is(testfile, TEST_STREAM_FLAGS);
     67        test_iarchive ar(is, TEST_ARCHIVE_FLAGS);
     68
     69        ar >> BOOST_SERIALIZATION_NVP(pPtrHldr);
     70
     71        BOOST_CHECK(0 != pPtrHldr);
     72        if (0 != pPtrHldr)
     73        {
     74            BOOST_CHECK(pPtrHldr->ptr == pPtrHldr);
     75        }
     76
     77        delete pPtrHldr;
     78    }
     79
     80    std::remove(testfile);
     81
     82    return EXIT_SUCCESS;
     83}