Home | History | Annotate | Download | only in list.ops
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <list>
     11 
     12 // void remove(const value_type& value);
     13 
     14 #include <list>
     15 #include <cassert>
     16 
     17 #include "test_macros.h"
     18 #include "min_allocator.h"
     19 
     20 struct S {
     21   S(int i) : i_(new int(i)) {}
     22   S(const S &rhs) : i_(new int(*rhs.i_)) {}
     23   S &operator=(const S &rhs) {
     24     *i_ = *rhs.i_;
     25     return *this;
     26   }
     27   ~S() {
     28     delete i_;
     29     i_ = NULL;
     30   }
     31   bool operator==(const S &rhs) const { return *i_ == *rhs.i_; }
     32   int get() const { return *i_; }
     33   int *i_;
     34 };
     35 
     36 int main() {
     37   {
     38     int a1[] = {1, 2, 3, 4};
     39     int a2[] = {1, 2, 4};
     40     std::list<int> c(a1, a1 + 4);
     41     c.remove(3);
     42     assert(c == std::list<int>(a2, a2 + 3));
     43   }
     44   { // LWG issue #526
     45     int a1[] = {1, 2, 1, 3, 5, 8, 11};
     46     int a2[] = {2, 3, 5, 8, 11};
     47     std::list<int> c(a1, a1 + 7);
     48     c.remove(c.front());
     49     assert(c == std::list<int>(a2, a2 + 5));
     50   }
     51   {
     52     int a1[] = {1, 2, 1, 3, 5, 8, 11, 1};
     53     int a2[] = {2, 3, 5, 8, 11};
     54     std::list<S> c;
     55     for (int *ip = a1; ip < a1 + 8; ++ip)
     56       c.push_back(S(*ip));
     57     c.remove(c.front());
     58     std::list<S>::const_iterator it = c.begin();
     59     for (int *ip = a2; ip < a2 + 5; ++ip, ++it) {
     60       assert(it != c.end());
     61       assert(*ip == it->get());
     62     }
     63     assert(it == c.end());
     64   }
     65   {
     66     typedef no_default_allocator<int> Alloc;
     67     typedef std::list<int, Alloc> List;
     68     int a1[] = {1, 2, 3, 4};
     69     int a2[] = {1, 2, 4};
     70     List c(a1, a1 + 4, Alloc::create());
     71     c.remove(3);
     72     assert(c == List(a2, a2 + 3, Alloc::create()));
     73   }
     74 #if TEST_STD_VER >= 11
     75   {
     76     int a1[] = {1, 2, 3, 4};
     77     int a2[] = {1, 2, 4};
     78     std::list<int, min_allocator<int>> c(a1, a1 + 4);
     79     c.remove(3);
     80     assert((c == std::list<int, min_allocator<int>>(a2, a2 + 3)));
     81   }
     82 #endif
     83 }
     84