test_slist_ptrs.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_list.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>
  9. #include <fstream>
  10. #include <boost/config.hpp>
  11. #include <cstdio> // remove
  12. #if defined(BOOST_NO_STDC_NAMESPACE)
  13. namespace std{
  14. using ::remove;
  15. }
  16. #endif
  17. #include <boost/type_traits/is_pointer.hpp>
  18. #include <boost/static_assert.hpp>
  19. #include <boost/checked_delete.hpp>
  20. #include <boost/archive/archive_exception.hpp>
  21. #include "test_tools.hpp"
  22. #include <boost/serialization/nvp.hpp>
  23. #include "A.hpp"
  24. #include "A.ipp"
  25. template<class T>
  26. struct ptr_equal_to {
  27. BOOST_STATIC_ASSERT(::boost::is_pointer< T >::value);
  28. bool operator()(T const _Left, T const _Right) const
  29. {
  30. if(NULL == _Left && NULL == _Right)
  31. return true;
  32. if(typeid(*_Left) != typeid(*_Right))
  33. return false;
  34. return *_Left == *_Right;
  35. }
  36. };
  37. #include <boost/serialization/slist.hpp>
  38. void test_slist(){
  39. const char * testfile = boost::archive::tmpnam(NULL);
  40. BOOST_REQUIRE(NULL != testfile);
  41. BOOST_STD_EXTENSION_NAMESPACE::slist<A *> aslist;
  42. {
  43. test_ostream os(testfile, TEST_STREAM_FLAGS);
  44. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  45. aslist.push_front(new A);
  46. aslist.push_front(new A);
  47. oa << boost::serialization::make_nvp("aslist", aslist);
  48. }
  49. BOOST_STD_EXTENSION_NAMESPACE::slist<A *> aslist1;
  50. {
  51. test_istream is(testfile, TEST_STREAM_FLAGS);
  52. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  53. ia >> boost::serialization::make_nvp("aslist", aslist1);
  54. BOOST_CHECK(aslist.size() == aslist1.size() &&
  55. std::equal(aslist.begin(),aslist.end(),aslist1.begin(),ptr_equal_to<A *>())
  56. );
  57. }
  58. std::for_each(
  59. aslist.begin(),
  60. aslist.end(),
  61. boost::checked_deleter<A>()
  62. );
  63. std::for_each(
  64. aslist1.begin(),
  65. aslist1.end(),
  66. boost::checked_deleter<A>()
  67. );
  68. std::remove(testfile);
  69. }
  70. int test_main( int /* argc */, char* /* argv */[] )
  71. {
  72. test_slist();
  73. return EXIT_SUCCESS;
  74. }
  75. // EOF