partition.pass.cpp
3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// <algorithm>
// template<BidirectionalIterator Iter, Predicate<auto, Iter::value_type> Pred>
// requires ShuffleIterator<Iter>
// && CopyConstructible<Pred>
// Iter
// partition(Iter first, Iter last, Pred pred);
#include <algorithm>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
struct is_odd
{
bool operator()(const int& i) const {return i & 1;}
};
template <class Iter>
void
test()
{
// check mixed
int ia[] = {1, 2, 3, 4, 5, 6, 7, 8 ,9};
const unsigned sa = sizeof(ia)/sizeof(ia[0]);
Iter r = std::partition(Iter(ia), Iter(ia + sa), is_odd());
assert(base(r) == ia + 5);
for (int* i = ia; i < base(r); ++i)
assert(is_odd()(*i));
for (int* i = base(r); i < ia+sa; ++i)
assert(!is_odd()(*i));
// check empty
r = std::partition(Iter(ia), Iter(ia), is_odd());
assert(base(r) == ia);
// check all false
for (unsigned i = 0; i < sa; ++i)
ia[i] = 2*i;
r = std::partition(Iter(ia), Iter(ia+sa), is_odd());
assert(base(r) == ia);
// check all true
for (unsigned i = 0; i < sa; ++i)
ia[i] = 2*i+1;
r = std::partition(Iter(ia), Iter(ia+sa), is_odd());
assert(base(r) == ia+sa);
// check all true but last
for (unsigned i = 0; i < sa; ++i)
ia[i] = 2*i+1;
ia[sa-1] = 10;
r = std::partition(Iter(ia), Iter(ia+sa), is_odd());
assert(base(r) == ia+sa-1);
for (int* i = ia; i < base(r); ++i)
assert(is_odd()(*i));
for (int* i = base(r); i < ia+sa; ++i)
assert(!is_odd()(*i));
// check all true but first
for (unsigned i = 0; i < sa; ++i)
ia[i] = 2*i+1;
ia[0] = 10;
r = std::partition(Iter(ia), Iter(ia+sa), is_odd());
assert(base(r) == ia+sa-1);
for (int* i = ia; i < base(r); ++i)
assert(is_odd()(*i));
for (int* i = base(r); i < ia+sa; ++i)
assert(!is_odd()(*i));
// check all false but last
for (unsigned i = 0; i < sa; ++i)
ia[i] = 2*i;
ia[sa-1] = 11;
r = std::partition(Iter(ia), Iter(ia+sa), is_odd());
assert(base(r) == ia+1);
for (int* i = ia; i < base(r); ++i)
assert(is_odd()(*i));
for (int* i = base(r); i < ia+sa; ++i)
assert(!is_odd()(*i));
// check all false but first
for (unsigned i = 0; i < sa; ++i)
ia[i] = 2*i;
ia[0] = 11;
r = std::partition(Iter(ia), Iter(ia+sa), is_odd());
assert(base(r) == ia+1);
for (int* i = ia; i < base(r); ++i)
assert(is_odd()(*i));
for (int* i = base(r); i < ia+sa; ++i)
assert(!is_odd()(*i));
}
int main(int, char**)
{
test<bidirectional_iterator<int*> >();
test<random_access_iterator<int*> >();
test<int*>();
return 0;
}