test_polymorphic.cpp 3.0 KB

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