| 1 |
// { dg-do run { target c++23 } } |
| 2 |
|
| 3 |
#include <deque> |
| 4 |
#include <span> |
| 5 |
#include <testsuite_hooks.h> |
| 6 |
#include <testsuite_iterators.h> |
| 7 |
#include <testsuite_allocator.h> |
| 8 |
|
| 9 |
template<typename Range, typename Alloc> |
| 10 |
constexpr void |
| 11 |
do_test() |
| 12 |
{ |
| 13 |
// The deque's value_type. |
| 14 |
using V = typename std::allocator_traits<Alloc>::value_type; |
| 15 |
|
| 16 |
// The range's value_type. |
| 17 |
using T = std::ranges::range_value_t<Range>; |
| 18 |
T a[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14}; |
| 19 |
|
| 20 |
auto eq = [](const std::deque<V, Alloc>& l, std::span<T> r) { |
| 21 |
if (l.size() != r.size()) |
| 22 |
return false; |
| 23 |
for (auto i = 0u; i < l.size(); ++i) |
| 24 |
if (l[i] != r[i]) |
| 25 |
return false; |
| 26 |
return true; |
| 27 |
}; |
| 28 |
|
| 29 |
V const* p[std::size(a)]{}; |
| 30 |
auto stable = [](const std::deque<V, Alloc>& l, std::span<V const*> r) { |
| 31 |
if (l.size() != r.size()) |
| 32 |
return false; |
| 33 |
for (auto i = 0u; i < l.size(); ++i) |
| 34 |
{ |
| 35 |
if (r[i] == nullptr) |
| 36 |
r[i] = &l[i]; |
| 37 |
else if (&l[i] != r[i]) |
| 38 |
return false; |
| 39 |
} |
| 40 |
return true; |
| 41 |
}; |
| 42 |
|
| 43 |
std::deque<V, Alloc> d; |
| 44 |
d.prepend_range(Range(a, a)); |
| 45 |
VERIFY( d.empty() ); |
| 46 |
d.prepend_range(Range(a+8, a+14)); |
| 47 |
VERIFY( eq(d, {a+8, 6}) ); |
| 48 |
VERIFY( stable(d, {p+8, 6}) ); |
| 49 |
d.prepend_range(Range(a+4, a+8)); |
| 50 |
VERIFY( eq(d, {a+4, 10}) ); |
| 51 |
VERIFY( stable(d, {p+4, 10}) ); |
| 52 |
d.prepend_range(Range(a, a+4)); |
| 53 |
VERIFY( eq(d, a) ); |
| 54 |
VERIFY( stable(d, p) ); |
| 55 |
|
| 56 |
d.clear(); |
| 57 |
std::ranges::fill(p, nullptr); |
| 58 |
d.prepend_range(Range(a+12, a+14)); |
| 59 |
VERIFY( eq(d, {a+12, 2}) ); |
| 60 |
VERIFY( stable(d, {p+12, 2}) ); |
| 61 |
d.prepend_range(Range(a+8,a+12)); |
| 62 |
VERIFY( eq(d, {a+8, 6}) ); |
| 63 |
VERIFY( stable(d, {p+8, 6}) ); |
| 64 |
d.prepend_range(Range(a, a+8)); |
| 65 |
VERIFY( eq(d, a) ); |
| 66 |
VERIFY( stable(d, p) ); |
| 67 |
|
| 68 |
d.clear(); |
| 69 |
std::ranges::fill(p, nullptr); |
| 70 |
d.prepend_range(Range(a+12, a+14)); |
| 71 |
VERIFY( eq(d, {a+12, 2}) ); |
| 72 |
VERIFY( stable(d, {p+12, 2}) ); |
| 73 |
d.prepend_range(Range(a, a+12)); |
| 74 |
VERIFY( eq(d, a) ); |
| 75 |
VERIFY( stable(d, p) ); |
| 76 |
|
| 77 |
d.clear(); |
| 78 |
d.prepend_range(Range(a, a+14)); |
| 79 |
VERIFY( eq(d, a) ); |
| 80 |
} |
| 81 |
|
| 82 |
struct EightInBuf |
| 83 |
{ |
| 84 |
EightInBuf(int x) : elems{x} |
| 85 |
{ } |
| 86 |
|
| 87 |
private: |
| 88 |
int elems[512 / (sizeof(int) * 8)]; |
| 89 |
|
| 90 |
friend constexpr bool operator==(EightInBuf const& lhs, int rhs) |
| 91 |
{ return lhs.elems[0] == rhs; } |
| 92 |
}; |
| 93 |
|
| 94 |
|
| 95 |
template<typename Range> |
| 96 |
void |
| 97 |
do_test_a() |
| 98 |
{ |
| 99 |
do_test<Range, std::allocator<int>>(); |
| 100 |
do_test<Range, __gnu_test::SimpleAllocator<int>>(); |
| 101 |
do_test<Range, std::allocator<EightInBuf>>(); |
| 102 |
} |
| 103 |
|
| 104 |
bool |
| 105 |
test_ranges() |
| 106 |
{ |
| 107 |
using namespace __gnu_test; |
| 108 |
|
| 109 |
do_test_a<test_forward_range<int>>(); |
| 110 |
do_test_a<test_forward_sized_range<int>>(); |
| 111 |
do_test_a<test_sized_range_sized_sent<int, forward_iterator_wrapper>>(); |
| 112 |
|
| 113 |
do_test_a<test_input_range<int>>(); |
| 114 |
do_test_a<test_input_sized_range<int>>(); |
| 115 |
do_test_a<test_sized_range_sized_sent<int, input_iterator_wrapper>>(); |
| 116 |
|
| 117 |
do_test_a<test_range_nocopy<int, input_iterator_wrapper_nocopy>>(); |
| 118 |
do_test_a<test_sized_range<int, input_iterator_wrapper_nocopy>>(); |
| 119 |
do_test_a<test_sized_range_sized_sent<int, input_iterator_wrapper_nocopy>>(); |
| 120 |
|
| 121 |
do_test_a<test_forward_range<short>>(); |
| 122 |
do_test_a<test_input_range<short>>(); |
| 123 |
|
| 124 |
// Not lvalue-convertible to int |
| 125 |
struct C { |
| 126 |
C(int v) : val(v) { } |
| 127 |
operator int() && { return val; } |
| 128 |
bool operator==(int b) const { return b == val; } |
| 129 |
int val; |
| 130 |
}; |
| 131 |
using rvalue_input_range = test_range<C, input_iterator_wrapper_rval>; |
| 132 |
do_test<rvalue_input_range, std::allocator<int>>(); |
| 133 |
|
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
int main() |
| 138 |
{ |
| 139 |
test_ranges(); |
| 140 |
} |