| 1 | // { dg-do run { target c++23 } } |
| 2 | |
| 3 | #include <vector> |
| 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 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 | v.assign_range(Range(a, a)); |
| 33 | VERIFY( v.empty() ); |
| 34 | v.assign_range(Range(a, a+4)); |
| 35 | VERIFY( eq(v, {a, 4}) ); |
| 36 | v.assign_range(Range(a, a+9)); |
| 37 | VERIFY( eq(v, {a, 9}) ); |
| 38 | std::basic_string<V, CT, Alloc> const s = v; |
| 39 | v.assign_range(Range(a, a+20)); |
| 40 | VERIFY( eq(v, {a, 20}) ); |
| 41 | } |
| 42 | |
| 43 | template<typename Range> |
| 44 | void |
| 45 | do_test_a() |
| 46 | { |
| 47 | do_test<Range, std::allocator<char>>(); |
| 48 | do_test<Range, __gnu_test::SimpleAllocator<char>>(); |
| 49 | do_test<Range, std::allocator<wchar_t>>(); |
| 50 | do_test<Range, __gnu_test::SimpleAllocator<wchar_t>>(); |
| 51 | } |
| 52 | |
| 53 | bool |
| 54 | test_ranges() |
| 55 | { |
| 56 | using namespace __gnu_test; |
| 57 | |
| 58 | do_test_a<test_forward_range<char>>(); |
| 59 | do_test_a<test_forward_sized_range<char>>(); |
| 60 | do_test_a<test_sized_range_sized_sent<char, forward_iterator_wrapper>>(); |
| 61 | |
| 62 | do_test_a<test_input_range<char>>(); |
| 63 | do_test_a<test_input_sized_range<char>>(); |
| 64 | do_test_a<test_sized_range_sized_sent<char, input_iterator_wrapper>>(); |
| 65 | |
| 66 | do_test_a<test_range<char, input_iterator_wrapper_nocopy>>(); |
| 67 | do_test_a<test_sized_range<char, input_iterator_wrapper_nocopy>>(); |
| 68 | do_test_a<test_sized_range_sized_sent<char, input_iterator_wrapper_nocopy>>(); |
| 69 | |
| 70 | // Not lvalue-convertible to char |
| 71 | struct C { |
| 72 | C(char v) : val(v) { } |
| 73 | operator char() && { return val; } |
| 74 | bool operator==(char b) const { return b == val; } |
| 75 | char val; |
| 76 | }; |
| 77 | using rvalue_input_range = test_range<C, input_iterator_wrapper_rval>; |
| 78 | do_test<rvalue_input_range, std::allocator<char>>(); |
| 79 | |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | test_overlapping() |
| 85 | { |
| 86 | std::string const s = "1234abcd"; |
| 87 | |
| 88 | std::string c = s; |
| 89 | c.assign_range(std::string_view(c)); |
| 90 | VERIFY( c == "1234abcd" ); |
| 91 | |
| 92 | c = s; |
| 93 | c.assign_range(std::string_view(c).substr(4, 4)); |
| 94 | VERIFY( c == "abcd" ); |
| 95 | |
| 96 | c = s; |
| 97 | c.assign_range(std::string_view(c).substr(0, 4)); |
| 98 | VERIFY( c == "1234" ); |
| 99 | } |
| 100 | |
| 101 | constexpr bool |
| 102 | test_constexpr() |
| 103 | { |
| 104 | #if _GLIBCXX_USE_CXX11_ABI |
| 105 | // XXX: this doesn't test the non-forward_range code paths are constexpr. |
| 106 | do_test<std::string_view, std::allocator<char>>(); |
| 107 | #endif // _GLIBCXX_USE_CXX11_ABI |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | int main() |
| 112 | { |
| 113 | test_ranges(); |
| 114 | test_overlapping(); |
| 115 | static_assert( test_constexpr() ); |
| 116 | } |