allocate_shared_arrays_test.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright 2012-2015 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <boost/config.hpp>
  8. #if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
  9. #include <boost/core/lightweight_test.hpp>
  10. #include <boost/smart_ptr/make_shared.hpp>
  11. template<class T = void>
  12. struct creator {
  13. typedef T value_type;
  14. template<class U>
  15. struct rebind {
  16. typedef creator<U> other;
  17. };
  18. creator() { }
  19. template<class U>
  20. creator(const creator<U>&) { }
  21. T* allocate(std::size_t size) {
  22. return static_cast<T*>(::operator new(sizeof(T) * size));
  23. }
  24. void deallocate(T* ptr, std::size_t) {
  25. ::operator delete(ptr);
  26. }
  27. };
  28. template<class T, class U>
  29. inline bool
  30. operator==(const creator<T>&, const creator<U>&)
  31. {
  32. return true;
  33. }
  34. template<class T, class U>
  35. inline bool
  36. operator!=(const creator<T>&, const creator<U>&)
  37. {
  38. return false;
  39. }
  40. int main()
  41. {
  42. {
  43. boost::shared_ptr<int[][2]> result =
  44. boost::allocate_shared<int[][2]>(creator<int>(), 2, {0, 1});
  45. BOOST_TEST(result[0][0] == 0);
  46. BOOST_TEST(result[0][1] == 1);
  47. BOOST_TEST(result[1][0] == 0);
  48. BOOST_TEST(result[1][1] == 1);
  49. }
  50. {
  51. boost::shared_ptr<int[2][2]> result =
  52. boost::allocate_shared<int[2][2]>(creator<int>(), {0, 1});
  53. BOOST_TEST(result[0][0] == 0);
  54. BOOST_TEST(result[0][1] == 1);
  55. BOOST_TEST(result[1][0] == 0);
  56. BOOST_TEST(result[1][1] == 1);
  57. }
  58. {
  59. boost::shared_ptr<const int[][2]> result =
  60. boost::allocate_shared<const int[][2]>(creator<>(), 2, {0, 1});
  61. BOOST_TEST(result[0][0] == 0);
  62. BOOST_TEST(result[0][1] == 1);
  63. BOOST_TEST(result[1][0] == 0);
  64. BOOST_TEST(result[1][1] == 1);
  65. }
  66. {
  67. boost::shared_ptr<const int[2][2]> result =
  68. boost::allocate_shared<const int[2][2]>(creator<>(), {0, 1});
  69. BOOST_TEST(result[0][0] == 0);
  70. BOOST_TEST(result[0][1] == 1);
  71. BOOST_TEST(result[1][0] == 0);
  72. BOOST_TEST(result[1][1] == 1);
  73. }
  74. return boost::report_errors();
  75. }
  76. #else
  77. int main()
  78. {
  79. return 0;
  80. }
  81. #endif