Home | History | Annotate | Download | only in vector.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 // <vector>
     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 <vector>
     19 #include <cassert>
     20 #include "../../../stack_allocator.h"
     21 #include "min_allocator.h"
     22 #include "asan_testing.h"
     23 
     24 int main()
     25 {
     26     {
     27         std::vector<int> v(100);
     28         std::vector<int>::iterator i = v.insert(v.cbegin() + 10, 1);
     29         assert(v.size() == 101);
     30         assert(is_contiguous_container_asan_correct(v));
     31         assert(i == v.begin() + 10);
     32         int j;
     33         for (j = 0; j < 10; ++j)
     34             assert(v[j] == 0);
     35         assert(v[j] == 1);
     36         for (++j; j < 101; ++j)
     37             assert(v[j] == 0);
     38     }
     39     {
     40         std::vector<int> v(100);
     41         while(v.size() < v.capacity()) v.push_back(0); // force reallocation
     42         size_t sz = v.size();
     43         std::vector<int>::iterator i = v.insert(v.cbegin() + 10, 1);
     44         assert(v.size() == sz + 1);
     45         assert(is_contiguous_container_asan_correct(v));
     46         assert(i == v.begin() + 10);
     47         int j;
     48         for (j = 0; j < 10; ++j)
     49             assert(v[j] == 0);
     50         assert(v[j] == 1);
     51         for (++j; j < v.size(); ++j)
     52             assert(v[j] == 0);
     53     }
     54     {
     55         std::vector<int> v(100);
     56         while(v.size() < v.capacity()) v.push_back(0);
     57         v.pop_back(); v.pop_back(); // force no reallocation
     58         size_t sz = v.size();
     59         std::vector<int>::iterator i = v.insert(v.cbegin() + 10, 1);
     60         assert(v.size() == sz + 1);
     61         assert(is_contiguous_container_asan_correct(v));
     62         assert(i == v.begin() + 10);
     63         int j;
     64         for (j = 0; j < 10; ++j)
     65             assert(v[j] == 0);
     66         assert(v[j] == 1);
     67         for (++j; j < v.size(); ++j)
     68             assert(v[j] == 0);
     69     }
     70     {
     71         std::vector<int, stack_allocator<int, 300> > v(100);
     72         std::vector<int, stack_allocator<int, 300> >::iterator i = v.insert(v.cbegin() + 10, 1);
     73         assert(v.size() == 101);
     74         assert(is_contiguous_container_asan_correct(v));
     75         assert(i == v.begin() + 10);
     76         int j;
     77         for (j = 0; j < 10; ++j)
     78             assert(v[j] == 0);
     79         assert(v[j] == 1);
     80         for (++j; j < 101; ++j)
     81             assert(v[j] == 0);
     82     }
     83 #if _LIBCPP_DEBUG >= 1
     84     {
     85         std::vector<int> v1(3);
     86         std::vector<int> v2(3);
     87         int i = 4;
     88         v1.insert(v2.begin(), i);
     89         assert(false);
     90     }
     91 #endif
     92 #if __cplusplus >= 201103L
     93     {
     94         std::vector<int, min_allocator<int>> v(100);
     95         std::vector<int, min_allocator<int>>::iterator i = v.insert(v.cbegin() + 10, 1);
     96         assert(v.size() == 101);
     97         assert(is_contiguous_container_asan_correct(v));
     98         assert(i == v.begin() + 10);
     99         int j;
    100         for (j = 0; j < 10; ++j)
    101             assert(v[j] == 0);
    102         assert(v[j] == 1);
    103         for (++j; j < 101; ++j)
    104             assert(v[j] == 0);
    105     }
    106 #if _LIBCPP_DEBUG >= 1
    107     {
    108         std::vector<int, min_allocator<int>> v1(3);
    109         std::vector<int, min_allocator<int>> v2(3);
    110         int i = 4;
    111         v1.insert(v2.begin(), i);
    112         assert(false);
    113     }
    114 #endif
    115 #endif
    116 }
    117