| 1 | // { dg-do run { target c++23 } } |
| 2 | // Copyright (C) 2023-2025 Free Software Foundation, Inc. |
| 3 | // |
| 4 | // This file is part of the GNU ISO C++ Library. This library is free |
| 5 | // software; you can redistribute it and/or modify it under the |
| 6 | // terms of the GNU General Public License as published by the |
| 7 | // Free Software Foundation; either version 3, or (at your option) |
| 8 | // any later version. |
| 9 | |
| 10 | // This library is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU General Public License for more details. |
| 14 | |
| 15 | // You should have received a copy of the GNU General Public License along |
| 16 | // with this library; see the file COPYING3. If not see |
| 17 | // <http://www.gnu.org/licenses/>. |
| 18 | |
| 19 | #include <testsuite_hooks.h> |
| 20 | #include <generator> |
| 21 | |
| 22 | std::generator<int> |
| 23 | foo() |
| 24 | { |
| 25 | co_yield 0; |
| 26 | throw 3; /* dice roll */ |
| 27 | } |
| 28 | |
| 29 | std::generator<int> |
| 30 | foo_delegator() |
| 31 | { |
| 32 | co_yield 1; |
| 33 | co_yield std::ranges::elements_of { foo() }; |
| 34 | } |
| 35 | |
| 36 | bool catchy_caught = false; |
| 37 | |
| 38 | std::generator<int> |
| 39 | foo_catchy_delegator() |
| 40 | { |
| 41 | try |
| 42 | { |
| 43 | co_yield std::ranges::elements_of { foo() }; |
| 44 | VERIFY(false); |
| 45 | } |
| 46 | catch (int i) |
| 47 | { |
| 48 | catchy_caught = true; |
| 49 | VERIFY(i == 3); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | int |
| 54 | main() |
| 55 | { |
| 56 | { |
| 57 | auto gen = foo(); |
| 58 | try |
| 59 | { |
| 60 | auto it = gen.begin(); |
| 61 | VERIFY(*it == 0); |
| 62 | it++; |
| 63 | VERIFY(false); |
| 64 | } |
| 65 | catch (int x) |
| 66 | { |
| 67 | VERIFY(x == 3); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | { |
| 72 | auto gen = foo_delegator(); |
| 73 | auto it = gen.begin(); |
| 74 | VERIFY(*it == 1); |
| 75 | it++; |
| 76 | |
| 77 | try |
| 78 | { |
| 79 | VERIFY(*it == 0); |
| 80 | it++; |
| 81 | VERIFY(false); |
| 82 | } |
| 83 | catch (int x) |
| 84 | { |
| 85 | VERIFY(x == 3); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | for (auto x : foo_catchy_delegator()) |
| 90 | VERIFY(x == 0); |
| 91 | VERIFY(catchy_caught); |
| 92 | } |