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{
37 test0(S(), 1, S(), 0);
38
39 test0(S({1}), 1, S(), 1);
40 test0(S({1}), 2, S({1}), 0);
41
42 test0(S({1, 2}), 1, S({2}), 1);
43 test0(S({1, 2}), 2, S({1}), 1);
44 test0(S({1, 2}), 3, S({1, 2}), 0);
45 test0(S({1, 1}), 1, S(), 2);
46 test0(S({1, 1}), 3, S({1, 1}), 0);
47
48 test0(S({1, 2, 3}), 1, S({2, 3}), 1);
49 test0(S({1, 2, 3}), 2, S({1, 3}), 1);
50 test0(S({1, 2, 3}), 3, S({1, 2}), 1);
51 test0(S({1, 2, 3}), 4, S({1, 2, 3}), 0);
52
53 test0(S({1, 1, 1}), 1, S(), 3);
54 test0(S({1, 1, 1}), 2, S({1, 1, 1}), 0);
55 test0(S({1, 1, 2}), 1, S({2}), 2);
56 test0(S({1, 1, 2}), 2, S({1, 1}), 1);
57 test0(S({1, 1, 2}), 3, S({1, 1, 2}), 0);
58 test0(S({1, 2, 2}), 1, S({2, 2}), 1);
59 test0(S({1, 2, 2}), 2, S({1}), 2);
60 test0(S({1, 2, 2}), 3, S({1, 2, 2}), 0);
61
62 // Test cross-type erasure
63 using opt = std::optional<typename S::value_type>;
64 test0(S({1, 2, 1}), opt(), S({1, 2, 1}), 0);
65 test0(S({1, 2, 1}), opt(1), S({2}), 2);
66 test0(S({1, 2, 1}), opt(2), S({1, 1}), 1);
67 test0(S({1, 2, 1}), opt(3), S({1, 2, 1}), 0);
68}
69
70int main(int, char**)
71{
72 test<std::deque<int>>();
73 test<std::deque<int, min_allocator<int>>> ();
74 test<std::deque<int, safe_allocator<int>>> ();
75 test<std::deque<int, test_allocator<int>>> ();
76
77 test<std::deque<long>>();
78 test<std::deque<double>>();
79
80 return 0;
81}
82

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