make_unique_args_test.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/core/lightweight_test.hpp>
  10. #include <boost/smart_ptr/make_unique.hpp>
  11. class type {
  12. public:
  13. static unsigned instances;
  14. type(int v1 = 0,
  15. int v2 = 0,
  16. int v3 = 0,
  17. int v4 = 0,
  18. int v5 = 0,
  19. int v6 = 0,
  20. int v7 = 0,
  21. int v8 = 0,
  22. int v9 = 0)
  23. : sum_(v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9) {
  24. ++instances;
  25. }
  26. ~type() {
  27. --instances;
  28. }
  29. int sum() const {
  30. return sum_;
  31. }
  32. private:
  33. int sum_;
  34. type(const type&);
  35. type& operator=(const type&);
  36. };
  37. unsigned type::instances = 0;
  38. int main()
  39. {
  40. BOOST_TEST(type::instances == 0);
  41. {
  42. std::unique_ptr<type> result = boost::make_unique<type>();
  43. BOOST_TEST(result.get() != 0);
  44. BOOST_TEST(type::instances == 1);
  45. BOOST_TEST(result->sum() == 0);
  46. result.reset();
  47. BOOST_TEST(type::instances == 0);
  48. }
  49. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  50. {
  51. std::unique_ptr<type> result = boost::make_unique<type>(1);
  52. BOOST_TEST(result.get() != 0);
  53. BOOST_TEST(type::instances == 1);
  54. BOOST_TEST(result->sum() == 1);
  55. result.reset();
  56. BOOST_TEST(type::instances == 0);
  57. }
  58. {
  59. std::unique_ptr<type> result = boost::make_unique<type>(1, 2);
  60. BOOST_TEST(result.get() != 0);
  61. BOOST_TEST(type::instances == 1);
  62. BOOST_TEST(result->sum() == 1 + 2);
  63. result.reset();
  64. BOOST_TEST(type::instances == 0);
  65. }
  66. {
  67. std::unique_ptr<type> result =
  68. boost::make_unique<type>(1, 2, 3);
  69. BOOST_TEST(result.get() != 0);
  70. BOOST_TEST(type::instances == 1);
  71. BOOST_TEST(result->sum() == 1 + 2 + 3);
  72. result.reset();
  73. BOOST_TEST(type::instances == 0);
  74. }
  75. {
  76. std::unique_ptr<type> result =
  77. boost::make_unique<type>(1, 2, 3, 4);
  78. BOOST_TEST(result.get() != 0);
  79. BOOST_TEST(type::instances == 1);
  80. BOOST_TEST(result->sum() == 1 + 2 + 3 + 4);
  81. result.reset();
  82. BOOST_TEST(type::instances == 0);
  83. }
  84. {
  85. std::unique_ptr<type> result =
  86. boost::make_unique<type>(1, 2, 3, 4, 5);
  87. BOOST_TEST(result.get() != 0);
  88. BOOST_TEST(type::instances == 1);
  89. BOOST_TEST(result->sum() == 1 + 2 + 3 + 4 + 5);
  90. result.reset();
  91. BOOST_TEST(type::instances == 0);
  92. }
  93. {
  94. std::unique_ptr<type> result =
  95. boost::make_unique<type>(1, 2, 3, 4, 5, 6);
  96. BOOST_TEST(result.get() != 0);
  97. BOOST_TEST(type::instances == 1);
  98. BOOST_TEST(result->sum() == 1 + 2 + 3 + 4 + 5 + 6);
  99. result.reset();
  100. BOOST_TEST(type::instances == 0);
  101. }
  102. {
  103. std::unique_ptr<type> result =
  104. boost::make_unique<type>(1, 2, 3, 4, 5, 6, 7);
  105. BOOST_TEST(result.get() != 0);
  106. BOOST_TEST(type::instances == 1);
  107. BOOST_TEST(result->sum() == 1 + 2 + 3 + 4 + 5 + 6 + 7);
  108. result.reset();
  109. BOOST_TEST(type::instances == 0);
  110. }
  111. {
  112. std::unique_ptr<type> result =
  113. boost::make_unique<type>(1, 2, 3, 4, 5, 6, 7, 8);
  114. BOOST_TEST(result.get() != 0);
  115. BOOST_TEST(type::instances == 1);
  116. BOOST_TEST(result->sum() == 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8);
  117. result.reset();
  118. BOOST_TEST(type::instances == 0);
  119. }
  120. {
  121. std::unique_ptr<type> result =
  122. boost::make_unique<type>(1, 2, 3, 4, 5, 6, 7, 8, 9);
  123. BOOST_TEST(result.get() != 0);
  124. BOOST_TEST(type::instances == 1);
  125. BOOST_TEST(result->sum() == 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9);
  126. result.reset();
  127. BOOST_TEST(type::instances == 0);
  128. }
  129. #endif
  130. return boost::report_errors();
  131. }
  132. #else
  133. int main()
  134. {
  135. return 0;
  136. }
  137. #endif