| 1 | // { dg-do run { target c++11 } } |
| 2 | |
| 3 | // Copyright (C) 2008-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 | // C++11 23.3.4.6 Operations [forwardlist.ops] |
| 21 | |
| 22 | #include <forward_list> |
| 23 | #include <testsuite_hooks.h> |
| 24 | |
| 25 | // Comparison functor. |
| 26 | template<typename Num> |
| 27 | class Comp |
| 28 | { |
| 29 | public: |
| 30 | Comp(const Num & num) |
| 31 | { |
| 32 | n = num; |
| 33 | } |
| 34 | bool operator()(const Num i, const Num j) |
| 35 | { |
| 36 | return (n * i) < (n * j); |
| 37 | } |
| 38 | private: |
| 39 | Num n; |
| 40 | }; |
| 41 | |
| 42 | // This test verifies the following: |
| 43 | // sort |
| 44 | void |
| 45 | test01() |
| 46 | { |
| 47 | const unsigned int n = 13; |
| 48 | int order[][n] = { |
| 49 | { 0,1,2,3,4,5,6,7,8,9,10,11,12 }, |
| 50 | { 6,2,8,4,11,1,12,7,3,9,5,0,10 }, |
| 51 | { 12,11,10,9,8,7,6,5,4,3,2,1,0 }, |
| 52 | }; |
| 53 | std::forward_list<int> sorted(order[0], order[0] + n); |
| 54 | |
| 55 | for (unsigned int i = 0; i < sizeof(order)/sizeof(*order); ++i) |
| 56 | { |
| 57 | std::forward_list<int> head(order[i], order[i] + n); |
| 58 | |
| 59 | head.sort(); |
| 60 | |
| 61 | VERIFY(head == sorted); |
| 62 | } |
| 63 | |
| 64 | std::forward_list<int> reversed(order[2], order[2] + n); |
| 65 | for (unsigned int i = 0; i < sizeof(order)/sizeof(*order); ++i) |
| 66 | { |
| 67 | std::forward_list<int> head(order[i], order[i] + n); |
| 68 | |
| 69 | Comp<int> comp(-1); |
| 70 | head.sort( comp ); |
| 71 | |
| 72 | VERIFY(head == reversed); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | int |
| 77 | main() |
| 78 | { |
| 79 | test01(); |
| 80 | return 0; |
| 81 | } |