allocate_local_shared_array_value_test.cpp 2.2 KB

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