xref: /llvm-project/libcxx/test/std/containers/sequences/vector/vector.erasure/erase.pass.cpp (revision 6b77ebdc919d9212f50fd353b1da0d84d3815bf3)
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 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
9 
10 // <vector>
11 
12 // template <class T, class Allocator, class U>
13 //   void erase(vector<T, Allocator>& c, const U& value);
14 
15 
16 #include <vector>
17 #include <optional>
18 
19 #include "test_macros.h"
20 #include "test_allocator.h"
21 #include "min_allocator.h"
22 
23 template <class S, class U>
24 void
25 test0(S s,  U val, S expected)
26 {
27     ASSERT_SAME_TYPE(void, decltype(std::erase(s, val)));
28     std::erase(s, val);
29     assert(s == expected);
30 }
31 
32 template <class S>
33 void test()
34 {
35 
36     test0(S(), 1, S());
37 
38     test0(S({1}), 1, S());
39     test0(S({1}), 2, S({1}));
40 
41     test0(S({1,2}), 1, S({2}));
42     test0(S({1,2}), 2, S({1}));
43     test0(S({1,2}), 3, S({1,2}));
44     test0(S({1,1}), 1, S());
45     test0(S({1,1}), 3, S({1,1}));
46 
47     test0(S({1,2,3}), 1, S({2,3}));
48     test0(S({1,2,3}), 2, S({1,3}));
49     test0(S({1,2,3}), 3, S({1,2}));
50     test0(S({1,2,3}), 4, S({1,2,3}));
51 
52     test0(S({1,1,1}), 1, S());
53     test0(S({1,1,1}), 2, S({1,1,1}));
54     test0(S({1,1,2}), 1, S({2}));
55     test0(S({1,1,2}), 2, S({1,1}));
56     test0(S({1,1,2}), 3, S({1,1,2}));
57     test0(S({1,2,2}), 1, S({2,2}));
58     test0(S({1,2,2}), 2, S({1}));
59     test0(S({1,2,2}), 3, S({1,2,2}));
60 
61 //  Test cross-type erasure
62     using opt = std::optional<typename S::value_type>;
63     test0(S({1,2,1}), opt(),  S({1,2,1}));
64     test0(S({1,2,1}), opt(1), S({2}));
65     test0(S({1,2,1}), opt(2), S({1,1}));
66     test0(S({1,2,1}), opt(3), S({1,2,1}));
67 }
68 
69 int main(int, char**)
70 {
71     test<std::vector<int>>();
72     test<std::vector<int, min_allocator<int>>> ();
73     test<std::vector<int, test_allocator<int>>> ();
74 
75     test<std::vector<long>>();
76     test<std::vector<double>>();
77 
78   return 0;
79 }
80