xref: /llvm-project/libcxx/test/support/test_range.h (revision 808d794a45e169601ff16f72beae2f7bd79342a2)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LIBCXX_TEST_SUPPORT_TEST_RANGE_H
10 #define LIBCXX_TEST_SUPPORT_TEST_RANGE_H
11 
12 #include <concepts>
13 #include <functional>
14 #include <iterator>
15 #include <ranges>
16 #include <type_traits>
17 
18 #include "test_iterators.h"
19 
20 #if TEST_STD_VER < 17
21 #  error "test/support/test_range.h" can only be included in builds supporting ranges
22 #endif
23 
24 struct sentinel {
25   bool operator==(std::input_or_output_iterator auto const&) const;
26 };
27 
28 template <template <class...> class I, class T = int>
29   requires std::input_or_output_iterator<I<T*> >
30 struct test_range {
31   I<T*> begin();
32   I<T const*> begin() const;
33   sentinel end();
34   sentinel end() const;
35 };
36 
37 template <template <class...> class I, class T = int>
38   requires std::input_or_output_iterator<I<T*> >
39 struct test_non_const_range {
40   I<T*> begin();
41   sentinel end();
42 };
43 
44 template <template <class...> class I, class T = int>
45   requires std::input_or_output_iterator<I<T*> >
46 struct test_common_range {
47   I<T*> begin();
48   I<T const*> begin() const;
49   I<T*> end();
50   I<T const*> end() const;
51 };
52 
53 template <template <class...> class I, class T = int>
54   requires std::input_or_output_iterator<I<T*> >
55 struct test_non_const_common_range {
56   I<T*> begin();
57   I<T*> end();
58 };
59 
60 template <template <class...> class I, class T = int>
61   requires std::input_or_output_iterator<I<T*> >
62 struct test_view : std::ranges::view_base {
63   I<T*> begin();
64   I<T const*> begin() const;
65   sentinel end();
66   sentinel end() const;
67 };
68 
69 template <class T = int>
70 struct BorrowedRange {
71   T* begin() const;
72   T* end() const;
73   BorrowedRange(BorrowedRange&&) = delete;
74 };
75 template <class T>
76 inline constexpr bool std::ranges::enable_borrowed_range<BorrowedRange<T>> = true;
77 static_assert(!std::ranges::view<BorrowedRange<>>);
78 static_assert(std::ranges::borrowed_range<BorrowedRange<>>);
79 
80 using BorrowedView = std::ranges::empty_view<int>;
81 static_assert(std::ranges::view<BorrowedView>);
82 static_assert(std::ranges::borrowed_range<BorrowedView>);
83 
84 using NonBorrowedView = std::ranges::single_view<int>;
85 static_assert(std::ranges::view<NonBorrowedView>);
86 static_assert(!std::ranges::borrowed_range<NonBorrowedView>);
87 
88 template <class Range>
89 concept simple_view =
90     std::ranges::view<Range> && std::ranges::range<const Range> &&
91     std::same_as<std::ranges::iterator_t<Range>, std::ranges::iterator_t<const Range>> &&
92     std::same_as<std::ranges::sentinel_t<Range>, std::ranges::sentinel_t<const Range>>;
93 
94 template <class View, class T>
requires(View && view,T && t)95 concept CanBePiped = requires(View&& view, T&& t) {
96   { std::forward<View>(view) | std::forward<T>(t) };
97 };
98 
99 // See [concept.equalitycomparable]
100 template <class T, class U>
101 concept weakly_equality_comparable_with =
requires(const std::remove_reference_t<T> & t,const std::remove_reference_t<U> & u)102     requires(const std::remove_reference_t<T>& t, const std::remove_reference_t<U>& u) {
103       { t == u } -> std::same_as<bool>;
104       { t != u } -> std::same_as<bool>;
105       { u == t } -> std::same_as<bool>;
106       { u != t } -> std::same_as<bool>;
107     };
108 
109 #endif // LIBCXX_TEST_SUPPORT_TEST_RANGE_H
110