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 <cstddef> |
12 | #include <fstream> |
13 | |
14 | #include <boost/config.hpp> |
15 | #include <cstdio> // remove |
16 | #if defined(BOOST_NO_STDC_NAMESPACE) |
17 | namespace std{ |
18 | using ::remove; |
19 | } |
20 | #endif |
21 | |
22 | #include <boost/type_traits/is_pointer.hpp> |
23 | #include <boost/static_assert.hpp> |
24 | #include <boost/checked_delete.hpp> |
25 | |
26 | #include <boost/archive/archive_exception.hpp> |
27 | #include "test_tools.hpp" |
28 | |
29 | #include <boost/serialization/nvp.hpp> |
30 | |
31 | #include "A.hpp" |
32 | #include "A.ipp" |
33 | |
34 | template<class T> |
35 | struct ptr_equal_to { |
36 | BOOST_STATIC_ASSERT(::boost::is_pointer< T >::value); |
37 | bool operator()(T const _Left, T const _Right) const |
38 | { |
39 | if(NULL == _Left && NULL == _Right) |
40 | return true; |
41 | if(typeid(*_Left) != typeid(*_Right)) |
42 | return false; |
43 | return *_Left == *_Right; |
44 | } |
45 | }; |
46 | |
47 | #include <boost/serialization/list.hpp> |
48 | void test_list(){ |
49 | const char * testfile = boost::archive::tmpnam(NULL); |
50 | BOOST_REQUIRE(NULL != testfile); |
51 | |
52 | std::list<A *> alist; |
53 | { |
54 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
55 | test_oarchive oa(os, TEST_ARCHIVE_FLAGS); |
56 | A * free_a_ptr = new A; |
57 | alist.push_back(x: free_a_ptr); |
58 | alist.push_back(x: new A); |
59 | // verify that first element is the same as the free pointer |
60 | BOOST_CHECK((*alist.begin()) == free_a_ptr); |
61 | oa << boost::serialization::make_nvp(n: "alist" , v&: alist); |
62 | oa << boost::serialization::make_nvp(n: "free_a_ptr" , v&: free_a_ptr); |
63 | } |
64 | std::list<A *> alist1; |
65 | { |
66 | test_istream is(testfile, TEST_STREAM_FLAGS); |
67 | test_iarchive ia(is, TEST_ARCHIVE_FLAGS); |
68 | A * free_a_ptr1; |
69 | ia >> boost::serialization::make_nvp(n: "alist" , v&: alist1); |
70 | ia >> boost::serialization::make_nvp(n: "free_a_ptr" , v&: free_a_ptr1); |
71 | BOOST_CHECK( |
72 | alist.size() == alist1.size() |
73 | && std::equal(alist.begin(),alist.end(),alist1.begin(),ptr_equal_to<A *>()) |
74 | ); |
75 | // verify that first element is the same as the free pointer |
76 | BOOST_CHECK((*alist1.begin()) == free_a_ptr1); |
77 | } |
78 | |
79 | std::for_each( |
80 | first: alist.begin(), |
81 | last: alist.end(), |
82 | f: boost::checked_deleter<A>() |
83 | ); |
84 | std::for_each( |
85 | first: alist1.begin(), |
86 | last: alist1.end(), |
87 | f: boost::checked_deleter<A>() |
88 | ); |
89 | std::remove(filename: testfile); |
90 | } |
91 | |
92 | int test_main( int /* argc */, char* /* argv */[] ) |
93 | { |
94 | test_list(); |
95 | return EXIT_SUCCESS; |
96 | } |
97 | |
98 | // EOF |
99 | |