| 1 |
// { dg-do run { target c++23 } } |
| 2 |
|
| 3 |
#include <span> |
| 4 |
#include <string> |
| 5 |
#include <testsuite_allocator.h> |
| 6 |
#include <testsuite_hooks.h> |
| 7 |
#include <testsuite_iterators.h> |
| 8 |
|
| 9 |
template<typename Range, typename Alloc> |
| 10 |
constexpr void |
| 11 |
do_test() |
| 12 |
{ |
| 13 |
// The vector's value_type. |
| 14 |
using V = typename std::allocator_traits<Alloc>::value_type; |
| 15 |
using CT = std::char_traits<V>; |
| 16 |
|
| 17 |
// The range's value_type. |
| 18 |
using T = std::ranges::range_value_t<Range>; |
| 19 |
T a[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| 20 |
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}; |
| 21 |
|
| 22 |
auto eq = [](const std::basic_string<V, CT, Alloc>& l, std::span<T> r) { |
| 23 |
if (l.size() != r.size()) |
| 24 |
return false; |
| 25 |
for (auto i = 0u; i < l.size(); ++i) |
| 26 |
if (l[i] != r[i]) |
| 27 |
return false; |
| 28 |
return true; |
| 29 |
}; |
| 30 |
|
| 31 |
std::basic_string<V, CT, Alloc> v; |
| 32 |
auto it = v.insert_range(v.end(), Range(a, a)); |
| 33 |
VERIFY( v.empty() ); |
| 34 |
VERIFY( it == v.begin() ); |
| 35 |
it = v.insert_range(v.end(), Range(a, a+4)); |
| 36 |
VERIFY( eq(v, {a, 4}) ); |
| 37 |
VERIFY( it == v.begin() ); |
| 38 |
it = v.insert_range(v.end(), Range(a+4, a+9)); |
| 39 |
VERIFY( eq(v, {a, 9}) ); |
| 40 |
VERIFY( it == v.begin()+4 ); |
| 41 |
|
| 42 |
std::basic_string<V, CT, Alloc> s = v; |
| 43 |
it = v.insert_range(v.end(), Range(a+9, a+20)); |
| 44 |
VERIFY( eq(v, {a, 20}) ); |
| 45 |
VERIFY( it == v.begin()+9 ); |
| 46 |
|
| 47 |
v = std::basic_string<V, CT, Alloc>(); |
| 48 |
it = v.insert_range(v.begin(), Range(a, a+5)); |
| 49 |
VERIFY( it == v.begin() ); |
| 50 |
s = v; |
| 51 |
it = v.insert_range(v.begin() + 5, Range(a+5, a+20)); |
| 52 |
VERIFY( eq(v, {a, 20}) ); |
| 53 |
VERIFY( it == v.begin()+5 ); |
| 54 |
} |
| 55 |
|
| 56 |
template<typename Range> |
| 57 |
void |
| 58 |
do_test_a() |
| 59 |
{ |
| 60 |
do_test<Range, std::allocator<char>>(); |
| 61 |
do_test<Range, __gnu_test::SimpleAllocator<char>>(); |
| 62 |
do_test<Range, std::allocator<wchar_t>>(); |
| 63 |
do_test<Range, __gnu_test::SimpleAllocator<wchar_t>>(); |
| 64 |
} |
| 65 |
|
| 66 |
bool |
| 67 |
test_ranges() |
| 68 |
{ |
| 69 |
using namespace __gnu_test; |
| 70 |
|
| 71 |
do_test_a<test_forward_range<char>>(); |
| 72 |
do_test_a<test_forward_sized_range<char>>(); |
| 73 |
do_test_a<test_sized_range_sized_sent<char, forward_iterator_wrapper>>(); |
| 74 |
|
| 75 |
do_test_a<test_input_range<char>>(); |
| 76 |
do_test_a<test_input_sized_range<char>>(); |
| 77 |
do_test_a<test_sized_range_sized_sent<char, input_iterator_wrapper>>(); |
| 78 |
|
| 79 |
do_test_a<test_range<char, input_iterator_wrapper_nocopy>>(); |
| 80 |
do_test_a<test_sized_range<char, input_iterator_wrapper_nocopy>>(); |
| 81 |
do_test_a<test_sized_range_sized_sent<char, input_iterator_wrapper_nocopy>>(); |
| 82 |
|
| 83 |
// Not lvalue-convertible to char |
| 84 |
struct C { |
| 85 |
C(char v) : val(v) { } |
| 86 |
operator char() && { return val; } |
| 87 |
bool operator==(char b) const { return b == val; } |
| 88 |
char val; |
| 89 |
}; |
| 90 |
using rvalue_input_range = test_range<C, input_iterator_wrapper_rval>; |
| 91 |
do_test<rvalue_input_range, std::allocator<char>>(); |
| 92 |
|
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
void |
| 97 |
test_overlapping() |
| 98 |
{ |
| 99 |
std::string const s = "1234abcd"; |
| 100 |
|
| 101 |
std::string c = s; |
| 102 |
c.insert_range(c.end(), std::string_view(c)); |
| 103 |
VERIFY( c == "1234abcd1234abcd" ); |
| 104 |
|
| 105 |
c = s; |
| 106 |
c.insert_range(c.begin()+4, std::string_view(c).substr(4, 4)); |
| 107 |
VERIFY( c == "1234abcdabcd" ); |
| 108 |
|
| 109 |
c = s; |
| 110 |
c.reserve(12); |
| 111 |
c.insert_range(c.begin()+2, std::string_view(c).substr(0, 4)); |
| 112 |
VERIFY( c == "12123434abcd" ); |
| 113 |
} |
| 114 |
|
| 115 |
constexpr bool |
| 116 |
test_constexpr() |
| 117 |
{ |
| 118 |
#if _GLIBCXX_USE_CXX11_ABI |
| 119 |
// XXX: this doesn't test the non-forward_range code paths are constexpr. |
| 120 |
do_test<std::string_view, std::allocator<char>>(); |
| 121 |
#endif // _GLIBCXX_USE_CXX11_ABI |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
int main() |
| 126 |
{ |
| 127 |
test_ranges(); |
| 128 |
test_overlapping(); |
| 129 |
static_assert( test_constexpr() ); |
| 130 |
} |