make_shared.pass.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <memory>
  9. // shared_ptr
  10. // template<class T, class... Args> shared_ptr<T> make_shared(Args&&... args);
  11. #include <memory>
  12. #include <cassert>
  13. #include "test_macros.h"
  14. #include "count_new.h"
  15. #if TEST_STD_VER >= 11
  16. #define DELETE_FUNCTION = delete
  17. #else
  18. #define DELETE_FUNCTION
  19. #endif
  20. struct A
  21. {
  22. static int count;
  23. A(int i, char c) : int_(i), char_(c) {++count;}
  24. A(const A& a)
  25. : int_(a.int_), char_(a.char_)
  26. {++count;}
  27. ~A() {--count;}
  28. int get_int() const {return int_;}
  29. char get_char() const {return char_;}
  30. A* operator& () DELETE_FUNCTION;
  31. private:
  32. int int_;
  33. char char_;
  34. };
  35. int A::count = 0;
  36. struct Foo
  37. {
  38. Foo() = default;
  39. virtual ~Foo() = default;
  40. };
  41. #ifdef _LIBCPP_VERSION
  42. struct Result {};
  43. static Result theFunction() { return Result(); }
  44. static int resultDeletorCount;
  45. static void resultDeletor(Result (*pf)()) {
  46. assert(pf == theFunction);
  47. ++resultDeletorCount;
  48. }
  49. void test_pointer_to_function() {
  50. { // https://bugs.llvm.org/show_bug.cgi?id=27566
  51. std::shared_ptr<Result()> x(&theFunction, &resultDeletor);
  52. std::shared_ptr<Result()> y(theFunction, resultDeletor);
  53. }
  54. assert(resultDeletorCount == 2);
  55. }
  56. #else // _LIBCPP_VERSION
  57. void test_pointer_to_function() {}
  58. #endif // _LIBCPP_VERSION
  59. int main(int, char**)
  60. {
  61. int nc = globalMemCounter.outstanding_new;
  62. {
  63. int i = 67;
  64. char c = 'e';
  65. std::shared_ptr<A> p = std::make_shared<A>(i, c);
  66. assert(globalMemCounter.checkOutstandingNewEq(nc+1));
  67. assert(A::count == 1);
  68. assert(p->get_int() == 67);
  69. assert(p->get_char() == 'e');
  70. }
  71. { // https://bugs.llvm.org/show_bug.cgi?id=24137
  72. std::shared_ptr<Foo> p1 = std::make_shared<Foo>();
  73. assert(p1.get());
  74. std::shared_ptr<const Foo> p2 = std::make_shared<const Foo>();
  75. assert(p2.get());
  76. }
  77. test_pointer_to_function();
  78. #if TEST_STD_VER >= 11
  79. nc = globalMemCounter.outstanding_new;
  80. {
  81. char c = 'e';
  82. std::shared_ptr<A> p = std::make_shared<A>(67, c);
  83. assert(globalMemCounter.checkOutstandingNewEq(nc+1));
  84. assert(A::count == 1);
  85. assert(p->get_int() == 67);
  86. assert(p->get_char() == 'e');
  87. }
  88. #endif
  89. assert(A::count == 0);
  90. return 0;
  91. }