test_list_ptrs.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/list.hpp>
  38. void test_list(){
  39. const char * testfile = boost::archive::tmpnam(NULL);
  40. BOOST_REQUIRE(NULL != testfile);
  41. std::list<A *> alist;
  42. {
  43. test_ostream os(testfile, TEST_STREAM_FLAGS);
  44. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  45. A * free_a_ptr = new A;
  46. alist.push_back(free_a_ptr);
  47. alist.push_back(new A);
  48. // verify that first element is the same as the free pointer
  49. BOOST_CHECK((*alist.begin()) == free_a_ptr);
  50. oa << boost::serialization::make_nvp("alist", alist);
  51. oa << boost::serialization::make_nvp("free_a_ptr", free_a_ptr);
  52. }
  53. std::list<A *> alist1;
  54. {
  55. test_istream is(testfile, TEST_STREAM_FLAGS);
  56. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  57. A * free_a_ptr1;
  58. ia >> boost::serialization::make_nvp("alist", alist1);
  59. ia >> boost::serialization::make_nvp("free_a_ptr", free_a_ptr1);
  60. BOOST_CHECK(
  61. alist.size() == alist1.size()
  62. && std::equal(alist.begin(),alist.end(),alist1.begin(),ptr_equal_to<A *>())
  63. );
  64. // verify that first element is the same as the free pointer
  65. BOOST_CHECK((*alist1.begin()) == free_a_ptr1);
  66. }
  67. std::for_each(
  68. alist.begin(),
  69. alist.end(),
  70. boost::checked_deleter<A>()
  71. );
  72. std::for_each(
  73. alist1.begin(),
  74. alist1.end(),
  75. boost::checked_deleter<A>()
  76. );
  77. std::remove(testfile);
  78. }
  79. int test_main( int /* argc */, char* /* argv */[] )
  80. {
  81. test_list();
  82. return EXIT_SUCCESS;
  83. }
  84. // EOF