| 1 | // { dg-do run { target c++11 } } |
| 2 | |
| 3 | // Copyright (C) 2017-2025 Free Software Foundation, Inc. |
| 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 | |
| 22 | #include <list> |
| 23 | #include <testsuite_hooks.h> |
| 24 | |
| 25 | struct ThrowingComparator |
| 26 | { |
| 27 | ThrowingComparator(unsigned n) : throw_after(n), count(0) { } |
| 28 | unsigned int throw_after; |
| 29 | unsigned int count; |
| 30 | bool operator()(int, int) { |
| 31 | if (++count >= throw_after) { |
| 32 | throw 666; |
| 33 | } |
| 34 | return false; |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | struct X |
| 39 | { |
| 40 | X() = default; |
| 41 | X(int) {} |
| 42 | }; |
| 43 | |
| 44 | unsigned int throw_after_X = 0; |
| 45 | unsigned int count_X = 0; |
| 46 | |
| 47 | bool operator<(const X&, const X&) { |
| 48 | if (++count_X >= throw_after_X) { |
| 49 | throw 666; |
| 50 | } |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | int 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 { |
| 60 | a.merge(b, ThrowingComparator{4}); |
| 61 | } catch (...) { |
| 62 | } |
| 63 | VERIFY(a.size() == std::distance(a.begin(), a.end()) && |
| 64 | b.size() == std::distance(b.begin(), b.end())); |
| 65 | std::list<X> ax{1, 2, 3, 4}; |
| 66 | std::list<X> bx{5, 6, 7, 8, 9, 10, 11, 12}; |
| 67 | throw_after_X = 4; |
| 68 | try { |
| 69 | ax.merge(bx); |
| 70 | } catch (...) { |
| 71 | } |
| 72 | VERIFY(ax.size() == std::distance(ax.begin(), ax.end()) && |
| 73 | bx.size() == std::distance(bx.begin(), bx.end())); |
| 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 | } |