Debian Codegraph @

libstdc++-v3/testsuite/23_containers/set/cons/from_range.cc

raw
1 // { dg-do run { target c++23 } }
2
3 #include <set>
4
5 #if __cpp_lib_containers_ranges != 202202L
6 # error "Feature-test macro __cpp_lib_containers_ranges has wrong value in <set>"
7 #endif
8
9 #include <algorithm>
10 #include <ranges>
11 #include <span>
12 #include <testsuite_allocator.h>
13 #include <testsuite_hooks.h>
14 #include <testsuite_iterators.h>
15
16 struct StateCmp {
17 int state = 7;
18
19 template<typename T, typename U>
20 bool operator()(T const& l, U const & r) const {
21 return l > r;
22 }
23 };
24
25 void
26 test_deduction_guide(long* p)
27 {
28 __gnu_test::test_input_range<long> r(p, p);
29 std::set s(std::from_range, r);
30 static_assert(std::is_same_v<decltype(s), std::set<long>>);
31
32 StateCmp cmp;
33 std::set s2(std::from_range, r, cmp);
34 static_assert(std::is_same_v<decltype(s2), std::set<long, StateCmp>>);
35
36 using Alloc = __gnu_test::SimpleAllocator<long>;
37 Alloc alloc;
38 std::set s3(std::from_range, r, alloc);
39 static_assert(std::is_same_v<decltype(s3), std::set<long, std::less<long>, Alloc>>);
40
41 std::set s4(std::from_range, r, cmp, alloc);
42 static_assert(std::is_same_v<decltype(s4), std::set<long, StateCmp, Alloc>>);
43 }
44
45 template<typename T, typename U>
46 constexpr bool is_equal(std::less<T>, std::less<U>)
47 { return true; }
48
49 constexpr bool is_equal(StateCmp lhs, StateCmp rhs)
50 { return lhs.state = rhs.state; }
51
52 template<typename Range, typename Alloc, typename Cmp>
53 constexpr void
54 do_test(Alloc alloc, Cmp cmp)
55 {
56 // The set's value_typ.
57 using V = typename Alloc::value_type;
58
59 // The range's value_type.
60 using T = std::ranges::range_value_t<Range>;
61 T a[]{1,2,3,4,5,6,7,8,9,1,2,3,4,5};
62
63 auto eq = [&](std::set<V, Cmp, Alloc> const& l, std::span<T> r) {
64 if (l.size() != r.size())
65 return false;
66
67 std::vector<T> s(r.begin(), r.end());
68 std::ranges::sort(s, cmp);
69 for (auto const& [vl, vr] : std::views::zip(l, s)) {
70 if (vl != vr)
71 return false;
72 }
73 return true;
74 };
75
76 std::set<V, Cmp, Alloc> s0(std::from_range, Range(a, a+0));
77 VERIFY( s0.empty() );
78 VERIFY( s0.get_allocator() == Alloc() );
79 VERIFY( is_equal(s0.key_comp(), Cmp()) );
80
81 std::set<V, Cmp, Alloc> s4(std::from_range, Range(a, a+4), cmp);
82 VERIFY( eq(s4, {a, 4}) );
83 VERIFY( s4.get_allocator() == Alloc() );
84 VERIFY( is_equal(s4.key_comp(), Cmp()) );
85
86 std::set<V, Cmp, Alloc> s9(std::from_range, Range(a, a+9), alloc);
87 VERIFY( eq(s9, {a, 9}) );
88 VERIFY( s9.get_allocator() == alloc );
89 VERIFY( is_equal(s9.key_comp(), cmp) );
90
91 std::set<V, Cmp, Alloc> sr(std::from_range, Range(a, a+14), cmp, alloc);
92 VERIFY( eq(sr, {a, 9}) );
93 VERIFY( sr.get_allocator() == alloc );
94 VERIFY( is_equal(sr.key_comp(), cmp) );
95 }
96
97 template<typename Range>
98 void
99 do_test_ac()
100 {
101 do_test<Range>(std::allocator<int>(), std::less<int>());
102 do_test<Range>(std::allocator<int>(), StateCmp{17});
103 do_test<Range>(__gnu_test::uneq_allocator<int>(42), std::less<int>());
104 do_test<Range>(__gnu_test::uneq_allocator<int>(42), StateCmp{17});
105 }
106
107 bool
108 test_ranges()
109 {
110 using namespace __gnu_test;
111
112 do_test_ac<test_forward_range<int>>();
113 do_test_ac<test_range_nocopy<int, input_iterator_wrapper_nocopy>>();
114 do_test_ac<test_forward_range<short>>();
115
116 return true;
117 }
118
119 int main()
120 {
121 test_ranges();
122 }