test_boost_array.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_array.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 <stdlib.h>
  9. #include <boost/config.hpp>
  10. #include <cstddef>
  11. #include <fstream>
  12. #include <algorithm> // equal
  13. #include <cstdio> // remove
  14. #if defined(BOOST_NO_STDC_NAMESPACE)
  15. namespace std{
  16. using ::remove;
  17. }
  18. #endif
  19. #include "test_tools.hpp"
  20. #include <boost/core/no_exceptions_support.hpp>
  21. #include <boost/archive/archive_exception.hpp>
  22. #include <boost/serialization/boost_array.hpp>
  23. #include "A.hpp"
  24. #include "A.ipp"
  25. template <class T>
  26. int test_boost_array(){
  27. const char * testfile = boost::archive::tmpnam(NULL);
  28. BOOST_REQUIRE(NULL != testfile);
  29. // test array of objects
  30. const boost::array<T,10> a_array = {{T(),T(),T(),T(),T(),T(),T(),T(),T(),T()}};
  31. {
  32. test_ostream os(testfile, TEST_STREAM_FLAGS);
  33. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  34. oa << boost::serialization::make_nvp("a_array", a_array);
  35. }
  36. {
  37. boost::array<T,10> a_array1;
  38. test_istream is(testfile, TEST_STREAM_FLAGS);
  39. {
  40. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  41. ia >> boost::serialization::make_nvp("a_array", a_array1);
  42. }
  43. is.close();
  44. BOOST_CHECK(std::equal(a_array.begin(), a_array.end(), a_array1.begin()));
  45. }
  46. {
  47. boost::array<T, 9> a_array1;
  48. test_istream is(testfile, TEST_STREAM_FLAGS);
  49. {
  50. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  51. bool exception_invoked = false;
  52. BOOST_TRY {
  53. ia >> boost::serialization::make_nvp("a_array", a_array1);
  54. }
  55. BOOST_CATCH (boost::archive::archive_exception const& ae){
  56. BOOST_CHECK(
  57. boost::archive::archive_exception::array_size_too_short
  58. == ae.code
  59. );
  60. exception_invoked = true;
  61. }
  62. BOOST_CATCH_END
  63. BOOST_CHECK(exception_invoked);
  64. }
  65. is.close();
  66. }
  67. std::remove(testfile);
  68. return EXIT_SUCCESS;
  69. }
  70. int test_main( int /* argc */, char* /* argv */[] ){
  71. int res;
  72. // boost array
  73. res = test_boost_array<A>();
  74. if (res != EXIT_SUCCESS)
  75. return EXIT_FAILURE;
  76. // test an int array for which optimized versions should be available
  77. res = test_boost_array<int>();
  78. if (res != EXIT_SUCCESS)
  79. return EXIT_FAILURE;
  80. return EXIT_SUCCESS;
  81. }
  82. // EOF