make_unique_array_noinit_test.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 =
  29. boost::make_unique_noinit<int[]>(3);
  30. BOOST_TEST(result.get() != 0);
  31. }
  32. {
  33. std::unique_ptr<int[][2]> result =
  34. boost::make_unique_noinit<int[][2]>(2);
  35. BOOST_TEST(result.get() != 0);
  36. }
  37. BOOST_TEST(type::instances == 0);
  38. {
  39. std::unique_ptr<type[]> result =
  40. boost::make_unique_noinit<type[]>(3);
  41. BOOST_TEST(result.get() != 0);
  42. BOOST_TEST(type::instances == 3);
  43. result.reset();
  44. BOOST_TEST(type::instances == 0);
  45. }
  46. BOOST_TEST(type::instances == 0);
  47. {
  48. std::unique_ptr<type[][2]> result =
  49. boost::make_unique_noinit<type[][2]>(2);
  50. BOOST_TEST(result.get() != 0);
  51. BOOST_TEST(type::instances == 4);
  52. result.reset();
  53. BOOST_TEST(type::instances == 0);
  54. }
  55. BOOST_TEST(type::instances == 0);
  56. {
  57. std::unique_ptr<const type[]> result =
  58. boost::make_unique_noinit<const type[]>(3);
  59. BOOST_TEST(result.get() != 0);
  60. BOOST_TEST(type::instances == 3);
  61. result.reset();
  62. BOOST_TEST(type::instances == 0);
  63. }
  64. BOOST_TEST(type::instances == 0);
  65. {
  66. std::unique_ptr<const type[][2]> result =
  67. boost::make_unique_noinit<const type[][2]>(2);
  68. BOOST_TEST(result.get() != 0);
  69. BOOST_TEST(type::instances == 4);
  70. result.reset();
  71. BOOST_TEST(type::instances == 0);
  72. }
  73. return boost::report_errors();
  74. }
  75. #else
  76. int main()
  77. {
  78. return 0;
  79. }
  80. #endif