test_array.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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/serialization/array.hpp>
  21. #include <boost/core/no_exceptions_support.hpp>
  22. #include <boost/archive/archive_exception.hpp>
  23. #include "A.hpp"
  24. #include "A.ipp"
  25. template <class T>
  26. int test_std_array(){
  27. const char * testfile = boost::archive::tmpnam(NULL);
  28. BOOST_REQUIRE(NULL != testfile);
  29. // test array of objects
  30. const std::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. std::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. BOOST_CHECK(std::equal(a_array.begin(), a_array.end(), a_array1.begin()));
  44. }
  45. {
  46. std::array<T, 9> a_array1;
  47. test_istream is(testfile, TEST_STREAM_FLAGS);
  48. {
  49. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  50. bool exception_invoked = false;
  51. BOOST_TRY {
  52. ia >> boost::serialization::make_nvp("a_array", a_array1);
  53. }
  54. BOOST_CATCH (boost::archive::archive_exception const& ae){
  55. BOOST_CHECK(
  56. boost::archive::archive_exception::array_size_too_short
  57. == ae.code
  58. );
  59. exception_invoked = true;
  60. }
  61. BOOST_CATCH_END
  62. BOOST_CHECK(exception_invoked);
  63. }
  64. is.close();
  65. }
  66. std::remove(testfile);
  67. return EXIT_SUCCESS;
  68. }
  69. int test_main( int /* argc */, char* /* argv */[] ){
  70. int res;
  71. // std array
  72. res = test_std_array<A>();
  73. if (res != EXIT_SUCCESS)
  74. return EXIT_FAILURE;
  75. // test an int array for which optimized versions should be available
  76. res = test_std_array<int>();
  77. if (res != EXIT_SUCCESS)
  78. return EXIT_FAILURE;
  79. return EXIT_SUCCESS;
  80. }
  81. // EOF