1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <algorithm>
11
12// template<ForwardIterator Iter, class T>
13//   requires OutputIterator<Iter, RvalueOf<Iter::reference>::type>
14//         && HasEqualTo<Iter::value_type, T>
15//   constexpr Iter         // constexpr after C++17
16//   remove(Iter first, Iter last, const T& value);
17
18#include <algorithm>
19#include <cassert>
20#include <memory>
21
22#include "test_macros.h"
23#include "test_iterators.h"
24
25#if TEST_STD_VER > 17
26TEST_CONSTEXPR bool test_constexpr() {
27    int ia[] = {1, 3, 5, 2, 5, 6};
28
29    auto it = std::remove(std::begin(ia), std::end(ia), 5);
30
31    return (std::begin(ia) + std::size(ia) - 2) == it  // we removed two elements
32        && std::none_of(std::begin(ia), it, [](int a) {return a == 5; })
33           ;
34    }
35#endif
36
37template <class Iter>
38void
39test()
40{
41    int ia[] = {0, 1, 2, 3, 4, 2, 3, 4, 2};
42    const unsigned sa = sizeof(ia)/sizeof(ia[0]);
43    Iter r = std::remove(Iter(ia), Iter(ia+sa), 2);
44    assert(base(r) == ia + sa-3);
45    assert(ia[0] == 0);
46    assert(ia[1] == 1);
47    assert(ia[2] == 3);
48    assert(ia[3] == 4);
49    assert(ia[4] == 3);
50    assert(ia[5] == 4);
51}
52
53#if TEST_STD_VER >= 11
54template <class Iter>
55void
56test1()
57{
58    const unsigned sa = 9;
59    std::unique_ptr<int> ia[sa];
60    ia[0].reset(new int(0));
61    ia[1].reset(new int(1));
62    ia[3].reset(new int(3));
63    ia[4].reset(new int(4));
64    ia[6].reset(new int(3));
65    ia[7].reset(new int(4));
66    Iter r = std::remove(Iter(ia), Iter(ia+sa), std::unique_ptr<int>());
67    assert(base(r) == ia + sa-3);
68    assert(*ia[0] == 0);
69    assert(*ia[1] == 1);
70    assert(*ia[2] == 3);
71    assert(*ia[3] == 4);
72    assert(*ia[4] == 3);
73    assert(*ia[5] == 4);
74}
75#endif // TEST_STD_VER >= 11
76
77int main()
78{
79    test<forward_iterator<int*> >();
80    test<bidirectional_iterator<int*> >();
81    test<random_access_iterator<int*> >();
82    test<int*>();
83
84#if TEST_STD_VER >= 11
85    test1<forward_iterator<std::unique_ptr<int>*> >();
86    test1<bidirectional_iterator<std::unique_ptr<int>*> >();
87    test1<random_access_iterator<std::unique_ptr<int>*> >();
88    test1<std::unique_ptr<int>*>();
89#endif // TEST_STD_VER >= 11
90
91#if TEST_STD_VER > 17
92    static_assert(test_constexpr());
93#endif
94}
95