Debian Codegraph @

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