| 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 | // <list> |
| 10 | |
| 11 | // template <class InputIterator> |
| 12 | // list(InputIterator first, InputIterator last, const Allocator& = Allocator()); |
| 13 | |
| 14 | #include <list> |
| 15 | #include <cassert> |
| 16 | #include "test_macros.h" |
| 17 | #include "test_iterators.h" |
| 18 | #include "test_allocator.h" |
| 19 | #include "min_allocator.h" |
| 20 | #if TEST_STD_VER >= 11 |
| 21 | # include "emplace_constructible.h" |
| 22 | # include "container_test_types.h" |
| 23 | #endif |
| 24 | |
| 25 | void basic_test() { |
| 26 | { |
| 27 | int a[] = {0, 1, 2, 3}; |
| 28 | std::list<int> l( |
| 29 | cpp17_input_iterator<const int*>(a), cpp17_input_iterator<const int*>(a + sizeof(a) / sizeof(a[0]))); |
| 30 | assert(l.size() == sizeof(a) / sizeof(a[0])); |
| 31 | assert(std::distance(l.begin(), l.end()) == sizeof(a) / sizeof(a[0])); |
| 32 | int j = 0; |
| 33 | for (std::list<int>::const_iterator i = l.begin(), e = l.end(); i != e; ++i, ++j) |
| 34 | assert(*i == j); |
| 35 | } |
| 36 | { |
| 37 | int a[] = {0, 1, 2, 3}; |
| 38 | std::list<int> l(cpp17_input_iterator<const int*>(a), |
| 39 | cpp17_input_iterator<const int*>(a + sizeof(a) / sizeof(a[0])), |
| 40 | std::allocator<int>()); |
| 41 | assert(l.size() == sizeof(a) / sizeof(a[0])); |
| 42 | assert(std::distance(l.begin(), l.end()) == sizeof(a) / sizeof(a[0])); |
| 43 | int j = 0; |
| 44 | for (std::list<int>::const_iterator i = l.begin(), e = l.end(); i != e; ++i, ++j) |
| 45 | assert(*i == j); |
| 46 | } |
| 47 | { |
| 48 | int a[] = {0, 1, 2, 3}; |
| 49 | // Add 2 for implementations that dynamically allocate a sentinel node and container proxy. |
| 50 | std::list<int, limited_allocator<int, sizeof(a) / sizeof(a[0]) + 2> > l( |
| 51 | cpp17_input_iterator<const int*>(a), cpp17_input_iterator<const int*>(a + sizeof(a) / sizeof(a[0]))); |
| 52 | assert(l.size() == sizeof(a) / sizeof(a[0])); |
| 53 | assert(std::distance(l.begin(), l.end()) == sizeof(a) / sizeof(a[0])); |
| 54 | int j = 0; |
| 55 | for (std::list<int>::const_iterator i = l.begin(), e = l.end(); i != e; ++i, ++j) |
| 56 | assert(*i == j); |
| 57 | } |
| 58 | #if TEST_STD_VER >= 11 |
| 59 | { |
| 60 | int a[] = {0, 1, 2, 3}; |
| 61 | std::list<int, min_allocator<int>> l( |
| 62 | cpp17_input_iterator<const int*>(a), cpp17_input_iterator<const int*>(a + sizeof(a) / sizeof(a[0]))); |
| 63 | assert(l.size() == sizeof(a) / sizeof(a[0])); |
| 64 | assert(std::distance(l.begin(), l.end()) == sizeof(a) / sizeof(a[0])); |
| 65 | int j = 0; |
| 66 | for (std::list<int, min_allocator<int>>::const_iterator i = l.begin(), e = l.end(); i != e; ++i, ++j) |
| 67 | assert(*i == j); |
| 68 | } |
| 69 | { |
| 70 | int a[] = {0, 1, 2, 3}; |
| 71 | std::list<int, min_allocator<int>> l( |
| 72 | cpp17_input_iterator<const int*>(a), |
| 73 | cpp17_input_iterator<const int*>(a + sizeof(a) / sizeof(a[0])), |
| 74 | min_allocator<int>()); |
| 75 | assert(l.size() == sizeof(a) / sizeof(a[0])); |
| 76 | assert(std::distance(l.begin(), l.end()) == sizeof(a) / sizeof(a[0])); |
| 77 | int j = 0; |
| 78 | for (std::list<int, min_allocator<int>>::const_iterator i = l.begin(), e = l.end(); i != e; ++i, ++j) |
| 79 | assert(*i == j); |
| 80 | } |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | void test_emplacable_concept() { |
| 85 | #if TEST_STD_VER >= 11 |
| 86 | int arr1[] = {42}; |
| 87 | int arr2[] = {1, 101, 42}; |
| 88 | { |
| 89 | using T = EmplaceConstructible<int>; |
| 90 | using It = random_access_iterator<int*>; |
| 91 | { |
| 92 | std::list<T> v(It(arr1), It(std::end(arr1))); |
| 93 | auto I = v.begin(); |
| 94 | assert(I->value == 42); |
| 95 | } |
| 96 | { |
| 97 | std::list<T> v(It(arr2), It(std::end(arr2))); |
| 98 | auto I = v.begin(); |
| 99 | assert(I->value == 1); |
| 100 | ++I; |
| 101 | assert(I->value == 101); |
| 102 | ++I; |
| 103 | assert(I->value == 42); |
| 104 | } |
| 105 | } |
| 106 | { |
| 107 | using T = EmplaceConstructible<int>; |
| 108 | using It = cpp17_input_iterator<int*>; |
| 109 | { |
| 110 | std::list<T> v(It(arr1), It(std::end(arr1))); |
| 111 | auto I = v.begin(); |
| 112 | assert(I->value == 42); |
| 113 | } |
| 114 | { |
| 115 | std::list<T> v(It(arr2), It(std::end(arr2))); |
| 116 | auto I = v.begin(); |
| 117 | //assert(v[0].copied == 0); |
| 118 | assert(I->value == 1); |
| 119 | //assert(v[1].copied == 0); |
| 120 | ++I; |
| 121 | assert(I->value == 101); |
| 122 | ++I; |
| 123 | assert(I->value == 42); |
| 124 | } |
| 125 | } |
| 126 | #endif |
| 127 | } |
| 128 | |
| 129 | void test_emplacable_concept_with_alloc() { |
| 130 | #if TEST_STD_VER >= 11 |
| 131 | int arr1[] = {42}; |
| 132 | int arr2[] = {1, 101, 42}; |
| 133 | { |
| 134 | using T = EmplaceConstructible<int>; |
| 135 | using It = random_access_iterator<int*>; |
| 136 | std::allocator<T> a; |
| 137 | { |
| 138 | std::list<T> v(It(arr1), It(std::end(arr1)), a); |
| 139 | auto I = v.begin(); |
| 140 | assert(I->value == 42); |
| 141 | } |
| 142 | { |
| 143 | std::list<T> v(It(arr2), It(std::end(arr2)), a); |
| 144 | auto I = v.begin(); |
| 145 | assert(I->value == 1); |
| 146 | ++I; |
| 147 | assert(I->value == 101); |
| 148 | ++I; |
| 149 | assert(I->value == 42); |
| 150 | } |
| 151 | } |
| 152 | { |
| 153 | using T = EmplaceConstructible<int>; |
| 154 | using It = cpp17_input_iterator<int*>; |
| 155 | std::allocator<T> a; |
| 156 | { |
| 157 | std::list<T> v(It(arr1), It(std::end(arr1)), a); |
| 158 | auto I = v.begin(); |
| 159 | assert(I->value == 42); |
| 160 | } |
| 161 | { |
| 162 | std::list<T> v(It(arr2), It(std::end(arr2)), a); |
| 163 | auto I = v.begin(); |
| 164 | //assert(v[0].copied == 0); |
| 165 | assert(I->value == 1); |
| 166 | //assert(v[1].copied == 0); |
| 167 | ++I; |
| 168 | assert(I->value == 101); |
| 169 | ++I; |
| 170 | assert(I->value == 42); |
| 171 | } |
| 172 | } |
| 173 | #endif |
| 174 | } |
| 175 | |
| 176 | void test_ctor_under_alloc() { |
| 177 | #if TEST_STD_VER >= 11 |
| 178 | int arr1[] = {42}; |
| 179 | int arr2[] = {1, 101, 42}; |
| 180 | { |
| 181 | using C = TCT::list<>; |
| 182 | using It = forward_iterator<int*>; |
| 183 | { |
| 184 | ExpectConstructGuard<int&> G(1); |
| 185 | C v(It(arr1), It(std::end(arr1))); |
| 186 | } |
| 187 | { |
| 188 | ExpectConstructGuard<int&> G(3); |
| 189 | C v(It(arr2), It(std::end(arr2))); |
| 190 | } |
| 191 | } |
| 192 | { |
| 193 | using C = TCT::list<>; |
| 194 | using It = cpp17_input_iterator<int*>; |
| 195 | { |
| 196 | ExpectConstructGuard<int&> G(1); |
| 197 | C v(It(arr1), It(std::end(arr1))); |
| 198 | } |
| 199 | { |
| 200 | ExpectConstructGuard<int&> G(3); |
| 201 | C v(It(arr2), It(std::end(arr2))); |
| 202 | } |
| 203 | } |
| 204 | #endif |
| 205 | } |
| 206 | |
| 207 | void test_ctor_under_alloc_with_alloc() { |
| 208 | #if TEST_STD_VER >= 11 |
| 209 | int arr1[] = {42}; |
| 210 | int arr2[] = {1, 101, 42}; |
| 211 | { |
| 212 | using C = TCT::list<>; |
| 213 | using It = forward_iterator<int*>; |
| 214 | using Alloc = typename C::allocator_type; |
| 215 | Alloc a; |
| 216 | { |
| 217 | ExpectConstructGuard<int&> G(1); |
| 218 | C v(It(arr1), It(std::end(arr1)), a); |
| 219 | } |
| 220 | { |
| 221 | ExpectConstructGuard<int&> G(3); |
| 222 | C v(It(arr2), It(std::end(arr2)), a); |
| 223 | } |
| 224 | } |
| 225 | { |
| 226 | using C = TCT::list<>; |
| 227 | using It = cpp17_input_iterator<int*>; |
| 228 | using Alloc = typename C::allocator_type; |
| 229 | Alloc a; |
| 230 | { |
| 231 | ExpectConstructGuard<int&> G(1); |
| 232 | C v(It(arr1), It(std::end(arr1)), a); |
| 233 | } |
| 234 | { |
| 235 | ExpectConstructGuard<int&> G(3); |
| 236 | C v(It(arr2), It(std::end(arr2)), a); |
| 237 | } |
| 238 | } |
| 239 | #endif |
| 240 | } |
| 241 | |
| 242 | int main(int, char**) { |
| 243 | basic_test(); |
| 244 | test_emplacable_concept(); |
| 245 | test_emplacable_concept_with_alloc(); |
| 246 | test_ctor_under_alloc(); |
| 247 | test_ctor_under_alloc_with_alloc(); |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |