xref: /llvm-project/libcxx/test/support/test_range.h (revision 808d794a45e169601ff16f72beae2f7bd79342a2)
15a3309f8SChristopher Di Bella //===----------------------------------------------------------------------===//
25a3309f8SChristopher Di Bella //
35a3309f8SChristopher Di Bella // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45a3309f8SChristopher Di Bella // See https://llvm.org/LICENSE.txt for license information.
55a3309f8SChristopher Di Bella // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a3309f8SChristopher Di Bella //
75a3309f8SChristopher Di Bella //===----------------------------------------------------------------------===//
8480cd780SLouis Dionne 
95a3309f8SChristopher Di Bella #ifndef LIBCXX_TEST_SUPPORT_TEST_RANGE_H
105a3309f8SChristopher Di Bella #define LIBCXX_TEST_SUPPORT_TEST_RANGE_H
115a3309f8SChristopher Di Bella 
12e07a2f49SWill Hawkins #include <concepts>
13*808d794aSWill Hawkins #include <functional>
145a3309f8SChristopher Di Bella #include <iterator>
155a3309f8SChristopher Di Bella #include <ranges>
16*808d794aSWill Hawkins #include <type_traits>
175a3309f8SChristopher Di Bella 
185a3309f8SChristopher Di Bella #include "test_iterators.h"
195a3309f8SChristopher Di Bella 
20a1a30dc9SCasey Carter #if TEST_STD_VER < 17
21231cf0e8SJoe Loser #  error "test/support/test_range.h" can only be included in builds supporting ranges
225a3309f8SChristopher Di Bella #endif
235a3309f8SChristopher Di Bella 
245a3309f8SChristopher Di Bella struct sentinel {
25c05d1eedSChristopher Di Bella   bool operator==(std::input_or_output_iterator auto const&) const;
265a3309f8SChristopher Di Bella };
275a3309f8SChristopher Di Bella 
2839034388SChristopher Di Bella template <template <class...> class I, class T = int>
2939034388SChristopher Di Bella   requires std::input_or_output_iterator<I<T*> >
305a3309f8SChristopher Di Bella struct test_range {
3139034388SChristopher Di Bella   I<T*> begin();
3239034388SChristopher Di Bella   I<T const*> begin() const;
335a3309f8SChristopher Di Bella   sentinel end();
345a3309f8SChristopher Di Bella   sentinel end() const;
355a3309f8SChristopher Di Bella };
365a3309f8SChristopher Di Bella 
3739034388SChristopher Di Bella template <template <class...> class I, class T = int>
3839034388SChristopher Di Bella   requires std::input_or_output_iterator<I<T*> >
395a3309f8SChristopher Di Bella struct test_non_const_range {
4039034388SChristopher Di Bella   I<T*> begin();
415a3309f8SChristopher Di Bella   sentinel end();
425a3309f8SChristopher Di Bella };
435a3309f8SChristopher Di Bella 
4439034388SChristopher Di Bella template <template <class...> class I, class T = int>
4539034388SChristopher Di Bella   requires std::input_or_output_iterator<I<T*> >
465a3309f8SChristopher Di Bella struct test_common_range {
4739034388SChristopher Di Bella   I<T*> begin();
4839034388SChristopher Di Bella   I<T const*> begin() const;
4939034388SChristopher Di Bella   I<T*> end();
5039034388SChristopher Di Bella   I<T const*> end() const;
515a3309f8SChristopher Di Bella };
525a3309f8SChristopher Di Bella 
5339034388SChristopher Di Bella template <template <class...> class I, class T = int>
5439034388SChristopher Di Bella   requires std::input_or_output_iterator<I<T*> >
555a3309f8SChristopher Di Bella struct test_non_const_common_range {
5639034388SChristopher Di Bella   I<T*> begin();
5739034388SChristopher Di Bella   I<T*> end();
585a3309f8SChristopher Di Bella };
595a3309f8SChristopher Di Bella 
6039034388SChristopher Di Bella template <template <class...> class I, class T = int>
6139034388SChristopher Di Bella   requires std::input_or_output_iterator<I<T*> >
620f4b41e0Szoecarver struct test_view : std::ranges::view_base {
6339034388SChristopher Di Bella   I<T*> begin();
6439034388SChristopher Di Bella   I<T const*> begin() const;
650f4b41e0Szoecarver   sentinel end();
660f4b41e0Szoecarver   sentinel end() const;
670f4b41e0Szoecarver };
685a3309f8SChristopher Di Bella 
6939034388SChristopher Di Bella template <class T = int>
709021f368SArthur O'Dwyer struct BorrowedRange {
7139034388SChristopher Di Bella   T* begin() const;
7239034388SChristopher Di Bella   T* end() const;
739021f368SArthur O'Dwyer   BorrowedRange(BorrowedRange&&) = delete;
749021f368SArthur O'Dwyer };
7539034388SChristopher Di Bella template <class T>
7639034388SChristopher Di Bella inline constexpr bool std::ranges::enable_borrowed_range<BorrowedRange<T>> = true;
7739034388SChristopher Di Bella static_assert(!std::ranges::view<BorrowedRange<>>);
7839034388SChristopher Di Bella static_assert(std::ranges::borrowed_range<BorrowedRange<>>);
799021f368SArthur O'Dwyer 
809021f368SArthur O'Dwyer using BorrowedView = std::ranges::empty_view<int>;
819021f368SArthur O'Dwyer static_assert(std::ranges::view<BorrowedView>);
829021f368SArthur O'Dwyer static_assert(std::ranges::borrowed_range<BorrowedView>);
839021f368SArthur O'Dwyer 
849021f368SArthur O'Dwyer using NonBorrowedView = std::ranges::single_view<int>;
859021f368SArthur O'Dwyer static_assert(std::ranges::view<NonBorrowedView>);
869021f368SArthur O'Dwyer static_assert(!std::ranges::borrowed_range<NonBorrowedView>);
879021f368SArthur O'Dwyer 
88e07a2f49SWill Hawkins template <class Range>
89e07a2f49SWill Hawkins concept simple_view =
90e07a2f49SWill Hawkins     std::ranges::view<Range> && std::ranges::range<const Range> &&
91e07a2f49SWill Hawkins     std::same_as<std::ranges::iterator_t<Range>, std::ranges::iterator_t<const Range>> &&
92e07a2f49SWill Hawkins     std::same_as<std::ranges::sentinel_t<Range>, std::ranges::sentinel_t<const Range>>;
93e07a2f49SWill Hawkins 
94ba2236d3SWill Hawkins template <class View, class T>
requires(View && view,T && t)95ba2236d3SWill Hawkins concept CanBePiped = requires(View&& view, T&& t) {
96ba2236d3SWill Hawkins   { std::forward<View>(view) | std::forward<T>(t) };
97ba2236d3SWill Hawkins };
98ba2236d3SWill Hawkins 
99*808d794aSWill Hawkins // See [concept.equalitycomparable]
100*808d794aSWill Hawkins template <class T, class U>
101*808d794aSWill Hawkins concept weakly_equality_comparable_with =
requires(const std::remove_reference_t<T> & t,const std::remove_reference_t<U> & u)102*808d794aSWill Hawkins     requires(const std::remove_reference_t<T>& t, const std::remove_reference_t<U>& u) {
103*808d794aSWill Hawkins       { t == u } -> std::same_as<bool>;
104*808d794aSWill Hawkins       { t != u } -> std::same_as<bool>;
105*808d794aSWill Hawkins       { u == t } -> std::same_as<bool>;
106*808d794aSWill Hawkins       { u != t } -> std::same_as<bool>;
107*808d794aSWill Hawkins     };
108*808d794aSWill Hawkins 
1095a3309f8SChristopher Di Bella #endif // LIBCXX_TEST_SUPPORT_TEST_RANGE_H
110