Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/serialization/test/test_binary.cpp @ 12

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

added boost

File size: 2.4 KB
Line 
1/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2// test_simple_class.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 <cstdlib> // for rand()
14#include <boost/config.hpp>
15#if defined(BOOST_NO_STDC_NAMESPACE)
16namespace std{ 
17    using ::rand; 
18    using ::remove;
19}
20#endif
21
22#include "test_tools.hpp"
23#include <boost/preprocessor/stringize.hpp>
24#include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST)
25
26#include <boost/serialization/nvp.hpp>
27#include <boost/serialization/binary_object.hpp>
28
29class A {
30    friend class boost::serialization::access;
31    char data[150];
32    // note: from an aesthetic perspective, I would much prefer to have this
33    // defined out of line.  Unfortunately, this trips a bug in the VC 6.0
34    // compiler. So hold our nose and put it her to permit running of tests.
35    template<class Archive>
36    void serialize(Archive & ar, const unsigned int /* file_version */){
37        ar & boost::serialization::make_nvp(
38            "data",
39            boost::serialization::make_binary_object(data, sizeof(data))
40        );
41    }
42
43public:
44    A();
45    bool operator==(const A & rhs) const;
46};
47
48A::A(){
49    int i = sizeof(data);
50    while(i-- > 0)
51        data[i] = 0xff & std::rand();
52}
53
54bool A::operator==(const A & rhs) const {
55    int i = sizeof(data);
56    while(i-- > 0)
57        if(data[i] != rhs.data[i])
58            return false;
59    return true;
60}
61
62int test_main( int /* argc */, char* /* argv */[] )
63{
64    const char * testfile = boost::archive::tmpnam(NULL);
65    BOOST_REQUIRE(NULL != testfile);
66
67    const A a;
68    A a1;
69    const int i = 12345;
70    int i1 = 34790;
71    {   
72        test_ostream os(testfile, TEST_STREAM_FLAGS);
73        test_oarchive oa(os);
74        oa << BOOST_SERIALIZATION_NVP(a);
75        // note: add a little bit on the end of the archive to detect
76        // failure of text mode binary.
77        oa << BOOST_SERIALIZATION_NVP(i);
78    }
79    {
80        test_istream is(testfile, TEST_STREAM_FLAGS);
81        test_iarchive ia(is);
82        ia >> BOOST_SERIALIZATION_NVP(a1);
83        ia >> BOOST_SERIALIZATION_NVP(i1);
84    }
85    BOOST_CHECK(i == i1);
86    BOOST_CHECK(a == a1);
87    std::remove(testfile);
88    return EXIT_SUCCESS;
89}
90
91// EOF
Note: See TracBrowser for help on using the repository browser.