Debian Codegraph @

libstdc++-v3/testsuite/23_containers/list/operations/66742.cc

raw
1 // { dg-do run { target c++11 } }
2
3 #include <list>
4 #include <testsuite_allocator.h>
5 #include <testsuite_hooks.h>
6
7 // PR libstdc++/66742
8 // abort on sorting list with custom allocator that is not stateless
9
10 template<typename List, typename Cmp = std::less<typename List::value_type>>
11 bool is_sorted(const List& l, Cmp cmp = {})
12 {
13 auto it = l.begin();
14 auto next = it;
15 const auto end = l.end();
16 if (it == end)
17 return true;
18 while (++next != end)
19 if (cmp(*next, *it))
20 return false;
21 else
22 it = next;
23 return true;
24 }
25
26 void
27 test01()
28 {
29 using Alloc = __gnu_test::uneq_allocator<int>;
30 Alloc a1(1);
31 std::list<int, Alloc> l(a1);
32 for (int i = 0; i < 1000; ++i)
33 {
34 l.push_front(i);
35 l.push_back(i + (i % 3));
36 }
37 const auto orig = l;
38
39 l.sort();
40 VERIFY( is_sorted(l) );
41 l.sort();
42 VERIFY( is_sorted(l) );
43
44 l = orig;
45 l.sort(std::less<int>());
46 VERIFY( is_sorted(l) );
47 l.sort(std::greater<int>());
48 VERIFY( is_sorted(l, std::greater<int>()) );
49 }
50
51 void
52 test02()
53 {
54 // The standard doesn't require comparisons to be const-correct.
55 // The initial fix for PR 66742 caused a regression here.
56
57 struct X
58 {
59 bool operator<(X&) /* non-const */ { return false; }
60 };
61
62 struct Cmp
63 {
64 bool operator()(X&, X&) /* non-const */ { return false; }
65 };
66
67 std::list<X> l;
68 l.sort();
69 Cmp c;
70 l.sort(c);
71 }
72
73 int
74 main()
75 {
76 test01();
77 test02();
78 }