| 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 | // <vector> |
| 10 | |
| 11 | // iterator insert(const_iterator position, const value_type& x); |
| 12 | |
| 13 | #include <vector> |
| 14 | #include <cassert> |
| 15 | #include <cstddef> |
| 16 | |
| 17 | #include "test_macros.h" |
| 18 | #include "test_allocator.h" |
| 19 | #include "min_allocator.h" |
| 20 | #include "asan_testing.h" |
| 21 | |
| 22 | TEST_CONSTEXPR_CXX20 bool test() { |
| 23 | { |
| 24 | std::vector<int> v(100); |
| 25 | const int lvalue = 1; |
| 26 | std::vector<int>::iterator i = v.insert(position: v.cbegin() + 10, x: lvalue); |
| 27 | assert(v.size() == 101); |
| 28 | assert(is_contiguous_container_asan_correct(v)); |
| 29 | assert(i == v.begin() + 10); |
| 30 | int j; |
| 31 | for (j = 0; j < 10; ++j) |
| 32 | assert(v[j] == 0); |
| 33 | assert(v[j] == 1); |
| 34 | for (++j; j < 101; ++j) |
| 35 | assert(v[j] == 0); |
| 36 | } |
| 37 | { |
| 38 | const std::size_t n = 100; |
| 39 | std::vector<int> v(n); |
| 40 | v.reserve(n: n + 1); |
| 41 | const int lvalue = 1; |
| 42 | |
| 43 | // no reallocation expected |
| 44 | std::vector<int>::iterator it = v.insert(position: v.cbegin() + n, x: lvalue); |
| 45 | |
| 46 | assert(v.size() == n + 1); |
| 47 | assert(is_contiguous_container_asan_correct(v)); |
| 48 | assert(it == v.begin() + n); |
| 49 | for (std::size_t i = 0; i < n; ++i) { |
| 50 | assert(v[i] == 0); |
| 51 | } |
| 52 | assert(v[n] == lvalue); |
| 53 | } |
| 54 | { |
| 55 | std::vector<int> v(100); |
| 56 | while (v.size() < v.capacity()) |
| 57 | v.push_back(x: 0); // force reallocation |
| 58 | std::size_t sz = v.size(); |
| 59 | const int lvalue = 1; |
| 60 | std::vector<int>::iterator i = v.insert(position: v.cbegin() + 10, x: lvalue); |
| 61 | assert(v.size() == sz + 1); |
| 62 | assert(is_contiguous_container_asan_correct(v)); |
| 63 | assert(i == v.begin() + 10); |
| 64 | std::size_t j; |
| 65 | for (j = 0; j < 10; ++j) |
| 66 | assert(v[j] == 0); |
| 67 | assert(v[j] == 1); |
| 68 | for (++j; j < v.size(); ++j) |
| 69 | assert(v[j] == 0); |
| 70 | } |
| 71 | { |
| 72 | std::vector<int> v(100); |
| 73 | while (v.size() < v.capacity()) |
| 74 | v.push_back(x: 0); |
| 75 | v.pop_back(); |
| 76 | v.pop_back(); // force no reallocation |
| 77 | std::size_t sz = v.size(); |
| 78 | const int lvalue = 1; |
| 79 | std::vector<int>::iterator i = v.insert(position: v.cbegin() + 10, x: lvalue); |
| 80 | assert(v.size() == sz + 1); |
| 81 | assert(is_contiguous_container_asan_correct(v)); |
| 82 | assert(i == v.begin() + 10); |
| 83 | std::size_t j; |
| 84 | for (j = 0; j < 10; ++j) |
| 85 | assert(v[j] == 0); |
| 86 | assert(v[j] == 1); |
| 87 | for (++j; j < v.size(); ++j) |
| 88 | assert(v[j] == 0); |
| 89 | } |
| 90 | { |
| 91 | std::vector<int, limited_allocator<int, 300> > v(100); |
| 92 | const int lvalue = 1; |
| 93 | std::vector<int, limited_allocator<int, 300> >::iterator i = v.insert(v.cbegin() + 10, lvalue); |
| 94 | assert(v.size() == 101); |
| 95 | assert(is_contiguous_container_asan_correct(v)); |
| 96 | assert(i == v.begin() + 10); |
| 97 | int j; |
| 98 | for (j = 0; j < 10; ++j) |
| 99 | assert(v[j] == 0); |
| 100 | assert(v[j] == 1); |
| 101 | for (++j; j < 101; ++j) |
| 102 | assert(v[j] == 0); |
| 103 | } |
| 104 | #if TEST_STD_VER >= 11 |
| 105 | { |
| 106 | std::vector<int, min_allocator<int>> v(100); |
| 107 | const int lvalue = 1; |
| 108 | std::vector<int, min_allocator<int>>::iterator i = v.insert(v.cbegin() + 10, lvalue); |
| 109 | assert(v.size() == 101); |
| 110 | assert(is_contiguous_container_asan_correct(v)); |
| 111 | assert(i == v.begin() + 10); |
| 112 | int j; |
| 113 | for (j = 0; j < 10; ++j) |
| 114 | assert(v[j] == 0); |
| 115 | assert(v[j] == 1); |
| 116 | for (++j; j < 101; ++j) |
| 117 | assert(v[j] == 0); |
| 118 | } |
| 119 | { |
| 120 | std::vector<int, safe_allocator<int>> v(100); |
| 121 | const int lvalue = 1; |
| 122 | std::vector<int, safe_allocator<int>>::iterator i = v.insert(v.cbegin() + 10, lvalue); |
| 123 | assert(v.size() == 101); |
| 124 | assert(is_contiguous_container_asan_correct(v)); |
| 125 | assert(i == v.begin() + 10); |
| 126 | int j; |
| 127 | for (j = 0; j < 10; ++j) |
| 128 | assert(v[j] == 0); |
| 129 | assert(v[j] == 1); |
| 130 | for (++j; j < 101; ++j) |
| 131 | assert(v[j] == 0); |
| 132 | } |
| 133 | #endif |
| 134 | |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | int main(int, char**) { |
| 139 | test(); |
| 140 | #if TEST_STD_VER > 17 |
| 141 | static_assert(test()); |
| 142 | #endif |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |