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