make_unique_test.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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>();
  29. BOOST_TEST(result.get() != 0);
  30. BOOST_TEST(*result == 0);
  31. }
  32. {
  33. std::unique_ptr<const int> result =
  34. boost::make_unique<const int>();
  35. BOOST_TEST(result.get() != 0);
  36. BOOST_TEST(*result == 0);
  37. }
  38. BOOST_TEST(type::instances == 0);
  39. {
  40. std::unique_ptr<type> result =
  41. boost::make_unique<type>();
  42. BOOST_TEST(result.get() != 0);
  43. BOOST_TEST(type::instances == 1);
  44. result.reset();
  45. BOOST_TEST(type::instances == 0);
  46. }
  47. BOOST_TEST(type::instances == 0);
  48. {
  49. std::unique_ptr<const type> result =
  50. boost::make_unique<const type>();
  51. BOOST_TEST(result.get() != 0);
  52. BOOST_TEST(type::instances == 1);
  53. result.reset();
  54. BOOST_TEST(type::instances == 0);
  55. }
  56. return boost::report_errors();
  57. }
  58. #else
  59. int main()
  60. {
  61. return 0;
  62. }
  63. #endif