1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
2 | // test_vector.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> // NULL |
12 | #include <fstream> |
13 | |
14 | #include <cstdio> // remove |
15 | #include <boost/config.hpp> |
16 | #if defined(BOOST_NO_STDC_NAMESPACE) |
17 | namespace std{ |
18 | using ::remove; |
19 | } |
20 | #endif |
21 | #include <boost/static_assert.hpp> |
22 | #include "test_tools.hpp" |
23 | |
24 | #include <boost/serialization/vector.hpp> |
25 | |
26 | // normal class with default constructor |
27 | #include "A.hpp" |
28 | #include "A.ipp" |
29 | |
30 | template <class T> |
31 | int test_vector_detail(const std::vector<T> & avector) |
32 | { |
33 | const char * testfile = boost::archive::tmpnam(NULL); |
34 | BOOST_REQUIRE(NULL != testfile); |
35 | |
36 | // test array of objects |
37 | { |
38 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
39 | test_oarchive oa(os, TEST_ARCHIVE_FLAGS); |
40 | oa << boost::serialization::make_nvp("avector" , avector); |
41 | } |
42 | std::vector< T > avector1; |
43 | { |
44 | test_istream is(testfile, TEST_STREAM_FLAGS); |
45 | test_iarchive ia(is, TEST_ARCHIVE_FLAGS); |
46 | ia >> boost::serialization::make_nvp("avector" , avector1); |
47 | } |
48 | BOOST_CHECK(avector == avector1); |
49 | std::remove(filename: testfile); |
50 | return EXIT_SUCCESS; |
51 | } |
52 | |
53 | template <class T> |
54 | int test_default_constructible() |
55 | { |
56 | // test array of objects |
57 | std::vector<T> avector; |
58 | avector.push_back(T()); |
59 | avector.push_back(T()); |
60 | return test_vector_detail(avector); |
61 | } |
62 | |
63 | // class without default constructor |
64 | struct X { |
65 | //BOOST_DELETED_FUNCTION(X()); |
66 | public: |
67 | int m_i; |
68 | X(const X & x) : |
69 | m_i(x.m_i) |
70 | {} |
71 | X(const int & i) : |
72 | m_i(i) |
73 | {} |
74 | bool operator==(const X & rhs) const { |
75 | return m_i == rhs.m_i; |
76 | } |
77 | template<class Archive> |
78 | void serialize(Archive & ar, const unsigned int /*version*/){ |
79 | ar & BOOST_SERIALIZATION_NVP(m_i); |
80 | } |
81 | }; |
82 | |
83 | template<class Archive> |
84 | inline void save_construct_data( |
85 | Archive & ar, |
86 | const X * x, |
87 | const unsigned int /* file_version */ |
88 | ){ |
89 | // variable used for construction |
90 | ar << boost::serialization::make_nvp(n: "i" , v: x->m_i); |
91 | } |
92 | |
93 | template<class Archive> |
94 | inline void load_construct_data( |
95 | Archive & ar, |
96 | X * x, |
97 | const unsigned int /* file_version */ |
98 | ){ |
99 | int i; |
100 | ar >> boost::serialization::make_nvp(n: "i" , v&: i); |
101 | ::new(x)X(i); |
102 | } |
103 | |
104 | int test_non_default_constructible() |
105 | { |
106 | // test array of objects |
107 | std::vector<X> avector; |
108 | avector.push_back(x: X(123)); |
109 | avector.push_back(x: X(456)); |
110 | return test_vector_detail(avector); |
111 | } |
112 | |
113 | int test_main( int /* argc */, char* /* argv */[] ) |
114 | { |
115 | int res; |
116 | res = test_default_constructible<A>(); |
117 | // test an int vector for which optimized versions should be available |
118 | if (res == EXIT_SUCCESS) |
119 | res = test_default_constructible<int>(); |
120 | // test a bool vector |
121 | if (res == EXIT_SUCCESS) |
122 | res = test_default_constructible<bool>(); |
123 | if (res == EXIT_SUCCESS) |
124 | res = test_non_default_constructible(); |
125 | return res; |
126 | } |
127 | |
128 | // EOF |
129 | |