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