Skip to content

Commit 4907ba6

Browse files
<ranges>: Test c(begin|end) members of range factories (#3553)
Co-authored-by: Stephan T. Lavavej <[email protected]>
1 parent 1e7d460 commit 4907ba6

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed

tests/std/tests/P0896R4_istream_view/test.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,23 @@ void test_one_type() {
119119
static_assert(noexcept(views::istream<T>(wintstream)));
120120
assert(ranges::equal(input, expected_vals));
121121
}
122+
#if _HAS_CXX23
123+
{ // Using ranges::istream_view with const iterators
124+
istringstream intstream{"0 1 2 3"};
125+
T input[] = {-1, -1, -1, -1, -1};
126+
ranges::istream_view<T> v(intstream);
127+
ranges::copy(v.cbegin(), v.cend(), input);
128+
assert(ranges::equal(input, expected_vals));
129+
}
130+
131+
{ // Using ranges::wistream_view with const iterators
132+
wistringstream wintstream{L"0 1 2 3"};
133+
T input[] = {-1, -1, -1, -1, -1};
134+
ranges::wistream_view<T> v(wintstream);
135+
ranges::copy(v.cbegin(), v.cend(), input);
136+
assert(ranges::equal(input, expected_vals));
137+
}
138+
#endif // _HAS_CXX23
122139
}
123140

124141
istringstream some_stream{"42"};

tests/std/tests/P0896R4_views_empty/test.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ constexpr bool test_one_type() {
2020
static_assert(ranges::view<R> && ranges::contiguous_range<R> && ranges::sized_range<R> && ranges::common_range<R>);
2121
static_assert(ranges::borrowed_range<R>);
2222
static_assert(same_as<const R, decltype(views::empty<T>)>);
23-
auto& r = views::empty<T>;
23+
constexpr auto& r = views::empty<T>;
2424

2525
// validate member size
2626
static_assert(same_as<decltype(R::size()), size_t>);
@@ -50,7 +50,16 @@ constexpr bool test_one_type() {
5050
static_assert(noexcept(R::empty()));
5151
static_assert(noexcept(ranges::empty(r)));
5252

53-
// validate members inherited from view_interface
53+
#if _HAS_CXX23
54+
// validate members cbegin and cend inherited from view_interface
55+
static_assert(same_as<decltype(r.cbegin()), const_iterator<T*>>);
56+
static_assert(r.cbegin() == nullptr);
57+
58+
static_assert(same_as<decltype(r.cend()), const_iterator<T*>>);
59+
static_assert(r.cend() == nullptr);
60+
#endif // _HAS_CXX23
61+
62+
// validate other members inherited from view_interface
5463
assert(!r);
5564

5665
return true;

tests/std/tests/P0896R4_views_iota/test.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ constexpr void test_integral() {
197197
static_assert(noexcept(first != last)); // strengthened
198198
assert(last - first == 8);
199199
static_assert(noexcept(last - first)); // strengthened
200+
201+
#if _HAS_CXX23
202+
const same_as<ranges::const_iterator_t<R>> auto cfirst = rng.cbegin();
203+
assert(cfirst == first);
204+
const same_as<ranges::const_sentinel_t<R>> auto clast = rng.cend();
205+
assert(clast == last);
206+
assert(clast - cfirst == 8);
207+
#endif // _HAS_CXX23
200208
}
201209

202210
{
@@ -234,6 +242,21 @@ constexpr void test_integral() {
234242
}
235243

236244
static_assert(!CanSize<ranges::iota_view<T>>);
245+
246+
#if _HAS_CXX23
247+
{
248+
const same_as<R> auto rng = views::iota(low);
249+
const ranges::subrange crng{rng.cbegin(), rng.cend()};
250+
251+
auto i = low;
252+
for (const auto& e : crng) {
253+
assert(e == i);
254+
if (++i == high) {
255+
break;
256+
}
257+
}
258+
}
259+
#endif // _HAS_CXX23
237260
}
238261
}
239262

tests/std/tests/P0896R4_views_single/test.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ constexpr bool test_one_type(T value, Args&&... args) {
9494
static_assert(noexcept(cr0.end()));
9595
static_assert(noexcept(ranges::end(cr0)));
9696

97+
#if _HAS_CXX23
98+
// validate members cbegin and cend
99+
static_assert(same_as<decltype(r0.cbegin()), const T*>);
100+
assert(*r0.cbegin() == value);
101+
assert(r0.cbegin() == ptr);
102+
103+
static_assert(same_as<decltype(r0.cend()), const T*>);
104+
assert(r0.cend() == ptr + 1);
105+
106+
static_assert(same_as<decltype(cr0.cbegin()), const T*>);
107+
assert(*cr0.cbegin() == value);
108+
assert(cr0.cbegin() == cptr);
109+
110+
static_assert(same_as<decltype(cr0.cend()), const T*>);
111+
assert(cr0.cend() == cptr + 1);
112+
#endif // _HAS_CXX23
113+
97114
// validate CTAD and T&& constructor
98115
const same_as<R> auto cr1 = ranges::single_view{move(value)};
99116
assert(cr1.data() != nullptr);

tests/std/tests/P2474R2_views_repeat/test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,16 @@ constexpr void test_common(T val, B bound = unreachable_sentinel) {
252252
assert(cmp_equal(last - first, rng.size()));
253253
static_assert(noexcept(last - first)); // strengthened
254254
}
255+
256+
const same_as<ranges::const_iterator_t<R>> auto cfirst = rng.cbegin();
257+
assert(cfirst == first);
258+
const same_as<ranges::const_sentinel_t<R>> auto clast = rng.cend();
259+
if constexpr (ranges::common_range<R>) {
260+
assert(clast == last);
261+
assert(cmp_equal(clast - cfirst, rng.size()));
262+
} else {
263+
static_assert(same_as<remove_const_t<decltype(clast)>, unreachable_sentinel_t>);
264+
}
255265
}
256266

257267
struct move_tester {

0 commit comments

Comments
 (0)