| 1 | // { dg-do run { target c++11 } } |
| 2 | |
| 3 | // Copyright (C) 2010-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 | // Check that iterators ownership is correctly manage on swap. |
| 21 | #include <vector> |
| 22 | #include <forward_list> |
| 23 | #include <iostream> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | void |
| 27 | test01() |
| 28 | { |
| 29 | std::forward_list<int> fl1{1, 3, 5}; |
| 30 | std::forward_list<int> fl2{2, 4, 6}; |
| 31 | |
| 32 | std::vector<std::forward_list<int>::iterator> fl1_its; |
| 33 | fl1_its.push_back(fl1.before_begin()); |
| 34 | for (std::forward_list<int>::iterator it = fl1.begin(); |
| 35 | it != fl1.end(); ++it) |
| 36 | { |
| 37 | fl1_its.push_back(it); |
| 38 | } |
| 39 | fl1_its.push_back(fl1.end()); |
| 40 | |
| 41 | std::vector<std::forward_list<int>::iterator> fl2_its; |
| 42 | fl2_its.push_back(fl2.before_begin()); |
| 43 | for (std::forward_list<int>::iterator it = fl2.begin(); |
| 44 | it != fl2.end(); ++it) |
| 45 | { |
| 46 | fl2_its.push_back(it); |
| 47 | } |
| 48 | fl2_its.push_back(fl2.end()); |
| 49 | |
| 50 | fl1.swap(fl2); |
| 51 | |
| 52 | auto fit = fl1.before_begin(); |
| 53 | // before-begin iterator is not transfered: |
| 54 | // TODO: Validate with LWG group how before begin should be |
| 55 | // treated. |
| 56 | VERIFY( fit == fl1_its[0] ); |
| 57 | // All other iterators are, even paste-the-end ones: |
| 58 | for (size_t i = 1; i != fl2_its.size(); ++i) |
| 59 | { |
| 60 | VERIFY( ++fit == fl2_its[i] ); |
| 61 | } |
| 62 | |
| 63 | fit = fl2.before_begin(); |
| 64 | // TODO: Validate with LWG group how before begin should be |
| 65 | // treated. |
| 66 | VERIFY( fit == fl2_its[0] ); |
| 67 | for (size_t i = 1; i != fl1_its.size(); ++i) |
| 68 | { |
| 69 | VERIFY( ++fit == fl1_its[i] ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | int |
| 74 | main() |
| 75 | { |
| 76 | test01(); |
| 77 | return 0; |
| 78 | } |