performance_polymorphic.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_polymorphic.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 <cstdio> // remove
  10. #include <boost/config.hpp>
  11. #if defined(BOOST_NO_STDC_NAMESPACE)
  12. namespace std{
  13. using ::remove;
  14. }
  15. #endif
  16. #include "../test/test_tools.hpp"
  17. #if !defined(BOOST_ARCHIVE_TEST)
  18. #define BOOST_ARCHIVE_TEST polymorphic_text_archive.hpp
  19. #endif
  20. // the following is to ensure that when one of the libraries changes
  21. // BJAM rebuilds and relinks the test.
  22. /*
  23. #include "polymorphic_text_archive.hpp"
  24. #include "polymorphic_text_warchive.hpp"
  25. #include "polymorphic_binary_archive.hpp"
  26. #include "polymorphic_xml_archive.hpp"
  27. #include "polymorphic_xml_warchive.hpp"
  28. */
  29. #include <boost/preprocessor/stringize.hpp>
  30. // #include <boost/preprocessor/cat.hpp>
  31. // the following fails with (only!) gcc 3.4
  32. // #include BOOST_PP_STRINGIZE(BOOST_PP_CAT(../test/,BOOST_ARCHIVE_TEST))
  33. // just copy over the files from the test directory
  34. #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST)
  35. #include <boost/archive/polymorphic_oarchive.hpp>
  36. #include <boost/archive/polymorphic_iarchive.hpp>
  37. #include <boost/serialization/nvp.hpp>
  38. #include "../test/test_polymorphic_A.hpp"
  39. int test_main(int /* argc */, char * /* argv */ [])
  40. {
  41. const char * testfile = boost::archive::tmpnam(NULL);
  42. BOOST_REQUIRE(NULL != testfile);
  43. const data d;
  44. data d1;
  45. // test using using polymorphic interface
  46. {
  47. test_ostream os(testfile, TEST_STREAM_FLAGS);
  48. test_oarchive oa_implementation(os);
  49. boost::archive::polymorphic_oarchive & oa_interface = oa_implementation;
  50. oa_interface << BOOST_SERIALIZATION_NVP(d);
  51. }
  52. {
  53. test_istream is(testfile, TEST_STREAM_FLAGS);
  54. test_iarchive ia_implementation(is);
  55. boost::archive::polymorphic_iarchive & ia_interface = ia_implementation;
  56. ia_interface >> BOOST_SERIALIZATION_NVP(d1);
  57. }
  58. BOOST_CHECK(d == d1);
  59. std::remove(testfile);
  60. // test using using polymorphic implementation.
  61. {
  62. test_ostream os(testfile, TEST_STREAM_FLAGS);
  63. test_oarchive oa_implementation(os);
  64. oa_implementation << BOOST_SERIALIZATION_NVP(d);
  65. }
  66. {
  67. test_istream is(testfile, TEST_STREAM_FLAGS);
  68. test_iarchive ia_implementation(is);
  69. ia_implementation >> BOOST_SERIALIZATION_NVP(d1);
  70. }
  71. BOOST_CHECK(d == d1);
  72. std::remove(testfile);
  73. // test using using polymorphic implementation.
  74. {
  75. test_ostream os(testfile, TEST_STREAM_FLAGS);
  76. boost::archive::polymorphic_oarchive * oa_implementation
  77. = new test_oarchive(os);
  78. *oa_implementation << BOOST_SERIALIZATION_NVP(d);
  79. delete oa_implementation;
  80. }
  81. {
  82. test_istream is(testfile, TEST_STREAM_FLAGS);
  83. boost::archive::polymorphic_iarchive * ia_implementation
  84. = new test_iarchive(is);
  85. *ia_implementation >> BOOST_SERIALIZATION_NVP(d1);
  86. delete ia_implementation;
  87. }
  88. BOOST_CHECK(d == d1);
  89. std::remove(testfile);
  90. return EXIT_SUCCESS;
  91. }