peformance_array.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <fstream>
  9. #include <cstdio> // remove
  10. #include <boost/config.hpp>
  11. #if defined(BOOST_NO_STDC_NAMESPACE)
  12. namespace std{
  13. using ::remove;
  14. }
  15. #endif
  16. #include "../test/test_tools.hpp"
  17. #include <boost/preprocessor/stringize.hpp>
  18. //#include <boost/preprocessor/cat.hpp>
  19. // the following fails with (only!) gcc 3.4
  20. // #include BOOST_PP_STRINGIZE(BOOST_PP_CAT(../test/,BOOST_ARCHIVE_TEST))
  21. // just copy over the files from the test directory
  22. #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST)
  23. #include <boost/detail/no_exceptions_support.hpp>
  24. #include <boost/archive/archive_exception.hpp>
  25. #include <boost/serialization/nvp.hpp>
  26. #include "../test/A.hpp"
  27. #include "../test/A.ipp"
  28. struct array_equal_to //: public std::binary_function<T, T, bool>
  29. {
  30. template<class T, class U>
  31. bool operator()(const T & _Left, const U & _Right) const
  32. {
  33. // consider alignment
  34. int count_left = sizeof(_Left) / (
  35. static_cast<const char *>(static_cast<const void *>(&_Left[1]))
  36. - static_cast<const char *>(static_cast<const void *>(&_Left[0]))
  37. );
  38. int count_right = sizeof(_Right) / (
  39. static_cast<const char *>(static_cast<const void *>(&_Right[1]))
  40. - static_cast<const char *>(static_cast<const void *>(&_Right[0]))
  41. );
  42. if(count_right != count_left)
  43. return false;
  44. while(count_left-- > 0){
  45. if(_Left[count_left] == _Right[count_left])
  46. continue;
  47. return false;
  48. }
  49. return true;
  50. }
  51. };
  52. template <class T>
  53. int test_array(T)
  54. {
  55. const char * testfile = boost::archive::tmpnam(NULL);
  56. BOOST_REQUIRE(NULL != testfile);
  57. // test array of objects
  58. const T a_array[10]={T(),T(),T(),T(),T(),T(),T(),T(),T(),T()};
  59. {
  60. test_ostream os(testfile, TEST_STREAM_FLAGS);
  61. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  62. oa << boost::serialization::make_nvp("a_array", a_array);
  63. }
  64. {
  65. T a_array1[10];
  66. test_istream is(testfile, TEST_STREAM_FLAGS);
  67. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  68. ia >> boost::serialization::make_nvp("a_array", a_array1);
  69. array_equal_to/*<A[10]>*/ Compare;
  70. BOOST_CHECK(Compare(a_array, a_array1));
  71. }
  72. {
  73. T a_array1[9];
  74. test_istream is(testfile, TEST_STREAM_FLAGS);
  75. BOOST_TRY {
  76. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  77. bool exception_invoked = false;
  78. BOOST_TRY {
  79. ia >> boost::serialization::make_nvp("a_array", a_array1);
  80. }
  81. BOOST_CATCH (boost::archive::archive_exception ae){
  82. BOOST_CHECK(
  83. boost::archive::archive_exception::array_size_too_short
  84. == ae.code
  85. );
  86. exception_invoked = true;
  87. }
  88. BOOST_CATCH_END
  89. BOOST_CHECK(exception_invoked);
  90. }
  91. BOOST_CATCH (boost::archive::archive_exception ae){}
  92. BOOST_CATCH_END
  93. }
  94. std::remove(testfile);
  95. return EXIT_SUCCESS;
  96. }
  97. int test_main( int /* argc */, char* /* argv */[] )
  98. {
  99. int res = test_array(A());
  100. // test an int array for which optimized versions should be available
  101. if (res == EXIT_SUCCESS)
  102. res = test_array(0);
  103. return res;
  104. }
  105. // EOF