performance_binary.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_simple_class.cpp
  3. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  4. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // should pass compilation and execution
  8. #include <fstream>
  9. #include <cstdlib> // for rand()
  10. #include <cstdio> // remove
  11. #include <boost/config.hpp>
  12. #if defined(BOOST_NO_STDC_NAMESPACE)
  13. namespace std{
  14. using ::rand;
  15. using ::remove;
  16. }
  17. #endif
  18. #include "../test/test_tools.hpp"
  19. #include <boost/preprocessor/stringize.hpp>
  20. // #include <boost/preprocessor/cat.hpp>
  21. // the following fails with (only!) gcc 3.4
  22. // #include BOOST_PP_STRINGIZE(BOOST_PP_CAT(../test/,BOOST_ARCHIVE_TEST))
  23. // just copy over the files from the test directory
  24. #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST)
  25. #include <boost/serialization/nvp.hpp>
  26. #include <boost/serialization/binary_object.hpp>
  27. class A {
  28. friend class boost::serialization::access;
  29. char data[150];
  30. // note: from an aesthetic perspective, I would much prefer to have this
  31. // defined out of line. Unfortunately, this trips a bug in the VC 6.0
  32. // compiler. So hold our nose and put it her to permit running of tests.
  33. template<class Archive>
  34. void serialize(Archive & ar, const unsigned int /* file_version */){
  35. ar & boost::serialization::make_nvp(
  36. "data",
  37. boost::serialization::make_binary_object(data, sizeof(data))
  38. );
  39. }
  40. public:
  41. A();
  42. bool operator==(const A & rhs) const;
  43. };
  44. A::A(){
  45. int i = sizeof(data);
  46. while(i-- > 0)
  47. data[i] = 0xff & std::rand();
  48. }
  49. bool A::operator==(const A & rhs) const {
  50. int i = sizeof(data);
  51. while(i-- > 0)
  52. if(data[i] != rhs.data[i])
  53. return false;
  54. return true;
  55. }
  56. int test_main( int /* argc */, char* /* argv */[] )
  57. {
  58. const char * testfile = boost::archive::tmpnam(NULL);
  59. BOOST_REQUIRE(NULL != testfile);
  60. const A a;
  61. A a1;
  62. const int i = 12345;
  63. int i1 = 34790;
  64. {
  65. test_ostream os(testfile, TEST_STREAM_FLAGS);
  66. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  67. oa << BOOST_SERIALIZATION_NVP(a);
  68. // note: add a little bit on the end of the archive to detect
  69. // failure of text mode binary.
  70. oa << BOOST_SERIALIZATION_NVP(i);
  71. }
  72. {
  73. test_istream is(testfile, TEST_STREAM_FLAGS);
  74. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  75. ia >> BOOST_SERIALIZATION_NVP(a1);
  76. ia >> BOOST_SERIALIZATION_NVP(i1);
  77. }
  78. BOOST_CHECK(i == i1);
  79. BOOST_CHECK(a == a1);
  80. std::remove(testfile);
  81. return EXIT_SUCCESS;
  82. }
  83. // EOF