xref: /llvm-project/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove.pass.cpp (revision 2df59c50688c122bbcae7467d3eaf862c3ea3088)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <algorithm>
10 
11 // template<ForwardIterator Iter, class T>
12 //   requires OutputIterator<Iter, RvalueOf<Iter::reference>::type>
13 //         && HasEqualTo<Iter::value_type, T>
14 //   constexpr Iter         // constexpr after C++17
15 //   remove(Iter first, Iter last, const T& value);
16 
17 #include <algorithm>
18 #include <cassert>
19 #include <memory>
20 
21 #include "test_macros.h"
22 #include "test_iterators.h"
23 
24 #if TEST_STD_VER > 17
test_constexpr()25 TEST_CONSTEXPR bool test_constexpr() {
26     int ia[] = {1, 3, 5, 2, 5, 6};
27 
28     auto it = std::remove(std::begin(ia), std::end(ia), 5);
29 
30     return (std::begin(ia) + std::size(ia) - 2) == it  // we removed two elements
31         && std::none_of(std::begin(ia), it, [](int a) {return a == 5; })
32            ;
33     }
34 #endif
35 
36 template <class Iter>
37 void
test()38 test()
39 {
40     int ia[] = {0, 1, 2, 3, 4, 2, 3, 4, 2};
41     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
42     Iter r = std::remove(Iter(ia), Iter(ia+sa), 2);
43     assert(base(r) == ia + sa-3);
44     assert(ia[0] == 0);
45     assert(ia[1] == 1);
46     assert(ia[2] == 3);
47     assert(ia[3] == 4);
48     assert(ia[4] == 3);
49     assert(ia[5] == 4);
50 }
51 
52 #if TEST_STD_VER >= 11
53 template <class Iter>
54 void
test1()55 test1()
56 {
57     const unsigned sa = 9;
58     std::unique_ptr<int> ia[sa];
59     ia[0].reset(new int(0));
60     ia[1].reset(new int(1));
61     ia[3].reset(new int(3));
62     ia[4].reset(new int(4));
63     ia[6].reset(new int(3));
64     ia[7].reset(new int(4));
65     Iter r = std::remove(Iter(ia), Iter(ia+sa), std::unique_ptr<int>());
66     assert(base(r) == ia + sa-3);
67     assert(*ia[0] == 0);
68     assert(*ia[1] == 1);
69     assert(*ia[2] == 3);
70     assert(*ia[3] == 4);
71     assert(*ia[4] == 3);
72     assert(*ia[5] == 4);
73 }
74 #endif // TEST_STD_VER >= 11
75 
main(int,char **)76 int main(int, char**)
77 {
78     test<forward_iterator<int*> >();
79     test<bidirectional_iterator<int*> >();
80     test<random_access_iterator<int*> >();
81     test<int*>();
82 
83 #if TEST_STD_VER >= 11
84     test1<forward_iterator<std::unique_ptr<int>*> >();
85     test1<bidirectional_iterator<std::unique_ptr<int>*> >();
86     test1<random_access_iterator<std::unique_ptr<int>*> >();
87     test1<std::unique_ptr<int>*>();
88 #endif // TEST_STD_VER >= 11
89 
90 #if TEST_STD_VER > 17
91     static_assert(test_constexpr());
92 #endif
93 
94   return 0;
95 }
96