| 1 | // { dg-do run { target c++11 } } |
| 2 | |
| 3 | // Copyright (C) 2009-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 | // std::stable_partition is not freestanding. |
| 21 | // { dg-require-effective-target hosted } |
| 22 | |
| 23 | // 25.2.12 [lib.alg.partitions] Partitions. |
| 24 | |
| 25 | #include <algorithm> |
| 26 | #include <functional> |
| 27 | #include <testsuite_hooks.h> |
| 28 | #include <testsuite_iterators.h> |
| 29 | #include <testsuite_rvalref.h> |
| 30 | |
| 31 | using __gnu_test::test_container; |
| 32 | using __gnu_test::random_access_iterator_wrapper; |
| 33 | using __gnu_test::rvalstruct; |
| 34 | |
| 35 | typedef test_container<rvalstruct, random_access_iterator_wrapper> Container; |
| 36 | |
| 37 | const int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}; |
| 38 | const int B[] = {2, 4, 6, 8, 10, 12, 14, 16, 1, 3, 5, 7, 9, 11, 13, 15, 17}; |
| 39 | const int N = sizeof(A) / sizeof(int); |
| 40 | |
| 41 | // Check that starting with a true predicate works too. (PR52822) |
| 42 | const int A2[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}; |
| 43 | const int B2[] = {2, 4, 6, 8, 10, 12, 14, 16, 3, 5, 7, 9, 11, 13, 15, 17}; |
| 44 | const int N2 = sizeof(A2) / sizeof(int); |
| 45 | |
| 46 | struct Pred |
| 47 | { |
| 48 | bool |
| 49 | operator()(const rvalstruct& x) const |
| 50 | { return (x.val % 2) == 0; } |
| 51 | }; |
| 52 | |
| 53 | // 25.2.12 stable_partition(), starting with a false predicate. |
| 54 | void |
| 55 | test01() |
| 56 | { |
| 57 | rvalstruct s1[N]; |
| 58 | std::copy(A, A + N, s1); |
| 59 | Container con(s1, s1 + N); |
| 60 | |
| 61 | std::stable_partition(con.begin(), con.end(), Pred()); |
| 62 | VERIFY( std::equal(s1, s1 + N, B) ); |
| 63 | } |
| 64 | |
| 65 | // 25.2.12 stable_partition(), starting with a true predicate. |
| 66 | void |
| 67 | test02() |
| 68 | { |
| 69 | rvalstruct s1[N2]; |
| 70 | std::copy(A2, A2 + N2, s1); |
| 71 | Container con(s1, s1 + N2); |
| 72 | |
| 73 | std::stable_partition(con.begin(), con.end(), Pred()); |
| 74 | VERIFY( std::equal(s1, s1 + N2, B2) ); |
| 75 | } |
| 76 | |
| 77 | int |
| 78 | main() |
| 79 | { |
| 80 | test01(); |
| 81 | test02(); |
| 82 | return 0; |
| 83 | } |