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