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