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 U>
15// typename deque<T, Allocator>::size_type
16// erase(deque<T, Allocator>& c, const U& value);
17
18#include "asan_testing.h"
19#include <deque>
20#include <optional>
21
22#include "test_macros.h"
23#include "test_allocator.h"
24#include "min_allocator.h"
25
26template <class S, class U>
27void test0(S s, U val, S expected, std::size_t expected_erased_count) {
28 ASSERT_SAME_TYPE(typename S::size_type, decltype(std::erase(s, val)));
29 assert(expected_erased_count == std::erase(s, val));
30 assert(s == expected);
31 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(s));
32}
33
34template <class S>
35void test() {
36 test0(S(), 1, S(), 0);
37
38 test0(S({1}), 1, S(), 1);
39 test0(S({1}), 2, S({1}), 0);
40
41 test0(S({1, 2}), 1, S({2}), 1);
42 test0(S({1, 2}), 2, S({1}), 1);
43 test0(S({1, 2}), 3, S({1, 2}), 0);
44 test0(S({1, 1}), 1, S(), 2);
45 test0(S({1, 1}), 3, S({1, 1}), 0);
46
47 test0(S({1, 2, 3}), 1, S({2, 3}), 1);
48 test0(S({1, 2, 3}), 2, S({1, 3}), 1);
49 test0(S({1, 2, 3}), 3, S({1, 2}), 1);
50 test0(S({1, 2, 3}), 4, S({1, 2, 3}), 0);
51
52 test0(S({1, 1, 1}), 1, S(), 3);
53 test0(S({1, 1, 1}), 2, S({1, 1, 1}), 0);
54 test0(S({1, 1, 2}), 1, S({2}), 2);
55 test0(S({1, 1, 2}), 2, S({1, 1}), 1);
56 test0(S({1, 1, 2}), 3, S({1, 1, 2}), 0);
57 test0(S({1, 2, 2}), 1, S({2, 2}), 1);
58 test0(S({1, 2, 2}), 2, S({1}), 2);
59 test0(S({1, 2, 2}), 3, S({1, 2, 2}), 0);
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}), 0);
64 test0(S({1, 2, 1}), opt(1), S({2}), 2);
65 test0(S({1, 2, 1}), opt(2), S({1, 1}), 1);
66 test0(S({1, 2, 1}), opt(3), S({1, 2, 1}), 0);
67}
68
69int main(int, char**) {
70 test<std::deque<int>>();
71 test<std::deque<int, min_allocator<int>>>();
72 test<std::deque<int, safe_allocator<int>>>();
73 test<std::deque<int, test_allocator<int>>>();
74
75 test<std::deque<long>>();
76 test<std::deque<double>>();
77
78 return 0;
79}
80

source code of libcxx/test/std/containers/sequences/deque/deque.erasure/erase.pass.cpp