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