libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

weakord.pass.cpp (4333B)


      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 weak_ordering
     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 
     25 void test_static_members() {
     26   DoNotOptimize(&std::weak_ordering::less);
     27   DoNotOptimize(&std::weak_ordering::equivalent);
     28   DoNotOptimize(&std::weak_ordering::greater);
     29 }
     30 
     31 void test_signatures() {
     32   auto& Eq = std::weak_ordering::equivalent;
     33 
     34   ASSERT_NOEXCEPT(Eq == 0);
     35   ASSERT_NOEXCEPT(0 == Eq);
     36   ASSERT_NOEXCEPT(Eq != 0);
     37   ASSERT_NOEXCEPT(0 != Eq);
     38   ASSERT_NOEXCEPT(0 < Eq);
     39   ASSERT_NOEXCEPT(Eq < 0);
     40   ASSERT_NOEXCEPT(0 <= Eq);
     41   ASSERT_NOEXCEPT(Eq <= 0);
     42   ASSERT_NOEXCEPT(0 > Eq);
     43   ASSERT_NOEXCEPT(Eq > 0);
     44   ASSERT_NOEXCEPT(0 >= Eq);
     45   ASSERT_NOEXCEPT(Eq >= 0);
     46 #ifndef TEST_HAS_NO_SPACESHIP_OPERATOR
     47   ASSERT_NOEXCEPT(0 <=> Eq);
     48   ASSERT_NOEXCEPT(Eq <=> 0);
     49   ASSERT_SAME_TYPE(decltype(Eq <=> 0), std::weak_ordering);
     50   ASSERT_SAME_TYPE(decltype(0 <=> Eq), std::weak_ordering);
     51 #endif
     52 }
     53 
     54 constexpr bool test_conversion() {
     55   static_assert(std::is_convertible<const std::weak_ordering&,
     56       std::weak_equality>::value, "");
     57   { // value == 0
     58     auto V = std::weak_ordering::equivalent;
     59     std::weak_equality WV = V;
     60     assert(WV == 0);
     61   }
     62   std::weak_ordering WeakTestCases[] = {
     63       std::weak_ordering::less,
     64       std::weak_ordering::greater,
     65   };
     66   for (auto V : WeakTestCases)
     67   { // value != 0
     68     std::weak_equality WV = V;
     69     assert(WV != 0);
     70   }
     71   static_assert(std::is_convertible<const std::weak_ordering&,
     72       std::partial_ordering>::value, "");
     73   { // value == 0
     74     auto V = std::weak_ordering::equivalent;
     75     std::partial_ordering WV = V;
     76     assert(WV == 0);
     77   }
     78   { // value < 0
     79     auto V = std::weak_ordering::less;
     80     std::partial_ordering WV = V;
     81     assert(WV < 0);
     82   }
     83   { // value > 0
     84     auto V = std::weak_ordering::greater;
     85     std::partial_ordering WV = V;
     86     assert(WV > 0);
     87   }
     88   return true;
     89 }
     90 
     91 constexpr bool test_constexpr() {
     92   auto& Eq = std::weak_ordering::equivalent;
     93   auto& Less = std::weak_ordering::less;
     94   auto& Greater = std::weak_ordering::greater;
     95   struct {
     96     std::weak_ordering Value;
     97     bool ExpectEq;
     98     bool ExpectNeq;
     99     bool ExpectLess;
    100     bool ExpectGreater;
    101   } TestCases[] = {
    102       {Eq, true, false, false, false},
    103       {Less, false, true, true, false},
    104       {Greater, false, true, false, true},
    105   };
    106   for (auto TC : TestCases) {
    107     auto V = TC.Value;
    108     assert((V == 0) == TC.ExpectEq);
    109     assert((0 == V) == TC.ExpectEq);
    110     assert((V != 0) == TC.ExpectNeq);
    111     assert((0 != V) == TC.ExpectNeq);
    112 
    113     assert((V < 0) == TC.ExpectLess);
    114     assert((V > 0) == TC.ExpectGreater);
    115     assert((V <= 0) == (TC.ExpectLess || TC.ExpectEq));
    116     assert((V >= 0) == (TC.ExpectGreater || TC.ExpectEq));
    117 
    118     assert((0 < V) == TC.ExpectGreater);
    119     assert((0 > V) == TC.ExpectLess);
    120     assert((0 <= V) == (TC.ExpectGreater || TC.ExpectEq));
    121     assert((0 >= V) == (TC.ExpectLess || TC.ExpectEq));
    122   }
    123 #ifndef TEST_HAS_NO_SPACESHIP_OPERATOR
    124   {
    125     std::weak_ordering res = (Eq <=> 0);
    126     ((void)res);
    127     res = (0 <=> Eq);
    128     ((void)res);
    129   }
    130   enum ExpectRes {
    131     ER_Greater,
    132     ER_Less,
    133     ER_Equiv
    134   };
    135   struct {
    136     std::weak_ordering Value;
    137     ExpectRes Expect;
    138   } SpaceshipTestCases[] = {
    139       {std::weak_ordering::equivalent, ER_Equiv},
    140       {std::weak_ordering::less, ER_Less},
    141       {std::weak_ordering::greater, ER_Greater},
    142   };
    143   for (auto TC : SpaceshipTestCases)
    144   {
    145     std::weak_ordering Res = (TC.Value <=> 0);
    146     switch (TC.Expect) {
    147     case ER_Equiv:
    148       assert(Res == 0);
    149       assert(0 == Res);
    150       break;
    151     case ER_Less:
    152       assert(Res < 0);
    153       break;
    154     case ER_Greater:
    155       assert(Res > 0);
    156       break;
    157     }
    158   }
    159 #endif
    160 
    161   return true;
    162 }
    163 
    164 int main() {
    165   test_static_members();
    166   test_signatures();
    167   static_assert(test_conversion(), "conversion test failed");
    168   static_assert(test_constexpr(), "constexpr test failed");
    169 }