xref: /llvm-project/libcxx/test/std/ranges/range.adaptors/range.elements/iterator/decrement.pass.cpp (revision 94461822c75d5080bf648f86552f7a59b76905c9)
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 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 
11 // constexpr iterator& operator--() requires bidirectional_range<Base>;
12 // constexpr iterator operator--(int) requires bidirectional_range<Base>;
13 
14 #include <array>
15 #include <cassert>
16 #include <ranges>
17 #include <tuple>
18 
19 #include "test_iterators.h"
20 
21 template <class Iter>
22 concept CanPreDecrement = requires(Iter it) { --it; };
23 
24 template <class Iter>
25 concept CanPostDecrement = requires(Iter it) { it--; };
26 
27 template <class Iter, class Sent = sentinel_wrapper<Iter>>
testOne()28 constexpr void testOne() {
29   using Range          = std::ranges::subrange<Iter, Sent>;
30   std::tuple<int> ts[] = {{1}, {2}, {3}};
31   auto ev              = Range{Iter{&ts[0]}, Sent{Iter{&ts[0] + 3}}} | std::views::elements<0>;
32 
33   using ElementIter = std::ranges::iterator_t<decltype(ev)>;
34 
35   if constexpr (!std::bidirectional_iterator<Iter>) {
36     auto it = ev.begin();
37     static_assert(!CanPreDecrement<decltype(it)>);
38     static_assert(!CanPostDecrement<decltype(it)>);
39   } else {
40     // --i
41     {
42       auto it = ev.begin();
43       static_assert(CanPreDecrement<decltype(it)>);
44 
45       ++it;
46       assert(base(it.base()) == &ts[1]);
47 
48       decltype(auto) result = --it;
49 
50       static_assert(std::is_same_v<decltype(result), ElementIter&>);
51       assert(&result == &it);
52 
53       assert(base(it.base()) == &ts[0]);
54     }
55 
56     // i--
57     {
58       auto it = ev.begin();
59       static_assert(CanPostDecrement<decltype(it)>);
60 
61       ++it;
62       assert(base(it.base()) == &ts[1]);
63 
64       decltype(auto) result = it--;
65 
66       static_assert(std::is_same_v<decltype(result), ElementIter>);
67 
68       assert(base(it.base()) == &ts[0]);
69       assert(base(result.base()) == &ts[1]);
70     }
71   }
72 }
73 
test()74 constexpr bool test() {
75   using Ptr = std::tuple<int>*;
76   testOne<cpp20_input_iterator<Ptr>>();
77   testOne<forward_iterator<Ptr>>();
78   testOne<bidirectional_iterator<Ptr>>();
79   testOne<random_access_iterator<Ptr>>();
80   testOne<contiguous_iterator<Ptr>>();
81   testOne<Ptr>();
82 
83   return true;
84 }
85 
main(int,char **)86 int main(int, char**) {
87   test();
88   static_assert(test());
89 
90   return 0;
91 }
92