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, c++20 |
10 | |
11 | // constexpr iterator& operator++(); |
12 | // constexpr void operator++(int); |
13 | // constexpr iterator operator++(int) requires all_forward<Const, Views...>; |
14 | |
15 | #include <array> |
16 | #include <cassert> |
17 | #include <ranges> |
18 | #include <tuple> |
19 | |
20 | #include "../types.h" |
21 | |
22 | struct InputRange : IntBufferView { |
23 | using IntBufferView::IntBufferView; |
24 | using iterator = cpp20_input_iterator<int*>; |
25 | constexpr iterator begin() const { return iterator(buffer_); } |
26 | constexpr sentinel_wrapper<iterator> end() const { return sentinel_wrapper<iterator>(iterator(buffer_ + size_)); } |
27 | }; |
28 | |
29 | constexpr bool test() { |
30 | std::array a{1, 2, 3, 4}; |
31 | std::array b{4.1, 3.2, 4.3}; |
32 | { |
33 | // random/contiguous |
34 | std::ranges::zip_view v(a, b, std::views::iota(0, 5)); |
35 | auto it = v.begin(); |
36 | using Iter = decltype(it); |
37 | |
38 | assert(&(std::get<0>(*it)) == &(a[0])); |
39 | assert(&(std::get<1>(*it)) == &(b[0])); |
40 | assert(std::get<2>(*it) == 0); |
41 | |
42 | static_assert(std::is_same_v<decltype(++it), Iter&>); |
43 | |
44 | auto& it_ref = ++it; |
45 | assert(&it_ref == &it); |
46 | |
47 | assert(&(std::get<0>(*it)) == &(a[1])); |
48 | assert(&(std::get<1>(*it)) == &(b[1])); |
49 | assert(std::get<2>(*it) == 1); |
50 | |
51 | static_assert(std::is_same_v<decltype(it++), Iter>); |
52 | auto original = it; |
53 | auto copy = it++; |
54 | assert(original == copy); |
55 | assert(&(std::get<0>(*it)) == &(a[2])); |
56 | assert(&(std::get<1>(*it)) == &(b[2])); |
57 | assert(std::get<2>(*it) == 2); |
58 | } |
59 | |
60 | { |
61 | // bidi |
62 | int buffer[2] = {1, 2}; |
63 | |
64 | std::ranges::zip_view v(BidiCommonView{buffer}); |
65 | auto it = v.begin(); |
66 | using Iter = decltype(it); |
67 | |
68 | assert(&(std::get<0>(*it)) == &(buffer[0])); |
69 | |
70 | static_assert(std::is_same_v<decltype(++it), Iter&>); |
71 | auto& it_ref = ++it; |
72 | assert(&it_ref == &it); |
73 | assert(&(std::get<0>(*it)) == &(buffer[1])); |
74 | |
75 | static_assert(std::is_same_v<decltype(it++), Iter>); |
76 | auto original = it; |
77 | auto copy = it++; |
78 | assert(copy == original); |
79 | assert(&(std::get<0>(*it)) == &(buffer[2])); |
80 | } |
81 | |
82 | { |
83 | // forward |
84 | int buffer[2] = {1, 2}; |
85 | |
86 | std::ranges::zip_view v(ForwardSizedView{buffer}); |
87 | auto it = v.begin(); |
88 | using Iter = decltype(it); |
89 | |
90 | assert(&(std::get<0>(*it)) == &(buffer[0])); |
91 | |
92 | static_assert(std::is_same_v<decltype(++it), Iter&>); |
93 | auto& it_ref = ++it; |
94 | assert(&it_ref == &it); |
95 | assert(&(std::get<0>(*it)) == &(buffer[1])); |
96 | |
97 | static_assert(std::is_same_v<decltype(it++), Iter>); |
98 | auto original = it; |
99 | auto copy = it++; |
100 | assert(copy == original); |
101 | assert(&(std::get<0>(*it)) == &(buffer[2])); |
102 | } |
103 | |
104 | { |
105 | // all input+ |
106 | int buffer[3] = {4, 5, 6}; |
107 | std::ranges::zip_view v(a, InputRange{buffer}); |
108 | auto it = v.begin(); |
109 | using Iter = decltype(it); |
110 | |
111 | assert(&(std::get<0>(*it)) == &(a[0])); |
112 | assert(&(std::get<1>(*it)) == &(buffer[0])); |
113 | |
114 | static_assert(std::is_same_v<decltype(++it), Iter&>); |
115 | auto& it_ref = ++it; |
116 | assert(&it_ref == &it); |
117 | assert(&(std::get<0>(*it)) == &(a[1])); |
118 | assert(&(std::get<1>(*it)) == &(buffer[1])); |
119 | |
120 | static_assert(std::is_same_v<decltype(it++), void>); |
121 | it++; |
122 | assert(&(std::get<0>(*it)) == &(a[2])); |
123 | assert(&(std::get<1>(*it)) == &(buffer[2])); |
124 | } |
125 | |
126 | return true; |
127 | } |
128 | |
129 | int main(int, char**) { |
130 | test(); |
131 | static_assert(test()); |
132 | |
133 | return 0; |
134 | } |
135 | |