Skip to content

Commit 123474b

Browse files
decrement test
1 parent 6cf838e commit 123474b

File tree

1 file changed

+60
-18
lines changed

1 file changed

+60
-18
lines changed

libcxx/test/std/ranges/range.adaptors/range.concat/iterator/decrement.pass.cpp

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,74 +13,116 @@
1313
#include <array>
1414
#include <cassert>
1515
#include "test_macros.h"
16-
#include "../types.h"
16+
#include "../../range_adaptor_types.h"
17+
18+
template <class Iter>
19+
concept canDecrement = requires(Iter it) { --it; } || requires(Iter it) { it--; };
20+
21+
struct NonBidi : IntBufferView {
22+
using IntBufferView::IntBufferView;
23+
using iterator = forward_iterator<int*>;
24+
constexpr iterator begin() const { return iterator(buffer_); }
25+
constexpr sentinel_wrapper<iterator> end() const { return sentinel_wrapper<iterator>(iterator(buffer_ + size_)); }
26+
};
27+
28+
constexpr bool test() {
29+
std::array<int, 4> a{1, 2, 3, 4};
30+
std::array<int, 4> b{5, 6, 7, 8};
1731

18-
constexpr void test() {
1932
// Test with a single view
2033
{
21-
constexpr static std::array<int, 5> array{0, 1, 2, 3, 4};
22-
constexpr static std::ranges::concat_view view(std::views::all(array));
34+
std::ranges::concat_view view(a);
2335
auto it = std::ranges::next(view.begin(), view.end());
2436
assert(it == view.end());
2537

2638
auto& result = --it;
2739
ASSERT_SAME_TYPE(decltype(result)&, decltype(--it));
2840
assert(&result == &it);
29-
assert(result == view.begin() + 4);
41+
assert(result == view.begin() + 3);
3042
}
3143

3244
// Test with more than one view
3345
{
34-
constexpr static std::array<int, 3> array{0, 1, 2};
35-
constexpr static std::array<int, 3> array1{3, 4, 5};
36-
constexpr std::ranges::concat_view view(std::views::all(array), std::views::all(array1));
46+
std::ranges::concat_view view(a, b);
3747
auto it = std::ranges::next(view.begin(), view.end());
3848
assert(it == view.end());
3949

4050
auto& result = --it;
4151
assert(&result == &it);
4252

4353
--it;
44-
assert(*it == 4);
45-
assert(it == view.begin() + 4);
54+
assert(*it == 7);
55+
assert(it == view.begin() + 6);
4656
}
4757

4858
// Test going forward and then backward on the same iterator
4959
{
50-
constexpr static std::array<int, 5> array{0, 1, 2, 3, 4};
51-
constexpr static std::ranges::concat_view view(std::views::all(array));
60+
std::ranges::concat_view view(a, b);
5261
auto it = view.begin();
5362
++it;
5463
--it;
55-
assert(*it == array[0]);
64+
assert(*it == a[0]);
5665
++it;
5766
++it;
5867
--it;
59-
assert(*it == array[1]);
68+
assert(*it == a[1]);
6069
++it;
6170
++it;
6271
--it;
63-
assert(*it == array[2]);
72+
assert(*it == a[2]);
6473
++it;
6574
++it;
6675
--it;
67-
assert(*it == array[3]);
76+
assert(*it == a[3]);
6877
}
6978

7079
// Test post-decrement
7180
{
72-
std::array<int, 5> array{0, 1, 2, 3, 4};
73-
std::ranges::concat_view view(std::views::all(array));
81+
std::ranges::concat_view view(a, b);
7482
auto it = std::ranges::next(view.begin(), view.end());
7583
assert(it == view.end()); // test the test
7684
auto result = it--;
7785
ASSERT_SAME_TYPE(decltype(result), decltype(it--));
7886
assert(result == view.end());
7987
assert(it == (result - 1));
8088
}
89+
90+
// bidirectional
91+
{
92+
int buffer[2] = {1, 2};
93+
94+
std::ranges::concat_view v(BidiCommonView{buffer}, std::views::iota(0, 5));
95+
auto it = v.begin();
96+
using Iter = decltype(it);
97+
98+
++it;
99+
++it;
100+
101+
static_assert(std::is_same_v<decltype(--it), Iter&>);
102+
auto& it_ref = --it;
103+
assert(&it_ref == &it);
104+
105+
assert(it == ++v.begin());
106+
107+
static_assert(std::is_same_v<decltype(it--), Iter>);
108+
auto tmp = it--;
109+
assert(it == v.begin());
110+
assert(tmp == ++v.begin());
111+
}
112+
113+
// non bidirectional
114+
{
115+
int buffer[3] = {4, 5, 6};
116+
std::ranges::zip_view v(a, NonBidi{buffer});
117+
using Iter = std::ranges::iterator_t<decltype(v)>;
118+
static_assert(!canDecrement<Iter>);
119+
}
120+
121+
return true;
81122
}
82123

83124
int main(int, char**) {
84125
test();
126+
static_assert(test());
85127
return 0;
86128
}

0 commit comments

Comments
 (0)