Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/serialization/test/test_list_ptrs.cpp @ 33

Last change on this file since 33 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 3.7 KB
Line 
1/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2// test_list.cpp
3
4// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
5// Use, modification and distribution is subject to the Boost Software
6// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8
9// should pass compilation and execution
10
11#include <fstream>
12
13#include <cstdio> // remove
14#include <boost/config.hpp>
15#if defined(BOOST_NO_STDC_NAMESPACE)
16namespace std{ 
17    using ::remove;
18}
19#endif
20
21#include <boost/type_traits/is_pointer.hpp>
22#include <boost/static_assert.hpp>
23#include <boost/checked_delete.hpp>
24
25#include <boost/archive/archive_exception.hpp>
26#include "test_tools.hpp"
27#include <boost/preprocessor/stringize.hpp>
28#include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST)
29
30#include <boost/serialization/list.hpp>
31#ifdef BOOST_HAS_SLIST
32#include <boost/serialization/slist.hpp>
33#endif
34#include <boost/serialization/nvp.hpp>
35
36#include "A.hpp"
37
38template<class T>
39struct ptr_equal_to : public std::binary_function<T, T, bool> 
40{
41    BOOST_STATIC_ASSERT(::boost::is_pointer<T>::value);
42    bool operator()(T const _Left, T const _Right) const
43    {
44        if(NULL == _Left && NULL == _Right)
45            return true;
46        if(typeid(*_Left) != typeid(*_Right))
47            return false;
48        return *_Left == *_Right;
49    }
50};
51
52int test_main( int /* argc */, char* /* argv */[] )
53{
54    const char * testfile = boost::archive::tmpnam(NULL);
55    BOOST_REQUIRE(NULL != testfile);
56
57    std::list<A *> alist;
58    {   
59        test_ostream os(testfile, TEST_STREAM_FLAGS);
60        test_oarchive oa(os);
61        A * free_a_ptr = new A;
62        alist.push_back(free_a_ptr);
63        alist.push_back(new A);
64        // verify that first element is the same as the free pointer
65        BOOST_CHECK((*alist.begin()) == free_a_ptr);
66        oa << boost::serialization::make_nvp("alist", alist);
67        oa << boost::serialization::make_nvp("free_a_ptr", free_a_ptr);
68    }
69    std::list<A *> alist1;
70    {
71        test_istream is(testfile, TEST_STREAM_FLAGS);
72        test_iarchive ia(is);
73        A * free_a_ptr1;
74        ia >> boost::serialization::make_nvp("alist", alist1);
75        ia >> boost::serialization::make_nvp("free_a_ptr", free_a_ptr1);
76        BOOST_CHECK(
77            alist.size() == alist1.size() 
78            && std::equal(alist.begin(),alist.end(),alist1.begin(),ptr_equal_to<A *>())
79        );
80        // verify that first element is the same as the free pointer
81        BOOST_CHECK((*alist1.begin()) == free_a_ptr1);
82    }
83
84    std::for_each(
85        alist.begin(), 
86        alist.end(), 
87        boost::checked_deleter<A>()
88    );
89    std::for_each(
90        alist1.begin(), 
91        alist1.end(), 
92        boost::checked_deleter<A>()
93    );
94   
95    #ifdef BOOST_HAS_SLIST
96    std::list<A *> aslist;
97    {   
98        aslist.push_back(new A);
99        aslist.push_back(new A);
100        test_ostream os(testfile, TEST_STREAM_FLAGS);
101        test_oarchive oa(os);
102        aslist.push_back(new A);
103        aslist.push_back(new A);
104        oa << boost::serialization::make_nvp("aslist", aslist);
105    }
106    std::list<A *> aslist1;
107    {
108        test_istream is(testfile, TEST_STREAM_FLAGS);
109        test_iarchive ia(is);
110        ia >> boost::serialization::make_nvp("aslist", aslist1);
111        BOOST_CHECK(aslist.size() == aslist1.size() &&
112            std::equal(aslist.begin(),aslist.end(),aslist1.begin(),ptr_equal_to<A *>())
113        );
114    }
115    std::for_each(
116        aslist.begin(), 
117        aslist.end(), 
118        boost::checked_deleter<A>()
119    );
120    std::for_each(
121        aslist1.begin(), 
122        aslist1.end(), 
123        boost::checked_deleter<A>()
124    ); 
125    #endif
126    std::remove(testfile);
127    return EXIT_SUCCESS;
128}
129
130// EOF
Note: See TracBrowser for help on using the repository browser.