|
16 | 16 | #include <type_traits> |
17 | 17 | #include <utility> |
18 | 18 | #include "test_iterators.h" |
19 | | -#include "../types.h" |
20 | 19 |
|
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" |
27 | 21 |
|
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 | +}; |
32 | 28 |
|
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 |
34 | 34 | { |
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 | + } |
37 | 45 |
|
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&>); |
39 | 53 | auto& result = ++it; |
40 | | - ASSERT_SAME_TYPE(ConcatIterator&, decltype(++it)); |
41 | 54 | 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); |
43 | 81 | } |
44 | 82 |
|
45 | 83 | // Increment an iterator multiple times |
46 | 84 | { |
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); |
49 | 86 |
|
50 | | - ConcatIterator it = view.begin(); |
51 | | - assert(*it == array[0]); |
| 87 | + auto it = view.begin(); |
| 88 | + assert(*it == a[0]); |
52 | 89 |
|
53 | 90 | ++it; |
54 | | - assert(*it == array[1]); |
55 | | - ++it; |
56 | | - assert(*it == array[2]); |
| 91 | + assert(*it == a[1]); |
57 | 92 | ++it; |
58 | | - assert(*it == array[3]); |
| 93 | + assert(*it == a[2]); |
59 | 94 | ++it; |
60 | | - assert(*it == array[4]); |
| 95 | + assert(*it == a[3]); |
61 | 96 | } |
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* >(); |
71 | 97 |
|
72 | 98 | return true; |
73 | 99 | } |
74 | 100 |
|
75 | 101 | int main(int, char**) { |
76 | | - tests(); |
| 102 | + test(); |
| 103 | + static_assert(test()); |
77 | 104 | return 0; |
78 | 105 | } |
0 commit comments