xref: /llvm-project/libcxx/test/std/containers/sequences/deque/deque.erasure/erase_if.pass.cpp (revision 10ec9276d40024c23a481e6671dad1521151dd85)
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 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
11 
12 // <deque>
13 
14 // template <class T, class Allocator, class Predicate>
15 //   typename deque<T, Allocator>::size_type
16 //   erase_if(deque<T, Allocator>& c, Predicate pred);
17 
18 #include "asan_testing.h"
19 #include <deque>
20 
21 #include "test_macros.h"
22 #include "test_allocator.h"
23 #include "min_allocator.h"
24 
25 template <class S, class Pred>
test0(S s,Pred p,S expected,std::size_t expected_erased_count)26 void test0(S s, Pred p, S expected, std::size_t expected_erased_count) {
27   ASSERT_SAME_TYPE(typename S::size_type, decltype(std::erase_if(s, p)));
28   assert(expected_erased_count == std::erase_if(s, p));
29   assert(s == expected);
30   LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(s));
31 }
32 
33 template <typename S>
test()34 void test()
35 {
36     auto is1 = [](auto v) { return v == 1;};
37     auto is2 = [](auto v) { return v == 2;};
38     auto is3 = [](auto v) { return v == 3;};
39     auto is4 = [](auto v) { return v == 4;};
40     auto True  = [](auto) { return true; };
41     auto False = [](auto) { return false; };
42 
43     test0(S(), is1, S(), 0);
44 
45     test0(S({1}), is1, S(), 1);
46     test0(S({1}), is2, S({1}), 0);
47 
48     test0(S({1, 2}), is1, S({2}), 1);
49     test0(S({1, 2}), is2, S({1}), 1);
50     test0(S({1, 2}), is3, S({1, 2}), 0);
51     test0(S({1, 1}), is1, S(), 2);
52     test0(S({1, 1}), is3, S({1, 1}), 0);
53 
54     test0(S({1, 2, 3}), is1, S({2, 3}), 1);
55     test0(S({1, 2, 3}), is2, S({1, 3}), 1);
56     test0(S({1, 2, 3}), is3, S({1, 2}), 1);
57     test0(S({1, 2, 3}), is4, S({1, 2, 3}), 0);
58 
59     test0(S({1, 1, 1}), is1, S(), 3);
60     test0(S({1, 1, 1}), is2, S({1, 1, 1}), 0);
61     test0(S({1, 1, 2}), is1, S({2}), 2);
62     test0(S({1, 1, 2}), is2, S({1, 1}), 1);
63     test0(S({1, 1, 2}), is3, S({1, 1, 2}), 0);
64     test0(S({1, 2, 2}), is1, S({2, 2}), 1);
65     test0(S({1, 2, 2}), is2, S({1}), 2);
66     test0(S({1, 2, 2}), is3, S({1, 2, 2}), 0);
67 
68     test0(S({1, 2, 3}), True, S(), 3);
69     test0(S({1, 2, 3}), False, S({1, 2, 3}), 0);
70 }
71 
main(int,char **)72 int main(int, char**)
73 {
74     test<std::deque<int>>();
75     test<std::deque<int, min_allocator<int>>> ();
76     test<std::deque<int, safe_allocator<int>>> ();
77     test<std::deque<int, test_allocator<int>>> ();
78 
79     test<std::deque<long>>();
80     test<std::deque<double>>();
81 
82   return 0;
83 }
84