xref: /llvm-project/libcxx/test/std/containers/sequences/list/list.ops/remove.pass.cpp (revision 355e0ce3c5366bd0b564a25e4b6675353da3c53e)
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 // <list>
10 
11 // void      remove(const value_type& value); // pre-c++20
12 // size_type remove(const value_type& value); // c++20 and later
13 
14 #include <list>
15 #include <cassert>
16 
17 #include "test_macros.h"
18 #include "min_allocator.h"
19 
20 struct S {
SS21   S(int i) : i_(new int(i)) {}
SS22   S(const S &rhs) : i_(new int(*rhs.i_)) {}
operator =S23   S &operator=(const S &rhs) {
24     *i_ = *rhs.i_;
25     return *this;
26   }
~SS27   ~S() {
28     delete i_;
29     i_ = NULL;
30   }
operator ==S31   bool operator==(const S &rhs) const { return *i_ == *rhs.i_; }
getS32   int get() const { return *i_; }
33   int *i_;
34 };
35 
main(int,char **)36 int main(int, char**) {
37   {
38     int a1[] = {1, 2, 3, 4};
39     int a2[] = {1, 2, 4};
40     typedef std::list<int> L;
41     L c(a1, a1 + 4);
42 #if TEST_STD_VER > 17
43     assert(c.remove(3) == 1);
44     ASSERT_SAME_TYPE(L::size_type, decltype(c.remove(3)));
45 #else
46     ASSERT_SAME_TYPE(void,         decltype(c.remove(3)));
47     c.remove(3);
48 #endif
49 
50     assert(c == std::list<int>(a2, a2 + 3));
51   }
52   { // LWG issue #526
53     int a1[] = {1, 2, 1, 3, 5, 8, 11};
54     int a2[] = {2, 3, 5, 8, 11};
55     std::list<int> c(a1, a1 + 7);
56     c.remove(c.front());
57     assert(c == std::list<int>(a2, a2 + 5));
58   }
59   {
60     int a1[] = {1, 2, 1, 3, 5, 8, 11, 1};
61     int a2[] = {2, 3, 5, 8, 11};
62     std::list<S> c;
63     for (int *ip = a1; ip < a1 + 8; ++ip)
64       c.push_back(S(*ip));
65 #if TEST_STD_VER > 17
66     assert(c.remove(c.front()) == 3);
67 #else
68     c.remove(c.front());
69 #endif
70     std::list<S>::const_iterator it = c.begin();
71     for (int *ip = a2; ip < a2 + 5; ++ip, ++it) {
72       assert(it != c.end());
73       assert(*ip == it->get());
74     }
75     assert(it == c.end());
76   }
77   {
78     typedef no_default_allocator<int> Alloc;
79     typedef std::list<int, Alloc> List;
80     int a1[] = {1, 2, 3, 4};
81     int a2[] = {1, 2, 4};
82     List c(a1, a1 + 4, Alloc::create());
83 #if TEST_STD_VER > 17
84     assert(c.remove(3) == 1);
85 #else
86     c.remove(3);
87 #endif
88     assert(c == List(a2, a2 + 3, Alloc::create()));
89   }
90 #if TEST_STD_VER >= 11
91   {
92     int a1[] = {1, 2, 3, 4};
93     int a2[] = {1, 2, 4};
94     std::list<int, min_allocator<int>> c(a1, a1 + 4);
95 #if TEST_STD_VER > 17
96     assert(c.remove(3) == 1);
97 #else
98     c.remove(3);
99 #endif
100     assert((c == std::list<int, min_allocator<int>>(a2, a2 + 3)));
101   }
102 #endif
103 
104   return 0;
105 }
106