Skip to content

Commit 6cf838e

Browse files
increment test iterator
1 parent d0521d4 commit 6cf838e

File tree

1 file changed

+63
-36
lines changed

1 file changed

+63
-36
lines changed

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

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,63 +16,90 @@
1616
#include <type_traits>
1717
#include <utility>
1818
#include "test_iterators.h"
19-
#include "../types.h"
2019

21-
template <class Iterator>
22-
constexpr void test() {
23-
using Sentinel = sentinel_wrapper<Iterator>;
24-
using View = minimal_view<Iterator, Sentinel>;
25-
using ConcatView = std::ranges::concat_view<View>;
26-
using ConcatIterator = std::ranges::iterator_t<ConcatView>;
20+
#include "../../range_adaptor_types.h"
2721

28-
auto make_concat_view = [](auto begin, auto end) {
29-
View view{Iterator(begin), Sentinel(Iterator(end))};
30-
return ConcatView(std::move(view));
31-
};
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+
};
3228

33-
// Increment an iterator when it won't find another satisfied value after begin()
29+
constexpr bool test() {
30+
std::array<int, 4> a{1, 2, 3, 4};
31+
std::array<double, 4> b{1.1, 2.2, 3.3};
32+
33+
// one view
3434
{
35-
std::array<int, 5> array{0, 1, 2, 3, 4};
36-
ConcatView view = make_concat_view(array.data(), array.data() + array.size());
35+
std::ranges::concat_view view(a);
36+
auto it = view.begin();
37+
using Iter = decltype(it);
38+
static_assert(std::is_same_v<decltype(it++), Iter>);
39+
static_assert(std::is_same_v<decltype(++it), Iter&>);
40+
41+
auto& result = ++it;
42+
assert(&result == &it);
43+
assert(*result == 2);
44+
}
3745

38-
auto it = view.begin();
46+
// more than one view
47+
{
48+
std::ranges::concat_view view(a, b);
49+
auto it = view.begin();
50+
using Iter = decltype(it);
51+
static_assert(std::is_same_v<decltype(it++), Iter>);
52+
static_assert(std::is_same_v<decltype(++it), Iter&>);
3953
auto& result = ++it;
40-
ASSERT_SAME_TYPE(ConcatIterator&, decltype(++it));
4154
assert(&result == &it);
42-
assert(*result == 1);
55+
assert(*result == 2);
56+
}
57+
58+
// more than one view
59+
{
60+
std::ranges::concat_view view(a, b, std::views::iota(0, 5));
61+
auto it = view.begin();
62+
using Iter = decltype(it);
63+
static_assert(std::is_same_v<decltype(it++), Iter>);
64+
static_assert(std::is_same_v<decltype(++it), Iter&>);
65+
auto& result = ++it;
66+
assert(&result == &it);
67+
assert(*result == 2);
68+
}
69+
70+
// input range
71+
{
72+
int buffer[3] = {4, 5, 6};
73+
std::ranges::concat_view view(a, InputRange{buffer});
74+
auto it = view.begin();
75+
using Iter = decltype(it);
76+
static_assert(std::is_same_v<decltype(it++), void>);
77+
static_assert(std::is_same_v<decltype(++it), Iter&>);
78+
auto& result = ++it;
79+
assert(&result == &it);
80+
assert(*result == 2);
4381
}
4482

4583
// Increment an iterator multiple times
4684
{
47-
std::array<int, 10> array{0, 1, 2, 3, 4};
48-
ConcatView view = make_concat_view(array.data(), array.data() + array.size());
85+
std::ranges::concat_view view(a);
4986

50-
ConcatIterator it = view.begin();
51-
assert(*it == array[0]);
87+
auto it = view.begin();
88+
assert(*it == a[0]);
5289

5390
++it;
54-
assert(*it == array[1]);
55-
++it;
56-
assert(*it == array[2]);
91+
assert(*it == a[1]);
5792
++it;
58-
assert(*it == array[3]);
93+
assert(*it == a[2]);
5994
++it;
60-
assert(*it == array[4]);
95+
assert(*it == a[3]);
6196
}
62-
}
63-
64-
constexpr bool tests() {
65-
test<cpp17_input_iterator<int*> >();
66-
test<forward_iterator<int*> >();
67-
test<bidirectional_iterator<int*> >();
68-
test<random_access_iterator<int*> >();
69-
test<contiguous_iterator<int*> >();
70-
test<int* >();
7197

7298
return true;
7399
}
74100

75101
int main(int, char**) {
76-
tests();
102+
test();
103+
static_assert(test());
77104
return 0;
78105
}

0 commit comments

Comments
 (0)