allocate_local_shared_arrays_test.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Copyright 2017 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_RVALUE_REFERENCES) && \
  9. !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
  10. !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
  11. #include <boost/core/lightweight_test.hpp>
  12. #include <boost/smart_ptr/make_local_shared.hpp>
  13. template<class T = void>
  14. struct creator {
  15. typedef T value_type;
  16. template<class U>
  17. struct rebind {
  18. typedef creator<U> other;
  19. };
  20. creator() { }
  21. template<class U>
  22. creator(const creator<U>&) { }
  23. T* allocate(std::size_t size) {
  24. return static_cast<T*>(::operator new(sizeof(T) * size));
  25. }
  26. void deallocate(T* ptr, std::size_t) {
  27. ::operator delete(ptr);
  28. }
  29. };
  30. template<class T, class U>
  31. inline bool
  32. operator==(const creator<T>&, const creator<U>&)
  33. {
  34. return true;
  35. }
  36. template<class T, class U>
  37. inline bool
  38. operator!=(const creator<T>&, const creator<U>&)
  39. {
  40. return false;
  41. }
  42. int main()
  43. {
  44. {
  45. boost::local_shared_ptr<int[][2]> result =
  46. boost::allocate_local_shared<int[][2]>(creator<int>(), 2, {0, 1});
  47. BOOST_TEST(result[0][0] == 0);
  48. BOOST_TEST(result[0][1] == 1);
  49. BOOST_TEST(result[1][0] == 0);
  50. BOOST_TEST(result[1][1] == 1);
  51. }
  52. {
  53. boost::local_shared_ptr<int[2][2]> result =
  54. boost::allocate_local_shared<int[2][2]>(creator<int>(), {0, 1});
  55. BOOST_TEST(result[0][0] == 0);
  56. BOOST_TEST(result[0][1] == 1);
  57. BOOST_TEST(result[1][0] == 0);
  58. BOOST_TEST(result[1][1] == 1);
  59. }
  60. {
  61. boost::local_shared_ptr<const int[][2]> result =
  62. boost::allocate_local_shared<const int[][2]>(creator<>(), 2, {0, 1});
  63. BOOST_TEST(result[0][0] == 0);
  64. BOOST_TEST(result[0][1] == 1);
  65. BOOST_TEST(result[1][0] == 0);
  66. BOOST_TEST(result[1][1] == 1);
  67. }
  68. {
  69. boost::local_shared_ptr<const int[2][2]> result =
  70. boost::allocate_local_shared<const int[2][2]>(creator<>(), {0, 1});
  71. BOOST_TEST(result[0][0] == 0);
  72. BOOST_TEST(result[0][1] == 1);
  73. BOOST_TEST(result[1][0] == 0);
  74. BOOST_TEST(result[1][1] == 1);
  75. }
  76. return boost::report_errors();
  77. }
  78. #else
  79. int main()
  80. {
  81. return 0;
  82. }
  83. #endif