Debian Codegraph @

libstdc++-v3/testsuite/24_iterators/normal_iterator/cmp_c++20.cc

raw
1 // Copyright (C) 2020-2025 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-do compile { target c++20 } }
19
20 #include <iterator>
21 #include <vector>
22 #include <testsuite_allocator.h>
23
24 void
25 test01()
26 {
27 using V = std::vector<int>;
28 static_assert( std::totally_ordered<V::iterator> );
29 static_assert( std::three_way_comparable<V::iterator> );
30 using C = std::compare_three_way_result_t<V::iterator>;
31 static_assert( std::same_as<C, std::strong_ordering> );
32
33 static_assert( std::random_access_iterator<V::iterator> );
34 static_assert( std::random_access_iterator<V::const_iterator> );
35 }
36
37 // User-defined pointer type that supports operator< but not operator<=>
38 template<typename T>
39 struct Pointer : __gnu_test::PointerBase<Pointer<T>, T>
40 {
41 using __gnu_test::PointerBase<Pointer<T>, T>::PointerBase;
42
43 friend bool operator<(const Pointer& lhs, const Pointer& rhs) noexcept
44 { return lhs.value < rhs.value; }
45
46 std::partial_ordering operator<=>(const Pointer&) const = delete;
47 };
48
49 // Minimal allocator using Pointer<T>
50 template<typename T>
51 struct Alloc
52 {
53 typedef T value_type;
54 typedef Pointer<T> pointer;
55
56 Alloc() = default;
57 template<typename U>
58 Alloc(const Alloc<U>&) { }
59
60 pointer allocate(std::size_t n)
61 { return pointer(std::allocator<T>().allocate(n)); }
62
63 void deallocate(pointer p, std::size_t n)
64 { std::allocator<T>().deallocate(p.operator->(), n); }
65 };
66
67 void
68 test02()
69 {
70 using V = std::vector<int, Alloc<int>>;
71 static_assert( std::totally_ordered<V::iterator> );
72 static_assert( std::three_way_comparable<V::iterator> );
73 using C = std::compare_three_way_result_t<V::iterator>;
74 static_assert( std::same_as<C, std::weak_ordering> );
75
76 static_assert( std::random_access_iterator<V::iterator> );
77 static_assert( std::random_access_iterator<V::const_iterator> );
78 }
79
80 void
81 test03()
82 {
83 struct P : Pointer<int> {
84 bool operator<(const P&) const = delete;
85 };
86
87 struct C {
88 using pointer = P;
89 };
90
91 using I = __gnu_cxx::__normal_iterator<P, C>;
92 static_assert( ! std::totally_ordered<I> );
93 static_assert( ! std::three_way_comparable<I> );
94 }