xref: /llvm-project/libcxx/test/std/containers/sequences/vector/vector.erasure/erase.pass.cpp (revision fb855eb941b6d740cc6560297d0b4d3201dcaf9f)
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++03, c++11, c++14, c++17
9 
10 // <vector>
11 
12 // template <class T, class Allocator, class U>
13 //   typename vector<T, Allocator>::size_type
14 //   erase(vector<T, Allocator>& c, const U& value);
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>
test0(S s,U val,S expected,std::size_t expected_erased_count)24 TEST_CONSTEXPR_CXX20 void test0(S s, U val, S expected, std::size_t expected_erased_count) {
25   ASSERT_SAME_TYPE(typename S::size_type, decltype(std::erase(s, val)));
26   assert(expected_erased_count == std::erase(s, val));
27   assert(s == expected);
28 }
29 
30 template <class S>
test()31 TEST_CONSTEXPR_CXX20 void test()
32 {
33   test0(S(), 1, S(), 0);
34 
35   test0(S({1}), 1, S(), 1);
36   test0(S({1}), 2, S({1}), 0);
37 
38   test0(S({1, 2}), 1, S({2}), 1);
39   test0(S({1, 2}), 2, S({1}), 1);
40   test0(S({1, 2}), 3, S({1, 2}), 0);
41   test0(S({1, 1}), 1, S(), 2);
42   test0(S({1, 1}), 3, S({1, 1}), 0);
43 
44   test0(S({1, 2, 3}), 1, S({2, 3}), 1);
45   test0(S({1, 2, 3}), 2, S({1, 3}), 1);
46   test0(S({1, 2, 3}), 3, S({1, 2}), 1);
47   test0(S({1, 2, 3}), 4, S({1, 2, 3}), 0);
48 
49   test0(S({1, 1, 1}), 1, S(), 3);
50   test0(S({1, 1, 1}), 2, S({1, 1, 1}), 0);
51   test0(S({1, 1, 2}), 1, S({2}), 2);
52   test0(S({1, 1, 2}), 2, S({1, 1}), 1);
53   test0(S({1, 1, 2}), 3, S({1, 1, 2}), 0);
54   test0(S({1, 2, 2}), 1, S({2, 2}), 1);
55   test0(S({1, 2, 2}), 2, S({1}), 2);
56   test0(S({1, 2, 2}), 3, S({1, 2, 2}), 0);
57 
58   //  Test cross-type erasure
59   using opt = std::optional<typename S::value_type>;
60   test0(S({1, 2, 1}), opt(), S({1, 2, 1}), 0);
61   test0(S({1, 2, 1}), opt(1), S({2}), 2);
62   test0(S({1, 2, 1}), opt(2), S({1, 1}), 1);
63   test0(S({1, 2, 1}), opt(3), S({1, 2, 1}), 0);
64 }
65 
tests()66 TEST_CONSTEXPR_CXX20 bool tests()
67 {
68     test<std::vector<int>>();
69     test<std::vector<int, min_allocator<int>>> ();
70     test<std::vector<int, test_allocator<int>>> ();
71     test<std::vector<int, safe_allocator<int>>>();
72 
73     test<std::vector<long>>();
74     test<std::vector<double>>();
75 
76     return true;
77 }
78 
main(int,char **)79 int main(int, char**)
80 {
81   tests();
82 #if TEST_STD_VER > 17
83   static_assert(tests());
84 #endif
85   return 0;
86 }
87