allocate_shared.pass.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 A, class... Args>
  11. // shared_ptr<T> allocate_shared(const A& a, Args&&... args);
  12. #include <memory>
  13. #include <new>
  14. #include <cstdlib>
  15. #include <cassert>
  16. #include "test_macros.h"
  17. #include "test_allocator.h"
  18. #include "min_allocator.h"
  19. #if TEST_STD_VER >= 11
  20. #define DELETE_FUNCTION = delete
  21. #else
  22. #define DELETE_FUNCTION
  23. #endif
  24. int new_count = 0;
  25. struct A
  26. {
  27. static int count;
  28. A(int i, char c) : int_(i), char_(c) {++count;}
  29. A(const A& a)
  30. : int_(a.int_), char_(a.char_)
  31. {++count;}
  32. ~A() {--count;}
  33. int get_int() const {return int_;}
  34. char get_char() const {return char_;}
  35. A* operator& () DELETE_FUNCTION;
  36. private:
  37. int int_;
  38. char char_;
  39. };
  40. int A::count = 0;
  41. struct Zero
  42. {
  43. static int count;
  44. Zero() {++count;}
  45. Zero(Zero const &) {++count;}
  46. ~Zero() {--count;}
  47. };
  48. int Zero::count = 0;
  49. struct One
  50. {
  51. static int count;
  52. int value;
  53. explicit One(int v) : value(v) {++count;}
  54. One(One const & o) : value(o.value) {++count;}
  55. ~One() {--count;}
  56. };
  57. int One::count = 0;
  58. struct Two
  59. {
  60. static int count;
  61. int value;
  62. Two(int v, int) : value(v) {++count;}
  63. Two(Two const & o) : value(o.value) {++count;}
  64. ~Two() {--count;}
  65. };
  66. int Two::count = 0;
  67. struct Three
  68. {
  69. static int count;
  70. int value;
  71. Three(int v, int, int) : value(v) {++count;}
  72. Three(Three const & o) : value(o.value) {++count;}
  73. ~Three() {--count;}
  74. };
  75. int Three::count = 0;
  76. template <class Alloc>
  77. void test()
  78. {
  79. int const bad = -1;
  80. {
  81. std::shared_ptr<Zero> p = std::allocate_shared<Zero>(Alloc());
  82. assert(Zero::count == 1);
  83. }
  84. assert(Zero::count == 0);
  85. {
  86. int const i = 42;
  87. std::shared_ptr<One> p = std::allocate_shared<One>(Alloc(), i);
  88. assert(One::count == 1);
  89. assert(p->value == i);
  90. }
  91. assert(One::count == 0);
  92. {
  93. int const i = 42;
  94. std::shared_ptr<Two> p = std::allocate_shared<Two>(Alloc(), i, bad);
  95. assert(Two::count == 1);
  96. assert(p->value == i);
  97. }
  98. assert(Two::count == 0);
  99. {
  100. int const i = 42;
  101. std::shared_ptr<Three> p = std::allocate_shared<Three>(Alloc(), i, bad, bad);
  102. assert(Three::count == 1);
  103. assert(p->value == i);
  104. }
  105. assert(Three::count == 0);
  106. }
  107. int main(int, char**)
  108. {
  109. test<bare_allocator<void> >();
  110. test<test_allocator<void> >();
  111. {
  112. int i = 67;
  113. char c = 'e';
  114. std::shared_ptr<A> p = std::allocate_shared<A>(test_allocator<A>(54), i, c);
  115. assert(test_allocator<A>::alloc_count == 1);
  116. assert(A::count == 1);
  117. assert(p->get_int() == 67);
  118. assert(p->get_char() == 'e');
  119. }
  120. assert(A::count == 0);
  121. assert(test_allocator<A>::alloc_count == 0);
  122. {
  123. int i = 67;
  124. char c = 'e';
  125. std::shared_ptr<A> p = std::allocate_shared<A>(min_allocator<void>(), i, c);
  126. assert(A::count == 1);
  127. assert(p->get_int() == 67);
  128. assert(p->get_char() == 'e');
  129. }
  130. assert(A::count == 0);
  131. {
  132. int i = 68;
  133. char c = 'f';
  134. std::shared_ptr<A> p = std::allocate_shared<A>(bare_allocator<void>(), i, c);
  135. assert(A::count == 1);
  136. assert(p->get_int() == 68);
  137. assert(p->get_char() == 'f');
  138. }
  139. assert(A::count == 0);
  140. return 0;
  141. }