| 1 |
// { dg-do run { target c++11 } } |
| 2 |
|
| 3 |
// Copyright (C) 2005-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 |
// 25.2.12 [lib.alg.partitions] Partitions. |
| 21 |
|
| 22 |
#undef _GLIBCXX_CONCEPT_CHECKS |
| 23 |
|
| 24 |
#include <algorithm> |
| 25 |
#include <functional> |
| 26 |
#include <testsuite_hooks.h> |
| 27 |
#include <testsuite_rvalref.h> |
| 28 |
#include <testsuite_iterators.h> |
| 29 |
|
| 30 |
using __gnu_test::test_container; |
| 31 |
using __gnu_test::forward_iterator_wrapper; |
| 32 |
using __gnu_test::bidirectional_iterator_wrapper; |
| 33 |
using __gnu_test::rvalstruct; |
| 34 |
|
| 35 |
typedef test_container<rvalstruct, forward_iterator_wrapper> Fcontainer; |
| 36 |
typedef test_container<rvalstruct, bidirectional_iterator_wrapper> Bcontainer; |
| 37 |
|
| 38 |
const int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}; |
| 39 |
const int B[] = {2, 4, 6, 8, 10, 12, 14, 16, 1, 3, 5, 7, 9, 11, 13, 15, 17}; |
| 40 |
const int N = sizeof(A) / sizeof(int); |
| 41 |
|
| 42 |
struct Pred |
| 43 |
{ |
| 44 |
bool |
| 45 |
operator()(const rvalstruct& x) const |
| 46 |
{ return (x.val % 2) == 0; } |
| 47 |
}; |
| 48 |
|
| 49 |
// 25.2.12 partition() |
| 50 |
void |
| 51 |
test01() |
| 52 |
{ |
| 53 |
using std::partition; |
| 54 |
|
| 55 |
rvalstruct farray[N]; |
| 56 |
rvalstruct barray[N]; |
| 57 |
|
| 58 |
std::copy(A, A + N, farray); |
| 59 |
std::copy(A, A + N, barray); |
| 60 |
|
| 61 |
Fcontainer fcon(farray, farray + N); |
| 62 |
Bcontainer bcon(barray, barray + N); |
| 63 |
|
| 64 |
Pred pred; |
| 65 |
|
| 66 |
VERIFY(partition(fcon.begin(), fcon.end(), pred).ptr - farray == N/2); |
| 67 |
for (const rvalstruct* i = farray; i < farray+N/2; ++i) |
| 68 |
VERIFY(pred(*i)); |
| 69 |
|
| 70 |
for (const rvalstruct* i = farray+N/2; i < farray + N; ++i) |
| 71 |
VERIFY(!pred(*i)); |
| 72 |
|
| 73 |
VERIFY(partition(bcon.begin(), bcon.end(), pred).ptr - barray == N/2); |
| 74 |
|
| 75 |
for (const rvalstruct* i = barray; i < barray+N/2; ++i) |
| 76 |
VERIFY(pred(*i)); |
| 77 |
for (const rvalstruct* i = barray+N/2; i < barray + N; ++i) |
| 78 |
VERIFY(!pred(*i)); |
| 79 |
} |
| 80 |
|
| 81 |
int |
| 82 |
main() |
| 83 |
{ |
| 84 |
test01(); |
| 85 |
return 0; |
| 86 |
} |