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