]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/23_containers/list/operations/78389.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / list / operations / 78389.cc
CommitLineData
e5dcfacf
VV
1// { dg-do run { target c++11 } }
2
a945c346 3// Copyright (C) 2017-2024 Free Software Foundation, Inc.
e5dcfacf
VV
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License along
17// with this library; see the file COPYING3. If not see
18// <http://www.gnu.org/licenses/>.
19
20// 23.2.2.4 list operations [lib.list.ops]
21
e5dcfacf 22#include <list>
881191e8 23#include <testsuite_hooks.h>
e5dcfacf
VV
24
25struct ThrowingComparator
26{
881191e8
JW
27 ThrowingComparator(unsigned n) : throw_after(n), count(0) { }
28 unsigned int throw_after;
29 unsigned int count;
e5dcfacf
VV
30 bool operator()(int, int) {
31 if (++count >= throw_after) {
32 throw 666;
33 }
881191e8 34 return false;
e5dcfacf
VV
35 }
36};
37
38struct X
39{
40 X() = default;
41 X(int) {}
42};
43
44unsigned int throw_after_X = 0;
45unsigned int count_X = 0;
46
47bool operator<(const X&, const X&) {
48 if (++count_X >= throw_after_X) {
49 throw 666;
50 }
01e3c229 51 return false;
e5dcfacf
VV
52}
53
54
55int main()
56{
57 std::list<int> a{1, 2, 3, 4};
58 std::list<int> b{5, 6, 7, 8, 9, 10, 11, 12};
59 try {
53426b63 60 a.merge(b, ThrowingComparator{4});
e5dcfacf
VV
61 } catch (...) {
62 }
53426b63
VV
63 VERIFY(a.size() == std::distance(a.begin(), a.end()) &&
64 b.size() == std::distance(b.begin(), b.end()));
e5dcfacf
VV
65 std::list<X> ax{1, 2, 3, 4};
66 std::list<X> bx{5, 6, 7, 8, 9, 10, 11, 12};
53426b63 67 throw_after_X = 4;
e5dcfacf
VV
68 try {
69 ax.merge(bx);
70 } catch (...) {
71 }
53426b63
VV
72 VERIFY(ax.size() == std::distance(ax.begin(), ax.end()) &&
73 bx.size() == std::distance(bx.begin(), bx.end()));
e5dcfacf
VV
74 std::list<int> ay{5, 6, 7, 8, 9, 10, 11, 12};
75 try {
76 ay.sort(ThrowingComparator{5});
77 } catch (...) {
78 }
79 VERIFY(ay.size() == 8);
80 std::list<X> az{5, 6, 7, 8, 9, 10, 11, 12};
81 throw_after_X = 5;
82 try {
83 az.sort();
84 } catch (...) {
85 }
86 VERIFY(az.size() == 8);
87}