test_vector.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_vector.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 <fstream>
  10. #include <cstdio> // remove
  11. #include <boost/config.hpp>
  12. #if defined(BOOST_NO_STDC_NAMESPACE)
  13. namespace std{
  14. using ::remove;
  15. }
  16. #endif
  17. #include <boost/static_assert.hpp>
  18. #include "test_tools.hpp"
  19. #include <boost/serialization/vector.hpp>
  20. // normal class with default constructor
  21. #include "A.hpp"
  22. #include "A.ipp"
  23. template <class T>
  24. int test_vector_detail(const std::vector<T> & avector)
  25. {
  26. const char * testfile = boost::archive::tmpnam(NULL);
  27. BOOST_REQUIRE(NULL != testfile);
  28. // test array of objects
  29. {
  30. test_ostream os(testfile, TEST_STREAM_FLAGS);
  31. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  32. oa << boost::serialization::make_nvp("avector", avector);
  33. }
  34. std::vector< T > avector1;
  35. {
  36. test_istream is(testfile, TEST_STREAM_FLAGS);
  37. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  38. ia >> boost::serialization::make_nvp("avector", avector1);
  39. }
  40. BOOST_CHECK(avector == avector1);
  41. std::remove(testfile);
  42. return EXIT_SUCCESS;
  43. }
  44. template <class T>
  45. int test_default_constructible()
  46. {
  47. // test array of objects
  48. std::vector<T> avector;
  49. avector.push_back(T());
  50. avector.push_back(T());
  51. return test_vector_detail(avector);
  52. }
  53. // class without default constructor
  54. struct X {
  55. //BOOST_DELETED_FUNCTION(X());
  56. public:
  57. int m_i;
  58. X(const X & x) :
  59. m_i(x.m_i)
  60. {}
  61. X(const int & i) :
  62. m_i(i)
  63. {}
  64. bool operator==(const X & rhs) const {
  65. return m_i == rhs.m_i;
  66. }
  67. template<class Archive>
  68. void serialize(Archive & ar, const unsigned int /*version*/){
  69. ar & BOOST_SERIALIZATION_NVP(m_i);
  70. }
  71. };
  72. template<class Archive>
  73. inline void save_construct_data(
  74. Archive & ar,
  75. const X * x,
  76. const unsigned int /* file_version */
  77. ){
  78. // variable used for construction
  79. ar << boost::serialization::make_nvp("i", x->m_i);
  80. }
  81. template<class Archive>
  82. inline void load_construct_data(
  83. Archive & ar,
  84. X * x,
  85. const unsigned int /* file_version */
  86. ){
  87. int i;
  88. ar >> boost::serialization::make_nvp("i", i);
  89. ::new(x)X(i);
  90. }
  91. int test_non_default_constructible()
  92. {
  93. // test array of objects
  94. std::vector<X> avector;
  95. avector.push_back(X(123));
  96. avector.push_back(X(456));
  97. return test_vector_detail(avector);
  98. }
  99. int test_main( int /* argc */, char* /* argv */[] )
  100. {
  101. int res;
  102. res = test_default_constructible<A>();
  103. // test an int vector for which optimized versions should be available
  104. if (res == EXIT_SUCCESS)
  105. res = test_default_constructible<int>();
  106. // test a bool vector
  107. if (res == EXIT_SUCCESS)
  108. res = test_default_constructible<bool>();
  109. if (res == EXIT_SUCCESS)
  110. res = test_non_default_constructible();
  111. return res;
  112. }
  113. // EOF