make_unique_array_test.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright 2014 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_SMART_PTR)
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <boost/smart_ptr/make_unique.hpp>
  11. class type {
  12. public:
  13. static unsigned instances;
  14. type() {
  15. ++instances;
  16. }
  17. ~type() {
  18. --instances;
  19. }
  20. private:
  21. type(const type&);
  22. type& operator=(const type&);
  23. };
  24. unsigned type::instances = 0;
  25. int main()
  26. {
  27. {
  28. std::unique_ptr<int[]> result = boost::make_unique<int[]>(3);
  29. BOOST_TEST(result.get() != 0);
  30. BOOST_TEST(result[0] == 0);
  31. BOOST_TEST(result[1] == 0);
  32. BOOST_TEST(result[2] == 0);
  33. }
  34. {
  35. std::unique_ptr<int[][2]> result =
  36. boost::make_unique<int[][2]>(2);
  37. BOOST_TEST(result.get() != 0);
  38. BOOST_TEST(result[0][0] == 0);
  39. BOOST_TEST(result[0][1] == 0);
  40. BOOST_TEST(result[1][0] == 0);
  41. BOOST_TEST(result[1][1] == 0);
  42. }
  43. {
  44. std::unique_ptr<const int[]> result =
  45. boost::make_unique<const int[]>(3);
  46. BOOST_TEST(result.get() != 0);
  47. BOOST_TEST(result[0] == 0);
  48. BOOST_TEST(result[1] == 0);
  49. BOOST_TEST(result[2] == 0);
  50. }
  51. {
  52. std::unique_ptr<const int[][2]> result =
  53. boost::make_unique<const int[][2]>(2);
  54. BOOST_TEST(result.get() != 0);
  55. BOOST_TEST(result[0][0] == 0);
  56. BOOST_TEST(result[0][1] == 0);
  57. BOOST_TEST(result[1][0] == 0);
  58. BOOST_TEST(result[1][1] == 0);
  59. }
  60. BOOST_TEST(type::instances == 0);
  61. {
  62. std::unique_ptr<type[]> result =
  63. boost::make_unique<type[]>(3);
  64. BOOST_TEST(result.get() != 0);
  65. BOOST_TEST(type::instances == 3);
  66. result.reset();
  67. BOOST_TEST(type::instances == 0);
  68. }
  69. BOOST_TEST(type::instances == 0);
  70. {
  71. std::unique_ptr<type[][2]> result =
  72. boost::make_unique<type[][2]>(2);
  73. BOOST_TEST(result.get() != 0);
  74. BOOST_TEST(type::instances == 4);
  75. result.reset();
  76. BOOST_TEST(type::instances == 0);
  77. }
  78. BOOST_TEST(type::instances == 0);
  79. {
  80. std::unique_ptr<const type[]> result =
  81. boost::make_unique<const type[]>(3);
  82. BOOST_TEST(result.get() != 0);
  83. BOOST_TEST(type::instances == 3);
  84. result.reset();
  85. BOOST_TEST(type::instances == 0);
  86. }
  87. BOOST_TEST(type::instances == 0);
  88. {
  89. std::unique_ptr<const type[][2]> result =
  90. boost::make_unique<const type[][2]>(2);
  91. BOOST_TEST(result.get() != 0);
  92. BOOST_TEST(type::instances == 4);
  93. result.reset();
  94. BOOST_TEST(type::instances == 0);
  95. }
  96. return boost::report_errors();
  97. }
  98. #else
  99. int main()
  100. {
  101. return 0;
  102. }
  103. #endif