libsupc++: Implement comparison algorithms for C++20
[official-gcc.git] / libstdc++-v3 / testsuite / 18_support / comparisons / algorithms / weak_order.cc
blob03a162b86d56c63766d049fa9048add1bb5bf1ea
1 // Copyright (C) 2019 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
21 #include <compare>
22 #include <limits>
23 #include <testsuite_hooks.h>
25 using std::weak_order;
26 using std::weak_ordering;
28 void
29 test01()
31 int one = 1, two = 2;
33 VERIFY( weak_order(one, two) == weak_ordering::less );
34 VERIFY( weak_order(one, one) == weak_ordering::equivalent );
35 VERIFY( weak_order(two, one) == weak_ordering::greater );
36 static_assert( noexcept(weak_order(1, 1)) );
39 constexpr weak_ordering different_cv_quals(int i, const int j)
41 return weak_order(i, j);
44 void
45 test02()
47 int fortytwo = 42, nines = 999, lots = 1000;
49 VERIFY( different_cv_quals(fortytwo, nines) == weak_ordering::less );
50 VERIFY( different_cv_quals(-nines, -nines) == weak_ordering::equivalent );
51 VERIFY( different_cv_quals(-nines, -lots) == weak_ordering::greater );
54 void
55 test03()
57 double zero = 0.0;
58 VERIFY( weak_order(zero, zero) == weak_ordering::equivalent );
59 VERIFY( weak_order(-zero, -zero) == weak_ordering::equivalent );
60 VERIFY( weak_order(-zero, zero) == weak_ordering::equivalent );
61 VERIFY( weak_order(zero, -zero) == weak_ordering::equivalent );
63 double min = std::numeric_limits<double>::lowest();
64 double max = std::numeric_limits<double>::max();
65 double nan = std::numeric_limits<double>::quiet_NaN();
66 double inf = std::numeric_limits<double>::infinity();
67 double denorm = std::numeric_limits<double>::denorm_min();
68 double smallest = std::numeric_limits<double>::min();
69 double epsilon = std::numeric_limits<double>::epsilon();
70 VERIFY( weak_order(denorm, smallest) == weak_ordering::less );
71 VERIFY( weak_order(denorm, 0.0) == weak_ordering::greater );
72 VERIFY( weak_order(0.0, nan) == weak_ordering::less );
73 VERIFY( weak_order(nan, nan) == weak_ordering::equivalent );
74 VERIFY( weak_order(nan, -nan) == weak_ordering::greater );
75 VERIFY( weak_order(-nan, nan) == weak_ordering::less );
76 VERIFY( weak_order(nan, 0.0) == weak_ordering::greater );
77 VERIFY( weak_order(-nan, 0.0) == weak_ordering::less );
78 VERIFY( weak_order(-nan, min) == weak_ordering::less );
79 VERIFY( weak_order(-inf, min) == weak_ordering::less );
80 VERIFY( weak_order(-nan, -inf) == weak_ordering::less );
81 VERIFY( weak_order(-inf, -nan) == weak_ordering::greater );
82 VERIFY( weak_order(max, inf) == weak_ordering::less );
83 VERIFY( weak_order(inf, max) == weak_ordering::greater );
84 VERIFY( weak_order(inf, nan) == weak_ordering::less );
85 VERIFY( weak_order(1.0, 1.0+epsilon) == weak_ordering::less );
88 namespace N
90 struct X { int i; };
92 constexpr weak_ordering operator<=>(X l, X r)
94 if (l.i < 0 && r.i < 0)
95 return weak_ordering::equivalent;
96 return r.i <=> l.i;
100 void
101 test04()
103 using N::X;
104 X one{1};
105 X negone{-1};
107 VERIFY( weak_order(one, X{1}) == weak_ordering::equivalent );
108 VERIFY( weak_order(negone, X{-2}) == weak_ordering::equivalent );
109 VERIFY( weak_order(one, X{2}) == weak_ordering::greater );
110 static_assert( !noexcept(weak_order(X{1}, X{2})) );
113 int main()
115 test01();
116 test02();
117 test03();
118 test04();