allocate_shared_array_value_test.cpp 2.0 KB

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