Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/serialization/test/test_set.cpp @ 20

Last change on this file since 20 was 12, checked in by landauf, 18 years ago

added boost

File size: 5.3 KB
Line 
1/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2// test_set.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 <algorithm>
12#include <vector>
13#include <fstream>
14
15#include <cstdio> // remove
16#include <boost/config.hpp>
17
18#if defined (__LIBCOMO__)
19#error - this test fails on comeau with disatrous results
20#endif
21
22#include <boost/detail/workaround.hpp>
23#if defined(BOOST_NO_STDC_NAMESPACE)
24namespace std{ 
25    using ::remove;
26}
27#endif
28
29#include <boost/archive/archive_exception.hpp>
30
31#include "test_tools.hpp"
32#include <boost/preprocessor/stringize.hpp>
33#include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST)
34
35#include <boost/serialization/set.hpp>
36#ifdef BOOST_HAS_HASH
37#include <boost/serialization/hash_set.hpp>
38#endif
39#include "A.hpp"
40
41#if defined(__LIBCOMO__) || (defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION))
42
43namespace std {
44    template<>
45    struct less<A> {
46        bool operator()(const A & lhs, const A & rhs) const {
47            return lhs < rhs;
48        }
49    };
50    template<>
51    struct equal_to<A> {
52        bool operator()(const A & lhs, const A & rhs) const {
53            return lhs == rhs;
54        }
55    };
56}   // namespace std
57
58namespace BOOST_STD_EXTENSION_NAMESPACE {
59    template<>
60    struct hash<A> {
61        std::size_t operator()(const A& a) const {
62            return (std::size_t)a;
63        }
64    };
65}   // BOOST_STD_EXTENSION_NAMESPACE
66
67#endif
68
69int test_main( int /* argc */, char* /* argv */[] )
70{
71    const char * testfile = boost::archive::tmpnam(NULL);
72    BOOST_REQUIRE(NULL != testfile);
73
74    // test array of objects
75    std::set<A> aset;
76    aset.insert(A());
77    aset.insert(A());
78    {   
79        test_ostream os(testfile, TEST_STREAM_FLAGS);
80        test_oarchive oa(os);
81        oa << boost::serialization::make_nvp("aset", aset);
82    }
83    std::set<A> aset1;
84    {
85        test_istream is(testfile, TEST_STREAM_FLAGS);
86        test_iarchive ia(is);
87        ia >> boost::serialization::make_nvp("aset", aset1);
88    }
89    BOOST_CHECK(aset == aset1);
90   
91    std::multiset<A> amultiset;
92    amultiset.insert(A());
93    amultiset.insert(A());
94    {   
95        test_ostream os(testfile, TEST_STREAM_FLAGS);
96        test_oarchive oa(os);
97        oa << boost::serialization::make_nvp("amultiset", amultiset);
98    }
99    std::multiset<A> amultiset1;
100    {
101        test_istream is(testfile, TEST_STREAM_FLAGS);
102        test_iarchive ia(is);
103        ia >> boost::serialization::make_nvp("amultiset", amultiset1);
104    }
105    BOOST_CHECK(amultiset == amultiset1);
106   
107    #if defined(BOOST_HAS_HASH)
108    #if defined(_STLPORT_VERSION) \
109    && _STLPORT_VERSION \
110    && (__SGI_STL_PORT <= 0x453)
111        BOOST_FAIL( 
112            "Hash sets known to fail in a catastrophic way with STLPort 4.5.3,\n"
113            "test skipped" 
114        );
115    #else
116
117    // test array of objects
118    BOOST_STD_EXTENSION_NAMESPACE::hash_set<A> ahash_set;
119    ahash_set.insert(A());
120    ahash_set.insert(A());
121    {   
122        test_ostream os(testfile, TEST_STREAM_FLAGS);
123        test_oarchive oa(os);
124        oa << boost::serialization::make_nvp("ahash_set", ahash_set);
125    }
126    BOOST_STD_EXTENSION_NAMESPACE::hash_set<A> ahash_set1;
127    {
128        test_istream is(testfile, TEST_STREAM_FLAGS);
129        test_iarchive ia(is);
130        ia >> boost::serialization::make_nvp("ahash_set", ahash_set1);
131    }
132    // at least one library - MSL notes: it doesn't make much sense
133    // to implement the == operator for hash collections - but goes ahead
134    // does it anyway even though it doesn't seem to work.  So sort into
135    // vectors and then compare.
136    std::vector<A> tvec, tvec1;
137    std::copy(ahash_set.begin(), ahash_set.end(), std::back_inserter(tvec));
138    std::sort(tvec.begin(), tvec.end());
139    tvec1.reserve(ahash_set1.size());
140    std::copy(ahash_set1.begin(), ahash_set1.end(), std::back_inserter(tvec1));
141    std::sort(tvec1.begin(), tvec1.end());
142    BOOST_CHECK(tvec == tvec1);
143   
144    BOOST_STD_EXTENSION_NAMESPACE::hash_multiset<A> ahash_multiset;
145    ahash_multiset.insert(A());
146    ahash_multiset.insert(A());
147    {   
148        test_ostream os(testfile, TEST_STREAM_FLAGS);
149        test_oarchive oa(os);
150        oa << boost::serialization::make_nvp("ahash_multiset", ahash_multiset);
151    }
152    BOOST_STD_EXTENSION_NAMESPACE::hash_multiset<A> ahash_multiset1;
153    {
154        test_istream is(testfile, TEST_STREAM_FLAGS);
155        test_iarchive ia(is);
156        ia >> boost::serialization::make_nvp("ahash_multiset", ahash_multiset1);
157    }
158    // at least one library - MSL notes: it doesn't make much sense
159    // to implement the == operator for hash collections - but goes ahead
160    // does it anyway even though it doesn't seem to work.  So sort into
161    // vectors and then compare.
162    tvec.clear();
163    std::copy(ahash_multiset.begin(), ahash_multiset.end(), std::back_inserter(tvec));
164    std::sort(tvec.begin(), tvec.end());
165    tvec1.clear();
166    tvec1.reserve(ahash_multiset1.size());
167    std::copy(ahash_multiset1.begin(), ahash_multiset1.end(), std::back_inserter(tvec1));
168    std::sort(tvec1.begin(), tvec1.end());
169    BOOST_CHECK(tvec == tvec1);
170    #endif // STLPort 4.5.3
171    #endif
172
173    std::remove(testfile);
174    return EXIT_SUCCESS;
175}
Note: See TracBrowser for help on using the repository browser.