test_native_array.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "A.hpp"
  23. #include "A.ipp"
  24. template <class T>
  25. int test_native_array(){
  26. const char * testfile = boost::archive::tmpnam(NULL);
  27. BOOST_REQUIRE(NULL != testfile);
  28. // test array of objects
  29. const T a_array[10]={T(),T(),T(),T(),T(),T(),T(),T(),T(),T()};
  30. const T b_array[2][3]={{T(),T(),T()},{T(),T(),T()}};
  31. {
  32. test_ostream os(testfile, TEST_STREAM_FLAGS);
  33. {
  34. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  35. oa << boost::serialization::make_nvp("a_array", a_array);
  36. oa << boost::serialization::make_nvp("b_array", b_array);
  37. }
  38. os.close();
  39. }
  40. {
  41. T a_array1[10];
  42. T b_array1[2][3];
  43. test_istream is(testfile, TEST_STREAM_FLAGS);
  44. {
  45. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  46. ia >> boost::serialization::make_nvp("a_array", a_array1);
  47. ia >> boost::serialization::make_nvp("b_array", b_array1);
  48. }
  49. is.close();
  50. BOOST_CHECK(std::equal(& a_array[0], & a_array[10], & a_array1[0]));
  51. BOOST_CHECK(b_array[0][0] == b_array1[0][0]);
  52. BOOST_CHECK(b_array[1][0] == b_array1[1][0]);
  53. }
  54. {
  55. T a_array1[9];
  56. T b_array1[2][3];
  57. test_istream is(testfile, TEST_STREAM_FLAGS);
  58. {
  59. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  60. bool exception_invoked = false;
  61. BOOST_TRY {
  62. ia >> boost::serialization::make_nvp("a_array", a_array1);
  63. ia >> boost::serialization::make_nvp("b_array", b_array1);
  64. }
  65. BOOST_CATCH (boost::archive::archive_exception const& ae){
  66. BOOST_CHECK(
  67. boost::archive::archive_exception::array_size_too_short
  68. == ae.code
  69. );
  70. exception_invoked = true;
  71. }
  72. BOOST_CATCH_END
  73. BOOST_CHECK(exception_invoked);
  74. }
  75. is.close();
  76. }
  77. std::remove(testfile);
  78. return EXIT_SUCCESS;
  79. }
  80. int test_main( int /* argc */, char* /* argv */[] ){
  81. int res;
  82. // native array
  83. res = test_native_array<A>();
  84. if (res != EXIT_SUCCESS)
  85. return EXIT_FAILURE;
  86. // test an int array for which optimized versions should be available
  87. res = test_native_array<int>();
  88. if (res != EXIT_SUCCESS)
  89. return EXIT_FAILURE;
  90. return EXIT_SUCCESS;
  91. }
  92. // EOF