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