// { dg-do run { target c++23 } } #include #if __cpp_lib_containers_ranges != 202202L # error "Feature-test macro __cpp_lib_containers_ranges has wrong value in " #endif #include #include #include #include #include struct StateHash { int state = 17; template size_t operator()(T const& t) const { return std::hash()(t) + 43; } }; struct StateEq { int state = 7; template bool operator()(T const& l, U const & r) const { return l == r; } }; void test_deduction_guide(long* p) { __gnu_test::test_input_range r(p, p); std::unordered_set s(std::from_range, r); static_assert(std::is_same_v>); std::unordered_set s2(std::from_range, r, 0); static_assert(std::is_same_v>); StateHash hf; std::unordered_set s3(std::from_range, r, 0, hf); static_assert(std::is_same_v< decltype(s3), std::unordered_set>); StateEq eq; std::unordered_set s4(std::from_range, r, 0, hf, eq); static_assert(std::is_same_v< decltype(s4), std::unordered_set>); using Alloc = __gnu_test::SimpleAllocator; Alloc alloc; std::unordered_set s5(std::from_range, r, alloc); static_assert(std::is_same_v< decltype(s5), std::unordered_set, std::equal_to, Alloc>>); std::unordered_set s6(std::from_range, r, 0, alloc); static_assert(std::is_same_v< decltype(s6), std::unordered_set, std::equal_to, Alloc>>); std::unordered_set s7(std::from_range, r, 0, hf, alloc); static_assert(std::is_same_v< decltype(s7), std::unordered_set, Alloc>>); std::unordered_set s8(std::from_range, r, 0, hf, eq, alloc); static_assert(std::is_same_v< decltype(s8), std::unordered_set>); } template constexpr bool is_equal(std::hash, std::hash) { return true; } template constexpr bool is_equal(std::equal_to, std::equal_to) { return true; } constexpr bool is_equal(StateHash lhs, StateHash rhs) { return lhs.state = rhs.state; } constexpr bool is_equal(StateEq lhs, StateEq rhs) { return lhs.state = rhs.state; } template constexpr void do_test(Alloc alloc, Hash hf, Equal eqf) { // The unordered_set's value_typ. using V = typename Alloc::value_type; // The range's value_type. using T = std::ranges::range_value_t; T a[]{1,2,3,4,5,6,7,8,9,1,2,3,4,5}; auto eq = [&](std::unordered_set const& l, std::span r) { if (l.size() != r.size()) return false; return std::ranges::is_permutation(l, r, eqf); }; std::unordered_set s0(std::from_range, Range(a, a+0)); VERIFY( s0.empty() ); VERIFY( is_equal(s0.hash_function(), Hash()) ); VERIFY( is_equal(s0.key_eq(), Equal()) ); VERIFY( s0.get_allocator() == Alloc() ); std::unordered_set s4(std::from_range, Range(a, a+4), 2); VERIFY( eq(s4, {a, 4}) ); VERIFY( s4.bucket_count() >= 2 ); VERIFY( is_equal(s4.hash_function(), Hash()) ); VERIFY( is_equal(s4.key_eq(), Equal()) ); VERIFY( s4.get_allocator() == Alloc() ); std::unordered_set s7(std::from_range, Range(a, a+7), 3, hf); VERIFY( eq(s7, {a, 7}) ); VERIFY( s7.bucket_count() >= 3 ); VERIFY( is_equal(s7.hash_function(), hf) ); VERIFY( is_equal(s7.key_eq(), Equal()) ); VERIFY( s7.get_allocator() == Alloc() ); std::unordered_set s9(std::from_range, Range(a, a+9), 5, hf, eqf); VERIFY( eq(s9, {a, 9}) ); VERIFY( s9.bucket_count() >= 5 ); VERIFY( s9.get_allocator() == Alloc() ); VERIFY( is_equal(s9.hash_function(), hf) ); VERIFY( is_equal(s9.key_eq(), eqf) ); std::unordered_set sa1(std::from_range, Range(a, a+14), alloc); VERIFY( eq(sa1, {a, 9}) ); VERIFY( is_equal(sa1.hash_function(), Hash()) ); VERIFY( is_equal(sa1.key_eq(), Equal()) ); VERIFY( sa1.get_allocator() == alloc ); std::unordered_set sa2(std::from_range, Range(a, a+14), 2, alloc); VERIFY( eq(sa2, {a, 9}) ); VERIFY( sa2.bucket_count() >= 2 ); VERIFY( is_equal(sa2.hash_function(), Hash()) ); VERIFY( is_equal(sa2.key_eq(), Equal()) ); VERIFY( sa2.get_allocator() == alloc ); std::unordered_set sa3(std::from_range, Range(a, a+14), 3, hf, alloc); VERIFY( eq(sa3, {a, 9}) ); VERIFY( sa3.bucket_count() >= 3 ); VERIFY( is_equal(sa3.hash_function(), hf) ); VERIFY( is_equal(sa3.key_eq(), Equal()) ); VERIFY( sa3.get_allocator() == alloc ); std::unordered_set sa4(std::from_range, Range(a, a+14), 5, hf, eqf, alloc); VERIFY( eq(sa4, {a, 9}) ); VERIFY( sa4.bucket_count() >= 5 ); VERIFY( is_equal(sa4.hash_function(), hf) ); VERIFY( is_equal(sa4.key_eq(), eqf) ); VERIFY( sa4.get_allocator() == alloc ); } template void do_test_ahe() { do_test(std::allocator(), std::hash(), std::equal_to()); do_test(std::allocator(), StateHash{27}, StateEq{17}); do_test(__gnu_test::uneq_allocator(42), std::hash(), std::equal_to()); do_test(__gnu_test::uneq_allocator(42), StateHash{27}, StateEq{17}); } bool test_ranges() { using namespace __gnu_test; do_test_ahe>(); do_test_ahe>(); do_test_ahe>(); return true; } int main() { test_ranges(); }