relops.pass.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. // UNSUPPORTED: c++98, c++03, c++11, c++14
  11. // <variant>
  12. // template <class ...Types>
  13. // constexpr bool
  14. // operator==(variant<Types...> const&, variant<Types...> const&) noexcept;
  15. //
  16. // template <class ...Types>
  17. // constexpr bool
  18. // operator!=(variant<Types...> const&, variant<Types...> const&) noexcept;
  19. //
  20. // template <class ...Types>
  21. // constexpr bool
  22. // operator<(variant<Types...> const&, variant<Types...> const&) noexcept;
  23. //
  24. // template <class ...Types>
  25. // constexpr bool
  26. // operator>(variant<Types...> const&, variant<Types...> const&) noexcept;
  27. //
  28. // template <class ...Types>
  29. // constexpr bool
  30. // operator<=(variant<Types...> const&, variant<Types...> const&) noexcept;
  31. //
  32. // template <class ...Types>
  33. // constexpr bool
  34. // operator>=(variant<Types...> const&, variant<Types...> const&) noexcept;
  35. #include <cassert>
  36. #include <type_traits>
  37. #include <utility>
  38. #include <variant>
  39. #include "test_macros.h"
  40. #ifndef TEST_HAS_NO_EXCEPTIONS
  41. struct MakeEmptyT {
  42. MakeEmptyT() = default;
  43. MakeEmptyT(MakeEmptyT &&) { throw 42; }
  44. MakeEmptyT &operator=(MakeEmptyT &&) { throw 42; }
  45. };
  46. inline bool operator==(MakeEmptyT const &, MakeEmptyT const &) {
  47. assert(false);
  48. return false;
  49. }
  50. inline bool operator!=(MakeEmptyT const &, MakeEmptyT const &) {
  51. assert(false);
  52. return false;
  53. }
  54. inline bool operator<(MakeEmptyT const &, MakeEmptyT const &) {
  55. assert(false);
  56. return false;
  57. }
  58. inline bool operator<=(MakeEmptyT const &, MakeEmptyT const &) {
  59. assert(false);
  60. return false;
  61. }
  62. inline bool operator>(MakeEmptyT const &, MakeEmptyT const &) {
  63. assert(false);
  64. return false;
  65. }
  66. inline bool operator>=(MakeEmptyT const &, MakeEmptyT const &) {
  67. assert(false);
  68. return false;
  69. }
  70. template <class Variant> void makeEmpty(Variant &v) {
  71. Variant v2(std::in_place_type<MakeEmptyT>);
  72. try {
  73. v = std::move(v2);
  74. assert(false);
  75. } catch (...) {
  76. assert(v.valueless_by_exception());
  77. }
  78. }
  79. #endif // TEST_HAS_NO_EXCEPTIONS
  80. void test_equality() {
  81. {
  82. using V = std::variant<int, long>;
  83. constexpr V v1(42);
  84. constexpr V v2(42);
  85. static_assert(v1 == v2, "");
  86. static_assert(v2 == v1, "");
  87. static_assert(!(v1 != v2), "");
  88. static_assert(!(v2 != v1), "");
  89. }
  90. {
  91. using V = std::variant<int, long>;
  92. constexpr V v1(42);
  93. constexpr V v2(43);
  94. static_assert(!(v1 == v2), "");
  95. static_assert(!(v2 == v1), "");
  96. static_assert(v1 != v2, "");
  97. static_assert(v2 != v1, "");
  98. }
  99. {
  100. using V = std::variant<int, long>;
  101. constexpr V v1(42);
  102. constexpr V v2(42l);
  103. static_assert(!(v1 == v2), "");
  104. static_assert(!(v2 == v1), "");
  105. static_assert(v1 != v2, "");
  106. static_assert(v2 != v1, "");
  107. }
  108. {
  109. using V = std::variant<int, long>;
  110. constexpr V v1(42l);
  111. constexpr V v2(42l);
  112. static_assert(v1 == v2, "");
  113. static_assert(v2 == v1, "");
  114. static_assert(!(v1 != v2), "");
  115. static_assert(!(v2 != v1), "");
  116. }
  117. #ifndef TEST_HAS_NO_EXCEPTIONS
  118. {
  119. using V = std::variant<int, MakeEmptyT>;
  120. V v1;
  121. V v2;
  122. makeEmpty(v2);
  123. assert(!(v1 == v2));
  124. assert(!(v2 == v1));
  125. assert(v1 != v2);
  126. assert(v2 != v1);
  127. }
  128. {
  129. using V = std::variant<int, MakeEmptyT>;
  130. V v1;
  131. makeEmpty(v1);
  132. V v2;
  133. assert(!(v1 == v2));
  134. assert(!(v2 == v1));
  135. assert(v1 != v2);
  136. assert(v2 != v1);
  137. }
  138. {
  139. using V = std::variant<int, MakeEmptyT>;
  140. V v1;
  141. makeEmpty(v1);
  142. V v2;
  143. makeEmpty(v2);
  144. assert(v1 == v2);
  145. assert(v2 == v1);
  146. assert(!(v1 != v2));
  147. assert(!(v2 != v1));
  148. }
  149. #endif
  150. }
  151. template <class Var>
  152. constexpr bool test_less(Var const &l, Var const &r, bool expect_less,
  153. bool expect_greater) {
  154. return ((l < r) == expect_less) && (!(l >= r) == expect_less) &&
  155. ((l > r) == expect_greater) && (!(l <= r) == expect_greater);
  156. }
  157. void test_relational() {
  158. { // same index, same value
  159. using V = std::variant<int, long>;
  160. constexpr V v1(1);
  161. constexpr V v2(1);
  162. static_assert(test_less(v1, v2, false, false), "");
  163. }
  164. { // same index, value < other_value
  165. using V = std::variant<int, long>;
  166. constexpr V v1(0);
  167. constexpr V v2(1);
  168. static_assert(test_less(v1, v2, true, false), "");
  169. }
  170. { // same index, value > other_value
  171. using V = std::variant<int, long>;
  172. constexpr V v1(1);
  173. constexpr V v2(0);
  174. static_assert(test_less(v1, v2, false, true), "");
  175. }
  176. { // LHS.index() < RHS.index()
  177. using V = std::variant<int, long>;
  178. constexpr V v1(0);
  179. constexpr V v2(0l);
  180. static_assert(test_less(v1, v2, true, false), "");
  181. }
  182. { // LHS.index() > RHS.index()
  183. using V = std::variant<int, long>;
  184. constexpr V v1(0l);
  185. constexpr V v2(0);
  186. static_assert(test_less(v1, v2, false, true), "");
  187. }
  188. #ifndef TEST_HAS_NO_EXCEPTIONS
  189. { // LHS.index() < RHS.index(), RHS is empty
  190. using V = std::variant<int, MakeEmptyT>;
  191. V v1;
  192. V v2;
  193. makeEmpty(v2);
  194. assert(test_less(v1, v2, false, true));
  195. }
  196. { // LHS.index() > RHS.index(), LHS is empty
  197. using V = std::variant<int, MakeEmptyT>;
  198. V v1;
  199. makeEmpty(v1);
  200. V v2;
  201. assert(test_less(v1, v2, true, false));
  202. }
  203. { // LHS.index() == RHS.index(), LHS and RHS are empty
  204. using V = std::variant<int, MakeEmptyT>;
  205. V v1;
  206. makeEmpty(v1);
  207. V v2;
  208. makeEmpty(v2);
  209. assert(test_less(v1, v2, false, false));
  210. }
  211. #endif
  212. }
  213. int main() {
  214. test_equality();
  215. test_relational();
  216. }