Debian Codegraph @

libstdc++-v3/testsuite/20_util/tuple/comparison_operators/three_way.cc

raw
1 // { dg-do run { target c++20 } }
2
3 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 // Tuple
21
22 #include <tuple>
23 #include <testsuite_hooks.h>
24
25 using namespace std;
26
27 template<typename T>
28 bool self_consistent(const T& x)
29 {
30 return std::is_eq(x <=> x) && x == x && !(x != x) && x <= x && !(x < x);
31 }
32
33 void
34 test01()
35 {
36 int i=0;
37 int j=0;
38 int k=2;
39 tuple<int, int, int> a(0, 0, 0);
40 tuple<int, int, int> b(0, 0, 1);
41 tuple<int& , int& , int&> c(i,j,k);
42 tuple<const int&, const int&, const int&> d(c);
43 VERIFY( self_consistent(a) );
44 VERIFY( self_consistent(b) );
45 VERIFY( self_consistent(c) );
46 VERIFY( self_consistent(d) );
47 VERIFY( !(a > a) && !(b > b) );
48 VERIFY( a >= a && b >= b );
49 VERIFY( a < b && !(b < a) && a <= b && !(b <= a) );
50 VERIFY( b > a && !(a > b) && b >= a && !(a >= b) );
51
52 VERIFY( std::is_lt(a <=> b) );
53 VERIFY( std::is_gt(b <=> a) );
54 VERIFY( std::is_gt(c <=> a) );
55 VERIFY( std::is_eq(c <=> d) );
56
57 static_assert( std::is_same_v<decltype(a <=> d), std::strong_ordering> );
58 }
59
60 template<typename T, typename U, typename C>
61 constexpr bool
62 check_compare(T&& t, U&& u, C c)
63 {
64 using R = std::compare_three_way_result_t<T, U>;
65 static_assert( std::same_as<C, R> );
66 return (t <=> u) == c;
67 }
68
69 void
70 test02()
71 {
72 using std::strong_ordering;
73 using std::weak_ordering;
74 using std::partial_ordering;
75
76 using T0 = std::tuple<>;
77 static_assert( check_compare(T0(), T0(), strong_ordering::equal) );
78
79 using Ti = std::tuple<int>;
80 using Tu = std::tuple<unsigned>;
81 static_assert( check_compare(Ti(1), Tu(1u), weak_ordering::equivalent) );
82 static_assert( check_compare(Ti(1), Tu(2u), weak_ordering::less) );
83 static_assert( check_compare(Ti(-1), Tu(1u), weak_ordering::greater) );
84
85 using Tii = std::tuple<int, int>;
86 using Tlu = std::tuple<long, unsigned>;
87 static_assert( check_compare(Tii(1, 2), Tlu(2l, 1u), weak_ordering::less) );
88
89 using Tid = std::tuple<int, double>;
90 static_assert( check_compare(Tii(3, 4), Tid(2, 0.9), partial_ordering::greater) );
91
92 static_assert( !std::three_way_comparable_with<T0, Ti> );
93 static_assert( !std::three_way_comparable_with<Ti, Tii> );
94 static_assert( !std::three_way_comparable_with<Ti, Tid> );
95 }
96
97 int main()
98 {
99 test01();
100 test02();
101 }