| 1 | // { dg-do run { target c++11 } } |
| 2 | // { dg-options "-g -O0" } |
| 3 | |
| 4 | // |
| 5 | // Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 6 | // |
| 7 | // This file is part of the GNU ISO C++ Library. This library is free |
| 8 | // software; you can redistribute it and/or modify it under the |
| 9 | // terms of the GNU General Public License as published by the |
| 10 | // Free Software Foundation; either version 3, or (at your option) |
| 11 | // any later version. |
| 12 | // |
| 13 | // This library is distributed in the hope that it will be useful, |
| 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | // GNU General Public License for more details. |
| 17 | // |
| 18 | // You should have received a copy of the GNU General Public License along |
| 19 | // with this library; see the file COPYING3. If not see |
| 20 | // <http://www.gnu.org/licenses/>. |
| 21 | |
| 22 | #include <forward_list> |
| 23 | #include <memory> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | struct PredLWG526 |
| 27 | { |
| 28 | PredLWG526(int i) : i_(i) {}; |
| 29 | ~PredLWG526() { i_ = -32767; } |
| 30 | |
| 31 | bool |
| 32 | operator() (const PredLWG526& p) const { return p.i_ == i_; } |
| 33 | |
| 34 | bool |
| 35 | operator==(int i) const { return i == i_; } |
| 36 | |
| 37 | bool |
| 38 | operator() (const PredLWG526& lhs, const PredLWG526& rhs) const |
| 39 | { |
| 40 | VERIFY( i_ != -32767 ); |
| 41 | return lhs.i_ == rhs.i_; |
| 42 | } |
| 43 | |
| 44 | int i_; |
| 45 | }; |
| 46 | |
| 47 | void test01() |
| 48 | { |
| 49 | int a1[] = {1, 2, 1, 3, 5, 8, 11}; |
| 50 | int a2[] = {2, 3, 5, 8, 11}; |
| 51 | std::forward_list<PredLWG526> fl(a1, a1 + 7); |
| 52 | |
| 53 | VERIFY( std::distance(fl.begin(), fl.end()) == 7 ); |
| 54 | |
| 55 | fl.remove_if(std::cref(fl.front())); |
| 56 | VERIFY( std::distance(fl.begin(), fl.end()) == 5 ); |
| 57 | for (size_t i = 0; !fl.empty(); ++i) |
| 58 | { |
| 59 | VERIFY( fl.front() == a2[i] ); |
| 60 | fl.pop_front(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void test02() |
| 65 | { |
| 66 | int a1[] = {1, 1, 1, 2, 3, 5, 8, 11}; |
| 67 | int a2[] = {1, 2, 3, 5, 8, 11}; |
| 68 | std::forward_list<PredLWG526> fl(a1, a1 + 8); |
| 69 | |
| 70 | VERIFY( std::distance(fl.begin(), fl.end()) == 8 ); |
| 71 | |
| 72 | auto it = fl.begin(); |
| 73 | ++it; |
| 74 | fl.unique(std::cref(*it)); |
| 75 | VERIFY( std::distance(fl.begin(), fl.end()) == 6 ); |
| 76 | for (size_t i = 0; !fl.empty(); ++i) |
| 77 | { |
| 78 | VERIFY( fl.front() == a2[i] ); |
| 79 | fl.pop_front(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | int main() |
| 84 | { |
| 85 | test01(); |
| 86 | test02(); |
| 87 | return 0; |
| 88 | } |