• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
11 
12 // <compare>
13 
14 // class strong_equality
15 
16 
17 #include <compare>
18 #include <type_traits>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 
23 const volatile void* volatile sink;
24 
test_static_members()25 void test_static_members() {
26   DoNotOptimize(&std::strong_equality::equal);
27   DoNotOptimize(&std::strong_equality::nonequal);
28   DoNotOptimize(&std::strong_equality::equivalent);
29   DoNotOptimize(&std::strong_equality::nonequivalent);
30 }
31 
test_signatures()32 void test_signatures() {
33   auto& Eq = std::strong_equality::equivalent;
34 
35   ASSERT_NOEXCEPT(Eq == 0);
36   ASSERT_NOEXCEPT(0 == Eq);
37   ASSERT_NOEXCEPT(Eq != 0);
38   ASSERT_NOEXCEPT(0 != Eq);
39 #ifndef TEST_HAS_NO_SPACESHIP_OPERATOR
40   ASSERT_NOEXCEPT(0 <=> Eq);
41   ASSERT_NOEXCEPT(Eq <=> 0);
42   ASSERT_SAME_TYPE(decltype(Eq <=> 0), std::strong_equality);
43   ASSERT_SAME_TYPE(decltype(0 <=> Eq), std::strong_equality);
44 #endif
45 }
46 
test_conversion()47 void test_conversion() {
48   constexpr std::weak_equality res = std::strong_equality::equivalent;
49   static_assert(res == 0, "");
50   static_assert(std::is_convertible<const std::strong_equality&,
51       std::weak_equality>::value, "");
52   static_assert(res == 0, "expected equal");
53 
54   constexpr std::weak_equality neq_res = std::strong_equality::nonequivalent;
55   static_assert(neq_res != 0, "expected not equal");
56 }
57 
test_constexpr()58 constexpr bool test_constexpr() {
59   auto& Eq = std::strong_equality::equal;
60   auto& NEq = std::strong_equality::nonequal;
61   auto& Equiv = std::strong_equality::equivalent;
62   auto& NEquiv = std::strong_equality::nonequivalent;
63   assert((Eq == 0) == true);
64   assert((0 == Eq) == true);
65   assert((Equiv == 0) == true);
66   assert((0 == Equiv) == true);
67   assert((NEq == 0) == false);
68   assert((0 == NEq) == false);
69   assert((NEquiv == 0) == false);
70   assert((0 == NEquiv) == false);
71 
72   assert((Eq != 0) == false);
73   assert((0 != Eq) == false);
74   assert((Equiv != 0) == false);
75   assert((0 != Equiv) == false);
76   assert((NEq != 0) == true);
77   assert((0 != NEq) == true);
78   assert((NEquiv != 0) == true);
79   assert((0 != NEquiv) == true);
80 
81 #ifndef TEST_HAS_NO_SPACESHIP_OPERATOR
82   std::strong_equality res = (Eq <=> 0);
83   ((void)res);
84   res = (0 <=> Eq);
85   ((void)res);
86 #endif
87 
88   return true;
89 }
90 
main()91 int main() {
92   test_static_members();
93   test_signatures();
94   test_conversion();
95   static_assert(test_constexpr(), "constexpr test failed");
96 }
97