Skip to content

Commit ea055e2

Browse files
random access iterator test
1 parent 5dd4754 commit ea055e2

File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed

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

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,71 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// REQUIRES: has-unix-headers, std-at-least-c++26
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10+
11+
// constexpr auto operator[](difference_type n) const requires
12+
// all_random_access<Const, Views...>
1013

1114
#include <ranges>
1215
#include <cassert>
13-
#include <vector>
16+
17+
#include "../../range_adaptor_types.h"
1418

1519
constexpr bool test() {
16-
std::vector<int> v1 = {1, 2, 3};
17-
std::vector<int> v2 = {4, 5, 6};
18-
auto cv = std::views::concat(v1, v2);
19-
static_assert(std::random_access_iterator<decltype(cv.begin())>);
20-
assert(cv[0] == 1);
21-
assert(cv[2] == 3);
22-
assert(cv[3] == 4);
23-
assert(cv[5] == 6);
20+
int buffer1[8] = {1, 2, 3, 4, 5, 6, 7, 8};
21+
int buffer2[8] = {1, 2, 3, 4, 5, 6, 7, 8};
22+
23+
{
24+
// random_access_range and common range
25+
std::ranges::concat_view v(SimpleCommonRandomAccessSized{buffer1});
26+
auto it = v.begin();
27+
assert(it[0] == *it);
28+
assert(it[2] == *(it + 2));
29+
assert(it[4] == *(it + 4));
30+
31+
static_assert(std::is_same_v<decltype(it[2]), const int&>);
32+
}
33+
34+
{
35+
// random_access_range and common range, with last view is not a common range
36+
std::ranges::concat_view v(SimpleCommonRandomAccessSized{buffer1}, SimpleNonCommonRandomAccessSized{buffer2});
37+
auto it = v.begin();
38+
assert(it[0] == *it);
39+
assert(it[2] == *(it + 2));
40+
assert(it[4] == *(it + 4));
41+
42+
static_assert(std::is_same_v<decltype(it[2]), const int&>);
43+
}
44+
45+
{
46+
// random_access_range and non common range
47+
std::ranges::concat_view v(SimpleNonCommonRandomAccessSized{buffer1}, NonSimpleCommonRandomAccessSized{buffer2});
48+
auto it = v.begin();
49+
const auto canSubscript = [](auto&& it) { return requires { it[0]; }; };
50+
static_assert(!canSubscript(it));
51+
52+
static_assert(std::is_same_v<decltype(*it), const int&>);
53+
}
54+
55+
{
56+
// contiguous_range
57+
std::ranges::concat_view v(ContiguousCommonView{buffer1}, ContiguousCommonView{buffer2});
58+
auto it = v.begin();
59+
assert(it[0] == *it);
60+
assert(it[2] == *(it + 2));
61+
assert(it[4] == *(it + 4));
62+
63+
static_assert(std::is_same_v<decltype(it[2]), int&>);
64+
}
65+
66+
{
67+
// non random_access_range
68+
std::ranges::concat_view v(BidiCommonView{buffer1});
69+
auto iter = v.begin();
70+
const auto canSubscript = [](auto&& it) { return requires { it[0]; }; };
71+
static_assert(!canSubscript(iter));
72+
}
73+
2474
return true;
2575
}
2676

0 commit comments

Comments
 (0)