assign_value.pass.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 U> optional<T>& operator=(U&& v);
  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. struct ThrowAssign {
  20. static int dtor_called;
  21. ThrowAssign() = default;
  22. ThrowAssign(int) { TEST_THROW(42); }
  23. ThrowAssign& operator=(int) {
  24. TEST_THROW(42);
  25. }
  26. ~ThrowAssign() { ++dtor_called; }
  27. };
  28. int ThrowAssign::dtor_called = 0;
  29. template <class T, class Arg = T, bool Expect = true>
  30. void assert_assignable() {
  31. static_assert(std::is_assignable<optional<T>&, Arg>::value == Expect, "");
  32. static_assert(!std::is_assignable<const optional<T>&, Arg>::value, "");
  33. }
  34. struct MismatchType {
  35. explicit MismatchType(int) {}
  36. explicit MismatchType(char*) {}
  37. explicit MismatchType(int*) = delete;
  38. MismatchType& operator=(int) { return *this; }
  39. MismatchType& operator=(int*) { return *this; }
  40. MismatchType& operator=(char*) = delete;
  41. };
  42. void test_sfinae() {
  43. using I = TestTypes::TestType;
  44. using E = ExplicitTestTypes::TestType;
  45. assert_assignable<int>();
  46. assert_assignable<int, int&>();
  47. assert_assignable<int, int const&>();
  48. // Implicit test type
  49. assert_assignable<I, I const&>();
  50. assert_assignable<I, I&&>();
  51. assert_assignable<I, int>();
  52. assert_assignable<I, void*, false>();
  53. // Explicit test type
  54. assert_assignable<E, E const&>();
  55. assert_assignable<E, E &&>();
  56. assert_assignable<E, int>();
  57. assert_assignable<E, void*, false>();
  58. // Mismatch type
  59. assert_assignable<MismatchType, int>();
  60. assert_assignable<MismatchType, int*, false>();
  61. assert_assignable<MismatchType, char*, false>();
  62. }
  63. void test_with_test_type()
  64. {
  65. using T = TestTypes::TestType;
  66. T::reset();
  67. { // to empty
  68. optional<T> opt;
  69. opt = 3;
  70. assert(T::alive == 1);
  71. assert(T::constructed == 1);
  72. assert(T::value_constructed == 1);
  73. assert(T::assigned == 0);
  74. assert(T::destroyed == 0);
  75. assert(static_cast<bool>(opt) == true);
  76. assert(*opt == T(3));
  77. }
  78. { // to existing
  79. optional<T> opt(42);
  80. T::reset_constructors();
  81. opt = 3;
  82. assert(T::alive == 1);
  83. assert(T::constructed == 0);
  84. assert(T::assigned == 1);
  85. assert(T::value_assigned == 1);
  86. assert(T::destroyed == 0);
  87. assert(static_cast<bool>(opt) == true);
  88. assert(*opt == T(3));
  89. }
  90. { // test default argument
  91. optional<T> opt;
  92. T::reset_constructors();
  93. opt = {1, 2};
  94. assert(T::alive == 1);
  95. assert(T::constructed == 2);
  96. assert(T::value_constructed == 1);
  97. assert(T::move_constructed == 1);
  98. assert(T::assigned == 0);
  99. assert(T::destroyed == 1);
  100. assert(static_cast<bool>(opt) == true);
  101. assert(*opt == T(1, 2));
  102. }
  103. { // test default argument
  104. optional<T> opt(42);
  105. T::reset_constructors();
  106. opt = {1, 2};
  107. assert(T::alive == 1);
  108. assert(T::constructed == 1);
  109. assert(T::value_constructed == 1);
  110. assert(T::assigned == 1);
  111. assert(T::move_assigned == 1);
  112. assert(T::destroyed == 1);
  113. assert(static_cast<bool>(opt) == true);
  114. assert(*opt == T(1, 2));
  115. }
  116. { // test default argument
  117. optional<T> opt;
  118. T::reset_constructors();
  119. opt = {1};
  120. assert(T::alive == 1);
  121. assert(T::constructed == 2);
  122. assert(T::value_constructed == 1);
  123. assert(T::move_constructed == 1);
  124. assert(T::assigned == 0);
  125. assert(T::destroyed == 1);
  126. assert(static_cast<bool>(opt) == true);
  127. assert(*opt == T(1));
  128. }
  129. { // test default argument
  130. optional<T> opt(42);
  131. T::reset_constructors();
  132. opt = {};
  133. assert(static_cast<bool>(opt) == false);
  134. assert(T::alive == 0);
  135. assert(T::constructed == 0);
  136. assert(T::assigned == 0);
  137. assert(T::destroyed == 1);
  138. }
  139. }
  140. template <class T, class Value = int>
  141. void test_with_type() {
  142. { // to empty
  143. optional<T> opt;
  144. opt = Value(3);
  145. assert(static_cast<bool>(opt) == true);
  146. assert(*opt == T(3));
  147. }
  148. { // to existing
  149. optional<T> opt(Value(42));
  150. opt = Value(3);
  151. assert(static_cast<bool>(opt) == true);
  152. assert(*opt == T(3));
  153. }
  154. { // test const
  155. optional<T> opt(Value(42));
  156. const T t(Value(3));
  157. opt = t;
  158. assert(static_cast<bool>(opt) == true);
  159. assert(*opt == T(3));
  160. }
  161. { // test default argument
  162. optional<T> opt;
  163. opt = {Value(1)};
  164. assert(static_cast<bool>(opt) == true);
  165. assert(*opt == T(1));
  166. }
  167. { // test default argument
  168. optional<T> opt(Value(42));
  169. opt = {};
  170. assert(static_cast<bool>(opt) == false);
  171. }
  172. }
  173. template <class T>
  174. void test_with_type_multi() {
  175. test_with_type<T>();
  176. { // test default argument
  177. optional<T> opt;
  178. opt = {1, 2};
  179. assert(static_cast<bool>(opt) == true);
  180. assert(*opt == T(1, 2));
  181. }
  182. { // test default argument
  183. optional<T> opt(42);
  184. opt = {1, 2};
  185. assert(static_cast<bool>(opt) == true);
  186. assert(*opt == T(1, 2));
  187. }
  188. }
  189. void test_throws()
  190. {
  191. #ifndef TEST_HAS_NO_EXCEPTIONS
  192. using T = ThrowAssign;
  193. {
  194. using T = ThrowAssign;
  195. optional<T> opt;
  196. try {
  197. opt = 42;
  198. assert(false);
  199. } catch (int) {}
  200. assert(static_cast<bool>(opt) == false);
  201. }
  202. assert(T::dtor_called == 0);
  203. {
  204. T::dtor_called = 0;
  205. optional<T> opt(std::in_place);
  206. try {
  207. opt = 42;
  208. assert(false);
  209. } catch (int) {}
  210. assert(static_cast<bool>(opt) == true);
  211. assert(T::dtor_called == 0);
  212. }
  213. assert(T::dtor_called == 1);
  214. #endif
  215. }
  216. enum MyEnum { Zero, One, Two, Three, FortyTwo = 42 };
  217. using Fn = void(*)();
  218. int main()
  219. {
  220. test_sfinae();
  221. // Test with instrumented type
  222. test_with_test_type();
  223. // Test with various scalar types
  224. test_with_type<int>();
  225. test_with_type<MyEnum, MyEnum>();
  226. test_with_type<int, MyEnum>();
  227. test_with_type<Fn, Fn>();
  228. // Test types with multi argument constructors
  229. test_with_type_multi<ConstexprTestTypes::TestType>();
  230. test_with_type_multi<TrivialTestTypes::TestType>();
  231. // Test move only types
  232. {
  233. optional<std::unique_ptr<int>> opt;
  234. opt = std::unique_ptr<int>(new int(3));
  235. assert(static_cast<bool>(opt) == true);
  236. assert(**opt == 3);
  237. }
  238. {
  239. optional<std::unique_ptr<int>> opt(std::unique_ptr<int>(new int(2)));
  240. opt = std::unique_ptr<int>(new int(3));
  241. assert(static_cast<bool>(opt) == true);
  242. assert(**opt == 3);
  243. }
  244. test_throws();
  245. }