Home | History | Annotate | Download | only in list.modifiers
      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 // iterator insert(const_iterator position, const value_type& x);
     13 
     14 #if _LIBCPP_DEBUG >= 1
     15 #define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
     16 #endif
     17 
     18 #include <list>
     19 #include <cstdlib>
     20 #include <cassert>
     21 
     22 #include "min_allocator.h"
     23 
     24 int throw_next = 0xFFFF;
     25 int count = 0;
     26 
     27 void* operator new(std::size_t s) throw(std::bad_alloc)
     28 {
     29     if (throw_next == 0)
     30         throw std::bad_alloc();
     31     --throw_next;
     32     ++count;
     33     return std::malloc(s);
     34 }
     35 
     36 void  operator delete(void* p) throw()
     37 {
     38     --count;
     39     std::free(p);
     40 }
     41 
     42 int main()
     43 {
     44     {
     45     int a1[] = {1, 2, 3};
     46     int a2[] = {1, 4, 2, 3};
     47     std::list<int> l1(a1, a1+3);
     48     std::list<int>::iterator i = l1.insert(next(l1.cbegin()), 4);
     49     assert(i == next(l1.begin()));
     50     assert(l1.size() == 4);
     51     assert(distance(l1.begin(), l1.end()) == 4);
     52     assert(l1 == std::list<int>(a2, a2+4));
     53     throw_next = 0;
     54     int save_count = count;
     55     try
     56     {
     57         i = l1.insert(i, 5);
     58         assert(false);
     59     }
     60     catch (...)
     61     {
     62     }
     63     throw_next = 0xFFFF;
     64     assert(save_count == count);
     65     assert(l1 == std::list<int>(a2, a2+4));
     66     }
     67 #if _LIBCPP_DEBUG >= 1
     68     {
     69         std::list<int> v1(3);
     70         std::list<int> v2(3);
     71         int i = 4;
     72         v1.insert(v2.begin(), i);
     73         assert(false);
     74     }
     75 #endif
     76 #if __cplusplus >= 201103L
     77     {
     78     int a1[] = {1, 2, 3};
     79     int a2[] = {1, 4, 2, 3};
     80     std::list<int, min_allocator<int>> l1(a1, a1+3);
     81     std::list<int, min_allocator<int>>::iterator i = l1.insert(next(l1.cbegin()), 4);
     82     assert(i == next(l1.begin()));
     83     assert(l1.size() == 4);
     84     assert(distance(l1.begin(), l1.end()) == 4);
     85     assert((l1 == std::list<int, min_allocator<int>>(a2, a2+4)));
     86     throw_next = 0;
     87     int save_count = count;
     88     try
     89     {
     90         i = l1.insert(i, 5);
     91         assert(false);
     92     }
     93     catch (...)
     94     {
     95     }
     96     throw_next = 0xFFFF;
     97     assert(save_count == count);
     98     assert((l1 == std::list<int, min_allocator<int>>(a2, a2+4)));
     99     }
    100 #if _LIBCPP_DEBUG >= 1
    101     {
    102         std::list<int, min_allocator<int>> v1(3);
    103         std::list<int, min_allocator<int>> v2(3);
    104         int i = 4;
    105         v1.insert(v2.begin(), i);
    106         assert(false);
    107     }
    108 #endif
    109 #endif
    110 }
    111