| 1 | // { dg-do run { target c++23 } } |
| 2 | |
| 3 | #include <string> |
| 4 | |
| 5 | #if __cpp_lib_containers_ranges != 202202L |
| 6 | # error "Feature-test macro __cpp_lib_containers_ranges has wrong value in <string>" |
| 7 | #endif |
| 8 | |
| 9 | #include <span> |
| 10 | #include <testsuite_hooks.h> |
| 11 | #include <testsuite_iterators.h> |
| 12 | #include <testsuite_allocator.h> |
| 13 | |
| 14 | void |
| 15 | test_deduction_guide(char* p) |
| 16 | { |
| 17 | __gnu_test::test_input_range<char> r(nullptr, nullptr); |
| 18 | std::basic_string v(std::from_range, r); |
| 19 | static_assert(std::is_same_v<decltype(v), std::string>); |
| 20 | |
| 21 | using Alloc = __gnu_test::SimpleAllocator<char>; |
| 22 | Alloc alloc; |
| 23 | std::basic_string v2(std::from_range, r, alloc); |
| 24 | static_assert(std::is_same_v<decltype(v2), std::basic_string<char, std::char_traits<char>, Alloc>>); |
| 25 | |
| 26 | __gnu_test::test_input_range<wchar_t> wr(nullptr, nullptr); |
| 27 | std::basic_string w(std::from_range, wr); |
| 28 | static_assert(std::is_same_v<decltype(w), std::wstring>); |
| 29 | |
| 30 | using WAlloc = __gnu_test::SimpleAllocator<wchar_t>; |
| 31 | WAlloc walloc; |
| 32 | std::basic_string w2(std::from_range, wr, walloc); |
| 33 | static_assert(std::is_same_v<decltype(w2), std::basic_string<wchar_t, std::char_traits<wchar_t>, WAlloc>>); |
| 34 | } |
| 35 | |
| 36 | template<typename Range, typename Alloc> |
| 37 | constexpr void |
| 38 | do_test(Alloc alloc) |
| 39 | { |
| 40 | // The basic_string's value_type. |
| 41 | using V = typename std::allocator_traits<Alloc>::value_type; |
| 42 | using CT = std::char_traits<V>; |
| 43 | |
| 44 | // The range's value_type. |
| 45 | using T = std::ranges::range_value_t<Range>; |
| 46 | T a[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| 47 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}; |
| 48 | |
| 49 | auto eq = [](const std::basic_string<V, CT, Alloc>& l, std::span<T> r) { |
| 50 | if (l.size() != r.size()) |
| 51 | return false; |
| 52 | for (auto i = 0u; i < l.size(); ++i) |
| 53 | if (l[i] != r[i]) |
| 54 | return false; |
| 55 | return true; |
| 56 | }; |
| 57 | |
| 58 | std::basic_string<V, CT, Alloc> v0(std::from_range, Range(a, a+0)); |
| 59 | VERIFY( v0.empty() ); |
| 60 | VERIFY( v0.get_allocator() == Alloc() ); |
| 61 | |
| 62 | std::basic_string<V, CT, Alloc> v4(std::from_range, Range(a, a+4)); |
| 63 | VERIFY( eq(v4, {a, 4}) ); |
| 64 | VERIFY( v4.get_allocator() == Alloc() ); |
| 65 | |
| 66 | std::basic_string<V, CT, Alloc> v9(std::from_range, Range(a, a+9), alloc); |
| 67 | VERIFY( eq(v9, {a, 9}) ); |
| 68 | VERIFY( v9.get_allocator() == alloc ); |
| 69 | |
| 70 | std::basic_string<V, CT, Alloc> v20(std::from_range, Range(a, a+20), alloc); |
| 71 | VERIFY( eq(v20, {a, 20}) ); |
| 72 | VERIFY( v20.get_allocator() == alloc ); |
| 73 | } |
| 74 | |
| 75 | template<typename Range> |
| 76 | void |
| 77 | do_test_a() |
| 78 | { |
| 79 | do_test<Range>(std::allocator<char>()); |
| 80 | do_test<Range>(__gnu_test::uneq_allocator<char>(42)); |
| 81 | do_test<Range>(std::allocator<wchar_t>()); |
| 82 | do_test<Range>(__gnu_test::uneq_allocator<wchar_t>(42)); |
| 83 | } |
| 84 | |
| 85 | bool |
| 86 | test_ranges() |
| 87 | { |
| 88 | using namespace __gnu_test; |
| 89 | |
| 90 | do_test_a<test_forward_range<char>>(); |
| 91 | do_test_a<test_forward_sized_range<char>>(); |
| 92 | do_test_a<test_sized_range_sized_sent<char, forward_iterator_wrapper>>(); |
| 93 | |
| 94 | do_test_a<test_input_range<char>>(); |
| 95 | do_test_a<test_input_sized_range<char>>(); |
| 96 | do_test_a<test_sized_range_sized_sent<char, input_iterator_wrapper>>(); |
| 97 | |
| 98 | do_test_a<test_range<char, input_iterator_wrapper_nocopy>>(); |
| 99 | do_test_a<test_sized_range<char, input_iterator_wrapper_nocopy>>(); |
| 100 | do_test_a<test_sized_range_sized_sent<char, input_iterator_wrapper_nocopy>>(); |
| 101 | |
| 102 | // Not lvalue-convertible to char |
| 103 | struct C { |
| 104 | C(char v) : val(v) { } |
| 105 | operator char() && { return val; } |
| 106 | bool operator==(char b) const { return b == val; } |
| 107 | char val; |
| 108 | }; |
| 109 | using rvalue_input_range = test_range<C, input_iterator_wrapper_rval>; |
| 110 | do_test<rvalue_input_range>(std::allocator<char>()); |
| 111 | |
| 112 | return true; |
| 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 | static_assert( test_constexpr() ); |
| 129 | } |