emplace.pass.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // <optional>
  11. // template <class... Args> void optional<T>::emplace(Args&&... args);
  12. #include <optional>
  13. #include <type_traits>
  14. #include <cassert>
  15. #include <memory>
  16. #include "test_macros.h"
  17. #include "archetypes.hpp"
  18. using std::optional;
  19. class X
  20. {
  21. int i_;
  22. int j_ = 0;
  23. public:
  24. X() : i_(0) {}
  25. X(int i) : i_(i) {}
  26. X(int i, int j) : i_(i), j_(j) {}
  27. friend bool operator==(const X& x, const X& y)
  28. {return x.i_ == y.i_ && x.j_ == y.j_;}
  29. };
  30. class Y
  31. {
  32. public:
  33. static bool dtor_called;
  34. Y() = default;
  35. Y(int) { TEST_THROW(6);}
  36. ~Y() {dtor_called = true;}
  37. };
  38. bool Y::dtor_called = false;
  39. template <class T>
  40. void test_one_arg() {
  41. using Opt = std::optional<T>;
  42. {
  43. Opt opt;
  44. opt.emplace();
  45. assert(static_cast<bool>(opt) == true);
  46. assert(*opt == T(0));
  47. }
  48. {
  49. Opt opt;
  50. opt.emplace(1);
  51. assert(static_cast<bool>(opt) == true);
  52. assert(*opt == T(1));
  53. }
  54. {
  55. Opt opt(2);
  56. opt.emplace();
  57. assert(static_cast<bool>(opt) == true);
  58. assert(*opt == T(0));
  59. }
  60. {
  61. Opt opt(2);
  62. opt.emplace(1);
  63. assert(static_cast<bool>(opt) == true);
  64. assert(*opt == T(1));
  65. }
  66. }
  67. template <class T>
  68. void test_multi_arg()
  69. {
  70. test_one_arg<T>();
  71. using Opt = std::optional<T>;
  72. Opt opt;
  73. {
  74. opt.emplace(101, 41);
  75. assert(static_cast<bool>(opt) == true);
  76. assert(*opt == T(101, 41));
  77. }
  78. {
  79. Opt opt;
  80. opt.emplace({1, 2, 3, 4});
  81. assert(static_cast<bool>(opt) == true);
  82. assert(*opt == T(4)); // T sets its value to the size of the init list
  83. }
  84. {
  85. Opt opt;
  86. opt.emplace({1, 2, 3, 4, 5}, 6);
  87. assert(static_cast<bool>(opt) == true);
  88. assert(*opt == T(5)); // T sets its value to the size of the init list
  89. }
  90. }
  91. template <class T>
  92. void test_on_test_type() {
  93. T::reset();
  94. optional<T> opt;
  95. assert(T::alive == 0);
  96. {
  97. T::reset_constructors();
  98. opt.emplace();
  99. assert(T::alive == 1);
  100. assert(T::constructed == 1);
  101. assert(T::default_constructed == 1);
  102. assert(T::destroyed == 0);
  103. assert(static_cast<bool>(opt) == true);
  104. assert(*opt == T());
  105. }
  106. {
  107. T::reset_constructors();
  108. opt.emplace();
  109. assert(T::alive == 1);
  110. assert(T::constructed == 1);
  111. assert(T::default_constructed == 1);
  112. assert(T::destroyed == 1);
  113. assert(static_cast<bool>(opt) == true);
  114. assert(*opt == T());
  115. }
  116. {
  117. T::reset_constructors();
  118. opt.emplace(101);
  119. assert(T::alive == 1);
  120. assert(T::constructed == 1);
  121. assert(T::value_constructed == 1);
  122. assert(T::destroyed == 1);
  123. assert(static_cast<bool>(opt) == true);
  124. assert(*opt == T(101));
  125. }
  126. {
  127. T::reset_constructors();
  128. opt.emplace(-10, 99);
  129. assert(T::alive == 1);
  130. assert(T::constructed == 1);
  131. assert(T::value_constructed == 1);
  132. assert(T::destroyed == 1);
  133. assert(static_cast<bool>(opt) == true);
  134. assert(*opt == T(-10, 99));
  135. }
  136. {
  137. T::reset_constructors();
  138. opt.emplace(-10, 99);
  139. assert(T::alive == 1);
  140. assert(T::constructed == 1);
  141. assert(T::value_constructed == 1);
  142. assert(T::destroyed == 1);
  143. assert(static_cast<bool>(opt) == true);
  144. assert(*opt == T(-10, 99));
  145. }
  146. {
  147. T::reset_constructors();
  148. opt.emplace({-10, 99, 42, 1});
  149. assert(T::alive == 1);
  150. assert(T::constructed == 1);
  151. assert(T::value_constructed == 1);
  152. assert(T::destroyed == 1);
  153. assert(static_cast<bool>(opt) == true);
  154. assert(*opt == T(4)); // size of the initializer list
  155. }
  156. {
  157. T::reset_constructors();
  158. opt.emplace({-10, 99, 42, 1}, 42);
  159. assert(T::alive == 1);
  160. assert(T::constructed == 1);
  161. assert(T::value_constructed == 1);
  162. assert(T::destroyed == 1);
  163. assert(static_cast<bool>(opt) == true);
  164. assert(*opt == T(4)); // size of the initializer list
  165. }
  166. }
  167. int main()
  168. {
  169. {
  170. test_on_test_type<TestTypes::TestType>();
  171. test_on_test_type<ExplicitTestTypes::TestType>();
  172. }
  173. {
  174. using T = int;
  175. test_one_arg<T>();
  176. test_one_arg<const T>();
  177. }
  178. {
  179. using T = ConstexprTestTypes::TestType;
  180. test_multi_arg<T>();
  181. }
  182. {
  183. using T = ExplicitConstexprTestTypes::TestType;
  184. test_multi_arg<T>();
  185. }
  186. {
  187. using T = TrivialTestTypes::TestType;
  188. test_multi_arg<T>();
  189. }
  190. {
  191. using T = ExplicitTrivialTestTypes::TestType;
  192. test_multi_arg<T>();
  193. }
  194. {
  195. optional<const int> opt;
  196. opt.emplace(42);
  197. assert(*opt == 42);
  198. opt.emplace();
  199. assert(*opt == 0);
  200. }
  201. #ifndef TEST_HAS_NO_EXCEPTIONS
  202. Y::dtor_called = false;
  203. {
  204. Y y;
  205. optional<Y> opt(y);
  206. try
  207. {
  208. assert(static_cast<bool>(opt) == true);
  209. assert(Y::dtor_called == false);
  210. opt.emplace(1);
  211. }
  212. catch (int i)
  213. {
  214. assert(i == 6);
  215. assert(static_cast<bool>(opt) == false);
  216. assert(Y::dtor_called == true);
  217. }
  218. }
  219. #endif
  220. }