]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/18_support/comparisons/algorithms/partial_order.cc
libsupc++: Implement comparison algorithms for C++20
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 18_support / comparisons / algorithms / partial_order.cc
CommitLineData
0ff15d21
JW
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.
8
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.
13
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/>.
17
18// { dg-options "-std=gnu++2a" }
19// { dg-do run { target c++2a } }
20
21#include <compare>
22#include <limits>
23#include <testsuite_hooks.h>
24
25using std::partial_order;
26using std::partial_ordering;
27
28void
29test01()
30{
31 int one = 1, two = 2;
32
33 VERIFY( partial_order(one, two) == partial_ordering::less );
34 VERIFY( partial_order(one, one) == partial_ordering::equivalent );
35 VERIFY( partial_order(two, one) == partial_ordering::greater );
36 static_assert( noexcept(partial_order(1, 1)) );
37}
38
39constexpr partial_ordering different_cv_quals(int i, const int j)
40{
41 return partial_order(i, j);
42}
43
44void
45test02()
46{
47 int fortytwo = 42, nines = 999, lots = 1000;
48 VERIFY( different_cv_quals(fortytwo, nines) == partial_ordering::less );
49 VERIFY( different_cv_quals(-nines, -nines) == partial_ordering::equivalent );
50 VERIFY( different_cv_quals(-nines, -lots) == partial_ordering::greater );
51}
52
53void
54test03()
55{
56 double zero = 0.0;
57 VERIFY( partial_order(zero, zero) == partial_ordering::equivalent );
58 VERIFY( partial_order(-zero, -zero) == partial_ordering::equivalent );
59 VERIFY( partial_order(-zero, zero) == partial_ordering::equivalent );
60 VERIFY( partial_order(zero, -zero) == partial_ordering::equivalent );
61 static_assert( noexcept(partial_order(zero, 1.0)) );
62 static_assert( partial_order(0.0, 1.0) == std::partial_ordering::less );
63
64 double min = std::numeric_limits<double>::lowest();
65 double max = std::numeric_limits<double>::max();
66 double nan = std::numeric_limits<double>::quiet_NaN();
67 double inf = std::numeric_limits<double>::infinity();
68 double denorm = std::numeric_limits<double>::denorm_min();
69 double smallest = std::numeric_limits<double>::min();
70 double epsilon = std::numeric_limits<double>::epsilon();
71 VERIFY( partial_order(denorm, smallest) == partial_ordering::less );
72 VERIFY( partial_order(denorm, 0.0) == partial_ordering::greater );
73 VERIFY( partial_order(0.0, nan) == partial_ordering::unordered );
74 VERIFY( partial_order(nan, nan) == partial_ordering::unordered );
75 VERIFY( partial_order(nan, 0.0) == partial_ordering::unordered );
76 VERIFY( partial_order(-nan, 0.0) == partial_ordering::unordered );
77 VERIFY( partial_order(-nan, min) == partial_ordering::unordered );
78 VERIFY( partial_order(-inf, min) == partial_ordering::less );
79 VERIFY( partial_order(-nan, -inf) == partial_ordering::unordered );
80 VERIFY( partial_order(-inf, -nan) == partial_ordering::unordered );
81 VERIFY( partial_order(max, inf) == partial_ordering::less );
82 VERIFY( partial_order(inf, max) == partial_ordering::greater );
83 VERIFY( partial_order(inf, nan) == partial_ordering::unordered );
84 VERIFY( partial_order(1.0, 1.0+epsilon) == partial_ordering::less );
85}
86
87namespace N
88{
89 struct X { int i; };
90
91 constexpr partial_ordering operator<=>(X l, X r)
92 {
93 if (l.i < 0 && r.i < 0)
94 return partial_ordering::equivalent;
95 return r.i <=> l.i;
96 }
97}
98
99void
100test04()
101{
102 using N::X;
103 X one{1};
104 X negone{-1};
105
106 VERIFY( partial_order(one, X{1}) == partial_ordering::equivalent );
107 VERIFY( partial_order(negone, X{-2}) == partial_ordering::equivalent );
108 VERIFY( partial_order(one, X{2}) == partial_ordering::greater );
109 static_assert( !noexcept(partial_order(X{1}, X{2})) );
110}
111
112int main()
113{
114 test01();
115 test02();
116 test03();
117 test04();
118}