1111// constexpr auto begin() requires (!simple-view<V>);
1212// constexpr auto begin() const requires range<const V>;
1313
14- #include < ranges>
1514#include < cassert>
15+ #include < ranges>
16+ #include < utility>
1617
1718#include " test_macros.h"
1819#include " test_iterators.h"
@@ -27,55 +28,104 @@ struct NonCommonSimpleView : std::ranges::view_base {
2728static_assert (std::ranges::sized_range<NonCommonSimpleView>);
2829static_assert (!std::ranges::sized_range<const NonCommonSimpleView>);
2930
31+ using CommonInputIterPtrConstInt = common_input_iterator<const int *>;
32+ using CountedCommonInputIterPtrConstInt = std::counted_iterator<CommonInputIterPtrConstInt>;
33+
3034constexpr bool test () {
3135 int buffer[8 ] = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
3236
33- // sized_range && random_access_iterator
37+ // simple-view<V> && sized_range<V> && random_access_range<V>
3438 {
35- std::ranges::take_view<SizedRandomAccessView> tv ( SizedRandomAccessView (buffer), 4 ) ;
36- assert (tv. begin () == SizedRandomAccessView (buffer). begin () );
37- ASSERT_SAME_TYPE ( decltype (tv. begin ()), RandomAccessIter );
38- }
39+ using ViewTested = SizedRandomAccessView ;
40+ static_assert (simple_view<ViewTested> );
41+ static_assert (std::ranges::sized_range<ViewTested> );
42+ static_assert (std::ranges::random_access_range<ViewTested>);
3943
40- {
41- const std::ranges::take_view<SizedRandomAccessView> tv (SizedRandomAccessView (buffer), 4 );
42- assert (tv.begin () == SizedRandomAccessView (buffer).begin ());
44+ std::ranges::take_view<ViewTested> tv (ViewTested (buffer), 4 );
45+ assert (tv.begin () == ViewTested (buffer).begin ());
4346 ASSERT_SAME_TYPE (decltype (tv.begin ()), RandomAccessIter);
44- }
4547
46- // sized_range && !random_access_iterator
47- {
48- std::ranges::take_view<SizedForwardView> tv (SizedForwardView{buffer}, 4 );
49- assert (tv.begin () == std::counted_iterator<ForwardIter>(ForwardIter (buffer), 4 ));
50- ASSERT_SAME_TYPE (decltype (tv.begin ()), std::counted_iterator<ForwardIter>);
48+ const std::ranges::take_view<ViewTested> ctv (ViewTested (buffer), 4 );
49+ assert (ctv.begin () == ViewTested (buffer).begin ());
50+ ASSERT_SAME_TYPE (decltype (ctv.begin ()), RandomAccessIter);
5151 }
5252
53+ // simple-view<V> && sized_range<V> && !random_access_range<V>
5354 {
54- const std::ranges::take_view<SizedForwardView> tv (SizedForwardView{buffer}, 4 );
55- assert (tv.begin () == std::counted_iterator<ForwardIter>(ForwardIter (buffer), 4 ));
55+ using ViewTested = SizedForwardView;
56+ static_assert (simple_view<ViewTested>);
57+ static_assert (std::ranges::sized_range<ViewTested>);
58+ static_assert (!std::ranges::random_access_range<ViewTested>);
59+
60+ std::ranges::take_view<ViewTested> tv (ViewTested{buffer}, 16 ); // underlying size is 8
61+ assert (tv.begin () == std::counted_iterator<ForwardIter>(ForwardIter (buffer), 8 )); // expect min(8, 16)
5662 ASSERT_SAME_TYPE (decltype (tv.begin ()), std::counted_iterator<ForwardIter>);
63+
64+ const std::ranges::take_view<ViewTested> ctv (ViewTested{buffer}, 4 );
65+ assert (ctv.begin () == std::counted_iterator<ForwardIter>(ForwardIter (buffer), 4 ));
66+ ASSERT_SAME_TYPE (decltype (ctv.begin ()), std::counted_iterator<ForwardIter>);
5767 }
5868
59- // !sized_range
69+ // simple-view<V> && !sized_range<V>
6070 {
61- std::ranges::take_view<MoveOnlyView> tv (MoveOnlyView{buffer}, 4 );
71+ using ViewTested = MoveOnlyView;
72+ static_assert (simple_view<ViewTested>);
73+ std::ranges::take_view<ViewTested> tv (ViewTested{buffer}, 4 );
6274 assert (tv.begin () == std::counted_iterator<int *>(buffer, 4 ));
6375 ASSERT_SAME_TYPE (decltype (tv.begin ()), std::counted_iterator<int *>);
76+
77+ const std::ranges::take_view<ViewTested> ctv (ViewTested{buffer}, 4 );
78+ assert (ctv.begin () == std::counted_iterator<int *>(buffer, 4 ));
79+ ASSERT_SAME_TYPE (decltype (ctv.begin ()), std::counted_iterator<int *>);
6480 }
6581
82+ // simple-view<V> && sized_range<V> && !sized_range<const V>
6683 {
67- const std::ranges::take_view<MoveOnlyView> tv (MoveOnlyView{buffer}, 4 );
68- assert (tv.begin () == std::counted_iterator<int *>(buffer, 4 ));
84+ using ViewTested = NonCommonSimpleView;
85+ static_assert (simple_view<ViewTested>);
86+ static_assert (std::ranges::sized_range<ViewTested>);
87+ static_assert (!std::ranges::sized_range<const ViewTested>);
88+
89+ std::ranges::take_view<ViewTested> tv{};
6990 ASSERT_SAME_TYPE (decltype (tv.begin ()), std::counted_iterator<int *>);
91+ ASSERT_SAME_TYPE (decltype (std::as_const (tv).begin ()), std::counted_iterator<int *>);
7092 }
7193
72- // simple-view<V> && sized_range<V> && !size_range<! V>
94+ // ! simple-view<V> && ! sized_range<V>
7395 {
74- std::ranges::take_view<NonCommonSimpleView> tv{};
75- ASSERT_SAME_TYPE (decltype (tv.begin ()), std::counted_iterator<int *>);
76- ASSERT_SAME_TYPE (decltype (std::as_const (tv).begin ()), std::counted_iterator<int *>);
96+ using ViewTested = NonSimpleNonSizedView;
97+ static_assert (!simple_view<ViewTested>);
98+ static_assert (!std::ranges::sized_range<ViewTested>);
99+
100+ std::ranges::take_view<ViewTested> tv{ViewTested{buffer, buffer + 2 }, 4 };
101+ // The count for the counted iterator is the count of the take_view (i.e., 4)
102+ assert (tv.begin () == CountedCommonInputIterPtrConstInt (CommonInputIterPtrConstInt (buffer), 4 ));
103+ ASSERT_SAME_TYPE (decltype (tv.begin ()), CountedCommonInputIterPtrConstInt);
77104 }
78105
106+ // !simple-view<V> && sized_range<V>
107+ {
108+ using ViewTested = NonSimpleSizedView;
109+ static_assert (!simple_view<ViewTested>);
110+ static_assert (std::ranges::sized_range<ViewTested>);
111+
112+ std::ranges::take_view<ViewTested> tv{ViewTested{buffer, buffer + 2 }, 4 };
113+ // The count for the counted iterator is the min(2, 4) (i.e., 2).
114+ assert (tv.begin () == CountedCommonInputIterPtrConstInt (CommonInputIterPtrConstInt (buffer), 2 ));
115+ ASSERT_SAME_TYPE (decltype (tv.begin ()), CountedCommonInputIterPtrConstInt);
116+ }
117+
118+ // !simple-view<V> && sized_range<V> && random_access_range<V>
119+ {
120+ using ViewTested = NonSimpleSizedRandomView;
121+ static_assert (!simple_view<ViewTested>);
122+ static_assert (std::ranges::sized_range<ViewTested>);
123+ static_assert (std::ranges::random_access_range<ViewTested>);
124+
125+ std::ranges::take_view<ViewTested> tv{ViewTested{buffer, buffer + 2 }, 4 };
126+ assert (tv.begin () == random_access_iterator<const int *>(buffer));
127+ ASSERT_SAME_TYPE (decltype (tv.begin ()), random_access_iterator<const int *>);
128+ }
79129 return true ;
80130}
81131
0 commit comments