construct.pass.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. // UNSUPPORTED: c++98, c++03
  9. // <memory>
  10. // template <class OuterAlloc, class... InnerAllocs>
  11. // class scoped_allocator_adaptor
  12. // template <class T, class... Args> void construct(T* p, Args&&... args);
  13. #include <scoped_allocator>
  14. #include <cassert>
  15. #include <string>
  16. #include "test_macros.h"
  17. #include "allocators.h"
  18. struct B
  19. {
  20. static bool constructed;
  21. typedef A1<B> allocator_type;
  22. explicit B(std::allocator_arg_t, const allocator_type& a, int i)
  23. {
  24. assert(a.id() == 5);
  25. assert(i == 6);
  26. constructed = true;
  27. }
  28. };
  29. bool B::constructed = false;
  30. struct C
  31. {
  32. static bool constructed;
  33. typedef std::scoped_allocator_adaptor<A2<C>> allocator_type;
  34. explicit C(std::allocator_arg_t, const allocator_type& a, int i)
  35. {
  36. assert(a.id() == 7);
  37. assert(i == 8);
  38. constructed = true;
  39. }
  40. };
  41. bool C::constructed = false;
  42. struct D
  43. {
  44. static bool constructed;
  45. typedef std::scoped_allocator_adaptor<A2<D>> allocator_type;
  46. explicit D(int i, int j, const allocator_type& a)
  47. {
  48. assert(i == 1);
  49. assert(j == 2);
  50. assert(a.id() == 3);
  51. constructed = true;
  52. }
  53. };
  54. bool D::constructed = false;
  55. struct E
  56. {
  57. static bool constructed;
  58. typedef std::scoped_allocator_adaptor<A1<E>> allocator_type;
  59. explicit E(int i, int j, const allocator_type& a)
  60. {
  61. assert(i == 1);
  62. assert(j == 2);
  63. assert(a.id() == 50);
  64. constructed = true;
  65. }
  66. };
  67. bool E::constructed = false;
  68. struct F
  69. {
  70. static bool constructed;
  71. typedef std::scoped_allocator_adaptor<A2<F>> allocator_type;
  72. explicit F(int i, int j)
  73. {
  74. assert(i == 1);
  75. assert(j == 2);
  76. }
  77. explicit F(int i, int j, const allocator_type& a)
  78. {
  79. assert(i == 1);
  80. assert(j == 2);
  81. assert(a.id() == 50);
  82. constructed = true;
  83. }
  84. };
  85. bool F::constructed = false;
  86. int main(int, char**)
  87. {
  88. {
  89. typedef std::scoped_allocator_adaptor<A1<std::string>> A;
  90. A a;
  91. char buf[100];
  92. typedef std::string S;
  93. S* s = (S*)buf;
  94. a.construct(s, 4, 'c');
  95. assert(*s == "cccc");
  96. s->~S();
  97. }
  98. {
  99. typedef std::scoped_allocator_adaptor<A1<B>> A;
  100. A a(A1<B>(5));
  101. char buf[100];
  102. typedef B S;
  103. S* s = (S*)buf;
  104. a.construct(s, 6);
  105. assert(S::constructed);
  106. s->~S();
  107. }
  108. {
  109. typedef std::scoped_allocator_adaptor<A1<int>, A2<C>> A;
  110. A a(A1<int>(5), A2<C>(7));
  111. char buf[100];
  112. typedef C S;
  113. S* s = (S*)buf;
  114. a.construct(s, 8);
  115. assert(S::constructed);
  116. s->~S();
  117. }
  118. {
  119. typedef std::scoped_allocator_adaptor<A1<int>, A2<D>> A;
  120. A a(A1<int>(5), A2<D>(3));
  121. char buf[100];
  122. typedef D S;
  123. S* s = (S*)buf;
  124. a.construct(s, 1, 2);
  125. assert(S::constructed);
  126. s->~S();
  127. }
  128. {
  129. typedef std::scoped_allocator_adaptor<A3<E>, A2<E>> K;
  130. typedef std::scoped_allocator_adaptor<K, A1<E>> A;
  131. A a(K(), A1<E>(50));
  132. char buf[100];
  133. typedef E S;
  134. S* s = (S*)buf;
  135. A3<E>::constructed = false;
  136. a.construct(s, 1, 2);
  137. assert(S::constructed);
  138. assert(A3<E>::constructed);
  139. s->~S();
  140. }
  141. {
  142. typedef std::scoped_allocator_adaptor<A3<F>, A2<F>> K;
  143. typedef std::scoped_allocator_adaptor<K, A1<F>> A;
  144. A a(K(), A1<F>(50));
  145. char buf[100];
  146. typedef F S;
  147. S* s = (S*)buf;
  148. A3<F>::constructed = false;
  149. a.construct(s, 1, 2);
  150. assert(!S::constructed);
  151. assert(A3<F>::constructed);
  152. s->~S();
  153. }
  154. return 0;
  155. }