| 1 | // { dg-do run { target c++23 } } |
| 2 | |
| 3 | #include <unordered_set> |
| 4 | |
| 5 | #if __cpp_lib_containers_ranges != 202202L |
| 6 | # error "Feature-test macro __cpp_lib_containers_ranges has wrong value in <unordered_set>" |
| 7 | #endif |
| 8 | |
| 9 | #include <vector> |
| 10 | #include <span> |
| 11 | #include <testsuite_hooks.h> |
| 12 | #include <testsuite_iterators.h> |
| 13 | #include <testsuite_allocator.h> |
| 14 | |
| 15 | template<typename Range, typename Alloc> |
| 16 | constexpr void |
| 17 | do_test(Alloc alloc) |
| 18 | { |
| 19 | using T = std::ranges::range_value_t<Range>; |
| 20 | T a[]{1,1,0,1,0,0,1,0,0}; |
| 21 | |
| 22 | auto eq = [](const std::vector<bool, 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::vector<bool, Alloc> v0(std::from_range, Range(a, a+0)); |
| 32 | VERIFY( v0.empty() ); |
| 33 | VERIFY( v0.get_allocator() == Alloc() ); |
| 34 | |
| 35 | std::vector<bool, Alloc> v4(std::from_range, Range(a, a+4)); |
| 36 | VERIFY( eq(v4, {a, 4}) ); |
| 37 | VERIFY( v4.get_allocator() == Alloc() ); |
| 38 | |
| 39 | std::vector<bool, Alloc> v9(std::from_range, Range(a, a+9), alloc); |
| 40 | VERIFY( eq(v9, {a, 9}) ); |
| 41 | VERIFY( v9.get_allocator() == alloc ); |
| 42 | } |
| 43 | |
| 44 | template<typename Range> |
| 45 | void |
| 46 | do_test_a() |
| 47 | { |
| 48 | do_test<Range>(std::allocator<bool>()); |
| 49 | do_test<Range>(__gnu_test::uneq_allocator<bool>(42)); |
| 50 | } |
| 51 | |
| 52 | bool |
| 53 | test_ranges() |
| 54 | { |
| 55 | using namespace __gnu_test; |
| 56 | |
| 57 | do_test_a<test_forward_range<bool>>(); |
| 58 | do_test_a<test_forward_sized_range<bool>>(); |
| 59 | do_test_a<test_sized_range_sized_sent<bool, forward_iterator_wrapper>>(); |
| 60 | |
| 61 | do_test_a<test_input_range<bool>>(); |
| 62 | do_test_a<test_input_sized_range<bool>>(); |
| 63 | do_test_a<test_sized_range_sized_sent<bool, input_iterator_wrapper>>(); |
| 64 | |
| 65 | do_test_a<test_range<bool, input_iterator_wrapper_nocopy>>(); |
| 66 | do_test_a<test_sized_range<bool, input_iterator_wrapper_nocopy>>(); |
| 67 | do_test_a<test_sized_range_sized_sent<bool, input_iterator_wrapper_nocopy>>(); |
| 68 | |
| 69 | do_test_a<test_forward_range<short>>(); |
| 70 | do_test_a<test_input_range<short>>(); |
| 71 | |
| 72 | // Not lvalue-convertible to bool |
| 73 | struct C { |
| 74 | C(bool v) : val(v) { } |
| 75 | operator bool() && { return val; } |
| 76 | bool operator==(bool b) const { return b == val; } |
| 77 | bool val; |
| 78 | }; |
| 79 | using rvalue_input_range = test_range<C, input_iterator_wrapper_rval>; |
| 80 | do_test<rvalue_input_range>(std::allocator<bool>()); |
| 81 | |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | constexpr bool |
| 86 | test_constexpr() |
| 87 | { |
| 88 | // XXX: this doesn't test the non-forward_range code paths are constexpr. |
| 89 | do_test<std::span<bool>>(std::allocator<bool>()); |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | int main() |
| 94 | { |
| 95 | test_ranges(); |
| 96 | static_assert( test_constexpr() ); |
| 97 | } |